: 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=`,
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])
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]
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",
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",