def GetVolumeSize(self, uuid, readable=True): """Return the size of the volume with the given UUID. Args: uuid: str, ID of the volume in question readable: Optional boolean, default true: return a human-readable string when true, otherwise int number of bytes. Returns: str or int, see "readable" arg. Raises: Error: there was a problem getting volume info. InvalidUUIDError: The UUID is formatted incorrectly. """ if not util.UuidIsValid(uuid): raise storage.InvalidUUIDError('Invalid UUID: ' + uuid) try: plist = util.GetPlistFromExec( (DISKUTIL, 'corestorage', 'info', '-plist', uuid)) except util.ExecError: logging.exception('GetVolumeSize() failed to get volume info: %s', uuid) raise storage.Error num_bytes = plist['CoreStorageLogicalVolumeSize'] if readable: return '%.2f GiB' % (num_bytes / (1 << 30)) else: return num_bytes
def UnlockVolume(self, uuid, passphrase): """Unlock an APFS encrypted volume. Args: uuid: str, uuid of the volume to unlock. passphrase: str, passphrase to unlock the volume. Raises: CouldNotUnlockError: the volume cannot be unlocked. InvalidUUIDError: The UUID is formatted incorrectly. """ if not util.UuidIsValid(uuid): raise storage.InvalidUUIDError('Invalid UUID: ' + uuid) returncode, _, stderr = util.Exec( (DISKUTIL, 'apfs', 'unlockVolume', uuid, '-stdinpassphrase'), stdin=passphrase) if (returncode != 0 and 'volume is not locked' not in stderr and 'is already unlocked' not in stderr): raise storage.CouldNotUnlockError('Could not unlock volume (%s).' % returncode)
def RevertVolume(self, uuid, passphrase, passwd=''): """Disable encryption on an APFS system. Args: uuid: str, uuid of the volume to revert. passphrase: str, passphrase to unlock the volume. passwd: str, password for sudo Raises: CouldNotRevertError: the volume was unlocked, but cannot be reverted. CouldNotUnlockError: the volume cannot be unlocked. InvalidUUIDError: The UUID is formatted incorrectly. """ if not util.UuidIsValid(uuid): raise storage.InvalidUUIDError('Invalid UUID: ' + uuid) self.UnlockVolume(uuid, passphrase) returncode, _, _ = util.Exec(('sudo', '-k', '-S', FDESETUP, 'disable'), stdin=passwd + '\n') if returncode != 0: raise storage.CouldNotRevertError( 'Could not disable encryption (%s).' % (returncode))
def RevertVolume(self, uuid, passphrase, unused_passwd=''): """Revert a core storage encrypted volume (to unencrypted state). Args: uuid: str, uuid of the volume to revert. passphrase: str, passphrase to unlock the volume. unused_passwd: str, password for sudo Raises: CouldNotRevertError: the volume was unlocked, but cannot be reverted. CouldNotUnlockError: the volume cannot be unlocked. InvalidUUIDError: The UUID is formatted incorrectly. """ if not util.UuidIsValid(uuid): raise storage.InvalidUUIDError('Invalid UUID: ' + uuid) self.UnlockVolume(uuid, passphrase) returncode, _, _ = util.Exec( (DISKUTIL, 'corestorage', 'revert', uuid, '-stdinpassphrase'), stdin=passphrase) if returncode != 0: raise storage.CouldNotRevertError('Could not revert volume (%s).' % returncode)