From 335a7b792c38ec1f4d08dc3c89dea19abd2d2034 Mon Sep 17 00:00:00 2001 From: codefiles <11915375+codefiles@users.noreply.github.com> Date: Mon, 13 Apr 2026 04:41:59 -0400 Subject: [PATCH] Refactor FilesystemType (#4417) * Use UPPER_CASE for FilesystemType member names * Use StrEnum for FilesystemType * Use FilesystemType member values --- archinstall/lib/disk/device_handler.py | 18 ++++----- archinstall/lib/disk/disk_menu.py | 16 ++++---- archinstall/lib/disk/filesystem.py | 8 ++-- archinstall/lib/disk/partitioning_menu.py | 12 +++--- archinstall/lib/global_menu.py | 4 +- archinstall/lib/installer.py | 14 +++---- archinstall/lib/models/device.py | 49 +++++++++++++---------- docs/examples/python.rst | 2 +- examples/full_automated_installation.py | 2 +- 9 files changed, 65 insertions(+), 60 deletions(-) diff --git a/archinstall/lib/disk/device_handler.py b/archinstall/lib/disk/device_handler.py index b54d45ad..1c7c5923 100644 --- a/archinstall/lib/disk/device_handler.py +++ b/archinstall/lib/disk/device_handler.py @@ -99,7 +99,7 @@ class DeviceHandler: fs_type = self._determine_fs_type(partition, lsblk_info) subvol_infos = [] - if fs_type == FilesystemType.Btrfs: + if fs_type == FilesystemType.BTRFS: subvol_infos = self.get_btrfs_info(partition.path, lsblk_info) partition_infos.append( @@ -147,8 +147,8 @@ class DeviceHandler: ) -> FilesystemType | None: try: if partition.fileSystem: - if partition.fileSystem.type == FilesystemType.LinuxSwap.parted_value: - return FilesystemType.LinuxSwap + if partition.fileSystem.type == FilesystemType.LINUX_SWAP.parted_value: + return FilesystemType.LINUX_SWAP return FilesystemType(partition.fileSystem.type) elif lsblk_info is not None: return FilesystemType(lsblk_info.fstype) if lsblk_info.fstype else None @@ -241,20 +241,20 @@ class DeviceHandler: options = [] match fs_type: - case FilesystemType.Btrfs | FilesystemType.Xfs: + case FilesystemType.BTRFS | FilesystemType.XFS: # Force overwrite options.append('-f') - case FilesystemType.F2fs: + case FilesystemType.F2FS: options.append('-f') options.extend(('-O', 'extra_attr')) - case FilesystemType.Ext2 | FilesystemType.Ext3 | FilesystemType.Ext4: + case FilesystemType.EXT2 | FilesystemType.EXT3 | FilesystemType.EXT4: # Force create options.append('-F') - case FilesystemType.Fat12 | FilesystemType.Fat16 | FilesystemType.Fat32: + case FilesystemType.FAT12 | FilesystemType.FAT16 | FilesystemType.FAT32: mkfs_type = 'fat' # Set FAT size options.extend(('-F', fs_type.value.removeprefix(mkfs_type))) - case FilesystemType.LinuxSwap: + case FilesystemType.LINUX_SWAP: command = 'mkswap' case _: raise UnknownFilesystemFormat(f'Filetype "{fs_type.value}" is not supported') @@ -505,7 +505,7 @@ class DeviceHandler: debug(f'Unmounting: {partition.path}') # un-mount for existing encrypted partitions - if partition.fs_type == FilesystemType.Crypto_luks: + if partition.fs_type == FilesystemType.CRYPTO_LUKS: Luks2(partition.path).lock() else: umount(partition.path, recursive=True) diff --git a/archinstall/lib/disk/disk_menu.py b/archinstall/lib/disk/disk_menu.py index 35f11331..245d096d 100644 --- a/archinstall/lib/disk/disk_menu.py +++ b/archinstall/lib/disk/disk_menu.py @@ -508,17 +508,17 @@ def _boot_partition(sector_size: SectorSize, using_gpt: bool) -> PartitionModifi start=start, length=size, mountpoint=Path('/boot'), - fs_type=FilesystemType.Fat32, + fs_type=FilesystemType.FAT32, flags=flags, ) async def select_main_filesystem_format() -> FilesystemType: items = [ - MenuItem('btrfs', value=FilesystemType.Btrfs), - MenuItem('ext4', value=FilesystemType.Ext4), - MenuItem('xfs', value=FilesystemType.Xfs), - MenuItem('f2fs', value=FilesystemType.F2fs), + MenuItem(FilesystemType.BTRFS.value, value=FilesystemType.BTRFS), + MenuItem(FilesystemType.EXT4.value, value=FilesystemType.EXT4), + MenuItem(FilesystemType.XFS.value, value=FilesystemType.XFS), + MenuItem(FilesystemType.F2FS.value, value=FilesystemType.F2FS), ] group = MenuItemGroup(items, sort_items=False) @@ -601,7 +601,7 @@ async def suggest_single_disk_layout( available_space = total_size min_size_to_allow_home_part = Size(64, Unit.GiB, sector_size) - if filesystem_type == FilesystemType.Btrfs: + if filesystem_type == FilesystemType.BTRFS: prompt = tr('Would you like to use BTRFS subvolumes with a default structure?') + '\n' result = await Confirmation( @@ -734,7 +734,7 @@ async def suggest_multi_disk_layout( _ = await Notify(text).show() return [] - if filesystem_type == FilesystemType.Btrfs: + if filesystem_type == FilesystemType.BTRFS: mount_options = await select_mount_options() device_paths = ', '.join(str(d.device_info.path) for d in devices) @@ -817,7 +817,7 @@ async def suggest_lvm_layout( if not filesystem_type: filesystem_type = await select_main_filesystem_format() - if filesystem_type == FilesystemType.Btrfs: + if filesystem_type == FilesystemType.BTRFS: prompt = tr('Would you like to use BTRFS subvolumes with a default structure?') + '\n' result = await Confirmation(header=prompt, allow_skip=False, preset=True).show() diff --git a/archinstall/lib/disk/filesystem.py b/archinstall/lib/disk/filesystem.py index 28786530..67636491 100644 --- a/archinstall/lib/disk/filesystem.py +++ b/archinstall/lib/disk/filesystem.py @@ -70,7 +70,7 @@ class FilesystemHandler: self._format_partitions(mod.partitions) for part_mod in mod.partitions: - if part_mod.fs_type == FilesystemType.Btrfs and part_mod.is_create_or_modify(): + if part_mod.fs_type == FilesystemType.BTRFS and part_mod.is_create_or_modify(): device_handler.create_btrfs_volumes(part_mod, enc_conf=self._enc_config) def _format_partitions( @@ -113,7 +113,7 @@ class FilesystemHandler: # verify that all partitions have a path set (which implies that they have been created) lambda x: x.dev_path is None: ValueError('When formatting, all partitions must have a path set'), # crypto luks is not a valid file system type - lambda x: x.fs_type is FilesystemType.Crypto_luks: ValueError('Crypto luks cannot be set as a filesystem type'), + lambda x: x.fs_type is FilesystemType.CRYPTO_LUKS: ValueError('Crypto luks cannot be set as a filesystem type'), # file system type must be set lambda x: x.fs_type is None: ValueError('File system type must be set for modification'), } @@ -230,7 +230,7 @@ class FilesystemHandler: # find the mapper device yet device_handler.format(vol.fs_type, path) - if vol.fs_type == FilesystemType.Btrfs: + if vol.fs_type == FilesystemType.BTRFS: device_handler.create_lvm_btrfs_subvolumes(path, vol.btrfs_subvols, vol.mount_options) def _lvm_create_pvs( @@ -318,7 +318,7 @@ class FilesystemHandler: # from arch wiki: # If a logical volume will be formatted with ext4, leave at least 256 MiB # free space in the volume group to allow using e2scrub - if any([vol.fs_type == FilesystemType.Ext4 for vol in vol_gp.volumes]): + if any([vol.fs_type == FilesystemType.EXT4 for vol in vol_gp.volumes]): largest_vol = max(vol_gp.volumes, key=lambda x: x.length) lvm_vol_reduce( diff --git a/archinstall/lib/disk/partitioning_menu.py b/archinstall/lib/disk/partitioning_menu.py index feb97c6c..b0ae7c11 100644 --- a/archinstall/lib/disk/partitioning_menu.py +++ b/archinstall/lib/disk/partitioning_menu.py @@ -255,7 +255,7 @@ class PartitioningList(ListManager[DiskSegment]): ] # non btrfs partitions shouldn't get btrfs options - if selection.segment.fs_type != FilesystemType.Btrfs: + if selection.segment.fs_type != FilesystemType.BTRFS: not_filter += [ self._actions['btrfs_mark_compressed'], self._actions['btrfs_mark_nodatacow'], @@ -332,7 +332,7 @@ class PartitioningList(ListManager[DiskSegment]): partition.flags = [] partition.set_flag(PartitionFlag.SWAP) # btrfs subvolumes will define mountpoints - if fs_type == FilesystemType.Btrfs: + if fs_type == FilesystemType.BTRFS: partition.mountpoint = None case 'btrfs_mark_compressed': self._toggle_mount_option(partition, BtrfsMountOption.compress) @@ -399,12 +399,12 @@ class PartitioningList(ListManager[DiskSegment]): # If we mark a partition for formatting, but the format is CRYPTO LUKS, there's no point in formatting it really # without asking the user which inner-filesystem they want to use. Since the flag 'encrypted' = True is already set, # it's safe to change the filesystem for this partition. - if partition.fs_type == FilesystemType.Crypto_luks: + if partition.fs_type == FilesystemType.CRYPTO_LUKS: prompt = tr('This partition is currently encrypted, to format it a filesystem has to be specified') + '\n' fs_type = await self._prompt_partition_fs_type(prompt) partition.fs_type = fs_type - if fs_type == FilesystemType.Btrfs: + if fs_type == FilesystemType.BTRFS: partition.mountpoint = None async def _prompt_mountpoint(self) -> Path: @@ -417,7 +417,7 @@ class PartitioningList(ListManager[DiskSegment]): return mountpoint async def _prompt_partition_fs_type(self, prompt: str | None = None) -> FilesystemType: - fs_types = filter(lambda fs: fs != FilesystemType.Crypto_luks, FilesystemType) + fs_types = filter(lambda fs: fs != FilesystemType.CRYPTO_LUKS, FilesystemType) items = [MenuItem(fs.value, value=fs) for fs in fs_types] group = MenuItemGroup(items, sort_items=False) @@ -522,7 +522,7 @@ class PartitioningList(ListManager[DiskSegment]): fs_type = await self._prompt_partition_fs_type() mountpoint = None - if fs_type not in (FilesystemType.Btrfs, FilesystemType.LinuxSwap): + if fs_type not in (FilesystemType.BTRFS, FilesystemType.LINUX_SWAP): mountpoint = await self._prompt_mountpoint() partition = PartitionModification( diff --git a/archinstall/lib/global_menu.py b/archinstall/lib/global_menu.py index 6e4b6720..2baef93d 100644 --- a/archinstall/lib/global_menu.py +++ b/archinstall/lib/global_menu.py @@ -486,11 +486,11 @@ class GlobalMenu(AbstractMenu[None]): if efi_partition is None: return 'EFI system partition (ESP) not found' - if efi_partition.fs_type not in [FilesystemType.Fat12, FilesystemType.Fat16, FilesystemType.Fat32]: + if efi_partition.fs_type not in [FilesystemType.FAT12, FilesystemType.FAT16, FilesystemType.FAT32]: return 'ESP must be formatted as a FAT filesystem' if bootloader == Bootloader.Limine: - if boot_partition.fs_type not in [FilesystemType.Fat12, FilesystemType.Fat16, FilesystemType.Fat32]: + if boot_partition.fs_type not in [FilesystemType.FAT12, FilesystemType.FAT16, FilesystemType.FAT32]: return 'Limine does not support booting with a non-FAT boot partition' elif bootloader == Bootloader.Refind: diff --git a/archinstall/lib/installer.py b/archinstall/lib/installer.py index 43077c63..17b4d0d0 100644 --- a/archinstall/lib/installer.py +++ b/archinstall/lib/installer.py @@ -369,7 +369,7 @@ class Installer: if part_mod.mountpoint: target = self.target / part_mod.relative_mountpoint mount(part_mod.dev_path, target, options=part_mod.mount_options) - elif part_mod.fs_type == FilesystemType.Btrfs: + elif part_mod.fs_type == FilesystemType.BTRFS: # Only mount BTRFS subvolumes that have mountpoints specified subvols_with_mountpoints = [sv for sv in part_mod.btrfs_subvols if sv.mountpoint is not None] if subvols_with_mountpoints: @@ -382,12 +382,12 @@ class Installer: swapon(part_mod.dev_path) def _mount_lvm_vol(self, volume: LvmVolume) -> None: - if volume.fs_type != FilesystemType.Btrfs: + if volume.fs_type != FilesystemType.BTRFS: if volume.mountpoint and volume.dev_path: target = self.target / volume.relative_mountpoint mount(volume.dev_path, target, options=volume.mount_options) - if volume.fs_type == FilesystemType.Btrfs and volume.dev_path: + if volume.fs_type == FilesystemType.BTRFS and volume.dev_path: # Only mount BTRFS subvolumes that have mountpoints specified subvols_with_mountpoints = [sv for sv in volume.btrfs_subvols if sv.mountpoint is not None] if subvols_with_mountpoints: @@ -397,7 +397,7 @@ class Installer: if not luks_handler.mapper_dev: return None - if part_mod.fs_type == FilesystemType.Btrfs and part_mod.btrfs_subvols: + if part_mod.fs_type == FilesystemType.BTRFS and part_mod.btrfs_subvols: # Only mount BTRFS subvolumes that have mountpoints specified subvols_with_mountpoints = [sv for sv in part_mod.btrfs_subvols if sv.mountpoint is not None] if subvols_with_mountpoints: @@ -407,12 +407,12 @@ class Installer: mount(luks_handler.mapper_dev, target, options=part_mod.mount_options) def _mount_luks_volume(self, volume: LvmVolume, luks_handler: Luks2) -> None: - if volume.fs_type != FilesystemType.Btrfs: + if volume.fs_type != FilesystemType.BTRFS: if volume.mountpoint and luks_handler.mapper_dev: target = self.target / volume.relative_mountpoint mount(luks_handler.mapper_dev, target, options=volume.mount_options) - if volume.fs_type == FilesystemType.Btrfs and luks_handler.mapper_dev: + if volume.fs_type == FilesystemType.BTRFS and luks_handler.mapper_dev: # Only mount BTRFS subvolumes that have mountpoints specified subvols_with_mountpoints = [sv for sv in volume.btrfs_subvols if sv.mountpoint is not None] if subvols_with_mountpoints: @@ -867,7 +867,7 @@ class Installer: self._base_packages.append(pkg) # https://github.com/archlinux/archinstall/issues/1837 - if fs_type == FilesystemType.Btrfs: + if fs_type == FilesystemType.BTRFS: self._disable_fstrim = True def _prepare_encrypt(self, before: str = 'filesystems') -> None: diff --git a/archinstall/lib/models/device.py b/archinstall/lib/models/device.py index be82e687..d7474aa5 100644 --- a/archinstall/lib/models/device.py +++ b/archinstall/lib/models/device.py @@ -2,7 +2,7 @@ import builtins import math import uuid from dataclasses import dataclass, field -from enum import Enum +from enum import Enum, StrEnum, auto from pathlib import Path from typing import NotRequired, Self, TypedDict, override from uuid import UUID @@ -131,9 +131,14 @@ class DiskLayoutConfiguration: for partition in entry.get('partitions', []): flags = [flag for f in partition.get('flags', []) if (flag := PartitionFlag.from_string(f))] + if fs_type := partition.get('fs_type'): + fs_type = FilesystemType(fs_type) + else: + fs_type = None + device_partition = PartitionModification( status=ModificationStatus(partition['status']), - fs_type=FilesystemType(partition['fs_type']) if partition.get('fs_type') else None, + fs_type=fs_type, start=Size.parse_args(partition['start']), length=Size.parse_args(partition['size']), mount_options=partition['mount_options'], @@ -200,7 +205,7 @@ class DiskLayoutConfiguration: def has_default_btrfs_vols(self) -> bool: for mod in self.device_modifications: for part in mod.partitions: - if not (part.is_create_or_modify() and part.fs_type == FilesystemType.Btrfs): + if not (part.is_create_or_modify() and part.fs_type == FilesystemType.BTRFS): continue if any(subvol.is_default_root() for subvol in part.btrfs_subvols): @@ -777,38 +782,38 @@ class PartitionGUID(Enum): return uuid.UUID(self.value).bytes -class FilesystemType(Enum): - Btrfs = 'btrfs' - Ext2 = 'ext2' - Ext3 = 'ext3' - Ext4 = 'ext4' - F2fs = 'f2fs' - Fat12 = 'fat12' - Fat16 = 'fat16' - Fat32 = 'fat32' - Ntfs = 'ntfs' - Xfs = 'xfs' - LinuxSwap = 'linux-swap' +class FilesystemType(StrEnum): + BTRFS = auto() + EXT2 = auto() + EXT3 = auto() + EXT4 = auto() + F2FS = auto() + FAT12 = auto() + FAT16 = auto() + FAT32 = auto() + NTFS = auto() + XFS = auto() + LINUX_SWAP = 'linux-swap' # this is not a FS known to parted, so be careful # with the usage from this enum - Crypto_luks = 'crypto_LUKS' + CRYPTO_LUKS = 'crypto_LUKS' def is_crypto(self) -> bool: - return self == FilesystemType.Crypto_luks + return self == FilesystemType.CRYPTO_LUKS @property def parted_value(self) -> str: - return self.value + '(v1)' if self == FilesystemType.LinuxSwap else self.value + return self.value + '(v1)' if self == FilesystemType.LINUX_SWAP else self.value @property def installation_pkg(self) -> str | None: match self: - case FilesystemType.Btrfs: + case FilesystemType.BTRFS: return 'btrfs-progs' - case FilesystemType.Xfs: + case FilesystemType.XFS: return 'xfsprogs' - case FilesystemType.F2fs: + case FilesystemType.F2FS: return 'f2fs-tools' case _: return None @@ -953,7 +958,7 @@ class PartitionModification: return False def is_swap(self) -> bool: - return self.fs_type == FilesystemType.LinuxSwap + return self.fs_type == FilesystemType.LINUX_SWAP def is_modify(self) -> bool: return self.status == ModificationStatus.Modify diff --git a/docs/examples/python.rst b/docs/examples/python.rst index 020ea57a..84fb57c0 100644 --- a/docs/examples/python.rst +++ b/docs/examples/python.rst @@ -65,7 +65,7 @@ After running ``python -m archinstall test_installer`` it should print something partition=, name='primary', type=, - fs_type=, + fs_type=, path='/dev/nvme0n1p1', start=Size(value=2048, unit=, sector_size=SectorSize(value=512, unit=)), length=Size(value=535822336, unit=, sector_size=SectorSize(value=512, unit=)), diff --git a/examples/full_automated_installation.py b/examples/full_automated_installation.py index 84920605..a137db91 100644 --- a/examples/full_automated_installation.py +++ b/examples/full_automated_installation.py @@ -42,7 +42,7 @@ boot_partition = PartitionModification( start=Size(1, Unit.MiB, device.device_info.sector_size), length=Size(512, Unit.MiB, device.device_info.sector_size), mountpoint=Path('/boot'), - fs_type=FilesystemType.Fat32, + fs_type=FilesystemType.FAT32, flags=[PartitionFlag.BOOT], ) device_modification.add_partition(boot_partition)