From 77dc8ea8dc455d287877c477829376caafe957ed Mon Sep 17 00:00:00 2001 From: xander-lin <277640093+xander-lin@users.noreply.github.com> Date: Wed, 28 Oct 2026 12:01:23 +0800 Subject: [PATCH] fix: require a length for a partition that is being created The interactive prompt has always refused a size below 1 MiB, but that was the only place the limit existed: `PartitionModification` checked the device path and the filesystem type but never the length, and the configuration loader only rejected lengths through its alignment check - which a zero-length partition passes, since zero is aligned. A configuration file or a script could therefore produce a zero-length partition that sat in the layout through the summary and the confirmation, and only failed when libparted tried to write it. Reject it where the partition is described. EXIST and MODIFY entries report a length that was measured on the disk rather than chosen, so they are left as they are. --- archinstall/lib/models/device.py | 9 +++++ tests/test_partition_length.py | 61 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 tests/test_partition_length.py diff --git a/archinstall/lib/models/device.py b/archinstall/lib/models/device.py index b4c069e659..f22ecf8d2d 100644 --- a/archinstall/lib/models/device.py +++ b/archinstall/lib/models/device.py @@ -930,6 +930,15 @@ def __post_init__(self) -> None: if self.fs_type is None and self.status == ModificationStatus.MODIFY: raise ValueError('FS type must not be empty on modifications with status type modify') + # A partition that is being created needs a real length. The interactive + # prompt has always enforced this, but a configuration file or a script + # could put a zero-length partition into the layout, where it survived until + # libparted refused it while writing - long after the layout had been + # summarised and confirmed. EXIST and MODIFY describe what is already on the + # disk, so their length is reported rather than chosen and is left alone. + if self.status == ModificationStatus.CREATE and self.length <= Size(0, Unit.B, self.start.sector_size): + raise ValueError(f'A created partition needs a length, got {self.length.format_size(Unit.B)}') + @override def __hash__(self) -> int: return hash(self._obj_id) diff --git a/tests/test_partition_length.py b/tests/test_partition_length.py new file mode 100644 index 0000000000..1033736d16 --- /dev/null +++ b/tests/test_partition_length.py @@ -0,0 +1,61 @@ +from pathlib import Path + +import pytest + +from archinstall.lib.models.device import ( + FilesystemType, + ModificationStatus, + PartitionModification, + PartitionType, + SectorSize, + Size, + Unit, +) + + +def _make( + sector_size: SectorSize, + status: ModificationStatus, + length: Size, + dev_path: Path | None = None, +) -> PartitionModification: + return PartitionModification( + status=status, + type=PartitionType.PRIMARY, + start=Size(1, Unit.MiB, sector_size), + length=length, + fs_type=FilesystemType.EXT4, + mountpoint=Path('/'), + dev_path=dev_path, + ) + + +@pytest.mark.parametrize( + 'length', + [ + Size(0, Unit.B, SectorSize.default()), + Size(0, Unit.sectors, SectorSize.default()), + ], +) +def test_a_created_partition_needs_a_length(length: Size) -> None: + """A zero-length partition cannot be written to a disk.""" + with pytest.raises(ValueError, match='needs a length'): + _make(SectorSize.default(), ModificationStatus.CREATE, length) + + +def test_a_created_partition_with_a_length_is_accepted() -> None: + sector_size = SectorSize.default() + + part = _make(sector_size, ModificationStatus.CREATE, Size(1, Unit.MiB, sector_size)) + + assert part.end > part.start + + +def test_existing_partitions_report_their_length() -> None: + """EXIST and MODIFY describe what is on the disk, so their length is not up for + validation - a partition found on the disk is accepted as it is.""" + sector_size = SectorSize.default() + + for status in (ModificationStatus.EXIST, ModificationStatus.MODIFY): + part = _make(sector_size, status, Size(0, Unit.B, sector_size), Path('/dev/sda1')) + assert part.length == Size(0, Unit.B, sector_size)