]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add option to run nspawn in current unit
authorRémi Palancher <remi@rackslab.io>
Mon, 24 Jan 2022 08:59:29 +0000 (09:59 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 5 Apr 2022 08:53:58 +0000 (10:53 +0200)
This commit adds --nspawn-keep-unit option to add --keep-unit option to
underlying systemd-nspawn commands. This makes systemd-nspawn uses the
current unit scope and allocated ressources. This can be notably useful
when mkosi is run by a system service.

mkosi.md
mkosi/__init__.py
mkosi/backend.py
tests/test_config_parser.py

index cd974fad81894ec77f619b3ee6576ad73e25629e..0ad3f43be76fa51a1f16b6ae50d0333e33190c52 100644 (file)
--- a/mkosi.md
+++ b/mkosi.md
@@ -1095,6 +1095,13 @@ a machine ID.
 : When used with the `qemu` verb, this options sets `qemu`'s `-m`
   argument which controls the amount of guest's RAM. Defaults to `1G`.
 
+`NspawnKeepUnit=`, `--nspawn-keep-unit`
+
+: When used, this option instructs underlying calls of systemd-nspawn to
+  use the current unit scope, instead of creating a dedicated transcient
+  scope unit for the containers. This option should be used when mkosi is
+  run by a service unit.
+
 `Netdev=`, `--netdev`
 
 : When used with the boot or qemu verbs, this option creates a virtual
index d529c081b4130525eb076f5d6879aee9b528324d..9714a355624e0ba23594e1a7515c2dfa0313c4d3 100644 (file)
@@ -5630,6 +5630,11 @@ def create_parser() -> ArgumentParserMkosi:
     group.add_argument("--qemu-headless", action=BooleanAction, help="Configure image for qemu's -nographic mode")
     group.add_argument("--qemu-smp", help="Configure guest's SMP settings", metavar="SMP", default="2")
     group.add_argument("--qemu-mem", help="Configure guest's RAM size", metavar="MEM", default="1G")
+    group.add_argument(
+        "--nspawn-keep-unit",
+        action=BooleanAction,
+        help="If specified, underlying systemd-nspawn containers use the ressources of the current unit."
+    )
     group.add_argument(
         "--network-veth",
         dest="netdev",
@@ -7258,6 +7263,9 @@ def run_build_script(args: MkosiArgs, root: Path, raw: Optional[BinaryIO]) -> No
         if args.usr_only:
             cmdline += [f"--bind={root_home(args, root)}:/root"]
 
+        if args.nspawn_keep_unit:
+            cmdline += ["--keep-unit"]
+
         cmdline += [f"/root/{args.build_script.name}"]
         cmdline += args.cmdline
 
@@ -7519,6 +7527,9 @@ def run_shell_cmdline(args: MkosiArgs, pipe: bool = False, commands: Optional[Se
 
     cmdline += ["--machine", virt_name(args)]
 
+    if args.nspawn_keep_unit:
+        cmdline += ["--keep-unit"]
+
     if commands or args.cmdline:
         # If the verb is 'shell', args.cmdline contains the command to run.
         # Otherwise, the verb is 'boot', and we assume args.cmdline contains nspawn arguments.
index f69277399258e902ab7e91e94721effab2494c41..36c856f99f73fdb87867d067a43b2c40a33d050e 100644 (file)
@@ -517,6 +517,9 @@ class MkosiArgs:
     qemu_smp: str
     qemu_mem: str
 
+    # systemd-nspawn specific options
+    nspawn_keep_unit: bool
+
     # Some extra stuff that's stored in MkosiArgs for convenience but isn't populated by arguments
     machine_id_is_fixed: bool
     original_umask: int
@@ -661,6 +664,9 @@ def run_workspace_command(
         stdout = subprocess.PIPE
         nspawn += ["--console=pipe"]
 
+    if args.nspawn_keep_unit:
+        nspawn += ["--keep-unit"]
+
     result = run([*nspawn, "--", *cmd], check=False, stdout=stdout, text=capture_stdout)
     if result.returncode != 0:
         if "workspace-command" in ARG_DEBUG:
index f51658dfd705f107d27c20603ad7bcfcfa208bff..7f8e1bd56e8f7b0dca049fdbd4f844aab3509b6a 100644 (file)
@@ -132,6 +132,7 @@ class MkosiConfig:
             "qemu_headless": False,
             "qemu_smp": "2",
             "qemu_mem": "1G",
+            "nspawn_keep_unit": False,
             "netdev": False,
             "ephemeral": False,
             "with_unified_kernel_images": True,