Enhanced the error handling on crypt-devices.

This commit is contained in:
Anton Hvornum 2021-03-14 12:50:47 +01:00
parent e4514e8fc3
commit 1c6d705714
No known key found for this signature in database
GPG Key ID: F1234C5BA67C59DF
1 changed files with 16 additions and 7 deletions

View File

@ -65,19 +65,28 @@ class luks2():
fh.write(password)
try:
# Try to setup the crypt-device
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()
try:
sys_command(f'cryptsetup close {partition.path}')
except SysCallError as err:
# 0 Means everything went smoothly,
# 1024 means the device was not found.
if err.exit_code not in (0, 1024):
raise err
# Get crypt-information about the device by doing a reverse lookup starting with the partition path
# For instance: /dev/sda
devinfo = json.loads(b''.join(sys_command(f"lsblk --fs -J {partition.path}")).decode('UTF-8'))['blockdevices'][0]
# For each child (sub-partition/sub-device)
if len(children := devinfo.get('children', [])):
for child in children:
# Unmount the child location
if child_mountpoint := child.get('mountpoint', None):
sys_command(f"umount {child_mountpoint}")
# And close it if possible.
sys_command(f"cryptsetup close {child['name']}")
# Then try again to set up the crypt-device
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