From: Daan De Meyer Date: Thu, 6 Jun 2024 09:43:25 +0000 (+0200) Subject: Introduce "default" and "host" for kernel modules include settings X-Git-Tag: v23.1~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d5f8481f4b6c32695b9d91c3f139b6745f101271;p=thirdparty%2Fmkosi.git Introduce "default" and "host" for kernel modules include settings --- diff --git a/NEWS.md b/NEWS.md index 64cf8fc4a..b3e2f3110 100644 --- a/NEWS.md +++ b/NEWS.md @@ -107,6 +107,12 @@ - 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 diff --git a/kernel-install/50-mkosi.install b/kernel-install/50-mkosi.install index 96686bf97..8dda03749 100644 --- a/kernel-install/50-mkosi.install +++ b/kernel-install/50-mkosi.install @@ -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", ] diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 654ecabd2..fbaab7405 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -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, ) diff --git a/mkosi/kmod.py b/mkosi/kmod.py index fa623367e..9f80ba0f3 100644 --- a/mkosi/kmod.py +++ b/mkosi/kmod.py @@ -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: diff --git a/mkosi/resources/mkosi.md b/mkosi/resources/mkosi.md index e85b1df43..e6418e4a0 100644 --- a/mkosi/resources/mkosi.md +++ b/mkosi/resources/mkosi.md @@ -1047,7 +1047,15 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`, relative to the `/usr/lib/modules//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=`,