|
| 1 | +from dataclasses import dataclass |
| 2 | +from enum import Enum, auto |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from archinstall.lib.models.bootloader import Bootloader, BootloaderConfiguration |
| 6 | +from archinstall.lib.models.device import DiskLayoutConfiguration |
| 7 | + |
| 8 | + |
| 9 | +class BootloaderValidationFailureKind(Enum): |
| 10 | + LimineNonFatBoot = auto() |
| 11 | + LimineLayout = auto() |
| 12 | + |
| 13 | + |
| 14 | +@dataclass(frozen=True) |
| 15 | +class BootloaderValidationFailure: |
| 16 | + kind: BootloaderValidationFailureKind |
| 17 | + description: str |
| 18 | + |
| 19 | + |
| 20 | +def validate_bootloader_layout( |
| 21 | + bootloader_config: BootloaderConfiguration | None, |
| 22 | + disk_config: DiskLayoutConfiguration | None, |
| 23 | +) -> BootloaderValidationFailure | None: |
| 24 | + """Validate bootloader configuration against disk layout. |
| 25 | +
|
| 26 | + Returns a failure with a human-readable description if the configuration |
| 27 | + would produce an unbootable system, or None if it is valid. |
| 28 | + """ |
| 29 | + if not (bootloader_config and disk_config): |
| 30 | + return None |
| 31 | + |
| 32 | + if bootloader_config.bootloader == Bootloader.Limine: |
| 33 | + boot_part = next( |
| 34 | + (p for m in disk_config.device_modifications if (p := m.get_boot_partition())), |
| 35 | + None, |
| 36 | + ) |
| 37 | + |
| 38 | + # Limine reads its config and kernels from the boot partition, which |
| 39 | + # must be FAT. |
| 40 | + if boot_part and (boot_part.fs_type is None or not boot_part.fs_type.is_fat()): |
| 41 | + return BootloaderValidationFailure( |
| 42 | + kind=BootloaderValidationFailureKind.LimineNonFatBoot, |
| 43 | + description='Limine does not support booting with a non-FAT boot partition.', |
| 44 | + ) |
| 45 | + |
| 46 | + # When the ESP is the boot partition but mounted outside /boot and |
| 47 | + # UKI is disabled, kernels end up on the root filesystem which |
| 48 | + # Limine cannot access. |
| 49 | + if not bootloader_config.uki: |
| 50 | + efi_part = next( |
| 51 | + (p for m in disk_config.device_modifications if (p := m.get_efi_partition())), |
| 52 | + None, |
| 53 | + ) |
| 54 | + if efi_part and efi_part == boot_part and efi_part.mountpoint != Path('/boot'): |
| 55 | + return BootloaderValidationFailure( |
| 56 | + kind=BootloaderValidationFailureKind.LimineLayout, |
| 57 | + description=( |
| 58 | + f'Limine requires kernels on a FAT partition. The ESP is mounted at {efi_part.mountpoint}, ' |
| 59 | + 'enable UKI or add a separate /boot partition to install Limine.' |
| 60 | + ), |
| 61 | + ) |
| 62 | + |
| 63 | + return None |
0 commit comments