From: Daan De Meyer Date: Sun, 7 May 2023 17:42:53 +0000 (+0200) Subject: Make KernelModulesInclude take priority over KernelModulesExclude= X-Git-Tag: v15~177^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=33841a2b73352db04b9532efa0f356a77b48dccf;p=thirdparty%2Fmkosi.git Make KernelModulesInclude take priority over KernelModulesExclude= If exclude takes priority over include, it becomes hard to exclude directories of drivers except for a few. If include takes priority over exclude, we can do stuff like exclude "drivers/gpu/*" and include "drivers/gpu/nvidia" to only include nvidia drivers. The current behavior easily be reproduced by adding exclude "*" followed by include patterns. --- diff --git a/mkosi.md b/mkosi.md index 64cfccedb..899f90f02 100644 --- a/mkosi.md +++ b/mkosi.md @@ -836,15 +836,16 @@ a boolean argument: either "1", "yes", or "true" to enable, or "0", : 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. + 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 + sense when used in combination with it because all kernel modules are included in the image by default. `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=`. + the image. `KernelModulesInitrd=`, `--kernel-modules-initrd=` diff --git a/mkosi/__init__.py b/mkosi/__init__.py index e2bd3f92f..d1a32c284 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -595,29 +595,26 @@ def filter_kernel_modules(root: Path, kver: str, include: Sequence[str], exclude modulesd = Path("usr/lib/modules") / kver modules = set(m.relative_to(root) for m in (root / modulesd).glob("**/*.ko*")) + keep = set() + for pattern in include: + regex = re.compile(pattern) + for m in modules: + rel = os.fspath(m.relative_to(modulesd / "kernel")) + if regex.search(rel): + logging.debug(f"Including module {rel}") + keep.add(m) + for pattern in exclude: regex = re.compile(pattern) remove = set() for m in modules: rel = os.fspath(m.relative_to(modulesd / "kernel")) - if regex.search(rel): + if rel not in keep and regex.search(rel): logging.debug(f"Excluding module {rel}") remove.add(m) modules -= remove - if include: - keep: set[Path] = set() - for pattern in include: - regex = re.compile(pattern) - for m in modules: - rel = os.fspath(m.relative_to(modulesd / "kernel")) - if regex.search(rel): - logging.debug(f"Including module {rel}") - keep.add(m) - - modules = keep - return sorted(modules)