]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add --cache-initrd option
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 5 Jul 2022 13:44:01 +0000 (15:44 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 5 Jul 2022 19:34:23 +0000 (21:34 +0200)
Building initrds is slow, let's add an option that allows building
the initrd as part of a cached image so that's it's only built once
and reused in any final images.

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

index 140d9c6536bcec3a756ee09b54d0e0e07e721d8c..c36cf5bb521581e5159a92df25e1f8a5695eaa9a 100644 (file)
--- a/mkosi.md
+++ b/mkosi.md
@@ -645,6 +645,13 @@ a boolean argument: either "1", "yes", or "true" to enable, or "0",
   supported distributions except Clear Linux and this option
   translates to enabling dracut's hostonly option.
 
+`CacheInitrd=`, `--cache-initrd`
+
+: If specified, and incremental mode is used, mkosi will build the initrd
+  in the cache image and reuse it in the final image. Note that this means
+  that any changes that are only applied to the final image and not the
+  cached image won't be included in the initrd.
+
 `UsrOnly=`, `--usr-only`
 
 : If specified, `mkosi` will only add the `/usr/` directory tree
index 321636520dd850283622913b30cce97d1f7b15d4..3a831e47393ba365e75658573cb64b64fcd307fc 100644 (file)
@@ -5620,6 +5620,12 @@ def create_parser() -> ArgumentParserMkosi:
         action=BooleanAction,
         help="Enable dracut hostonly option",
     )
+    group.add_argument(
+        "--cache-initrd",
+        metavar="BOOL",
+        action=BooleanAction,
+        help="When using incremental mode, build the initrd in the cache image and don't rebuild it in the final image",
+    )
     group.add_argument(
         "--split-artifacts",
         metavar="BOOL",
@@ -7391,8 +7397,14 @@ def setup_netdev(args: MkosiArgs, root: Path, do_run_build_script: bool, cached:
         run(["systemctl", "--root", root, "enable", "systemd-networkd"])
 
 
-def run_kernel_install(args: MkosiArgs, root: Path, do_run_build_script: bool, for_cache: bool) -> None:
-    if not args.bootable or do_run_build_script or for_cache:
+def run_kernel_install(args: MkosiArgs, root: Path, do_run_build_script: bool, for_cache: bool, cached: bool) -> None:
+    if not args.bootable or do_run_build_script:
+        return
+
+    if not args.cache_initrd and for_cache:
+        return
+
+    if args.cache_initrd and cached:
         return
 
     with complete_step("Generating initramfs images…"):
@@ -7504,7 +7516,7 @@ def build_image(
                 install_build_src(args, root, do_run_build_script, for_cache)
                 install_build_dest(args, root, do_run_build_script, for_cache)
                 install_extra_trees(args, root, for_cache)
-                run_kernel_install(args, root, do_run_build_script, for_cache)
+                run_kernel_install(args, root, do_run_build_script, for_cache, cached_tree)
                 install_boot_loader(args, root, loopdev, do_run_build_script, cached_tree)
                 set_root_password(args, root, do_run_build_script, cached_tree)
                 set_serial_terminal(args, root, do_run_build_script, cached_tree)
index 60a3f7f5b5ad0583706cdedb092d50ba87af27e2..a4a1c7ca8a38716588e025ee5dfe50762f381e88 100644 (file)
@@ -448,6 +448,7 @@ class MkosiArgs:
     with_unified_kernel_images: bool
     gpt_first_lba: Optional[int]
     hostonly_initrd: bool
+    cache_initrd: bool
     base_packages: Union[str, bool]
     packages: List[str]
     remove_packages: List[str]
index 52f6cbc64e6249bd2b24c05933dbfa19a3f99986..501841e0369d10b9f1ca31188276b4c712b0548d 100644 (file)
@@ -140,6 +140,7 @@ class MkosiConfig:
             "ephemeral": False,
             "with_unified_kernel_images": True,
             "hostonly_initrd": False,
+            "cache_initrd": False,
             "ssh": False,
             "ssh_key": None,
             "ssh_timeout": 0,
@@ -288,6 +289,8 @@ class MkosiConfig:
                 ]
             if "HostonlyInitrd" in mk_config_output:
                 self.reference_config[job_name]["hostonly_initrd"] = mk_config_output["HostonlyInitrd"]
+            if "CacheInitrd" in mk_config_output:
+                self.reference_config[job_name]["cache_initrd"] = mk_config_output["CacheInitrd"]
             if "MachineID" in mk_config_output:
                 self.reference_config[job_name]["MachineID"] = mk_config_output["MachineID"]
         if "Packages" in mk_config: