]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Introduce "default" and "host" for kernel modules include settings
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 6 Jun 2024 09:43:25 +0000 (11:43 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 6 Jun 2024 10:41:32 +0000 (12:41 +0200)
NEWS.md
kernel-install/50-mkosi.install
mkosi/__init__.py
mkosi/kmod.py
mkosi/resources/mkosi.md

diff --git a/NEWS.md b/NEWS.md
index 64cf8fc4a42a1c3cb42d286d865083c466db92ce..b3e2f311011157ce67857e7bdf171be689950556 100644 (file)
--- a/NEWS.md
+++ b/NEWS.md
 - The `versionlock` plugin is now enabled by default for dnf with a noop
   configuration.
 - `Repositories=` is now implemented for zypper.
+- `KernelModulesInclude=` and `KernelModulesInitrdInclude=` now take the
+  special values `host` and `default` to include the host's loaded
+  modules and the default kernel modules defined in `mkosi-initrd`
+  respectively.
+- `KernelModulesIncludeHost=` and `KernelModulesInitrdIncludeHost=` are
+  now deprecated.
 
 ## v22
 
index 96686bf975d92e07802cad62d0a29e34baaa3f71..8dda037491056bd7d9c06a887bd189a4a55b1ebd 100644 (file)
@@ -140,7 +140,7 @@ def main() -> None:
         "--extra-tree", f"/usr/lib/modules/{context.kernel_version}:/usr/lib/modules/{context.kernel_version}",
         "--extra-tree=/usr/lib/firmware:/usr/lib/firmware",
         "--kernel-modules-exclude=.*",
-        "--kernel-modules-include-host=yes",
+        "--kernel-modules-include=host",
         "--include=mkosi-initrd",
     ]
 
index 654ecabd23a12d7b4e62d15d91823ff2855329b6..fbaab7405bda1db32658e27e618ed5ab2c76bbb6 100644 (file)
@@ -56,7 +56,7 @@ from mkosi.config import (
 from mkosi.context import Context
 from mkosi.distributions import Distribution
 from mkosi.installer import clean_package_manager_metadata
-from mkosi.kmod import gen_required_kernel_modules, process_kernel_modules
+from mkosi.kmod import gen_required_kernel_modules, loaded_modules, process_kernel_modules
 from mkosi.log import ARG_DEBUG, complete_step, die, log_notice, log_step
 from mkosi.manifest import Manifest
 from mkosi.mounts import finalize_crypto_mounts, finalize_source_mounts, mount_overlay
@@ -1897,6 +1897,17 @@ def build_microcode_initrd(context: Context) -> list[Path]:
     return [microcode]
 
 
+def finalize_kernel_modules_include(context: Context, *, include: Sequence[str], host: bool) -> set[str]:
+    final = {i for i in include if i not in ("default", "host")}
+    if "default" in include:
+        initrd = finalize_default_initrd(context.args, context.config, resources=context.resources)
+        final.update(initrd.kernel_modules_include)
+    if host or "host" in include:
+        final.update(loaded_modules())
+
+    return final
+
+
 def build_kernel_modules_initrd(context: Context, kver: str) -> Path:
     kmods = context.workspace / f"kernel-modules-{kver}.initrd"
     if kmods.exists():
@@ -1906,9 +1917,12 @@ def build_kernel_modules_initrd(context: Context, kver: str) -> Path:
         context.root, kmods,
         files=gen_required_kernel_modules(
             context.root, kver,
-            include=context.config.kernel_modules_initrd_include,
+            include=finalize_kernel_modules_include(
+                context,
+                include=context.config.kernel_modules_initrd_include,
+                host=context.config.kernel_modules_initrd_include_host,
+            ),
             exclude=context.config.kernel_modules_initrd_exclude,
-            host=context.config.kernel_modules_initrd_include_host,
             sandbox=context.sandbox,
         ),
         sandbox=context.sandbox,
@@ -2934,9 +2948,12 @@ def run_depmod(context: Context, *, cache: bool = False) -> None:
         if not cache:
             process_kernel_modules(
                 context.root, kver,
-                include=context.config.kernel_modules_include,
+                include=finalize_kernel_modules_include(
+                    context,
+                    include=context.config.kernel_modules_include,
+                    host=context.config.kernel_modules_include_host,
+                ),
                 exclude=context.config.kernel_modules_exclude,
-                host=context.config.kernel_modules_include_host,
                 sandbox=context.sandbox,
             )
 
index fa623367ebe22a90b5e2f46c597b88337c8b96a5..9f80ba0f34f53557571e808b3d1bc92428ff83c6 100644 (file)
@@ -5,7 +5,7 @@ import logging
 import os
 import re
 import subprocess
-from collections.abc import Iterator, Sequence
+from collections.abc import Iterable, Iterator
 from pathlib import Path
 
 from mkosi.log import complete_step, log_step
@@ -18,20 +18,10 @@ def loaded_modules() -> list[str]:
     return [fr"{line.split()[0]}\.ko" for line in Path("/proc/modules").read_text().splitlines()]
 
 
-def filter_kernel_modules(
-    root: Path,
-    kver: str,
-    *,
-    include: Sequence[str],
-    exclude: Sequence[str],
-    host: bool,
-) -> list[Path]:
+def filter_kernel_modules(root: Path, kver: str, *, include: Iterable[str], exclude: Iterable[str]) -> list[Path]:
     modulesd = root / "usr/lib/modules" / kver
     modules = set(modulesd.rglob("*.ko*"))
 
-    if host:
-        include = [*include, *loaded_modules()]
-
     keep = set()
     if include:
         regex = re.compile("|".join(include))
@@ -66,7 +56,7 @@ def module_path_to_name(path: Path) -> str:
 def resolve_module_dependencies(
     root: Path,
     kver: str,
-    modules: Sequence[str],
+    modules: Iterable[str],
     *,
     sandbox: SandboxProtocol = nosandbox,
 ) -> tuple[set[Path], set[Path]]:
@@ -158,9 +148,8 @@ def gen_required_kernel_modules(
     root: Path,
     kver: str,
     *,
-    include: Sequence[str],
-    exclude: Sequence[str],
-    host: bool,
+    include: Iterable[str],
+    exclude: Iterable[str],
     sandbox: SandboxProtocol = nosandbox,
 ) -> Iterator[Path]:
     modulesd = root / "usr/lib/modules" / kver
@@ -169,7 +158,7 @@ def gen_required_kernel_modules(
     # we have to take the slow path to make sure we don't copy firmware into the initrd that is not depended on by any
     # kernel modules.
     if exclude or (root / "usr/lib/firmware").glob("*"):
-        modules = filter_kernel_modules(root, kver, include=include, exclude=exclude, host=host)
+        modules = filter_kernel_modules(root, kver, include=include, exclude=exclude)
         names = [module_path_to_name(m) for m in modules]
         mods, firmware = resolve_module_dependencies(root, kver, names, sandbox=sandbox)
     else:
@@ -198,18 +187,15 @@ def process_kernel_modules(
     root: Path,
     kver: str,
     *,
-    include: Sequence[str],
-    exclude: Sequence[str],
-    host: bool,
+    include: Iterable[str],
+    exclude: Iterable[str],
     sandbox: SandboxProtocol = nosandbox,
 ) -> None:
     if not exclude:
         return
 
     with complete_step("Applying kernel module filters"):
-        required = set(
-            gen_required_kernel_modules(root, kver, include=include, exclude=exclude, host=host, sandbox=sandbox)
-        )
+        required = set(gen_required_kernel_modules(root, kver, include=include, exclude=exclude, sandbox=sandbox))
 
         for m in sorted((root / "usr/lib/modules" / kver).rglob("*.ko*"), reverse=True):
             if m in required:
index e85b1df4352b806b05275bb88c1bec5ce07a6549..e6418e4a00e1ddfabeadc1279c72604323c6cad4 100644 (file)
@@ -1047,7 +1047,15 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
     relative to the `/usr/lib/modules/<kver>/kernel` directory. mkosi checks for a match anywhere in the module
     path (e.g. `i915` will match against `drivers/gpu/drm/i915.ko`). All modules that match any of the
     specified patterns are included in the image. All module and firmware dependencies of the matched modules
-    are included in the image as well. This setting takes priority over `KernelModulesExclude=` and only makes
+    are included in the image as well.
+
+    If the special value `default` is used, the default kernel modules
+    defined in the `mkosi-initrd` configuration are included as well.
+
+    If the special value `host` is used, the currently loaded modules on
+    the host system are included as well.
+
+    This setting takes priority over `KernelModulesExclude=` and only makes
     sense when used in combination with it because all kernel modules are included in the image by default.
 
 `KernelModulesExclude=`, `--kernel-modules-exclude=`
@@ -1055,13 +1063,6 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
     `KernelModulesInclude=` except that all modules that match any of the specified patterns are excluded from
     the image.
 
-`KernelModulesIncludeHost=`, `--kernel-modules-include-host=`
-:   Takes a boolean. Specifies whether to include the currently loaded
-    modules on the host system in the image. This setting takes priority
-    over `KernelModulesExclude=` and only makes sense when used in
-    combination with it because all kernel modules are included in the
-    image by default.
-
 `KernelModulesInitrd=`, `--kernel-modules-initrd=`
 :   Enable/Disable generation of the kernel modules initrd when building a bootable image. Enabled by default.
     If enabled, when building a bootable image, for each kernel that we assemble a unified kernel image for we
@@ -1075,9 +1076,6 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
 `KernelModulesInitrdExclude=`, `--kernel-modules-initrd-exclude=`
 :   Like `KernelModulesExclude=`, but applies to the kernel modules included in the kernel modules initrd.
 
-`KernelModulesInitrdIncludeHost=`, `--kernel-modules-initrd-include-host=`
-:   Like `KernelModulesIncludeHost=`, but applies to the kernel modules included in the kernel modules initrd.
-
 `Locale=`, `--locale=`, `LocaleMessages=`, `--locale-messages=`, `Keymap=`, `--keymap=`, `Timezone=`, `--timezone=`, `Hostname=`, `--hostname=`, `RootShell=`, `--root-shell=`
 :   The settings `Locale=`, `--locale=`, `LocaleMessages=`, `--locale-messages=`,
     `Keymap=`, `--keymap=`, `Timezone=`, `--timezone=`, `Hostname=`,