]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add KernelModulesInclude= and KernelModulesExclude=
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 7 May 2023 16:18:13 +0000 (18:18 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 8 May 2023 12:55:41 +0000 (14:55 +0200)
These are like KernelModulesInitrdInclude= and
KernelModulesInitrdExclude= but they apply to the image itself
instead of to the kernel modules initrd.

The main use case is when using mkosi as an initramfs builder and
you want to exclude kernel modules from the initramfs but it can
also be useful when distros only ship large kernel packages that
you want to trim down.

mkosi.md
mkosi/__init__.py
mkosi/config.py

index dbb6ab6921592448ba9ec1ec223641937261f5c7..64cfccedb3c6e57010655784345ce5a523146582 100644 (file)
--- a/mkosi.md
+++ b/mkosi.md
@@ -832,28 +832,35 @@ a boolean argument: either "1", "yes", or "true" to enable, or "0",
 : Add `/etc/initrd-release` and `/init` to the image so that it can be
   used as an initramfs.
 
+`KernelModulesInclude=`, `--kernel-modules-include=`
+
+: Takes a list of regex patterns that specify kernel modules to include in the image. Patterns should be
+  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. If not specified, all kernel modules are included in the final image.
+
+`KernelModulesExclude=`, `--kernel-modules-exclude=`
+
+: Takes a list of regex patterns that specify modules to exclude from the image. Behaves the same as
+  `KernelModulesInclude=` except that all modules that match any of the specified patterns are excluded from
+  the image. This setting takes priority over `KernelModulesInclude=`.
+
 `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
+  If enabled, when building a bootable image, for each kernel that we assemble a unified kernel image for we
   generate an extra initrd containing only the kernel modules for that kernel version and append it to the
-  prebuilt initrd. This allows generating kernel indepedent initrds which are augmented with the necessary
-  kernel modules when the UKI is assembled. By default all kernel modules and their required firmware files
-  are included.
+  prebuilt initrd. This allows generating kernel independent initrds which are augmented with the necessary
+  kernel modules when the UKI is assembled.
 
 `KernelModulesInitrdInclude=`, `--kernel-modules-initrd-include=`
 
-: Takes a list of regex patterns that specify modules to include in the kernel modules initrd. Patterns
-  should be 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 kernel modules initrd. All module and firmware dependencies of
-  the matched modules are included in the kernel modules initrd as well.
+: Like `KernelModulesInclude=`, but applies to the kernel modules included in the kernel modules initrd.
 
 `KernelModulesInitrdExclude=`, `--kernel-modules-initrd-exclude=`
 
-: Takes a list of regex patterns that specify modules to exclude from the kernel modules initrd. Behaves the
-  same as `KernelModulesInitrdInclude=` except that all modules that match any of the specified patterns are
-  excluded from the kernel modules initrd. This setting takes priority over `KernelModulesInitrdInclude=`.
+: Like `KernelModulesExclude=`, but applies to the kernel modules included in the kernel modules initrd.
 
 `Locale=`, `--locale=`,
 `LocaleMessages=`, `--locale-messages=`,
index feead5d4097ba29509b9b3a20381b99185eca0cd..e2bd3f92f555d65c873e24e4c7bd36a80c9db8a2 100644 (file)
@@ -1389,11 +1389,44 @@ def configure_initrd(state: MkosiState) -> None:
         state.root.joinpath("etc/initrd-release").symlink_to("/etc/os-release")
 
 
+def process_kernel_modules(state: MkosiState, kver: str) -> None:
+    if not state.config.kernel_modules_include and not state.config.kernel_modules_exclude:
+        return
+
+    with complete_step("Applying kernel module filters"):
+        modulesd = Path("usr/lib/modules") / kver
+        modules = filter_kernel_modules(state.root, kver,
+                                        state.config.kernel_modules_include,
+                                        state.config.kernel_modules_exclude)
+
+        names = [module_path_to_name(m) for m in modules]
+        mods, firmware = resolve_module_dependencies(state.root, kver, names)
+
+        allmodules = set(m.relative_to(state.root) for m in (state.root / modulesd).glob("**/*.ko*"))
+        allfirmware = set(m.relative_to(state.root) for m in (state.root / "usr/lib/firmware").glob("**/*") if not m.is_dir())
+
+        for m in allmodules:
+            if m in mods:
+                continue
+
+            logging.debug(f"Removing module {m}")
+            (state.root / m).unlink()
+
+        for fw in allfirmware:
+            if fw in firmware:
+                continue
+
+            logging.debug(f"Removing firmware {fw}")
+            (state.root / fw).unlink()
+
+
 def run_depmod(state: MkosiState) -> None:
     if state.config.bootable == ConfigFeature.disabled:
         return
 
     for kver, _ in gen_kernel_images(state):
+        process_kernel_modules(state, kver)
+
         with complete_step(f"Running depmod for {kver}"):
             run(["depmod", "--all", "--basedir", state.root, kver])
 
index a62a794a097a8231a991540dc72343d89fe50c41..54362abd18d8a4acd19bafd1c1e9090eeba3c0c1 100644 (file)
@@ -618,6 +618,8 @@ class MkosiConfig:
     workspace_dir: Optional[Path]
     initrds: list[Path]
     make_initrd: bool
+    kernel_modules_include: list[str]
+    kernel_modules_exclude: list[str]
     kernel_modules_initrd: bool
     kernel_modules_initrd_include: list[str]
     kernel_modules_initrd_exclude: list[str]
@@ -985,6 +987,16 @@ class MkosiConfigParser:
             section="Content",
             parse=config_parse_boolean,
         ),
+        MkosiConfigSetting(
+            dest="kernel_modules_include",
+            section="Content",
+            parse=config_make_list_parser(delimiter=","),
+        ),
+        MkosiConfigSetting(
+            dest="kernel_modules_exclude",
+            section="Content",
+            parse=config_make_list_parser(delimiter=","),
+        ),
         MkosiConfigSetting(
             dest="kernel_modules_initrd",
             section="Content",
@@ -1660,6 +1672,18 @@ class MkosiConfigParser:
             nargs="?",
             action=action,
         )
+        group.add_argument(
+            "--kernel-modules-include",
+            help="Only include the specified kernel modules in the image",
+            metavar="REGEX",
+            action=action,
+        )
+        group.add_argument(
+            "--kernel-modules-exclude",
+            help="Exclude the specified kernel modules from the image",
+            metavar="REGEX",
+            action=action,
+        )
         group.add_argument(
             "--kernel-modules-initrd",
             help="When building a bootable image, add an extra initrd containing the kernel modules",