From 4b07f8a3ae23680238f33a778217dd3b4f94a50d Mon Sep 17 00:00:00 2001 From: codefiles <11915375+codefiles@users.noreply.github.com> Date: Sun, 23 Feb 2025 15:16:11 -0500 Subject: [PATCH] Rework default partition table (#3197) --- archinstall/lib/disk/device_handler.py | 20 +++++++++++--------- archinstall/lib/disk/filesystem.py | 4 +--- archinstall/lib/interactions/disk_conf.py | 5 ++--- archinstall/lib/models/device_model.py | 16 ++++++++++++---- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/archinstall/lib/disk/device_handler.py b/archinstall/lib/disk/device_handler.py index 961bab2f..4606ab24 100644 --- a/archinstall/lib/disk/device_handler.py +++ b/archinstall/lib/disk/device_handler.py @@ -52,12 +52,17 @@ class DeviceHandler: def __init__(self) -> None: self._devices: dict[Path, BDevice] = {} + self._partition_table = PartitionTable.default() self.load_devices() @property def devices(self) -> list[BDevice]: return list(self._devices.values()) + @property + def partition_table(self) -> PartitionTable: + return self._partition_table + def load_devices(self) -> None: block_devices = {} @@ -86,7 +91,7 @@ class DeviceHandler: if dev_lsblk_info.pttype: disk = newDisk(device) else: - disk = freshDisk(device, PartitionTable.GPT.value) + disk = freshDisk(device, self.partition_table.value) except DiskException as err: debug(f'Unable to get disk from {device.path}: {err}') continue @@ -702,18 +707,15 @@ class DeviceHandler: """ Create a partition table on the block device and create all partitions. """ - if modification.wipe: - if partition_table is None: - raise ValueError('Modification is marked as wipe but no partitioning table was provided') - - if partition_table.is_mbr() and len(modification.partitions) > 3: - raise DiskError('Too many partitions on disk, MBR disks can only have 3 primary partitions') + partition_table = partition_table or self.partition_table # WARNING: the entire device will be wiped and all data lost if modification.wipe: + if partition_table.is_mbr() and len(modification.partitions) > 3: + raise DiskError('Too many partitions on disk, MBR disks can only have 3 primary partitions') + self.wipe_dev(modification.device) - part_table = partition_table.value if partition_table else None - disk = freshDisk(modification.device.disk.device, part_table) + disk = freshDisk(modification.device.disk.device, partition_table.value) else: info(f'Use existing device: {modification.device_path}') disk = modification.device.disk diff --git a/archinstall/lib/disk/filesystem.py b/archinstall/lib/disk/filesystem.py index 8947f209..5e4e97a7 100644 --- a/archinstall/lib/disk/filesystem.py +++ b/archinstall/lib/disk/filesystem.py @@ -18,7 +18,6 @@ from ..models.device_model import ( LvmVolume, LvmVolumeGroup, PartitionModification, - PartitionTable, SectorSize, Size, Unit, @@ -61,14 +60,13 @@ class FilesystemHandler: # Setup the blockdevice, filesystem (and optionally encryption). # Once that's done, we'll hand over to perform_installation() - partition_table = PartitionTable.default() # make sure all devices are unmounted for mod in device_mods: device_handler.umount_all_existing(mod.device_path) for mod in device_mods: - device_handler.partition(mod, partition_table=partition_table) + device_handler.partition(mod) device_handler.udev_sync() diff --git a/archinstall/lib/interactions/disk_conf.py b/archinstall/lib/interactions/disk_conf.py index 37e7819c..0cb4c19e 100644 --- a/archinstall/lib/interactions/disk_conf.py +++ b/archinstall/lib/interactions/disk_conf.py @@ -30,7 +30,6 @@ from archinstall.lib.models.device_model import ( from archinstall.lib.output import debug from archinstall.tui import Alignment, FrameProperties, MenuItem, MenuItemGroup, Orientation, PreviewStyle, ResultType, SelectMenu -from ..hardware import SysInfo from ..output import FormattedOutput from ..utils.util import prompt_dir @@ -354,7 +353,7 @@ def suggest_single_disk_layout( device_modification = DeviceModification(device, wipe=True) - using_gpt = SysInfo.has_uefi() + using_gpt = device_handler.partition_table.is_gpt() if using_gpt: available_space = available_space.gpt_end() @@ -515,7 +514,7 @@ def suggest_multi_disk_layout( root_device_sector_size = root_device_modification.device.device_info.sector_size home_device_sector_size = home_device_modification.device.device_info.sector_size - using_gpt = SysInfo.has_uefi() + using_gpt = device_handler.partition_table.is_gpt() # add boot partition to the root device boot_partition = _boot_partition(root_device_sector_size, using_gpt) diff --git a/archinstall/lib/models/device_model.py b/archinstall/lib/models/device_model.py index 222e71e4..0595ab2b 100644 --- a/archinstall/lib/models/device_model.py +++ b/archinstall/lib/models/device_model.py @@ -144,8 +144,6 @@ class DiskLayoutConfiguration: device_modification.partitions = device_partitions device_modifications.append(device_modification) - using_gpt = SysInfo.has_uefi() - for dev_mod in device_modifications: dev_mod.partitions.sort(key=lambda p: (not p.is_delete(), p.start)) @@ -186,8 +184,9 @@ class DiskLayoutConfiguration: last = create_partitions[-1] total_size = dev_mod.device.device_info.total_size - if using_gpt and last.end > total_size.gpt_end(): - raise ValueError('Partition overlaps backup GPT header') + if dev_mod.using_gpt(device_handler.partition_table): + if last.end > total_size.gpt_end(): + raise ValueError('Partition overlaps backup GPT header') elif last.end > total_size.align(): raise ValueError('Partition too large for device') @@ -202,6 +201,9 @@ class PartitionTable(Enum): GPT = 'gpt' MBR = 'msdos' + def is_gpt(self) -> bool: + return self == PartitionTable.GPT + def is_mbr(self) -> bool: return self == PartitionTable.MBR @@ -1359,6 +1361,12 @@ class DeviceModification: def device_path(self) -> Path: return self.device.device_info.path + def using_gpt(self, partition_table: PartitionTable) -> bool: + if self.wipe: + return partition_table.is_gpt() + + return self.device.disk.type == PartitionTable.GPT.value + def add_partition(self, partition: PartitionModification) -> None: self.partitions.append(partition)