]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Do prebuilt initrds by default
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 30 Apr 2023 20:41:54 +0000 (22:41 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 1 May 2023 08:09:19 +0000 (10:09 +0200)
Let's stop running kernel-install in favor of always doing prebuilt
initrds by default. We reimplement the depmod hook of kernel-install
ourselves (required for centos stream 8).

If no initrd is provided by the user, we build a minimal one ourselves
that's sufficient to boot in a qemu VM. If the default initrd is not
sufficient, the upcoming preset support can be used to build a custom
initrd instead.

17 files changed:
.github/mkosi.conf.d/10-common.conf
.github/mkosi.conf.d/20-alma.conf
.github/mkosi.conf.d/20-arch.conf
.github/mkosi.conf.d/20-centos.conf
.github/mkosi.conf.d/20-debian.conf
.github/mkosi.conf.d/20-fedora.conf
.github/mkosi.conf.d/20-opensuse.conf
.github/mkosi.conf.d/20-rocky.conf
.github/mkosi.conf.d/20-ubuntu.conf
.github/workflows/ci.yml
docs/bootable.md
mkosi.md
mkosi/__init__.py
mkosi/config.py
mkosi/distributions/__init__.py
mkosi/distributions/debian.py
mkosi/distributions/opensuse.py

index 6d34d64ba09a456cb74bc2616b6ce7b5e208d008..573ea82071bd28b64354791f1ed54f28d9c6df44 100644 (file)
@@ -4,5 +4,8 @@ KernelCommandLine=systemd.unit=mkosi-check-and-shutdown.service
                   systemd.log_target=console
                   systemd.default_standard_output=journal+console
 
+[Content]
+Bootable=yes
+
 [Host]
 Autologin=yes
index 197f12c9486cfbad5fd662ae8403359b06d55537..620ce5a4a4bcee928f966eb05b04211b37f08442 100644 (file)
@@ -9,4 +9,3 @@ Packages=kernel-core
          systemd
          systemd-boot
          systemd-udev
-         dracut
index fb8471f22dff5fc42687843eef3c5c69a19e604c..de760e4cad5740c80061c44521008da4a0c6fc44 100644 (file)
@@ -4,4 +4,3 @@ Distribution=arch
 [Content]
 Packages=linux
          systemd
-         dracut
index 9f9de2226b7bd2b63db10b5df137d3145d6e51ff..0033c61b952e27087a66ea9b54e65d9dac99e58c 100644 (file)
@@ -6,4 +6,3 @@ Packages=kernel-core
          systemd
          systemd-boot
          systemd-udev
-         dracut
index d7c358916fda919becf2bc7584d30fe5de4448e5..b6ec5f7a3881d848ca1be8b24daa214947705384 100644 (file)
@@ -7,5 +7,4 @@ Packages=linux-image-cloud-amd64
          systemd-boot
          systemd-sysv
          udev
-         dracut
          dbus
index e9c1da6ba3920d6c7f9bf3e081a255a3da3bb8ff..cf1171439963af9bbc83d331749e79039ef367f2 100644 (file)
@@ -6,5 +6,4 @@ Packages=kernel-core
          systemd
          systemd-boot
          systemd-udev
-         dracut
          util-linux
index 829dc2ef909fb5a4fbe040d093ffdacc21d6a0c2..9e811a699977356a6ca089ab6a840769046b1cae 100644 (file)
@@ -3,6 +3,5 @@ Distribution=opensuse
 
 [Content]
 Packages=kernel-default
-         dracut
          systemd
          udev
index 1ca46d7d5a1290a0a7d8d1e323eeca99c417bdee..72ad7f50009ac755e68ea7295ba59abf04f45ff9 100644 (file)
@@ -9,4 +9,3 @@ Packages=kernel-core
          systemd
          systemd-boot
          systemd-udev
-         dracut
index 24c4fe507f094447390fdc94b80a670cf5c6c7de..d847a27b1627419eeedc0b6e8c3640db089ffde3 100644 (file)
@@ -9,5 +9,4 @@ Packages=linux-virtual
          systemd
          systemd-sysv
          udev
-         dracut
          dbus
index ea93e425d967888bab909344e86b7c5bb89f50fb..6be8bf09c9587a3a28dede0a84c70a6cf4a5466c 100644 (file)
@@ -102,7 +102,7 @@ jobs:
     runs-on: ubuntu-22.04
     needs: unit-test
     concurrency:
-      group: ${{ github.workflow }}-${{ matrix.distro }}-${{ matrix.format }}-${{ matrix.prebuilt-initrd }}-${{ github.ref }}
+      group: ${{ github.workflow }}-${{ matrix.distro }}-${{ matrix.format }}-${{ github.ref }}
       cancel-in-progress: true
     defaults:
       run:
@@ -118,19 +118,14 @@ jobs:
           - fedora
           - rocky
           - alma
-          - gentoo
+          # Disabled until gentoo supports prebuilt initrds.
+          # - gentoo
           - opensuse
         format:
           - directory
           - tar
           - cpio
           - disk
-        prebuilt-initrd:
-          - no
-          - yes
-        exclude:
-          - distro: gentoo
-            prebuilt-initrd: yes
 
     steps:
     - uses: actions/checkout@v3
@@ -173,12 +168,6 @@ jobs:
     - name: Install
       run: sudo python3 -m pip install ..
 
-    - name: Build ${{ matrix.distro }} initrd
-      if: matrix.prebuilt-initrd == 'yes'
-      # Run the build in .github/workflows/ so we don't pick up any of the configuration files in .github/
-      run: python3 -m mkosi -d ${{ matrix.distro }} -t cpio -p systemd -p udev -p kmod --make-initrd -o $PWD/initrd build
-      working-directory: ./.github/workflows
-
     - name: Configure ${{ matrix.distro }}/${{ matrix.format }}
       run: |
         tee mkosi.conf.d/00-ci.conf <<- EOF
@@ -189,14 +178,6 @@ jobs:
         Format=${{ matrix.format }}
         EOF
 
-    - name: Configure ${{ matrix.distro }}/${{ matrix.format }} initrd
-      if: matrix.prebuilt-initrd == 'yes'
-      run: |
-        tee mkosi.conf.d/00-initrd.conf <<- EOF
-        [Distribution]
-        Initrds=workflows/initrd.zst
-        EOF
-
     - name: Resolve Arch geo mirror location
       if: matrix.distro == 'arch'
       run: curl -I geo.mirror.pkgbuild.com
index 43ed45be145d7cd8fb3c21c190b3a9473b9bef9d..501d1c902ca48d51b7875071bc82571f448e14b4 100644 (file)
@@ -10,7 +10,6 @@ distributions:
 [Content]
 Packages=linux
          systemd
-         dracut
 ```
 
 ## Fedora
@@ -21,7 +20,6 @@ Packages=kernel
          systemd
          systemd-boot
          systemd-udev
-         dracut
          util-linux
 ```
 
@@ -33,7 +31,6 @@ Packages=kernel
          systemd
          systemd-boot
          systemd-udev
-         dracut
 ```
 
 ## Debian
@@ -45,7 +42,6 @@ Packages=linux-image-generic
          systemd-boot
          systemd-sysv
          udev
-         dracut
          dbus
 ```
 
@@ -58,7 +54,6 @@ Packages=linux-image-generic
          systemd
          systemd-sysv
          udev
-         dracut
          dbus
 ```
 
@@ -67,7 +62,6 @@ Packages=linux-image-generic
 ```
 [Content]
 Packages=kernel-default
-         dracut
          systemd
          udev
 ```
index 356b94649b64b38f3337fec229f00bcd4ff5be64..426340f77bb153e890137ad2f9902088e4102d72 100644 (file)
--- a/mkosi.md
+++ b/mkosi.md
@@ -1380,7 +1380,7 @@ required components and let `mkosi` call `qemu` with all the right options:
 ```console
 $ mkosi -d fedora \
     --autologin \
-    -p systemd-udev,systemd-boot,dracut,kernel-core \
+    -p systemd-udev,systemd-boot,kernel-core \
     build
 $ mkosi -d fedora qemu
 ...
index ff923da467fdb2df4d7df3b44269b99ddd6ebdb6..bf82a5abaf96d270b0a1c775aa3daeb27a4bcca4 100644 (file)
@@ -23,9 +23,15 @@ from pathlib import Path
 from textwrap import dedent
 from typing import Callable, ContextManager, Optional, TextIO, TypeVar, Union, cast
 
-from mkosi.config import GenericVersion, MkosiArgs, MkosiConfig, machine_name
+from mkosi.config import (
+    GenericVersion,
+    MkosiArgs,
+    MkosiConfig,
+    MkosiConfigParser,
+    machine_name,
+)
 from mkosi.install import add_dropin_config_from_resource, copy_path, flock
-from mkosi.log import ARG_DEBUG, Style, color_error, complete_step, die, log_step
+from mkosi.log import Style, color_error, complete_step, die, log_step
 from mkosi.manifest import Manifest
 from mkosi.mounts import dissect_and_mount, mount_overlay, scandir_recursive
 from mkosi.pager import page
@@ -701,6 +707,47 @@ def install_unified_kernel(state: MkosiState, roothash: Optional[str]) -> None:
     if state.config.output_format == OutputFormat.cpio and state.config.bootable is None:
         return
 
+    initrds = []
+
+    if state.config.initrds:
+        initrds = state.config.initrds
+    elif any(gen_kernel_images(state)):
+        # Default values are assigned via the parser so we go via the argument parser to construct
+        # the config for the initrd.
+        with complete_step("Building initrd"):
+            args, config = MkosiConfigParser().parse([
+                "--directory", "",
+                "--distribution", str(state.config.distribution),
+                "--release", state.config.release,
+                "--architecture", state.config.architecture,
+                *(["--mirror", state.config.mirror] if state.config.mirror else []),
+                "--repository-key-check", yes_no(state.config.repository_key_check),
+                "--repositories", ",".join(state.config.repositories),
+                "--repo-dir", ",".join(str(p) for p in state.config.repo_dirs),
+                "--compress-output", str(state.config.compress_output),
+                "--with-network", yes_no(state.config.with_network),
+                "--cache-only", yes_no(state.config.cache_only),
+                *(["--output-dir", str(state.config.output_dir)] if state.config.output_dir else []),
+                *(["--workspace-dir", str(state.config.workspace_dir)] if state.config.workspace_dir else []),
+                "--cache-dir", str(state.cache.parent) if state.config.cache_dir else str(state.cache),
+                "--incremental", yes_no(state.config.incremental),
+                "--acl", yes_no(state.config.acl),
+                "--format", "cpio",
+                "--package", "systemd",
+                "--package", "udev",
+                "--package", "kmod",
+                "--output", "initrd",
+                "--make-initrd", "yes",
+                "--bootable", "no",
+                "--manifest-format", "",
+                "-f",
+                "build",
+            ])
+
+            build_stuff(state.uid, state.gid, args, config)
+
+            initrds = [config.output_compressed]
+
     for kver, kimg in gen_kernel_images(state):
         with complete_step(f"Generating unified kernel image for {kimg}"):
             image_id = state.config.image_id or f"mkosi-{state.config.distribution}"
@@ -767,16 +814,7 @@ def install_unified_kernel(state: MkosiState, roothash: Optional[str]) -> None:
                         "--pcr-banks", "sha1,sha256",
                     ]
 
-            if state.config.initrds:
-                initrds = state.config.initrds + [gen_kernel_modules_initrd(state, kver)]
-            else:
-                initrd = state.root / state.installer.initrd_path(kver)
-                if not initrd.exists():
-                    die(f"Initrd not found at {initrd}")
-
-                initrds = [initrd]
-
-            cmd += [state.root / kimg] + initrds
+            cmd += [state.root / kimg] + initrds + [gen_kernel_modules_initrd(state, kver)]
 
             run(cmd)
 
@@ -1289,49 +1327,13 @@ def configure_initrd(state: MkosiState) -> None:
         state.root.joinpath("etc/initrd-release").symlink_to("/etc/os-release")
 
 
-def run_kernel_install(state: MkosiState) -> None:
-    if state.config.initrds:
-        return
-
+def run_depmod(state: MkosiState) -> None:
     if state.config.bootable is False:
         return
 
-    if state.config.bootable is None and state.config.output_format == OutputFormat.cpio:
-        return
-
-    # CentOS Stream 8 has an old version of kernel-install that unconditionally writes initrds to
-    # /boot/<machine-id>/<kver>, so let's detect that and move them to the correct location.
-
-    if (p := state.root / "etc/machine-id").exists():
-        machine_id = p.read_text().strip()
-    else:
-        machine_id = None
-
-    # kernel-install on Debian/Ubuntu does not rebuild the dracut initrd, so we do it manually here.
-    if (state.root.joinpath("usr/bin/dracut").exists() and
-        state.config.distribution in (Distribution.ubuntu, Distribution.debian) and
-        not state.root.joinpath("usr/lib/kernel/install.d/50-dracut.install").exists() and
-        not state.root.joinpath("etc/kernel/install.d/50-dracut.install").exists()):
-        with complete_step("Running dpkg-reconfigure dracut…"):
-            run_workspace_command(state.root, ["dpkg-reconfigure", "dracut"],
-                                  env=dict(hostonly_l="no") | state.environment)
-            return
-
-    with complete_step("Running kernel-install…"):
-        for kver, kimg in gen_kernel_images(state):
-            cmd: list[PathString] = ["kernel-install", "add", kver, Path("/") / kimg]
-
-            if ARG_DEBUG.get():
-                cmd.insert(1, "--verbose")
-
-            # Make dracut think --no-host-only was passed via the CLI.
-            run_workspace_command(state.root, cmd, env=dict(hostonly_l="no") | state.environment)
-
-            if machine_id and (p := state.root / "boot" / machine_id / kver / "initrd").exists():
-                shutil.move(p, state.root / state.installer.initrd_path(kver))
-
-    if machine_id and (p := state.root / "boot" / machine_id).exists():
-        shutil.rmtree(p)
+    for kver, _ in gen_kernel_images(state):
+        with complete_step(f"Running depmod for {kver}"):
+            run(["depmod", "--basedir", state.root, kver])
 
 
 def run_sysusers(state: MkosiState) -> None:
@@ -1497,12 +1499,12 @@ def build_image(state: MkosiState, *, for_cache: bool, manifest: Optional[Manife
         run_build_script(state)
         install_build_dest(state)
         install_extra_trees(state)
-        run_kernel_install(state)
         install_boot_loader(state)
         configure_ssh(state)
         run_postinst_script(state)
         run_sysusers(state)
         run_preset_all(state)
+        run_depmod(state)
         remove_packages(state)
 
         if manifest:
index 939c365a72283442be9e176b4d6aa023f2d77073..261705982ee4787262c536e6875020294720c921 100644 (file)
@@ -1885,8 +1885,10 @@ def load_kernel_command_line_extra(args: argparse.Namespace) -> list[str]:
 
 
 def load_args(args: argparse.Namespace) -> MkosiArgs:
-    ARG_DEBUG.set(args.debug)
-    ARG_DEBUG_SHELL.set(args.debug_shell)
+    if args.debug:
+        ARG_DEBUG.set(args.debug)
+    if args.debug_shell:
+        ARG_DEBUG_SHELL.set(args.debug_shell)
 
     return MkosiArgs.from_namespace(args)
 
index e690a0f9a20ce510a87e67798f3089240ee9c218..bdc1b656816e5537e5625fc6288d75bcee221097 100644 (file)
@@ -17,10 +17,6 @@ class DistributionInstaller:
     def kernel_image(kver: str, architecture: str) -> Path:
         return Path("usr/lib/modules") / kver / "vmlinuz"
 
-    @staticmethod
-    def initrd_path(kver: str) -> Path:
-        return Path("boot") / f"initramfs-{kver}.img"
-
     @classmethod
     def install_packages(cls, state: "MkosiState", packages: Sequence[str]) -> None:
         raise NotImplementedError
index f33924714ecf66f5744e1252137d54d8d4b76d2f..3d407f70fdb6d160b8b58370d3a993587cdecca7 100644 (file)
@@ -21,10 +21,6 @@ class DebianInstaller(DistributionInstaller):
     def kernel_image(name: str, architecture: str) -> Path:
         return Path(f"boot/vmlinuz-{name}")
 
-    @staticmethod
-    def initrd_path(kver: str) -> Path:
-        return Path("boot") / f"initrd.img-{kver}"
-
     @staticmethod
     def repositories(state: MkosiState, local: bool = True) -> list[str]:
         repos = ' '.join(("main", *state.config.repositories))
index 38cb6f82490d18b0f4c66e62b5bad24649e95324..1e973ddc77f9a8b28407c195d5dca6c53b4467da 100644 (file)
@@ -1,7 +1,6 @@
 # SPDX-License-Identifier: LGPL-2.1+
 
 from collections.abc import Sequence
-from pathlib import Path
 from textwrap import dedent
 
 from mkosi.distributions import DistributionInstaller
@@ -52,10 +51,6 @@ class OpensuseInstaller(DistributionInstaller):
     def remove_packages(cls, state: MkosiState, packages: Sequence[str]) -> None:
         invoke_zypper(state, "remove", ["-y", "--clean-deps"], packages)
 
-    @staticmethod
-    def initrd_path(kver: str) -> Path:
-        return Path("boot") / f"initrd-{kver}"
-
 
 def setup_zypper(state: MkosiState, repos: Sequence[tuple[str, str]] = ()) -> None:
     with state.workspace.joinpath("zypp.conf").open("w") as f: