Added functions to auto-detect filesystems inside encrypted partitions (given a password)
This commit is contained in:
parent
586f8bc32e
commit
9f0d25bce3
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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}')
|
||||
|
|
|
|||
Loading…
Reference in New Issue