Tweaked SysCallError() exception to include the exit code in a machine readable manner. Since it's useful as an indicator where calls might go wrong and for what reason.
This commit is contained in:
parent
577428f1b2
commit
f589750a3c
|
|
@ -7,7 +7,10 @@ class UnknownFilesystemFormat(BaseException):
|
||||||
class ProfileError(BaseException):
|
class ProfileError(BaseException):
|
||||||
pass
|
pass
|
||||||
class SysCallError(BaseException):
|
class SysCallError(BaseException):
|
||||||
pass
|
def __init__(self, message, error_code):
|
||||||
|
super(SysCallError, self).__init__(message)
|
||||||
|
self.message = message
|
||||||
|
self.error_code = error_code
|
||||||
class ProfileNotFound(BaseException):
|
class ProfileNotFound(BaseException):
|
||||||
pass
|
pass
|
||||||
class HardwareIncompatibilityError(BaseException):
|
class HardwareIncompatibilityError(BaseException):
|
||||||
|
|
|
||||||
|
|
@ -251,7 +251,7 @@ class sys_command():#Thread):
|
||||||
if self.exit_code != 0 and not self.kwargs['suppress_errors']:
|
if self.exit_code != 0 and not self.kwargs['suppress_errors']:
|
||||||
#self.log(self.trace_log.decode('UTF-8'), level=LOG_LEVELS.Debug)
|
#self.log(self.trace_log.decode('UTF-8'), level=LOG_LEVELS.Debug)
|
||||||
#self.log(f"'{self.raw_cmd}' did not exit gracefully, exit code {self.exit_code}.", level=LOG_LEVELS.Error)
|
#self.log(f"'{self.raw_cmd}' did not exit gracefully, exit code {self.exit_code}.", level=LOG_LEVELS.Error)
|
||||||
raise SysCallError(f"{self.trace_log.decode('UTF-8')}\n'{self.raw_cmd}' did not exit gracefully (trace log above), exit code: {self.exit_code}")
|
raise SysCallError(message=f"{self.trace_log.decode('UTF-8')}\n'{self.raw_cmd}' did not exit gracefully (trace log above), exit code: {self.exit_code}", exit_code=self.exit_code)
|
||||||
|
|
||||||
self.ended = time.time()
|
self.ended = time.time()
|
||||||
with open(f'{self.cwd}/trace.log', 'wb') as fh:
|
with open(f'{self.cwd}/trace.log', 'wb') as fh:
|
||||||
|
|
|
||||||
|
|
@ -64,12 +64,16 @@ class luks2():
|
||||||
with open(key_file, 'wb') as fh:
|
with open(key_file, 'wb') as fh:
|
||||||
fh.write(password)
|
fh.write(password)
|
||||||
|
|
||||||
cmd_handle = sys_command(f'/usr/bin/cryptsetup -q -v --type luks2 --pbkdf argon2i --hash {hash_type} --key-size {key_size} --iter-time {iter_time} --key-file {os.path.abspath(key_file)} --use-urandom luksFormat {partition.path}')
|
try:
|
||||||
if cmd_handle.exit_code == 256:
|
|
||||||
# Partition was in use, unmount it and
|
|
||||||
partition.unmount()
|
|
||||||
sys_command(f'cryptsetup close {partition.path}')
|
|
||||||
cmd_handle = sys_command(f'/usr/bin/cryptsetup -q -v --type luks2 --pbkdf argon2i --hash {hash_type} --key-size {key_size} --iter-time {iter_time} --key-file {os.path.abspath(key_file)} --use-urandom luksFormat {partition.path}')
|
cmd_handle = sys_command(f'/usr/bin/cryptsetup -q -v --type luks2 --pbkdf argon2i --hash {hash_type} --key-size {key_size} --iter-time {iter_time} --key-file {os.path.abspath(key_file)} --use-urandom luksFormat {partition.path}')
|
||||||
|
except SysCallError as err:
|
||||||
|
if err.exit_code == 256:
|
||||||
|
# Partition was in use, unmount it and try again
|
||||||
|
partition.unmount()
|
||||||
|
sys_command(f'cryptsetup close {partition.path}')
|
||||||
|
cmd_handle = sys_command(f'/usr/bin/cryptsetup -q -v --type luks2 --pbkdf argon2i --hash {hash_type} --key-size {key_size} --iter-time {iter_time} --key-file {os.path.abspath(key_file)} --use-urandom luksFormat {partition.path}')
|
||||||
|
else:
|
||||||
|
raise err
|
||||||
|
|
||||||
if b'Command successful.' not in b''.join(cmd_handle):
|
if b'Command successful.' not in b''.join(cmd_handle):
|
||||||
raise DiskError(f'Could not encrypt volume "{partition.path}": {o}')
|
raise DiskError(f'Could not encrypt volume "{partition.path}": {o}')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue