From 1c6d70571481e30651f62f216fcc9653e03c9c62 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Sun, 14 Mar 2021 12:50:47 +0100 Subject: Enhanced the error handling on crypt-devices. --- archinstall/lib/luks.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'archinstall/lib') diff --git a/archinstall/lib/luks.py b/archinstall/lib/luks.py index 77622304..f4c787ab 100644 --- a/archinstall/lib/luks.py +++ b/archinstall/lib/luks.py @@ -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 -- cgit v1.2.3-54-g00ecf