From: Daan De Meyer Date: Sun, 7 May 2023 16:18:13 +0000 (+0200) Subject: Add KernelModulesInclude= and KernelModulesExclude= X-Git-Tag: v15~177^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c8752c2fc09de80b008d2f9f76f9ded7a564aab3;p=thirdparty%2Fmkosi.git Add KernelModulesInclude= and KernelModulesExclude= 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. --- diff --git a/mkosi.md b/mkosi.md index dbb6ab692..64cfccedb 100644 --- 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//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//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=`, diff --git a/mkosi/__init__.py b/mkosi/__init__.py index feead5d40..e2bd3f92f 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -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]) diff --git a/mkosi/config.py b/mkosi/config.py index a62a794a0..54362abd1 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -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",