]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Make Drive= flags more general
authorSeptatrix <24257556+Septatrix@users.noreply.github.com>
Fri, 14 Mar 2025 11:36:43 +0000 (12:36 +0100)
committerSeptatrix <24257556+Septatrix@users.noreply.github.com>
Thu, 3 Apr 2025 14:38:36 +0000 (16:38 +0200)
mkosi/config.py
mkosi/qemu.py
mkosi/resources/man/mkosi.1.md
tests/test_json.py

index 9b5e844ac07690a4cc6398b4aa9bc077822153d3..571d4044e8f17683fa2487923c5111874b8e54f2 100644 (file)
@@ -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", [])],
                 )
             )
 
index 0c406d818a9fa9dcce3be97d0c89e9df0d2a96fa..329133d62fa05883892bc8706d1225501b865e2c 100644 (file)
@@ -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:
index 80319847e29d3e5e62de718a7042814f89fbb1a3..08fbd9f03db7fb88a39d1f578920d0d1a580be13 100644 (file)
@@ -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>:<size>[:<directory>[:<options>[:<file-id>[:<persist>]]]]`. `id` specifies
+    `<id>:<size>[:<directory>[:<options>[:<file-id>[:<flags>]]]]`. `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 `/<directory>/mkosi-drive-<file-id>`
-    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
+    `/<directory>/mkosi-drive-<file-id>`.
+    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:**
index 3e46115a2511768d396c9de36902cfc2f8e6cd94..11cd2d7efb67283287e26a291d6b69874bd78cc0 100644 (file)
@@ -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"},