Skip to content

Commit

Permalink
Add proper parser for sector size
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanDeMeyer committed Nov 9, 2023
1 parent 8ab480d commit c10aa71
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mkosi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1993,7 +1993,7 @@ def make_image(
if split:
cmdline += ["--split=yes"]
if state.config.sector_size:
cmdline += ["--sector-size", state.config.sector_size]
cmdline += ["--sector-size", str(state.config.sector_size)]

if definitions:
for d in definitions:
Expand Down
24 changes: 21 additions & 3 deletions mkosi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from mkosi.pager import page
from mkosi.run import run
from mkosi.types import PathString, SupportsRead
from mkosi.util import INVOKING_USER, StrEnum, chdir, flatten
from mkosi.util import INVOKING_USER, StrEnum, chdir, flatten, is_power_of_2
from mkosi.versioncomp import GenericVersion

__version__ = "18"
Expand Down Expand Up @@ -572,6 +572,24 @@ def parse_drive(value: str) -> QemuDrive:
return QemuDrive(id=id, size=size, directory=directory, options=options)


def config_parse_sector_size(value: Optional[str], old: Optional[int]) -> Optional[int]:
if not value:
return None

try:
size = int(value)
except ValueError:
die(f"'{value}' is not a valid number")

if size < 512 or size > 4096:
die(f"Sector size not between 512 and 4096: {size}")

if not is_power_of_2(size):
die(f"Sector size not power of 2: {size}")

return size


@dataclasses.dataclass(frozen=True)
class MkosiConfigSetting:
dest: str
Expand Down Expand Up @@ -797,7 +815,7 @@ class MkosiConfig:
image_version: Optional[str]
split_artifacts: bool
repart_dirs: list[Path]
sector_size: Optional[str]
sector_size: Optional[int]
overlay: bool
use_subvolumes: ConfigFeature
seed: Optional[uuid.UUID]
Expand Down Expand Up @@ -1333,7 +1351,7 @@ def parse_ini(path: Path, only_sections: Collection[str] = ()) -> Iterator[tuple
MkosiConfigSetting(
dest="sector_size",
section="Output",
parse=config_parse_string,
parse=config_parse_sector_size,
help="Set the disk image sector size",
),
MkosiConfigSetting(
Expand Down
4 changes: 4 additions & 0 deletions mkosi/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,7 @@ def umask(mask: int) -> Iterator[None]:
yield
finally:
os.umask(old)


def is_power_of_2(x: int) -> bool:
return x > 0 and (x & x - 1 == 0)

0 comments on commit c10aa71

Please sign in to comment.