From: Daan De Meyer Date: Mon, 3 Jan 2022 16:29:12 +0000 (-0800) Subject: Support older versions of sfdisk without "grain" support X-Git-Tag: v13~96 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fdc405ca110e4c9385ef6ca325483f00c6e07103;p=thirdparty%2Fmkosi.git Support older versions of sfdisk without "grain" support If "grain" is not available, let's fall back to the default grain size of 1 MiB. To check if "grain" is available, let's run sfdisk against /dev/full and try to configure the "grain" which will fail if "grain" is not supported. --- diff --git a/mkosi/backend.py b/mkosi/backend.py index 3c3e82e8f..786845c36 100644 --- a/mkosi/backend.py +++ b/mkosi/backend.py @@ -6,6 +6,7 @@ import argparse import contextlib import dataclasses import enum +import functools import math import os import resource @@ -260,6 +261,18 @@ class Partition: return ', '.join(filter(None, desc)) +@functools.lru_cache(maxsize=None) +def sfdisk_grain_is_supported() -> bool: + cmd: List[PathString] = ["sfdisk", "--no-reread", "--no-act", "--quiet", "/dev/full"] + + try: + run(cmd, text=True, input='\n'.join(["label: gpt", "grain: 4096", "quit"]), stderr=subprocess.DEVNULL) + except subprocess.CalledProcessError: + return False + + return True + + @dataclasses.dataclass class PartitionTable: partitions: Dict[PartitionIdentifier, Partition] = dataclasses.field(default_factory=dict) @@ -269,6 +282,10 @@ class PartitionTable: grain: int = 4096 + def __post_init__(self) -> None: + if not sfdisk_grain_is_supported(): + self.grain = 1024 ** 2 # Use sfdisk default grain size of 1 MiB. + def first_partition_offset(self, max_partitions: int = 128) -> int: if self.first_lba is not None: # No rounding here, we honour the specified value exactly. @@ -323,7 +340,7 @@ class PartitionTable: def sfdisk_spec(self) -> str: table = ["label: gpt", - f"grain: {self.grain}", + f"grain: {self.grain}" if sfdisk_grain_is_supported() else '\n', f"first-lba: {self.first_partition_offset() // self.sector_size}", *(p.sfdisk_spec() for p in self.partitions.values())] return '\n'.join(table)