]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add RuntimeSize= setting 1944/head
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 1 Oct 2023 18:20:12 +0000 (20:20 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 2 Oct 2023 08:01:29 +0000 (10:01 +0200)
Currently we unconditionally grow disk images to 8G before booting
them in systemd-nspawn or qemu. Let's do better here by making the
size configurable and not growing the disk images by default.

We also move format_bytes() to config.py as most other formatting
functions are located there.

mkosi/__init__.py
mkosi/config.py
mkosi/qemu.py
mkosi/resources/mkosi.md
mkosi/util.py

index deed41f529619d278c0ac08e18e50ad0f102bbd2..a2175c7bbb5cda25ecb5394808af5c88cb7cac40 100644 (file)
@@ -34,6 +34,7 @@ from mkosi.config import (
     OutputFormat,
     SecureBootSignTool,
     Verb,
+    format_bytes,
     format_source_target,
     parse_config,
     summary,
@@ -60,7 +61,6 @@ from mkosi.types import _FILE, CompletedProcess, PathString
 from mkosi.util import (
     InvokingUser,
     flatten,
-    format_bytes,
     format_rlimit,
     one_zero,
     scopedenv,
@@ -2125,7 +2125,7 @@ def run_shell(args: MkosiArgs, config: MkosiConfig) -> None:
         if config.output_format == OutputFormat.disk and args.verb == Verb.boot:
             run(["systemd-repart",
                  "--image", fname,
-                 "--size", "8G",
+                 *([f"--size={config.runtime_size}"] if config.runtime_size else []),
                  "--no-pager",
                  "--dry-run=no",
                  "--offline=no",
index 7bbdfc6c5dde70071cc537a6ebc957ea0f724246..8c58d8dd05a0c8592cb2f715ada45194c8b01df6 100644 (file)
@@ -773,6 +773,7 @@ class MkosiConfig:
     tools_tree_release: Optional[str]
     tools_tree_packages: list[str]
     runtime_trees: list[tuple[Path, Optional[Path]]]
+    runtime_size: Optional[int]
 
     # QEMU-specific options
     qemu_gui: bool
@@ -1759,6 +1760,13 @@ SETTINGS = (
         parse=config_make_list_parser(delimiter=",", parse=make_source_target_paths_parser()),
         help="Additional mounts to add when booting the image",
     ),
+    MkosiConfigSetting(
+        dest="runtime_size",
+        metavar="SIZE",
+        section="Host",
+        parse=config_parse_bytes,
+        help="Grow disk images to the specified size before booting them",
+    ),
 )
 
 MATCHES = (
@@ -2429,6 +2437,21 @@ def line_join_source_target_list(array: Sequence[tuple[Path, Optional[Path]]]) -
     return "\n                                ".join(items)
 
 
+def format_bytes(num_bytes: int) -> str:
+    if num_bytes >= 1024**3:
+        return f"{num_bytes/1024**3 :0.1f}G"
+    if num_bytes >= 1024**2:
+        return f"{num_bytes/1024**2 :0.1f}M"
+    if num_bytes >= 1024:
+        return f"{num_bytes/1024 :0.1f}K"
+
+    return f"{num_bytes}B"
+
+
+def format_bytes_or_none(num_bytes: Optional[int]) -> str:
+    return format_bytes(num_bytes) if num_bytes is not None else "none"
+
+
 def summary(args: MkosiArgs, config: MkosiConfig) -> str:
     def bold(s: Any) -> str:
         return f"{Style.bold}{s}{Style.reset}"
@@ -2560,6 +2583,7 @@ Clean Package Manager Metadata: {yes_no_auto(config.clean_package_metadata)}
             Tools Tree Release: {none_to_none(config.tools_tree_release)}
            Tools Tree Packages: {line_join_list(config.tools_tree_packages)}
                  Runtime Trees: {line_join_source_target_list(config.runtime_trees)}
+                  Runtime Size: {format_bytes_or_none(config.runtime_size)}
 
                       QEMU GUI: {yes_no(config.qemu_gui)}
                 QEMU CPU Cores: {config.qemu_smp}
index 1982f7b1fa4d092b1028efac7187ef368c994de7..7b14d30ade5421d6d88aa3789b79ce968f2e01a5 100644 (file)
@@ -23,13 +23,14 @@ from mkosi.config import (
     MkosiConfig,
     OutputFormat,
     QemuFirmware,
+    format_bytes,
 )
 from mkosi.log import die
 from mkosi.partition import finalize_root, find_partitions
 from mkosi.run import MkosiAsyncioThread, run, spawn
 from mkosi.tree import copy_tree, rmtree
 from mkosi.types import PathString
-from mkosi.util import format_bytes, qemu_check_kvm_support, qemu_check_vsock_support
+from mkosi.util import qemu_check_kvm_support, qemu_check_vsock_support
 
 
 def machine_cid(config: MkosiConfig) -> int:
@@ -399,11 +400,11 @@ def run_qemu(args: MkosiArgs, config: MkosiConfig, uid: int, gid: int) -> None:
         else:
             fname = config.output_dir / config.output
 
-        if config.output_format == OutputFormat.disk and not config.qemu_cdrom:
+        if config.output_format == OutputFormat.disk and config.runtime_size:
             run(["systemd-repart",
                  "--definitions", "",
                  "--no-pager",
-                 "--size=8G",
+                 f"--size={config.runtime_size}",
                  "--pretty=no",
                  "--offline=yes",
                  fname])
index c7ce314515bc03be80a63df4d06e68290cd8e3c0..d761dd9be64678c370e14b0338b87119693cd194 100644 (file)
@@ -1242,6 +1242,13 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
   machine in these directories will be owned by the user running mkosi
   on the host.
 
+`RuntimeSize=`, `--runtime-size`
+
+: If specified, disk images are grown to the specified size before
+  they're booted with systemd-nspawn or qemu. 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.
+
 ## Supported distributions
 
 Images may be created containing installations of the following
index 3a8403117ad6c9dd2f429646e3300ff74744f795..ce7c73ada90d74f2465e1b79099a4c5aa8839870 100644 (file)
@@ -160,17 +160,6 @@ def qemu_check_vsock_support(log: bool) -> bool:
     return True
 
 
-def format_bytes(num_bytes: int) -> str:
-    if num_bytes >= 1024**3:
-        return f"{num_bytes/1024**3 :0.1f}G"
-    if num_bytes >= 1024**2:
-        return f"{num_bytes/1024**2 :0.1f}M"
-    if num_bytes >= 1024:
-        return f"{num_bytes/1024 :0.1f}K"
-
-    return f"{num_bytes}B"
-
-
 def make_executable(path: Path) -> None:
     st = path.stat()
     os.chmod(path, st.st_mode | stat.S_IEXEC)