From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Fri, 14 Mar 2025 11:36:43 +0000 (+0100) Subject: Make Drive= flags more general X-Git-Tag: v26~274^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a9cc90e93f54e05fe5916d71c71d78fcc69d96d2;p=thirdparty%2Fmkosi.git Make Drive= flags more general --- diff --git a/mkosi/config.py b/mkosi/config.py index 9b5e844ac..571d4044e 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -159,6 +159,10 @@ class ConfigTree: return f"{self.source}:{self.target}" if self.target else f"{self.source}" +class DriveFlag(StrEnum): + persist = enum.auto() + + @dataclasses.dataclass(frozen=True) class Drive: id: str @@ -166,7 +170,7 @@ class Drive: directory: Optional[Path] options: Optional[str] file_id: str - persist: bool + flags: list[DriveFlag] # We use negative numbers for specifying special constants @@ -1407,13 +1411,16 @@ def parse_drive(value: str) -> Drive: if not is_valid_filename(id): die(f"Unsupported path character in drive id '{id}'") + flag_parser = make_enum_parser(DriveFlag) + flag_list = p.split(",") if len(parts) > 5 and (p := parts[5]) else [] + return Drive( id=id, size=parse_bytes(parts[1]), directory=parse_path(p) if len(parts) > 2 and (p := parts[2]) else None, options=p if len(parts) > 3 and (p := parts[3]) else None, file_id=p if len(parts) > 4 and (p := parts[4]) else id, - persist=parse_boolean(p) if len(parts) > 5 and (p := parts[5]) else False, + flags=[flag_parser(f) for f in flag_list], ) @@ -5549,7 +5556,7 @@ def json_type_transformer(refcls: Union[type[Args], type[Config]]) -> Callable[[ directory=Path(d["Directory"]) if d.get("Directory") else None, options=d.get("Options"), file_id=d.get("FileId", d["Id"]), - persist=d.get("Persist", False), + flags=[DriveFlag(f) for f in d.get("Flags", [])], ) ) diff --git a/mkosi/qemu.py b/mkosi/qemu.py index 0c406d818..329133d62 100644 --- a/mkosi/qemu.py +++ b/mkosi/qemu.py @@ -32,6 +32,7 @@ from mkosi.config import ( ConfigFeature, ConsoleMode, Drive, + DriveFlag, Firmware, Network, OutputFormat, @@ -797,7 +798,7 @@ def apply_runtime_size(config: Config, image: Path) -> None: def finalize_drive(drive: Drive) -> Iterator[Path]: with contextlib.ExitStack() as stack: file: IO[bytes] - if drive.persist: + if DriveFlag.persist in drive.flags: path = Path(drive.directory or "/var/tmp") / f"mkosi-drive-{drive.id}" file = path.open("a+b") else: diff --git a/mkosi/resources/man/mkosi.1.md b/mkosi/resources/man/mkosi.1.md index 80319847e..08fbd9f03 100644 --- a/mkosi/resources/man/mkosi.1.md +++ b/mkosi/resources/man/mkosi.1.md @@ -1822,7 +1822,7 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`, `Drives=`, `--drive=` : Add a drive. Takes a colon-delimited string of format - `:[:[:[:[:]]]]`. `id` specifies + `:[:[:[:[:]]]]`. `id` specifies the ID assigned to the drive. This can be used as the `drive=` property in various **qemu** devices. `size` specifies the size of the drive. This takes a size in bytes. Additionally, the suffixes `K`, `M` @@ -1834,10 +1834,11 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`, backing the drive. If unset, this defaults to the drive ID. Drives with the same file ID will share the backing file. The directory and size of the file will be determined from the first drive with a given file ID. - `persist` takes a boolean value and determines whether the drive will be persisted across **qemu** invocations. - Enabling persistence also prevents suffixing the filename with a random string. - The file backing the drive will always be available under `//mkosi-drive-` - You can skip values by setting them to the empty string, specifying e.g. `myfs:1G::::yes` + `flags` takes a comma-separated list of drive flags which currently only supports `persist`. + `persist` determines whether the drive will be persisted across **qemu** invocations. + The files backing the drives will be created with the schema + `//mkosi-drive-`. + You can skip values by setting them to the empty string, specifying e.g. `myfs:1G::::persist` will create a persistent drive under `/var/tmp/mkosi-drive-myfs`. **Example usage:** diff --git a/tests/test_json.py b/tests/test_json.py index 3e46115a2..11cd2d7ef 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -25,6 +25,7 @@ from mkosi.config import ( ConsoleMode, DocFormat, Drive, + DriveFlag, Firmware, Incremental, InitrdProfile, @@ -153,25 +154,27 @@ def test_config() -> None: { "Directory": "/foo/bar", "FileId": "red", + "Flags": [], "Id": "abc", "Options": "abc,qed", - "Persist": false, "Size": 200 }, { "Directory": null, "FileId": "wcd", + "Flags": [], "Id": "abc", "Options": "", - "Persist": false, "Size": 200 }, { "Directory": null, "FileId": "bla", + "Flags": [ + "persist" + ], "Id": "abc", "Options": "", - "Persist": true, "Size": 200 } ], @@ -458,9 +461,9 @@ def test_config() -> None: dependencies=["dep1"], distribution=Distribution.fedora, drives=[ - Drive("abc", 200, Path("/foo/bar"), "abc,qed", "red", False), - Drive("abc", 200, None, "", "wcd", False), - Drive("abc", 200, None, "", "bla", True), + Drive("abc", 200, Path("/foo/bar"), "abc,qed", "red", []), + Drive("abc", 200, None, "", "wcd", []), + Drive("abc", 200, None, "", "bla", [DriveFlag.persist]), ], environment_files=[], environment={"foo": "foo", "BAR": "BAR", "Qux": "Qux"},