From d19e0f2faf627df371a1145cd3b18015b7db2597 Mon Sep 17 00:00:00 2001 From: xander-lin <277640093+xander-lin@users.noreply.github.com> Date: Wed, 28 Oct 2026 09:52:56 +0800 Subject: [PATCH 1/3] fix: report a configuration that is not understood The configuration is read key by key, so a key name that is not spelled exactly right, or one from an older release, was not an error anywhere: the option it was meant to set kept its default and the installation ran with something other than what the file asks for. Name the keys that were not understood. The `version` field of the file was never read at all - it was overwritten with the running version right after parsing - so a file written by an incompatible release was applied as if it were current. Report the mismatch. --- archinstall/lib/args.py | 44 +++++++++++++++++++++++++++++++++++++++ tests/test_config_keys.py | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 tests/test_config_keys.py diff --git a/archinstall/lib/args.py b/archinstall/lib/args.py index 7c82efa90d..0e821291a2 100644 --- a/archinstall/lib/args.py +++ b/archinstall/lib/args.py @@ -101,6 +101,29 @@ class ArchConfigType(StrEnum): USER_CREDS_FILE: Path = Path('user_credentials.json') DEFAULT_SAVE_PATH = logger.directory +# keys `from_config()` reads that are not an `ArchConfigType` member: deprecated +# spellings kept for backwards compatibility, and the locale fields that may sit +# at the top level of the file +_CONFIG_KEYS_READ_BY_HAND: frozenset[str] = frozenset( + { + '!root-password', + '!users', + 'additional-repositories', + 'archinstall-language', + 'audio_config', + 'bootloader', + 'console_font', + 'disk_encryption', + 'encryption_password', + 'kb_layout', + 'parallel_downloads', + 'root_enc_password', + 'sys_enc', + 'sys_lang', + 'uki', + }, +) + @dataclass class ArchConfig: @@ -428,6 +451,13 @@ def __init__(self) -> None: try: self._config = ArchConfig.from_config(config, self._args) + + # the file may record the release that wrote it; it is not the version that + # is running, and nothing used to look at it, so a file written by an + # incompatible release was applied as if it were current + if (written_by := config.get('version', None)) and str(written_by) != get_version(): + warn(f'The configuration was written by archinstall {written_by}, this is {get_version()}') + self._config.version = get_version() except ValueError as err: warn(str(err)) @@ -642,6 +672,7 @@ def _parse_config(self) -> dict[str, Any]: if config_data is not None: config.update(json.loads(config_data)) + self._warn_about_unknown_keys(config) if self._args.creds is not None: creds_data = self._read_file(self._args.creds) @@ -748,3 +779,16 @@ def _cleanup_config(self, config: Namespace | dict[str, Any]) -> dict[str, Any]: clean_args[key] = val return clean_args + + def _warn_about_unknown_keys(self, config: dict[str, Any]) -> None: + """Say which keys were not understood instead of dropping them silently. + + The configuration is read key by key, so a key name that is not spelled + exactly right - or one that belongs to an older release - is not an error + anywhere: the option it was meant to set simply keeps its default and the + installation runs with something other than what the file asks for. + """ + known = {key.value for key in ArchConfigType} | _CONFIG_KEYS_READ_BY_HAND + + if unknown := sorted(set(config) - known): + warn(f'Ignoring unknown configuration keys: {unknown}') diff --git a/tests/test_config_keys.py b/tests/test_config_keys.py new file mode 100644 index 0000000000..10617d86a3 --- /dev/null +++ b/tests/test_config_keys.py @@ -0,0 +1,42 @@ +import json +from pathlib import Path + +from _pytest.capture import CaptureFixture +from pytest import MonkeyPatch + +from archinstall.lib.args import ArchConfigHandler + + +def _write(tmp_path: Path, config: dict) -> Path: + path = tmp_path / 'config.json' + path.write_text(json.dumps(config)) + return path + + +def test_unknown_keys_are_reported( + monkeypatch: MonkeyPatch, + tmp_path: Path, + capsys: CaptureFixture[str], +) -> None: + """A misspelled key must not just leave the option at its default.""" + config_file = _write(tmp_path, {'boot_loader': 'grub', 'timezone': 'UTC'}) + monkeypatch.setattr('sys.argv', ['archinstall', '--config', str(config_file)]) + + ArchConfigHandler() + + output = capsys.readouterr() + assert 'boot_loader' in output.out + output.err + + +def test_config_written_by_another_version_is_reported( + monkeypatch: MonkeyPatch, + tmp_path: Path, + capsys: CaptureFixture[str], +) -> None: + config_file = _write(tmp_path, {'version': '0.0.0-ancient', 'timezone': 'UTC'}) + monkeypatch.setattr('sys.argv', ['archinstall', '--config', str(config_file)]) + + ArchConfigHandler() + + output = capsys.readouterr() + assert '0.0.0-ancient' in output.out + output.err From 1524e91d78e949485e54dee836b056470962da57 Mon Sep 17 00:00:00 2001 From: xander-lin <277640093+xander-lin@users.noreply.github.com> Date: Wed, 28 Oct 2026 09:47:34 +0800 Subject: [PATCH 2/3] fix: reject configurations that cannot describe what they ask for `DiskLayoutConfiguration.parse_arg()` dropped a device entry it could not find and returned a layout describing no disk and no partition at all - still truthy, so every later "is a layout set?" guard passed and the installation went on to do nothing. Raise instead, and report the devices that were probed. An EXIST or MODIFY partition entry was taken as-is even when its `dev_path` belongs to another disk, so the blueprint pointed at a partition nothing had seen. Require it to be one of the partitions of the listed device. Combining an encryption type that has no LVM recipe (LUKS, or no encryption) with `lvm_config` built no volume group and no volume, and the installation continued into a mount phase with nothing to mount; reject the combination both when parsing the configuration and in `_setup_lvm_encrypted()`. Encrypted swap is never activated - the mount code has no swap branch for a mapper device - so a hand written configuration describing it finished with no swap and no fstab entry for it. Reject it while the configuration is being read. --- archinstall/lib/disk/filesystem.py | 8 ++ archinstall/lib/models/device.py | 42 +++++++- tests/conftest.py | 32 ++++++ tests/test_args.py | 9 ++ tests/test_config_validation.py | 167 +++++++++++++++++++++++++++++ tests/test_configuration_output.py | 3 + 6 files changed, 259 insertions(+), 2 deletions(-) create mode 100644 tests/test_config_validation.py diff --git a/archinstall/lib/disk/filesystem.py b/archinstall/lib/disk/filesystem.py index 1bb3523caa..d84dbffd3d 100644 --- a/archinstall/lib/disk/filesystem.py +++ b/archinstall/lib/disk/filesystem.py @@ -157,6 +157,14 @@ def _setup_lvm_encrypted(self, lvm_config: LvmConfiguration, enc_config: DiskEnc # LVM volumes must remain active for later re-unlock during installation for luks in enc_vols.values(): luks.lock() + else: + # No encryption type describes how to put LVM on top of it. Falling through + # used to create no volume group and no volume at all, while the layout kept + # describing the volumes it expects, so the installation continued into a + # mount phase that had nothing to mount. + raise ValueError( + f'Encryption type "{enc_config.encryption_type.value}" cannot be combined with LVM: neither a volume group nor its volumes would be created', + ) def _setup_lvm( self, diff --git a/archinstall/lib/models/device.py b/archinstall/lib/models/device.py index b4c069e659..0e5fb95fcf 100644 --- a/archinstall/lib/models/device.py +++ b/archinstall/lib/models/device.py @@ -165,12 +165,18 @@ def parse_arg( device_path = Path(entry['device']) if entry.get('device', None) else None if not device_path: - continue + raise ValueError(f'A device modification entry does not name a device: {entry}') device = device_handler.get_device(device_path) if not device: - continue + # dropping the entry silently left a layout that describes no disk at + # all while still being truthy, so every later "is a layout set?" guard + # passed and the installation went on to do nothing + raise ValueError( + f'Device "{device_path}" from the configuration could not be found, ' + f'known devices are {[str(d.device_info.path) for d in device_handler.devices]}', + ) device_modification = DeviceModification( wipe=entry.get('wipe', False), @@ -201,6 +207,17 @@ def parse_arg( ) # special 'invisible' attr to internally identify the part mod device_partition._obj_id = partition['obj_id'] + + # an EXIST or MODIFY entry describes a partition that has to be on the + # device it is listed under; a path from another disk used to be taken + # as-is and the blueprint then pointed at a partition nothing had probed + if device_partition.dev_path is not None and device_partition.is_exists_or_modify(): + known = {str(part.path) for part in device.partition_infos} + if str(device_partition.dev_path) not in known: + raise ValueError( + f'Partition "{device_partition.dev_path}" is not a partition of "{device_path}", which has {sorted(known)}', + ) + device_partitions.append(device_partition) device_modification.partitions = device_partitions @@ -247,6 +264,17 @@ def parse_arg( if (enc_config := disk_config.get('disk_encryption', None)) is not None: config.disk_encryption = DiskEncryption.parse_arg(config, enc_config, enc_password) + # LVM on top of an encrypted setup only has two meaningful combinations; any + # other encryption type leaves the volume group unbuilt (see + # FilesystemHandler._setup_lvm_encrypted), so reject the layout here, before + # anything has been written to disk + if config.lvm_config and config.disk_encryption: + supported = (EncryptionType.LVM_ON_LUKS, EncryptionType.LUKS_ON_LVM) + if config.disk_encryption.encryption_type not in supported: + raise ValueError( + f'Encryption type "{config.disk_encryption.encryption_type.value}" cannot be combined with LVM, use one of {[e.value for e in supported]}', + ) + if config.has_default_btrfs_vols(): if (btrfs_arg := disk_config.get('btrfs_options', None)) is not None: config.btrfs_options = BtrfsOptions.parse_arg(btrfs_arg) @@ -1352,6 +1380,9 @@ def is_root(self) -> bool: return False + def is_swap(self) -> bool: + return self.fs_type == FilesystemType.LINUX_SWAP + @dataclass class LvmGroupInfo: @@ -1628,6 +1659,13 @@ def __post_init__(self) -> None: if self.encryption_type == EncryptionType.LUKS_ON_LVM and not self.lvm_volumes: raise ValueError('LuksOnLvm encryption require LMV volumes to be defined') + # an encrypted swap area is never activated: the mount code has no swap branch + # for a mapper device, so the installation would finish without any swap and + # without the fstab entry for it. The menu does not offer swap, a hand written + # configuration can still ask for it, so say so instead of dropping it. + for swap_mod in [p for p in self.partitions if p.is_swap()] + [v for v in self.lvm_volumes if v.is_swap()]: + raise ValueError(f'Encrypted swap is not supported, "{swap_mod.dev_path}" cannot be encrypted') + def should_generate_encryption_file(self, dev: PartitionModification | LvmVolume) -> bool: if isinstance(dev, PartitionModification): return dev in self.partitions and dev.mountpoint != Path('/') diff --git a/tests/conftest.py b/tests/conftest.py index d3f2bc7e34..af79dbc2e4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,40 @@ from pathlib import Path +from unittest.mock import MagicMock import pytest +@pytest.fixture +def probed_devices(monkeypatch: pytest.MonkeyPatch) -> list[Path]: + """Pretend the probe found the devices the sample configurations name. + + `DiskLayoutConfiguration.parse_arg()` rejects a configuration that names a + device the probe did not find instead of dropping the entry, so a test that + parses a sample config has to provide those devices. + """ + from archinstall.lib.models.device import SectorSize, Size, Unit + + sector_size = SectorSize.default() + paths = [Path('/dev/sda'), Path('/dev/sdb')] + devices = {} + + for path in paths: + device = MagicMock() + device.device_info.path = path + device.device_info.sector_size = sector_size + device.device_info.total_size = Size(128, Unit.GiB, sector_size) + device.partition_infos = [] + device.disk = MagicMock(type='gpt') + devices[path] = device + + monkeypatch.setattr( + 'archinstall.lib.disk.device_handler.device_handler._devices', + devices, + ) + + return paths + + @pytest.fixture(scope='session') def config_fixture() -> Path: return Path(__file__).parent / 'data' / 'test_config.json' diff --git a/tests/test_args.py b/tests/test_args.py index 0010e2dc91..dea1c17111 100644 --- a/tests/test_args.py +++ b/tests/test_args.py @@ -61,6 +61,7 @@ def test_correct_parsing_args( monkeypatch: MonkeyPatch, config_fixture: Path, creds_fixture: Path, + probed_devices: list[Path], ) -> None: monkeypatch.setattr( 'sys.argv', @@ -91,6 +92,8 @@ def test_correct_parsing_args( ], ) + assert probed_devices, 'the fixture registers the probed devices' + handler = ArchConfigHandler() args = handler.args @@ -119,6 +122,7 @@ def test_config_file_parsing( monkeypatch: MonkeyPatch, config_fixture: Path, creds_fixture: Path, + probed_devices: list[Path], ) -> None: monkeypatch.setattr( 'sys.argv', @@ -131,6 +135,8 @@ def test_config_file_parsing( ], ) + assert probed_devices, 'the fixture registers the probed devices' + handler = ArchConfigHandler() arch_config = handler.config @@ -396,6 +402,7 @@ def test_example_config_parsing( monkeypatch: MonkeyPatch, example_config_fixture: Path, example_creds_fixture: Path, + probed_devices: list[Path], ) -> None: monkeypatch.setattr( 'sys.argv', @@ -408,6 +415,8 @@ def test_example_config_parsing( ], ) + assert probed_devices, 'the fixture registers the probed devices' + handler = ArchConfigHandler() arch_config = handler.config diff --git a/tests/test_config_validation.py b/tests/test_config_validation.py new file mode 100644 index 0000000000..7850d11195 --- /dev/null +++ b/tests/test_config_validation.py @@ -0,0 +1,167 @@ +from pathlib import Path +from unittest.mock import MagicMock + +import pytest + +from archinstall.lib.disk.filesystem import FilesystemHandler +from archinstall.lib.models.device import ( + DiskEncryption, + DiskLayoutConfiguration, + DiskLayoutType, + EncryptionType, + FilesystemType, + LvmConfiguration, + LvmLayoutType, + LvmVolume, + LvmVolumeGroup, + ModificationStatus, + PartitionModification, + PartitionType, + SectorSize, + Size, + Unit, +) +from archinstall.lib.models.users import Password + + +def _size(value: int, unit: Unit, sector_size: SectorSize) -> Size: + return Size(value, unit, sector_size) + + +def _part( + sector_size: SectorSize, + status: ModificationStatus = ModificationStatus.CREATE, + dev_path: Path | None = None, + fs_type: FilesystemType = FilesystemType.EXT4, +) -> PartitionModification: + return PartitionModification( + status=status, + type=PartitionType.PRIMARY, + start=_size(1, Unit.MiB, sector_size), + length=_size(100, Unit.MiB, sector_size), + fs_type=fs_type, + dev_path=dev_path, + ) + + +def _register_device(monkeypatch: pytest.MonkeyPatch, path: Path, partitions: list[Path]) -> None: + sector_size = SectorSize.default() + + device = MagicMock() + device.device_info.path = path + device.device_info.sector_size = sector_size + device.device_info.total_size = _size(128, Unit.GiB, sector_size) + device.disk = MagicMock(type='gpt') + device.partition_infos = [MagicMock(path=p) for p in partitions] + + monkeypatch.setattr( + 'archinstall.lib.disk.device_handler.device_handler._devices', + {path: device}, + ) + + +def _config(device: str, partitions: list[dict]) -> dict: + return { + 'config_type': DiskLayoutType.Manual.value, + 'device_modifications': [{'device': device, 'wipe': True, 'partitions': partitions}], + } + + +def _partition_arg(sector_size: SectorSize, status: str = 'create', dev_path: str | None = None) -> dict: + return { + 'status': status, + 'type': 'primary', + 'start': _size(1, Unit.MiB, sector_size).json(), + 'size': _size(100, Unit.MiB, sector_size).json(), + 'mount_options': [], + 'mountpoint': '/', + 'dev_path': dev_path, + 'fs_type': 'ext4', + 'flags': [], + 'obj_id': 'c0ffee', + } + + +def test_config_names_a_device_that_was_not_found(monkeypatch: pytest.MonkeyPatch) -> None: + """A layout must not silently become an empty one.""" + _register_device(monkeypatch, Path('/dev/sda'), [Path('/dev/sda1')]) + + with pytest.raises(ValueError, match='could not be found'): + DiskLayoutConfiguration.parse_arg( + _config('/dev/sdz', [_partition_arg(SectorSize.default())]), + ) + + +def test_config_partition_must_belong_to_the_listed_device(monkeypatch: pytest.MonkeyPatch) -> None: + """An EXIST entry pointing at another disk used to be taken as-is.""" + _register_device(monkeypatch, Path('/dev/sda'), [Path('/dev/sda1')]) + + with pytest.raises(ValueError, match='is not a partition of'): + DiskLayoutConfiguration.parse_arg( + _config( + '/dev/sda', + [_partition_arg(SectorSize.default(), 'existing', '/dev/sdz9')], + ), + ) + + +def test_config_partition_on_the_listed_device_is_accepted(monkeypatch: pytest.MonkeyPatch) -> None: + _register_device(monkeypatch, Path('/dev/sda'), [Path('/dev/sda1')]) + + layout = DiskLayoutConfiguration.parse_arg( + _config('/dev/sda', [_partition_arg(SectorSize.default(), 'existing', '/dev/sda1')]), + ) + + assert layout is not None + assert layout.device_modifications[0].partitions[0].dev_path == Path('/dev/sda1') + + +def test_encryption_type_without_an_lvm_recipe_is_rejected() -> None: + """LUKS or no encryption next to an LVM config built no volume group at all.""" + sector_size = SectorSize.default() + lvm_config = LvmConfiguration( + config_type=LvmLayoutType.Default, + vol_groups=[ + LvmVolumeGroup( + name='vg0', + pvs=[_part(sector_size)], + volumes=[ + LvmVolume( + status=ModificationStatus.CREATE, + name='root', + fs_type=FilesystemType.EXT4, + length=_size(10, Unit.GiB, sector_size), + mountpoint=Path('/'), + ), + ], + ), + ], + ) + + disk_config = DiskLayoutConfiguration( + config_type=DiskLayoutType.Manual, + device_modifications=[], + lvm_config=lvm_config, + disk_encryption=DiskEncryption( + encryption_type=EncryptionType.NO_ENCRYPTION, + encryption_password=Password('secret'), + ), + ) + + handler = FilesystemHandler(disk_config) + + with pytest.raises(ValueError, match='cannot be combined with LVM'): + handler._setup_lvm_encrypted(lvm_config, disk_config.disk_encryption) + + +def test_encrypted_swap_is_rejected() -> None: + """Encrypted swap is never activated, so it must not be configurable.""" + sector_size = SectorSize.default() + swap_part = _part(sector_size, fs_type=FilesystemType.LINUX_SWAP) + + with pytest.raises(ValueError, match='Encrypted swap is not supported'): + DiskEncryption( + encryption_type=EncryptionType.LUKS, + encryption_password=Password('secret'), + partitions=[swap_part], + ) diff --git a/tests/test_configuration_output.py b/tests/test_configuration_output.py index 5bc54f6d6a..978b5cebca 100644 --- a/tests/test_configuration_output.py +++ b/tests/test_configuration_output.py @@ -9,9 +9,12 @@ def test_user_config_roundtrip( monkeypatch: MonkeyPatch, config_fixture: Path, + probed_devices: list[Path], ) -> None: monkeypatch.setattr('sys.argv', ['archinstall', '--config', str(config_fixture)]) + assert probed_devices, 'the fixture registers the probed devices' + handler = ArchConfigHandler() arch_config = handler.config From 11344c820d53bbc526951ca73f8827b63c010c04 Mon Sep 17 00:00:00 2001 From: xander-lin <277640093+xander-lin@users.noreply.github.com> Date: Wed, 28 Oct 2026 12:46:10 +0800 Subject: [PATCH 3/3] fix: name the key when the disk configuration carries a misspelled one The unknown-key check only looked at the top level of the file, so a misspelled key one level down - where the layout applied to the disks is described - was still dropped without a word. Walk the serialisation types so the message can name the path (device_modifications[0].partitions[0].mountpiont) instead of guessing, and so the check cannot drift away from what is parsed. --- archinstall/lib/args.py | 65 ++++++++++++++++++++++++++- archinstall/lib/models/device.py | 73 +++++++++++++++++++++++++++++- tests/test_config_keys.py | 77 ++++++++++++++++++++++++++++++++ 3 files changed, 212 insertions(+), 3 deletions(-) diff --git a/archinstall/lib/args.py b/archinstall/lib/args.py index 0e821291a2..6607231fab 100644 --- a/archinstall/lib/args.py +++ b/archinstall/lib/args.py @@ -23,7 +23,13 @@ from archinstall.lib.models.authentication import AuthenticationConfiguration from archinstall.lib.models.bootloader import Bootloader, BootloaderConfiguration from archinstall.lib.models.config import SubConfig, SummaryLevel -from archinstall.lib.models.device import DiskEncryption, DiskLayoutConfiguration +from archinstall.lib.models.device import ( + DiskEncryption, + DiskLayoutConfiguration, + serialization_child_kind, + serialization_children, + serialization_keys, +) from archinstall.lib.models.locale import LocaleConfiguration from archinstall.lib.models.mirrors import MirrorConfiguration from archinstall.lib.models.network import NetworkConfiguration @@ -441,6 +447,16 @@ def as_summary(self, level: SummaryLevel = SummaryLevel.BASIC) -> str: return as_key_value_pair(cfg, ignore_empty=True) +_CONFIG_KEYS_DOCUMENTED_ONLY: frozenset[str] = frozenset( + { + 'debug', + 'no_pkg_lookups', + 'offline', + 'silent', + }, +) + + class ArchConfigHandler: def __init__(self) -> None: self._parser: ArgumentParser = self._define_arguments() @@ -790,5 +806,50 @@ def _warn_about_unknown_keys(self, config: dict[str, Any]) -> None: """ known = {key.value for key in ArchConfigType} | _CONFIG_KEYS_READ_BY_HAND - if unknown := sorted(set(config) - known): + # these are documented configuration keys that `from_config()` never reads; they + # only exist as command line flags. Reporting them as a misspelling would point + # at the user's file, while the option is simply not wired up to the file. + documented_not_read = sorted(set(config) & _CONFIG_KEYS_DOCUMENTED_ONLY) + if documented_not_read: + warn( + f'These configuration keys are documented but not applied by this version, set them on the command line instead: {documented_not_read}', + ) + + if unknown := sorted(set(config) - known - _CONFIG_KEYS_DOCUMENTED_ONLY): warn(f'Ignoring unknown configuration keys: {unknown}') + + if disk_config := config.get('disk_config', None): + if disk_config_unknown := self._unknown_keys_in_disk_config(disk_config): + warn(f'Ignoring unknown keys in the disk configuration: {disk_config_unknown}') + + def _unknown_keys_in_disk_config(self, disk_config: Any) -> list[str]: + """Misspelled keys anywhere in the disk configuration, as dotted paths. + + The disk configuration is the largest and most nested part of the file, and + the one where a misspelled key costs the most: the layout it describes is + applied to the disk, and the option keeps its default without a word. + """ + keys = serialization_keys() + children = serialization_children() + root = '_DiskLayoutConfigurationSerialization' + unknown: list[str] = [] + + def walk(node: Any, kind: str, path: str) -> None: + if isinstance(node, list): + for index, item in enumerate(node): + walk(item, kind, f'{path}[{index}]') + return + + if not isinstance(node, dict): + return + + if allowed := keys.get(kind): + unknown.extend(f'{path}.{key}' if path else key for key in sorted(set(node) - allowed)) + + for child_key in children.get(kind, ()): + if (child := node.get(child_key)) is not None: + walk(child, serialization_child_kind(child_key), f'{path}.{child_key}' if path else child_key) + + walk(disk_config, root, '') + + return unknown diff --git a/archinstall/lib/models/device.py b/archinstall/lib/models/device.py index 0e5fb95fcf..916c170549 100644 --- a/archinstall/lib/models/device.py +++ b/archinstall/lib/models/device.py @@ -994,7 +994,10 @@ def from_existing_partition(cls, partition_info: _PartitionInfo) -> Self: SubvolumeModification.from_existing_subvol_info(i), ) else: - mountpoint = partition_info.mountpoints[0] if partition_info.mountpoints else None + # lsblk reports '[SWAP]' as the mountpoint of an active swap area. That is a + # display marker, not a path: a swap area has no mountpoint, and treating the + # marker as one leads to mounting the device on /[SWAP]. + mountpoint = next((p for p in partition_info.mountpoints if p.is_absolute()), None) subvol_mods = [] return cls( @@ -1848,3 +1851,71 @@ def serialize_size(self, size: Size) -> str: @classmethod def fields(cls) -> list[str]: return [field.alias or name for name, field in cls.model_fields.items() if name != 'children'] + + +# The serialisation of the disk configuration is a tree of TypedDicts. Reading it +# looks keys up by name, so a key that is misspelled - or one belonging to an older +# release - does not fail anywhere: the value it was meant to set keeps its default. +# Walking the same TypedDicts lets the loader point at the key instead of guessing. +_SERIALIZATION_CHILDREN: dict[str, tuple[str, ...]] = { + '_DiskLayoutConfigurationSerialization': ('device_modifications', 'lvm_config', 'disk_encryption', 'btrfs_options'), + '_DeviceModificationSerialization': ('partitions',), + '_PartitionModificationSerialization': ('start', 'size', 'btrfs'), + '_SizeSerialization': ('sector_size',), + '_LvmConfigurationSerialization': ('vol_groups',), + '_LvmVolumeGroupSerialization': ('volumes',), + '_LvmVolumeSerialization': ('length', 'btrfs'), + '_DiskEncryptionSerialization': ('hsm_device',), +} + + +def serialization_keys() -> dict[str, frozenset[str]]: + """The keys each node of the disk configuration serialisation accepts. + + Built from the type annotations rather than from a second hand written list, so + it cannot drift away from what is actually read. Every type that is reached + through a nested key is included, not only the ones that themselves have + children. + """ + kinds = set(_SERIALIZATION_CHILDREN) | set(_SERIALIZATION_CHILD_KINDS.values()) + + keys: dict[str, frozenset[str]] = {} + + for name in sorted(kinds): + serialization = globals().get(name) + + if serialization is None or not hasattr(serialization, '__annotations__'): + continue + + keys[name] = frozenset(serialization.__annotations__) + + return keys + + +def serialization_child_kind(key: str) -> str: + """The serialisation type stored under a nested key, if it is a known one.""" + return _SERIALIZATION_CHILD_KINDS.get(key, '') + + +def serialization_children() -> dict[str, tuple[str, ...]]: + """For each node, which of its keys hold a nested serialisation.""" + return dict(_SERIALIZATION_CHILDREN) + + +# which serialisation type sits under each nested key; the key names do not follow +# the type names, so the mapping has to be explicit +_SERIALIZATION_CHILD_KINDS: dict[str, str] = { + 'device_modifications': '_DeviceModificationSerialization', + 'partitions': '_PartitionModificationSerialization', + 'lvm_config': '_LvmConfigurationSerialization', + 'vol_groups': '_LvmVolumeGroupSerialization', + 'volumes': '_LvmVolumeSerialization', + 'disk_encryption': '_DiskEncryptionSerialization', + 'hsm_device': '_Fido2DeviceSerialization', + 'start': '_SizeSerialization', + 'size': '_SizeSerialization', + 'length': '_SizeSerialization', + 'sector_size': '_SectorSizeSerialization', + 'btrfs': '_SubvolumeModificationSerialization', + 'btrfs_options': '_BtrfsOptionsSerialization', +} diff --git a/tests/test_config_keys.py b/tests/test_config_keys.py index 10617d86a3..0c5613e9ff 100644 --- a/tests/test_config_keys.py +++ b/tests/test_config_keys.py @@ -40,3 +40,80 @@ def test_config_written_by_another_version_is_reported( output = capsys.readouterr() assert '0.0.0-ancient' in output.out + output.err + + +def _disk_config(partition_overrides: dict | None = None) -> dict: + partition = { + 'obj_id': 'x', + 'status': 'create', + 'type': 'primary', + 'start': {'value': 1, 'unit': 'MiB', 'sector_size': {'value': 512, 'unit': 'B'}}, + 'size': {'value': 100, 'unit': 'MiB', 'sector_size': {'value': 512, 'unit': 'B'}}, + 'fs_type': 'ext4', + 'mountpoint': '/', + 'mount_options': [], + 'flags': [], + 'dev_path': None, + 'btrfs': [], + } + partition.update(partition_overrides or {}) + + return { + 'config_type': 'manual_partitioning', + 'device_modifications': [{'device': '/dev/sda', 'wipe': True, 'partitions': [partition]}], + } + + +def test_unknown_keys_are_found_inside_the_disk_configuration( + monkeypatch: MonkeyPatch, + tmp_path: Path, + capsys: CaptureFixture[str], + probed_devices: list[Path], +) -> None: + """A misspelled partition key must be named, not dropped.""" + assert probed_devices, 'the fixture registers a device the config names' + + config_file = _write( + tmp_path, + {'timezone': 'UTC', 'disk_config': _disk_config({'mountpiont': '/x'})}, + ) + monkeypatch.setattr('sys.argv', ['archinstall', '--config', str(config_file)]) + + ArchConfigHandler() + + assert 'mountpiont' in capsys.readouterr().out + capsys.readouterr().err + + +def test_unknown_keys_are_found_in_a_nested_size( + monkeypatch: MonkeyPatch, + tmp_path: Path, + capsys: CaptureFixture[str], + probed_devices: list[Path], +) -> None: + assert probed_devices, 'the fixture registers a device the config names' + + config = _disk_config() + config['device_modifications'][0]['partitions'][0]['start']['valu'] = 1 + + config_file = _write(tmp_path, {'timezone': 'UTC', 'disk_config': config}) + monkeypatch.setattr('sys.argv', ['archinstall', '--config', str(config_file)]) + + ArchConfigHandler() + + assert 'valu' in capsys.readouterr().out + capsys.readouterr().err + + +def test_a_correct_disk_configuration_is_not_reported( + monkeypatch: MonkeyPatch, + tmp_path: Path, + capsys: CaptureFixture[str], + probed_devices: list[Path], +) -> None: + assert probed_devices, 'the fixture registers a device the config names' + + config_file = _write(tmp_path, {'timezone': 'UTC', 'disk_config': _disk_config()}) + monkeypatch.setattr('sys.argv', ['archinstall', '--config', str(config_file)]) + + ArchConfigHandler() + + assert 'Ignoring unknown keys in the disk configuration' not in capsys.readouterr().out + capsys.readouterr().err