From 9f0d25bce302459efa5ab5b9eaf22bb1f1feb575 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Tue, 9 Mar 2021 11:48:40 +0100 Subject: Added functions to auto-detect filesystems inside encrypted partitions (given a password) --- archinstall/lib/disk.py | 5 +++++ archinstall/lib/luks.py | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'archinstall') diff --git a/archinstall/lib/disk.py b/archinstall/lib/disk.py index 335ec42e..75cecb0b 100644 --- a/archinstall/lib/disk.py +++ b/archinstall/lib/disk.py @@ -188,6 +188,11 @@ class Partition(): return f"/dev/{parent}" raise DiskError(f'Could not find appropriate parent for encrypted partition {self}') + def detect_inner_filesystem(self, password): + from .luks import luks2 + with luks2(self, 'luksloop', password, auto_unmount=True) as unlocked_device: + return unlocked_device.filesystem + def has_content(self): temporary_mountpoint = '/tmp/'+hashlib.md5(bytes(f"{time.time()}", 'UTF-8')+os.urandom(12)).hexdigest() temporary_path = pathlib.Path(temporary_mountpoint) diff --git a/archinstall/lib/luks.py b/archinstall/lib/luks.py index c9946239..fa040788 100644 --- a/archinstall/lib/luks.py +++ b/archinstall/lib/luks.py @@ -6,13 +6,14 @@ from .output import log, LOG_LEVELS from .storage import storage class luks2(): - def __init__(self, partition, mountpoint, password, key_file=None, *args, **kwargs): + def __init__(self, partition, mountpoint, password, key_file=None, auto_unmount=False, *args, **kwargs): self.password = password self.partition = partition self.mountpoint = mountpoint self.args = args self.kwargs = kwargs self.key_file = key_file + self.auto_unmount = auto_unmount self.filesystem = 'crypto_LUKS' def __enter__(self): @@ -32,6 +33,9 @@ class luks2(): def __exit__(self, *args, **kwargs): # TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager + if self.auto_unmount: + self.close() + if len(args) >= 2 and args[1]: raise args[1] return True @@ -73,11 +77,12 @@ class luks2(): :param mountpoint: The name without absolute path, for instance "luksdev" will point to /dev/mapper/luksdev :type mountpoint: str """ + from .disk import get_filesystem_type if '/' in mountpoint: os.path.basename(mountpoint) # TODO: Raise exception instead? sys_command(f'/usr/bin/cryptsetup open {partition.path} {mountpoint} --key-file {os.path.abspath(key_file)} --type luks2') if os.path.islink(f'/dev/mapper/{mountpoint}'): - return Partition(f'/dev/mapper/{mountpoint}', encrypted=True) + return Partition(f'/dev/mapper/{mountpoint}', encrypted=True, filesystem=get_filesystem_type(f'/dev/mapper/{mountpoint}')) def close(self, mountpoint): sys_command(f'cryptsetup close /dev/mapper/{mountpoint}') -- cgit v1.2.3-70-g09d2