Added wipe support to layout definitions. Also changed default start positions of partitions to 1MiB in.

This commit is contained in:
Anton Hvornum 2021-06-10 19:29:10 +02:00
parent 857b5c0c3e
commit 4691bad46b
3 changed files with 73 additions and 67 deletions

View File

@ -571,15 +571,23 @@ class Filesystem:
return True
def load_layout(self, layout :dict):
for partition in layout:
# If the layout tells us to wipe the drive, we do so
if layout.get('wipe', False):
if self.mode == GPT:
if not self.parted_mklabel(self.blockdevice.device, "gpt"):
raise KeyError(f"Could not create a GPT label on {self}")
elif self.mode == MBR:
if not self.parted_mklabel(self.blockdevice.device, "msdos"):
raise KeyError(f"Could not create a MSDOS label on {self}")
# We then iterate the partitions in order
for partition in layout.get('partitions', []):
# We don't want to re-add an existing partition (those containing a UUID already)
if 'UUID' not in partition:
self.add_partition(partition.get('type', 'primary'),
start=partition.get('start', '1MiB'), # TODO: Revisit sane block starts (4MB for memorycards for instance)
end=partition.get('size', '100%'),
partition_format=partition.get('filesystem', {}).get('format', 'btrfs'))
exit(0)
@ -589,7 +597,9 @@ class Filesystem:
return partition
def raw_parted(self, string: str):
return SysCommand(f'/usr/bin/parted -s {string}')
if (cmd_handle := SysCommand(f'/usr/bin/parted -s {string}')).exit_code != 0:
log(f"Could not generate partition: {cmd_handle}", level=logging.ERROR, fg="red")
return cmd_handle
def parted(self, string: str):
"""
@ -601,35 +611,11 @@ class Filesystem:
return self.raw_parted(string).exit_code
def use_entire_disk(self, root_filesystem_type='ext4'):
log(f"Using and formatting the entire {self.blockdevice}.", level=logging.DEBUG)
if has_uefi():
self.add_partition('primary', start='1MiB', end='513MiB', partition_format='fat32')
self.set_name(0, 'EFI')
self.set(0, 'boot on')
# TODO: Probably redundant because in GPT mode 'esp on' is an alias for "boot on"?
# https://www.gnu.org/software/parted/manual/html_node/set.html
self.set(0, 'esp on')
self.add_partition('primary', start='513MiB', end='100%')
self.blockdevice.partition[0].filesystem = 'vfat'
self.blockdevice.partition[1].filesystem = root_filesystem_type
log(f"Set the root partition {self.blockdevice.partition[1]} to use filesystem {root_filesystem_type}.", level=logging.DEBUG)
self.blockdevice.partition[0].target_mountpoint = '/boot'
self.blockdevice.partition[1].target_mountpoint = '/'
self.blockdevice.partition[0].allow_formatting = True
self.blockdevice.partition[1].allow_formatting = True
else:
# we don't need a seprate boot partition it would be a waste of space
self.add_partition('primary', start='1MB', end='100%')
self.blockdevice.partition[0].filesystem = root_filesystem_type
log(f"Set the root partition {self.blockdevice.partition[0]} to use filesystem {root_filesystem_type}.", level=logging.DEBUG)
self.blockdevice.partition[0].target_mountpoint = '/'
self.blockdevice.partition[0].allow_formatting = True
# TODO: Implement this with declarative profiles instead.
raise ValueError("Installation().use_entire_disk() has to be re-worked.")
def add_partition(self, partition_type, start, end, partition_format=None):
log(f'Adding partition to {self.blockdevice}', level=logging.INFO)
log(f'Adding partition to {self.blockdevice}, {start}->{end}', level=logging.INFO)
previous_partitions = self.blockdevice.partitions
if self.mode == MBR:

View File

@ -322,6 +322,15 @@ class SysCommand:
for line in self.session:
yield line
def __getitem__(self, key):
if type(key) is slice:
start = key.start if key.start else 0
end = key.stop if key.stop else len(self.session._trace_log)
return self.session._trace_log[start:end]
else:
raise ValueError("SysCommand() doesn't have key & value pairs, only slices, SysCommand('ls')[:10] as an example.")
def __repr__(self, *args, **kwargs):
return self.session._trace_log.decode('UTF-8')

View File

@ -191,8 +191,6 @@ def generic_multi_select(options, text="Select one or more of the options above
return selected_options
def select_encrypted_partitions(blockdevices :dict) -> dict:
print(blockdevices[0])
if len(blockdevices) == 1:
if len(blockdevices[0]['partitions']) == 2:
root = find_partition_by_mountpoint(blockdevices[0]['partitions'], '/')
@ -617,40 +615,53 @@ def partition_overlap(partitions :list, start :str, end :str) -> bool:
def get_default_partition_layout(block_devices):
if len(block_devices) == 1:
return {
block_devices[0] : [
{ # Boot
"type" : "primary",
"start" : "0MiB",
"size" : "513MiB",
"boot" : True,
"mountpoint" : "/boot",
"filesystem" : {
"format" : "fat32"
}
},
{ # Root
"type" : "primary",
"start" : "513MiB",
"encrypted" : True,
"size" : f"{max(block_devices[0].size*0.2, 20)*1024}MiB", # Warning: Won't work on small where max size is 16GB for instance.
"mountpoint" : "/",
"filesystem" : {
"format" : "btrfs"
}
},
{ # Home
"type" : "primary",
"encrypted" : True,
"start" : f"{513 + (max(block_devices[0].size*0.2, 20)*1024)}MiB",
"size" : "100%",
"mountpoint" : "/home",
"filesystem" : {
"format" : "btrfs"
}
}
]
MIN_SIZE_TO_ALLOW_HOME_PART = 40 # Gb
layout = {
block_devices[0] : {
"wipe" : True,
"partitions" : []
}
}
layout[block_devices[0]]['partitions'].append({
# Boot
"type" : "primary",
"start" : "1MiB",
"size" : "513MiB",
"boot" : True,
"mountpoint" : "/boot",
"filesystem" : {
"format" : "fat32"
}
})
layout[block_devices[0]]['partitions'].append({
# Root
"type" : "primary",
"start" : "513MiB",
"encrypted" : True,
"size" : "100%" if block_devices[0].size < MIN_SIZE_TO_ALLOW_HOME_PART else f"{min(block_devices[0].size, 20)*1024}MiB",
"mountpoint" : "/",
"filesystem" : {
"format" : "btrfs"
}
})
if block_devices[0].size > MIN_SIZE_TO_ALLOW_HOME_PART:
layout[block_devices[0]]['partitions'].append({
# Home
"type" : "primary",
"encrypted" : True,
"start" : f"{min(block_devices[0].size*0.2, 20)*1024}MiB",
"size" : "100%",
"mountpoint" : "/home",
"filesystem" : {
"format" : "btrfs"
}
})
return layout
# TODO: Implement sane generic layout for 2+ drives
def wipe_and_create_partitions(block_device :BlockDevice) -> dict:
@ -663,7 +674,7 @@ def wipe_and_create_partitions(block_device :BlockDevice) -> dict:
suggested_layout = [
{ # Boot
"type" : "primary",
"start" : "0MiB",
"start" : "1MiB",
"size" : "513MiB",
"boot" : True,
"mountpoint" : "/boot",