feat: implement encryption for automatic partitioning

This commit is contained in:
Rudra Saraswat 2024-06-08 05:08:17 +00:00
parent 844a3f851b
commit f413571bce

View file

@ -32,12 +32,15 @@ def preexec():
signal.signal(signal.SIGQUIT, signal.SIG_IGN) signal.signal(signal.SIGQUIT, signal.SIG_IGN)
def exec(cmd): def exec(cmd, input=None):
if testing: if testing:
if input != None:
print(' '.join(cmd), '<--', input)
else:
print(' '.join(cmd)) print(' '.join(cmd))
else: else:
subprocess.call(cmd, shell=False, stdout=sys.stdout, subprocess.run(cmd, shell=False, stdout=sys.stdout,
stderr=sys.stderr, preexec_fn=preexec) stderr=sys.stderr, preexec_fn=preexec, input=input.encode()).returncode
def exec_chroot(cmd): def exec_chroot(cmd):
@ -134,6 +137,7 @@ def inst_partition(config):
mode = config['partition']['mode'] mode = config['partition']['mode']
efi = config['partition']['efi'] efi = config['partition']['efi']
partitions = config['partition']['partitions'] partitions = config['partition']['partitions']
password = config['partition']['password']
# Delete partition table # Delete partition table
exec(['dd', 'if=/dev/zero', f'of={device}', 'bs=512', 'count=1']) exec(['dd', 'if=/dev/zero', f'of={device}', 'bs=512', 'count=1'])
@ -162,10 +166,15 @@ def inst_partition(config):
exec(['mkfs.vfat', '-F32', f'{device}p1']) exec(['mkfs.vfat', '-F32', f'{device}p1'])
else: else:
exec(['mkfs.ext4', f'{device}p1']) exec(['mkfs.ext4', f'{device}p1'])
root_partition = f'{device}p2'
if password != '':
exec(['cryptsetup', '-q', 'luksFormat', root_partition], input=f'{password}\n')
exec(['cryptsetup', 'open', root_partition, 'new_root', '-'], input=f'{password}\n')
root_partition = '/dev/mapper/new_root'
# Format root partition # Format root partition
exec(['mkfs.ext4', f'{device}p2']) exec(['mkfs.ext4', root_partition])
# Mount partitions # Mount partitions
mount(f'{device}p2', '/mnt') mount(root_partition, '/mnt')
if efi: if efi:
mkdir('/mnt/boot/efi') mkdir('/mnt/boot/efi')
mount(f'{device}p1', '/mnt/boot/efi') mount(f'{device}p1', '/mnt/boot/efi')
@ -178,10 +187,15 @@ def inst_partition(config):
exec(['mkfs.vfat', '-F32', f'{device}1']) exec(['mkfs.vfat', '-F32', f'{device}1'])
else: else:
exec(['mkfs.ext4', f'{device}1']) exec(['mkfs.ext4', f'{device}1'])
root_partition = f'{device}2'
if password != '':
exec(['cryptsetup', '-q', 'luksFormat', root_partition], input=f'{password}\n')
exec(['cryptsetup', 'open', root_partition, 'new_root', '-'], input=f'{password}\n')
root_partition = '/dev/mapper/new_root'
# Format root partition # Format root partition
exec(['mkfs.ext4', f'{device}2']) exec(['mkfs.ext4', root_partition])
# Mount partitions # Mount partitions
mount(f'{device}2', '/mnt') mount(root_partition, '/mnt')
if efi: if efi:
mkdir('/mnt/boot/efi') mkdir('/mnt/boot/efi')
mount(f'{device}1', '/mnt/boot/efi') mount(f'{device}1', '/mnt/boot/efi')