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)