]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add option to persist runtime drives
authorSeptatrix <24257556+Septatrix@users.noreply.github.com>
Sat, 8 Mar 2025 13:21:34 +0000 (14:21 +0100)
committerJörg Behrmann <behrmann@physik.fu-berlin.de>
Mon, 10 Mar 2025 10:00:16 +0000 (11:00 +0100)
mkosi/config.py
mkosi/qemu.py
mkosi/resources/man/mkosi.1.md
tests/test_json.py

index 002b3e41bda018f2a80909330c462c1ff50a180f..7ebb3daa0a7c3ef547ea6bdcf306dff1d2b4995e 100644 (file)
@@ -156,6 +156,7 @@ class Drive:
     directory: Optional[Path]
     options: Optional[str]
     file_id: str
+    persist: bool
 
 
 # We use negative numbers for specifying special constants
@@ -1366,27 +1367,29 @@ def parse_profile(value: str) -> str:
 
 
 def parse_drive(value: str) -> Drive:
-    parts = value.split(":", maxsplit=4)
-    if not parts or not parts[0]:
+    parts = value.split(":")
+
+    if len(parts) > 6:
+        die(f"Too many components in drive '{value}")
+
+    if len(parts) < 1:
         die(f"No ID specified for drive '{value}'")
 
     if len(parts) < 2:
         die(f"Missing size in drive '{value}")
 
-    if len(parts) > 5:
-        die(f"Too many components in drive '{value}")
-
     id = parts[0]
     if not is_valid_filename(id):
         die(f"Unsupported path character in drive id '{id}'")
 
-    size = parse_bytes(parts[1])
-
-    directory = parse_path(parts[2]) if len(parts) > 2 and parts[2] else None
-    options = parts[3] if len(parts) > 3 and parts[3] else None
-    file_id = parts[4] if len(parts) > 4 and parts[4] else id
-
-    return Drive(id=id, size=size, directory=directory, options=options, file_id=file_id)
+    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,
+    )
 
 
 def config_parse_sector_size(value: Optional[str], old: Optional[int]) -> Optional[int]:
@@ -5288,6 +5291,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),
                 )
             )
 
index 1c2c7857a5079797f608c3f84167f1851a7fde96..ab21441789680fae2c7bd2b846e0adc0be946d33 100644 (file)
@@ -24,7 +24,7 @@ import textwrap
 import uuid
 from collections.abc import Iterator, Sequence
 from pathlib import Path
-from typing import Optional
+from typing import IO, Optional
 
 from mkosi.config import (
     Args,
@@ -795,10 +795,18 @@ def apply_runtime_size(config: Config, image: Path) -> None:
 
 @contextlib.contextmanager
 def finalize_drive(config: Config, drive: Drive) -> Iterator[Path]:
-    with tempfile.NamedTemporaryFile(
-        dir=drive.directory or "/var/tmp",
-        prefix=f"mkosi-drive-{drive.id}",
-    ) as file:
+    with contextlib.ExitStack() as stack:
+        file: IO[bytes]
+        if drive.persist:
+            path = Path(drive.directory or "/var/tmp") / f"mkosi-drive-{drive.id}"
+            file = path.open("a+b")
+        else:
+            file = stack.enter_context(
+                tempfile.NamedTemporaryFile(
+                    dir=drive.directory or "/var/tmp",
+                    prefix=f"mkosi-drive-{drive.id}",
+                )
+            )
         maybe_make_nocow(Path(file.name))
         file.truncate(round_up(drive.size, resource.getpagesize()))
         yield Path(file.name)
index 17230a24c63562e3905a1676f7693bec47e741eb..d46d3a0ae3becb1f404d5fc1a0b3008809101bbe 100644 (file)
@@ -1791,18 +1791,23 @@ 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>]]]`. `id` specifies
+    `<id>:<size>[:<directory>[:<options>[:<file-id>[:<persist>]]]]`. `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`
     and `G` can be used to specify a size in kilobytes, megabytes and
     gigabytes respectively. `directory` optionally specifies the directory
-    in which to create the file backing the drive. `options` optionally
-    specifies extra comma-delimited properties which are passed verbatim
+    in which to create the file backing the drive. If unset, the file will be created under `/var/tmp`.
+    `options` optionally specifies extra comma-delimited properties which are passed verbatim
     to **qemu**'s `-blockdev` option. `file-id` specifies the ID of the file
-    backing the drive. 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.
+    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`
+    will create a persistent drive under `/var/tmp/mkosi-drive-myfs`.
 
     **Example usage:**
 
index 1207459a3ca00be7a639802c61c25e1d07f96099..ff5d4bf4d7e11d26da59137948f5956912548cfc 100644 (file)
@@ -156,6 +156,7 @@ def test_config() -> None:
                     "FileId": "red",
                     "Id": "abc",
                     "Options": "abc,qed",
+                    "Persist": false,
                     "Size": 200
                 },
                 {
@@ -163,6 +164,15 @@ def test_config() -> None:
                     "FileId": "wcd",
                     "Id": "abc",
                     "Options": "",
+                    "Persist": false,
+                    "Size": 200
+                },
+                {
+                    "Directory": null,
+                    "FileId": "bla",
+                    "Id": "abc",
+                    "Options": "",
+                    "Persist": true,
                     "Size": 200
                 }
             ],
@@ -470,7 +480,11 @@ def test_config() -> None:
         credentials={"credkey": "credval"},
         dependencies=["dep1"],
         distribution=Distribution.fedora,
-        drives=[Drive("abc", 200, Path("/foo/bar"), "abc,qed", "red"), Drive("abc", 200, None, "", "wcd")],
+        drives=[
+            Drive("abc", 200, Path("/foo/bar"), "abc,qed", "red", False),
+            Drive("abc", 200, None, "", "wcd", False),
+            Drive("abc", 200, None, "", "bla", True),
+        ],
         environment_files=[],
         environment={"foo": "foo", "BAR": "BAR", "Qux": "Qux"},
         ephemeral=True,