]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Make sure we pass the right context to finalize_default_initrd()
authorDaanDeMeyer <daan.j.demeyer@gmail.com>
Thu, 8 Jan 2026 17:06:16 +0000 (18:06 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Sat, 10 Jan 2026 10:26:36 +0000 (11:26 +0100)
We mess around with the context object to make it for for use when
reading the subimages. But we need the full context again for parsing
the default initrd later on, so make a copy before we delete stuff and
pass that to finalize_default_initrd()

Fixes #4114

mkosi/config.py
tests/test_config.py

index 5e5574c1c26ec7543d1262e7a0a692b4ce86d340..f90fcff4a2d49eafe438ed29ec012a2a9cf50844 100644 (file)
@@ -5333,6 +5333,8 @@ def parse_config(
             context.parse_config_one(configdir, parse_profiles=True, parse_local=True)
 
     config = context.finalize()
+    # Keep a copy around for passing to finalize_default_initrd() later.
+    maincontext = copy.deepcopy(context)
 
     if config["history"] and want_new_history(args):
         Path(".mkosi-private/history").mkdir(parents=True, exist_ok=True)
@@ -5428,7 +5430,7 @@ def parse_config(
     subimages = [Config.from_dict(ns) for ns in images]
 
     if any(want_default_initrd(image) for image in subimages + [main]):
-        initrd = finalize_default_initrd(context, config, resources=resources)
+        initrd = finalize_default_initrd(maincontext, config, resources=resources)
 
         if want_default_initrd(main):
             main = dataclasses.replace(
index 20b5ecd40cd8d6ddd5e01a04c65ecf8a7cda1241..2468236c93555dc448c79185297929d091602930 100644 (file)
@@ -1652,3 +1652,34 @@ def test_assert(tmp_path: Path) -> None:
 
         parse_config(["--image-id", "abcde", "--environment", "ABC=QED"])
         parse_config(["--image-id", "abcde", "--environment", "DEF=QEE"])
+
+
+def test_initrd_packages(tmp_path: Path) -> None:
+    d = tmp_path
+
+    (d / "mkosi.conf").write_text(
+        """\
+        [Content]
+        InitrdPackages=package1
+        InitrdPackages=package2
+
+        [Content]
+        Bootable=yes
+        """
+    )
+
+    with chdir(d), resource_path(mkosi.resources) as resources:
+        _, _, [initrd, _] = parse_config(resources=resources)
+
+    assert "package1" in initrd.packages
+    assert "package2" in initrd.packages
+
+    # Make sure the InitrdPackages= are also picked up when a subimage is defined.
+    (d / "mkosi.images").mkdir()
+    (d / "mkosi.images/subimage.conf").touch()
+
+    with chdir(d), resource_path(mkosi.resources) as resources:
+        _, _, [_, initrd, _] = parse_config(resources=resources)
+
+    assert "package1" in initrd.packages
+    assert "package2" in initrd.packages