Unifying partioning logic into one function when managing partitions. Rather than giving the option between wiping and creating, and re-using and creating

This commit is contained in:
Anton Hvornum 2021-06-14 16:20:26 +02:00
parent 00b1dfde6d
commit eb4c134c80
1 changed files with 38 additions and 31 deletions

View File

@ -615,30 +615,35 @@ def manage_new_and_existing_partitions(block_device :BlockDevice) -> dict:
# return struct # return struct
partitions_result = [] # Test code: [part.__dump__() for part in block_device.partitions.values()] mountpoints = {}
block_device_struct = {
"partitions" : []
}
# Test code: [part.__dump__() for part in block_device.partitions.values()]
# TODO: Squeeze in BTRFS subvolumes here # TODO: Squeeze in BTRFS subvolumes here
while True: while True:
modes = [ modes = [
"Create new partition", "Create a new partition",
"Suggest partition layout", f"Suggest partition layout for {block_device}",
"Delete partition" if len(partitions_result) else "", "Delete a partition" if len(block_device_struct) else "",
"Assign mount-point for partition" if len(partitions_result) else "", "Clear/Delete all partitions" if len(block_device_struct) else "",
"Mark/Unmark a partition as encrypted" if len(partitions_result) else "", "Assign mount-point for a partition" if len(block_device_struct) else "",
"Mark/Unmark a partition as bootable (automatic for /boot)" if len(partitions_result) else "" "Mark/Unmark a partition as encrypted" if len(block_device_struct) else "",
"Mark/Unmark a partition as bootable (automatic for /boot)" if len(block_device_struct) else ""
] ]
# Print current partition layout: # Print current partition layout:
if len(partitions_result): if len(block_device_struct["partitions"]):
print('Current partition layout:') print('Current partition layout:')
for partition in partitions_result: for partition in block_device_struct["partitions"]:
print({key: val for key, val in partition.items() if val}) print(partition)
print() print()
task = generic_select(modes, task = generic_select(modes,
input_text=f"Select what to do with {block_device} (leave blank when done): ") input_text=f"Select what to do with {block_device} (leave blank when done): ")
if task == 'Create new partition': if task == 'Create a new partition':
if partition_type == 'gpt': if partition_type == 'gpt':
# https://www.gnu.org/software/parted/manual/html_node/mkpart.html # https://www.gnu.org/software/parted/manual/html_node/mkpart.html
# https://www.gnu.org/software/parted/manual/html_node/mklabel.html # https://www.gnu.org/software/parted/manual/html_node/mklabel.html
@ -648,11 +653,11 @@ def manage_new_and_existing_partitions(block_device :BlockDevice) -> dict:
end = input("Enter the end sector of the partition (percentage or block number, ex: 100%): ").strip() end = input("Enter the end sector of the partition (percentage or block number, ex: 100%): ").strip()
if valid_parted_position(start) and valid_parted_position(end) and valid_fs_type(fstype): if valid_parted_position(start) and valid_parted_position(end) and valid_fs_type(fstype):
if partition_overlap(partitions_result, start, end): if partition_overlap(block_device_struct, start, end):
log(f"This partition overlaps with other partitions on the drive! Ignoring this partition creation.", fg="red") log(f"This partition overlaps with other partitions on the drive! Ignoring this partition creation.", fg="red")
continue continue
partitions_result.append({ block_device_struct.append({
"type" : "primary", # Strictly only allowed under MSDOS, but GPT accepts it so it's "safe" to inject "type" : "primary", # Strictly only allowed under MSDOS, but GPT accepts it so it's "safe" to inject
"start" : start, "start" : start,
"size" : end, "size" : end,
@ -664,47 +669,49 @@ def manage_new_and_existing_partitions(block_device :BlockDevice) -> dict:
else: else:
log(f"Invalid start, end or fstype for this partition. Ignoring this partition creation.", fg="red") log(f"Invalid start, end or fstype for this partition. Ignoring this partition creation.", fg="red")
continue continue
elif task == "Suggest partition layout": elif task[:len("Suggest partition layout")] == "Suggest partition layout":
if len(partitions_result): if len(block_device_struct):
if input(f"{block_device} contains queued partitions, this will remove those, are you sure? y/N: ").strip().lower() in ('', 'n'): if input(f"{block_device} contains queued partitions, this will remove those, are you sure? y/N: ").strip().lower() in ('', 'n'):
continue continue
partitions_result = suggest_single_disk_layout(block_device)[block_device] block_device_struct = suggest_single_disk_layout(block_device)[block_device]
elif task is None: elif task is None:
return { return {
block_device : partitions_result block_device : block_device_struct
} }
else: else:
for index, partition in enumerate(partitions_result): for index, partition in enumerate(block_device_struct):
print(f"{index}: Start: {partition['start']}, End: {partition['size']} ({partition['filesystem']['format']}{', mounting at: '+partition['mountpoint'] if partition['mountpoint'] else ''})") print(f"{index}: Start: {partition['start']}, End: {partition['size']} ({partition['filesystem']['format']}{', mounting at: '+partition['mountpoint'] if partition['mountpoint'] else ''})")
if task == "Delete partition": if task == "Delete a partition":
if (partition := generic_select(partitions_result, 'Select which partition to delete: ', options_output=False)): if (partition := generic_select(block_device_struct, 'Select which partition to delete: ', options_output=False)):
del(partitions_result[partitions_result.index(partition)]) del(block_device_struct[block_device_struct.index(partition)])
elif task == "Assign mount-point for partition": elif task == "Clear/Delete all partitions":
if (partition := generic_select(partitions_result, 'Select which partition to mount where: ', options_output=False)): block_device_struct = []
elif task == "Assign mount-point for a partition":
if (partition := generic_select(block_device_struct, 'Select which partition to mount where: ', options_output=False)):
print(' * Partition mount-points are relative to inside the installation, the boot would be /boot as an example.') print(' * Partition mount-points are relative to inside the installation, the boot would be /boot as an example.')
mountpoint = input('Select where to mount partition (leave blank to remove mountpoint): ').strip() mountpoint = input('Select where to mount partition (leave blank to remove mountpoint): ').strip()
if len(mountpoint): if len(mountpoint):
partitions_result[partitions_result.index(partition)]['mountpoint'] = mountpoint block_device_struct[block_device_struct.index(partition)]['mountpoint'] = mountpoint
if mountpoint == '/boot': if mountpoint == '/boot':
log(f"Marked partition as bootable because mountpoint was set to /boot.", fg="yellow") log(f"Marked partition as bootable because mountpoint was set to /boot.", fg="yellow")
partitions_result[partitions_result.index(partition)]['boot'] = True block_device_struct[block_device_struct.index(partition)]['boot'] = True
else: else:
del(partitions_result[partitions_result.index(partition)]['mountpoint']) del(block_device_struct[block_device_struct.index(partition)]['mountpoint'])
elif task == "Mark/Unmark a partition as encrypted": elif task == "Mark/Unmark a partition as encrypted":
if (partition := generic_select(partitions_result, 'Select which partition to mark as encrypted: ', options_output=False)): if (partition := generic_select(block_device_struct, 'Select which partition to mark as encrypted: ', options_output=False)):
# Negate the current encryption marking # Negate the current encryption marking
partitions_result[partitions_result.index(partition)]['encrypted'] = not partitions_result[partitions_result.index(partition)].get('encrypted', False) block_device_struct[block_device_struct.index(partition)]['encrypted'] = not block_device_struct[block_device_struct.index(partition)].get('encrypted', False)
elif task == "Mark/Unmark a partition as bootable (automatic for /boot)": elif task == "Mark/Unmark a partition as bootable (automatic for /boot)":
if (partition := generic_select(partitions_result, 'Select which partition to mark as bootable: ', options_output=False)): if (partition := generic_select(block_device_struct, 'Select which partition to mark as bootable: ', options_output=False)):
partitions_result[partitions_result.index(partition)]['boot'] = not partitions_result[partitions_result.index(partition)].get('boot', False) block_device_struct[block_device_struct.index(partition)]['boot'] = not block_device_struct[block_device_struct.index(partition)].get('boot', False)
return { return {
block_device : partitions_result block_device : block_device_struct
} }
def select_individual_blockdevice_usage(block_devices :list): def select_individual_blockdevice_usage(block_devices :list):