Skip to content

Commit 335a7b7

Browse files
authored
Refactor FilesystemType (#4417)
* Use UPPER_CASE for FilesystemType member names * Use StrEnum for FilesystemType * Use FilesystemType member values
1 parent 5553cb9 commit 335a7b7

9 files changed

Lines changed: 65 additions & 60 deletions

File tree

archinstall/lib/disk/device_handler.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def load_devices(self) -> None:
9999
fs_type = self._determine_fs_type(partition, lsblk_info)
100100
subvol_infos = []
101101

102-
if fs_type == FilesystemType.Btrfs:
102+
if fs_type == FilesystemType.BTRFS:
103103
subvol_infos = self.get_btrfs_info(partition.path, lsblk_info)
104104

105105
partition_infos.append(
@@ -147,8 +147,8 @@ def _determine_fs_type(
147147
) -> FilesystemType | None:
148148
try:
149149
if partition.fileSystem:
150-
if partition.fileSystem.type == FilesystemType.LinuxSwap.parted_value:
151-
return FilesystemType.LinuxSwap
150+
if partition.fileSystem.type == FilesystemType.LINUX_SWAP.parted_value:
151+
return FilesystemType.LINUX_SWAP
152152
return FilesystemType(partition.fileSystem.type)
153153
elif lsblk_info is not None:
154154
return FilesystemType(lsblk_info.fstype) if lsblk_info.fstype else None
@@ -241,20 +241,20 @@ def format(
241241
options = []
242242

243243
match fs_type:
244-
case FilesystemType.Btrfs | FilesystemType.Xfs:
244+
case FilesystemType.BTRFS | FilesystemType.XFS:
245245
# Force overwrite
246246
options.append('-f')
247-
case FilesystemType.F2fs:
247+
case FilesystemType.F2FS:
248248
options.append('-f')
249249
options.extend(('-O', 'extra_attr'))
250-
case FilesystemType.Ext2 | FilesystemType.Ext3 | FilesystemType.Ext4:
250+
case FilesystemType.EXT2 | FilesystemType.EXT3 | FilesystemType.EXT4:
251251
# Force create
252252
options.append('-F')
253-
case FilesystemType.Fat12 | FilesystemType.Fat16 | FilesystemType.Fat32:
253+
case FilesystemType.FAT12 | FilesystemType.FAT16 | FilesystemType.FAT32:
254254
mkfs_type = 'fat'
255255
# Set FAT size
256256
options.extend(('-F', fs_type.value.removeprefix(mkfs_type)))
257-
case FilesystemType.LinuxSwap:
257+
case FilesystemType.LINUX_SWAP:
258258
command = 'mkswap'
259259
case _:
260260
raise UnknownFilesystemFormat(f'Filetype "{fs_type.value}" is not supported')
@@ -505,7 +505,7 @@ def umount_all_existing(self, device_path: Path) -> None:
505505
debug(f'Unmounting: {partition.path}')
506506

507507
# un-mount for existing encrypted partitions
508-
if partition.fs_type == FilesystemType.Crypto_luks:
508+
if partition.fs_type == FilesystemType.CRYPTO_LUKS:
509509
Luks2(partition.path).lock()
510510
else:
511511
umount(partition.path, recursive=True)

archinstall/lib/disk/disk_menu.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -508,17 +508,17 @@ def _boot_partition(sector_size: SectorSize, using_gpt: bool) -> PartitionModifi
508508
start=start,
509509
length=size,
510510
mountpoint=Path('/boot'),
511-
fs_type=FilesystemType.Fat32,
511+
fs_type=FilesystemType.FAT32,
512512
flags=flags,
513513
)
514514

515515

516516
async def select_main_filesystem_format() -> FilesystemType:
517517
items = [
518-
MenuItem('btrfs', value=FilesystemType.Btrfs),
519-
MenuItem('ext4', value=FilesystemType.Ext4),
520-
MenuItem('xfs', value=FilesystemType.Xfs),
521-
MenuItem('f2fs', value=FilesystemType.F2fs),
518+
MenuItem(FilesystemType.BTRFS.value, value=FilesystemType.BTRFS),
519+
MenuItem(FilesystemType.EXT4.value, value=FilesystemType.EXT4),
520+
MenuItem(FilesystemType.XFS.value, value=FilesystemType.XFS),
521+
MenuItem(FilesystemType.F2FS.value, value=FilesystemType.F2FS),
522522
]
523523

524524
group = MenuItemGroup(items, sort_items=False)
@@ -601,7 +601,7 @@ async def suggest_single_disk_layout(
601601
available_space = total_size
602602
min_size_to_allow_home_part = Size(64, Unit.GiB, sector_size)
603603

604-
if filesystem_type == FilesystemType.Btrfs:
604+
if filesystem_type == FilesystemType.BTRFS:
605605
prompt = tr('Would you like to use BTRFS subvolumes with a default structure?') + '\n'
606606

607607
result = await Confirmation(
@@ -734,7 +734,7 @@ async def suggest_multi_disk_layout(
734734
_ = await Notify(text).show()
735735
return []
736736

737-
if filesystem_type == FilesystemType.Btrfs:
737+
if filesystem_type == FilesystemType.BTRFS:
738738
mount_options = await select_mount_options()
739739

740740
device_paths = ', '.join(str(d.device_info.path) for d in devices)
@@ -817,7 +817,7 @@ async def suggest_lvm_layout(
817817
if not filesystem_type:
818818
filesystem_type = await select_main_filesystem_format()
819819

820-
if filesystem_type == FilesystemType.Btrfs:
820+
if filesystem_type == FilesystemType.BTRFS:
821821
prompt = tr('Would you like to use BTRFS subvolumes with a default structure?') + '\n'
822822
result = await Confirmation(header=prompt, allow_skip=False, preset=True).show()
823823

archinstall/lib/disk/filesystem.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def perform_filesystem_operations(self) -> None:
7070
self._format_partitions(mod.partitions)
7171

7272
for part_mod in mod.partitions:
73-
if part_mod.fs_type == FilesystemType.Btrfs and part_mod.is_create_or_modify():
73+
if part_mod.fs_type == FilesystemType.BTRFS and part_mod.is_create_or_modify():
7474
device_handler.create_btrfs_volumes(part_mod, enc_conf=self._enc_config)
7575

7676
def _format_partitions(
@@ -113,7 +113,7 @@ def _validate_partitions(self, partitions: list[PartitionModification]) -> None:
113113
# verify that all partitions have a path set (which implies that they have been created)
114114
lambda x: x.dev_path is None: ValueError('When formatting, all partitions must have a path set'),
115115
# crypto luks is not a valid file system type
116-
lambda x: x.fs_type is FilesystemType.Crypto_luks: ValueError('Crypto luks cannot be set as a filesystem type'),
116+
lambda x: x.fs_type is FilesystemType.CRYPTO_LUKS: ValueError('Crypto luks cannot be set as a filesystem type'),
117117
# file system type must be set
118118
lambda x: x.fs_type is None: ValueError('File system type must be set for modification'),
119119
}
@@ -230,7 +230,7 @@ def _format_lvm_vols(
230230
# find the mapper device yet
231231
device_handler.format(vol.fs_type, path)
232232

233-
if vol.fs_type == FilesystemType.Btrfs:
233+
if vol.fs_type == FilesystemType.BTRFS:
234234
device_handler.create_lvm_btrfs_subvolumes(path, vol.btrfs_subvols, vol.mount_options)
235235

236236
def _lvm_create_pvs(
@@ -318,7 +318,7 @@ def _lvm_vol_handle_e2scrub(self, vol_gp: LvmVolumeGroup) -> None:
318318
# from arch wiki:
319319
# If a logical volume will be formatted with ext4, leave at least 256 MiB
320320
# free space in the volume group to allow using e2scrub
321-
if any([vol.fs_type == FilesystemType.Ext4 for vol in vol_gp.volumes]):
321+
if any([vol.fs_type == FilesystemType.EXT4 for vol in vol_gp.volumes]):
322322
largest_vol = max(vol_gp.volumes, key=lambda x: x.length)
323323

324324
lvm_vol_reduce(

archinstall/lib/disk/partitioning_menu.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def filter_options(self, selection: DiskSegment, options: list[str]) -> list[str
255255
]
256256

257257
# non btrfs partitions shouldn't get btrfs options
258-
if selection.segment.fs_type != FilesystemType.Btrfs:
258+
if selection.segment.fs_type != FilesystemType.BTRFS:
259259
not_filter += [
260260
self._actions['btrfs_mark_compressed'],
261261
self._actions['btrfs_mark_nodatacow'],
@@ -332,7 +332,7 @@ async def handle_action(
332332
partition.flags = []
333333
partition.set_flag(PartitionFlag.SWAP)
334334
# btrfs subvolumes will define mountpoints
335-
if fs_type == FilesystemType.Btrfs:
335+
if fs_type == FilesystemType.BTRFS:
336336
partition.mountpoint = None
337337
case 'btrfs_mark_compressed':
338338
self._toggle_mount_option(partition, BtrfsMountOption.compress)
@@ -399,12 +399,12 @@ async def _prompt_formatting(self, partition: PartitionModification) -> None:
399399
# If we mark a partition for formatting, but the format is CRYPTO LUKS, there's no point in formatting it really
400400
# without asking the user which inner-filesystem they want to use. Since the flag 'encrypted' = True is already set,
401401
# it's safe to change the filesystem for this partition.
402-
if partition.fs_type == FilesystemType.Crypto_luks:
402+
if partition.fs_type == FilesystemType.CRYPTO_LUKS:
403403
prompt = tr('This partition is currently encrypted, to format it a filesystem has to be specified') + '\n'
404404
fs_type = await self._prompt_partition_fs_type(prompt)
405405
partition.fs_type = fs_type
406406

407-
if fs_type == FilesystemType.Btrfs:
407+
if fs_type == FilesystemType.BTRFS:
408408
partition.mountpoint = None
409409

410410
async def _prompt_mountpoint(self) -> Path:
@@ -417,7 +417,7 @@ async def _prompt_mountpoint(self) -> Path:
417417
return mountpoint
418418

419419
async def _prompt_partition_fs_type(self, prompt: str | None = None) -> FilesystemType:
420-
fs_types = filter(lambda fs: fs != FilesystemType.Crypto_luks, FilesystemType)
420+
fs_types = filter(lambda fs: fs != FilesystemType.CRYPTO_LUKS, FilesystemType)
421421
items = [MenuItem(fs.value, value=fs) for fs in fs_types]
422422
group = MenuItemGroup(items, sort_items=False)
423423

@@ -522,7 +522,7 @@ async def _create_new_partition(self, free_space: FreeSpace) -> PartitionModific
522522
fs_type = await self._prompt_partition_fs_type()
523523

524524
mountpoint = None
525-
if fs_type not in (FilesystemType.Btrfs, FilesystemType.LinuxSwap):
525+
if fs_type not in (FilesystemType.BTRFS, FilesystemType.LINUX_SWAP):
526526
mountpoint = await self._prompt_mountpoint()
527527

528528
partition = PartitionModification(

archinstall/lib/global_menu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,11 @@ def _validate_bootloader(self) -> str | None:
486486
if efi_partition is None:
487487
return 'EFI system partition (ESP) not found'
488488

489-
if efi_partition.fs_type not in [FilesystemType.Fat12, FilesystemType.Fat16, FilesystemType.Fat32]:
489+
if efi_partition.fs_type not in [FilesystemType.FAT12, FilesystemType.FAT16, FilesystemType.FAT32]:
490490
return 'ESP must be formatted as a FAT filesystem'
491491

492492
if bootloader == Bootloader.Limine:
493-
if boot_partition.fs_type not in [FilesystemType.Fat12, FilesystemType.Fat16, FilesystemType.Fat32]:
493+
if boot_partition.fs_type not in [FilesystemType.FAT12, FilesystemType.FAT16, FilesystemType.FAT32]:
494494
return 'Limine does not support booting with a non-FAT boot partition'
495495

496496
elif bootloader == Bootloader.Refind:

archinstall/lib/installer.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def _mount_partition(self, part_mod: PartitionModification) -> None:
369369
if part_mod.mountpoint:
370370
target = self.target / part_mod.relative_mountpoint
371371
mount(part_mod.dev_path, target, options=part_mod.mount_options)
372-
elif part_mod.fs_type == FilesystemType.Btrfs:
372+
elif part_mod.fs_type == FilesystemType.BTRFS:
373373
# Only mount BTRFS subvolumes that have mountpoints specified
374374
subvols_with_mountpoints = [sv for sv in part_mod.btrfs_subvols if sv.mountpoint is not None]
375375
if subvols_with_mountpoints:
@@ -382,12 +382,12 @@ def _mount_partition(self, part_mod: PartitionModification) -> None:
382382
swapon(part_mod.dev_path)
383383

384384
def _mount_lvm_vol(self, volume: LvmVolume) -> None:
385-
if volume.fs_type != FilesystemType.Btrfs:
385+
if volume.fs_type != FilesystemType.BTRFS:
386386
if volume.mountpoint and volume.dev_path:
387387
target = self.target / volume.relative_mountpoint
388388
mount(volume.dev_path, target, options=volume.mount_options)
389389

390-
if volume.fs_type == FilesystemType.Btrfs and volume.dev_path:
390+
if volume.fs_type == FilesystemType.BTRFS and volume.dev_path:
391391
# Only mount BTRFS subvolumes that have mountpoints specified
392392
subvols_with_mountpoints = [sv for sv in volume.btrfs_subvols if sv.mountpoint is not None]
393393
if subvols_with_mountpoints:
@@ -397,7 +397,7 @@ def _mount_luks_partition(self, part_mod: PartitionModification, luks_handler: L
397397
if not luks_handler.mapper_dev:
398398
return None
399399

400-
if part_mod.fs_type == FilesystemType.Btrfs and part_mod.btrfs_subvols:
400+
if part_mod.fs_type == FilesystemType.BTRFS and part_mod.btrfs_subvols:
401401
# Only mount BTRFS subvolumes that have mountpoints specified
402402
subvols_with_mountpoints = [sv for sv in part_mod.btrfs_subvols if sv.mountpoint is not None]
403403
if subvols_with_mountpoints:
@@ -407,12 +407,12 @@ def _mount_luks_partition(self, part_mod: PartitionModification, luks_handler: L
407407
mount(luks_handler.mapper_dev, target, options=part_mod.mount_options)
408408

409409
def _mount_luks_volume(self, volume: LvmVolume, luks_handler: Luks2) -> None:
410-
if volume.fs_type != FilesystemType.Btrfs:
410+
if volume.fs_type != FilesystemType.BTRFS:
411411
if volume.mountpoint and luks_handler.mapper_dev:
412412
target = self.target / volume.relative_mountpoint
413413
mount(luks_handler.mapper_dev, target, options=volume.mount_options)
414414

415-
if volume.fs_type == FilesystemType.Btrfs and luks_handler.mapper_dev:
415+
if volume.fs_type == FilesystemType.BTRFS and luks_handler.mapper_dev:
416416
# Only mount BTRFS subvolumes that have mountpoints specified
417417
subvols_with_mountpoints = [sv for sv in volume.btrfs_subvols if sv.mountpoint is not None]
418418
if subvols_with_mountpoints:
@@ -867,7 +867,7 @@ def _prepare_fs_type(
867867
self._base_packages.append(pkg)
868868

869869
# https://github.com/archlinux/archinstall/issues/1837
870-
if fs_type == FilesystemType.Btrfs:
870+
if fs_type == FilesystemType.BTRFS:
871871
self._disable_fstrim = True
872872

873873
def _prepare_encrypt(self, before: str = 'filesystems') -> None:

archinstall/lib/models/device.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import math
33
import uuid
44
from dataclasses import dataclass, field
5-
from enum import Enum
5+
from enum import Enum, StrEnum, auto
66
from pathlib import Path
77
from typing import NotRequired, Self, TypedDict, override
88
from uuid import UUID
@@ -131,9 +131,14 @@ def parse_arg(
131131
for partition in entry.get('partitions', []):
132132
flags = [flag for f in partition.get('flags', []) if (flag := PartitionFlag.from_string(f))]
133133

134+
if fs_type := partition.get('fs_type'):
135+
fs_type = FilesystemType(fs_type)
136+
else:
137+
fs_type = None
138+
134139
device_partition = PartitionModification(
135140
status=ModificationStatus(partition['status']),
136-
fs_type=FilesystemType(partition['fs_type']) if partition.get('fs_type') else None,
141+
fs_type=fs_type,
137142
start=Size.parse_args(partition['start']),
138143
length=Size.parse_args(partition['size']),
139144
mount_options=partition['mount_options'],
@@ -200,7 +205,7 @@ def parse_arg(
200205
def has_default_btrfs_vols(self) -> bool:
201206
for mod in self.device_modifications:
202207
for part in mod.partitions:
203-
if not (part.is_create_or_modify() and part.fs_type == FilesystemType.Btrfs):
208+
if not (part.is_create_or_modify() and part.fs_type == FilesystemType.BTRFS):
204209
continue
205210

206211
if any(subvol.is_default_root() for subvol in part.btrfs_subvols):
@@ -777,38 +782,38 @@ def bytes(self) -> builtins.bytes:
777782
return uuid.UUID(self.value).bytes
778783

779784

780-
class FilesystemType(Enum):
781-
Btrfs = 'btrfs'
782-
Ext2 = 'ext2'
783-
Ext3 = 'ext3'
784-
Ext4 = 'ext4'
785-
F2fs = 'f2fs'
786-
Fat12 = 'fat12'
787-
Fat16 = 'fat16'
788-
Fat32 = 'fat32'
789-
Ntfs = 'ntfs'
790-
Xfs = 'xfs'
791-
LinuxSwap = 'linux-swap'
785+
class FilesystemType(StrEnum):
786+
BTRFS = auto()
787+
EXT2 = auto()
788+
EXT3 = auto()
789+
EXT4 = auto()
790+
F2FS = auto()
791+
FAT12 = auto()
792+
FAT16 = auto()
793+
FAT32 = auto()
794+
NTFS = auto()
795+
XFS = auto()
796+
LINUX_SWAP = 'linux-swap'
792797

793798
# this is not a FS known to parted, so be careful
794799
# with the usage from this enum
795-
Crypto_luks = 'crypto_LUKS'
800+
CRYPTO_LUKS = 'crypto_LUKS'
796801

797802
def is_crypto(self) -> bool:
798-
return self == FilesystemType.Crypto_luks
803+
return self == FilesystemType.CRYPTO_LUKS
799804

800805
@property
801806
def parted_value(self) -> str:
802-
return self.value + '(v1)' if self == FilesystemType.LinuxSwap else self.value
807+
return self.value + '(v1)' if self == FilesystemType.LINUX_SWAP else self.value
803808

804809
@property
805810
def installation_pkg(self) -> str | None:
806811
match self:
807-
case FilesystemType.Btrfs:
812+
case FilesystemType.BTRFS:
808813
return 'btrfs-progs'
809-
case FilesystemType.Xfs:
814+
case FilesystemType.XFS:
810815
return 'xfsprogs'
811-
case FilesystemType.F2fs:
816+
case FilesystemType.F2FS:
812817
return 'f2fs-tools'
813818
case _:
814819
return None
@@ -953,7 +958,7 @@ def is_home(self) -> bool:
953958
return False
954959

955960
def is_swap(self) -> bool:
956-
return self.fs_type == FilesystemType.LinuxSwap
961+
return self.fs_type == FilesystemType.LINUX_SWAP
957962

958963
def is_modify(self) -> bool:
959964
return self.status == ModificationStatus.Modify

docs/examples/python.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ After running ``python -m archinstall test_installer`` it should print something
6565
partition=<parted.partition.Partition object at 0x7fbe166c4a90>,
6666
name='primary',
6767
type=<PartitionType.Primary: 'primary'>,
68-
fs_type=<FilesystemType.Fat32: 'fat32'>,
68+
fs_type=<FilesystemType.FAT32: 'fat32'>,
6969
path='/dev/nvme0n1p1',
7070
start=Size(value=2048, unit=<Unit.sectors: 'sectors'>, sector_size=SectorSize(value=512, unit=<Unit.B: 1>)),
7171
length=Size(value=535822336, unit=<Unit.B: 1>, sector_size=SectorSize(value=512, unit=<Unit.B: 1>)),

examples/full_automated_installation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
start=Size(1, Unit.MiB, device.device_info.sector_size),
4343
length=Size(512, Unit.MiB, device.device_info.sector_size),
4444
mountpoint=Path('/boot'),
45-
fs_type=FilesystemType.Fat32,
45+
fs_type=FilesystemType.FAT32,
4646
flags=[PartitionFlag.BOOT],
4747
)
4848
device_modification.add_partition(boot_partition)

0 commit comments

Comments
 (0)