]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Make KernelModulesInclude take priority over KernelModulesExclude=
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 7 May 2023 17:42:53 +0000 (19:42 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 8 May 2023 12:55:41 +0000 (14:55 +0200)
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.

mkosi.md
mkosi/__init__.py

index 64cfccedb3c6e57010655784345ce5a523146582..899f90f02e3a548e03875ad08e5b2b3e5170a160 100644 (file)
--- 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/<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.
+  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=`
 
index e2bd3f92f555d65c873e24e4c7bd36a80c9db8a2..d1a32c2843d7fc6f28ad8e7ccc2c9457430b4034 100644 (file)
@@ -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)