]> git.ipfire.org Git - thirdparty/mkosi.git/commit
Rework module-list settings to use globs instead of regexps
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 17 Feb 2025 17:29:06 +0000 (18:29 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 14 Mar 2025 09:56:32 +0000 (10:56 +0100)
commiteecf8b3b5c43e4832a11c8718ae43e559a755829
tree2eb1f2a1f71b0b1b6f1402649d474801e6d31139
parent684bb3c999012cd04c66a46a3aede5fcf746fb14
Rework module-list settings to use globs instead of regexps

"If you have a problem, and use a regexp, now you have two problems."
I don't think this quip applies in all cases, but the existing interface with
regexps was problematic for a few reasons:

- users usually want to match at a word boundary, but regexps apply anywhere in
  the string, so to actually match correctly, the regexp has to be carefully
  constructed with word boundary assertions.
  Even our own config for initrds included some modules by mistake,
  e.g. drivers/net/ethernet/chelsio/inline_crypto/ch_ipsec/ch_ipsec.ko.xz
  was matched by crypto/.

- once the regexps include the word boundary assertions, they became quite
  complex and hard to read.

- because of the separate evaluation of include and exlude patterns, we
  need to have exclude patterns in the include patterns. The example given
  in the review:
    ^drivers/net/(?!wireless/)
  But this means that we can do exclusions in two places, making the whole
  scheme very complex.

- the default to include all modules by default goes against the general
  design of mkosi, where only things that are explicitly configured are
  put in the image. That default is only useful when trying to build a
  "maximal" image that matches the current machine. In most uses, the
  default of including only requested modules makes more sense (initrd,
  addons, any not-host-only images).

A new setting that takes glob patterns is added. There is only one setting
instead of a pair, exclusion patterns are prefixed by '-'. The last matching
glob wins. The details of how those globs are interpreted is crafted to
match our particular use cases.

For a single glob, the rules are:
- 'foo' matches the basename, /some/path/foo.ko
- 'bar/foo' matches the last component of the path, bar/foo.ko
- '/full/path/bar' only matches /full/path/bar.ko.
- crypto/* or crypto/ match all modules any crypto/ directory.
- /crypto/* matches the modules in the top-level directory.

This might seem complicated at first glance, but apart from the special
handling of the suffix, those rules mostly match how 'ls' would handle
a local path argument.

Suffixes are not specified in the globs. '-' and '_' are treated as equivalent,
except when part of special glob syntax with […].

New settings are added:
KernelModules=GLOB
KernelInitrdModules=GLOB

(KernelModulesInitrd=BOOL already exists and specifies whether to create a
separate initrd.)

I think the new syntax is more pleasant to read and write. Backward
compatibility is maintained by keeping the old options in place. The change to
exclude all modules by default is a breaking change, but in most uses both
options were used in combination anyway, so I think this should be fine.

The example given in the review:
  KernelModulesInclude=
    ^drivers/net/(?!wireless/)
becomes
  KernelModules=
    /drivers/net/
    -/drivers/net/wireless/
mkosi/config.py
mkosi/kmod.py
tests/test_json.py
tests/test_kmod.py [new file with mode: 0644]