Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions archinstall/lib/models/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
61 changes: 61 additions & 0 deletions tests/test_partition_length.py
Original file line number Diff line number Diff line change
@@ -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)