Skip to content

libkmod: tools: Introduce 'module alternative' directory (was: module fallback) #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: master
Choose a base branch
from

Conversation

esposem
Copy link

@esposem esposem commented Nov 28, 2024

This PR is the improvement of #202 opened by @vittyvk .

Below you find a description of the original PR. The main change here is that if /lib/modules exists, nothing changes in kmod functionality. If, however, for some reasons it misses, /run/modules is used as search path.

The change itself is pretty easy: call twice the same logic for each tool with a different, customizable path (MODULE_DIRECTORY and MODULE_ALTERNATIVE_DIRECTORY).

I did not implement any test yet.

Original PR description:

Currently, traditional Linux systems using initramfs are forced to keep two copies of the modules which may be needed 
both during early boot and for normal system operation. This seems to be inevitable for the case when initramfs is built 
locally but with the rise of distro shipped UKIs the question whether it would be possible to avoid the redundancy arises.

To make it possible to reuse modules from initramfs without copying them to the root filesystem (which may be 
read-only), the suggested approach is to preserve them on tmpfs, e.g. in /run/modules/uname -r but the challenge is to 
make kmod search there.

[...] this part does not apply anymore [...]

Note, no merging of /run/modules/uname -r and /lib/modules/uname -r is performed. It is expected, that in case 
/lib/modules/uname -r exists, it is a superset and modules from initramfs are no longer needed.

To make a real world example of the usage, Fedora/CentOS/RHEL distros now ship 'kernel-uki-virt' package with a UKI 
which is self sufficient in many cases. The package, however, has to have a dependency on 'kernel-modules-core' so 
basically the same set of modules is available post boot. This is needed when e.g. a filesystem module which was not used 
for root filesystem need to be loaded to mount some other volume (and ESP is the most common example). The desire is 
to be able to break this dependency for 'minimal' cloud/VM images but still support the case when people want to install 
''kernel-modules-core'/'kernel-modules'/'kernel-modules-extra' post boot and enjoy additional modules.

@evelikov
Copy link
Collaborator

evelikov commented Dec 5, 2024

Can we get a manpage(s) update, on how things are supposed to work? Ideally it will include (either the doc or commit message) some details why the alternative ordering or behaviour isn't warranted.

Once that is solid and we're comfortable things won't backfire/respective workflows are supported/etc, we can beat the code and tests in shape.

@esposem
Copy link
Author

esposem commented Dec 6, 2024

Sure, I thought the commit messages and PR description were self explanatory.
The idea is the following:

  • If whatever component logic (say kmod_new) for the path MODULE_DIRECTORY (/lib/module) works correctly (no error returned), nothing else is done. The behavior is exactly as it was before this PR.
  • If the component logic fails for the path MODULE_DIRECTORY, try MODULE_ALTERNATIVE_DIRECTORY (/run/modules) and see if it works.
    This is done to cover the case where /lib/modules is missing but /run/modules is populated, and fall back into /run/modules ONLY IF /lib/modules is missing. The original /lib/modules has priority over the fallback directory.
    Does it clear some doubts? Happy to explain if something is not clear

@esposem
Copy link
Author

esposem commented Dec 19, 2024

Hi @evelikov I am re-reading your notification and I am not sure what you mean with some details why the alternative ordering or behaviour isn't warranted. Can you explain what you mean?

@esposem
Copy link
Author

esposem commented Jan 20, 2025

Hi @evelikov can you please answer my question? I will be happy to add manpage if you tell me what you want to see exactly

@esposem esposem force-pushed the initrd-modules2 branch 2 times, most recently from 8acb734 to 4c27973 Compare January 27, 2025 14:53
@esposem
Copy link
Author

esposem commented Jan 27, 2025

Added doc and the last two commits show a possible fix to the confusing error messages that come up when the first call to MODULES_DIRECTORY fails but the second to MODULES_ALTERNATIVE_DIRECTORY is successful.

@esposem
Copy link
Author

esposem commented Jan 30, 2025

Finished with first pass of fixes (hopefully waiting reviews).
I modinfo should be completed, meaning it has functionality, docs and test. I fixed (or tried to) all problems described in the PR. Before doing other tests for the other components, I would love to get any feedback on this.

This option allows the user to specify the alternative directory that
the kmod tools should look at when MODULE_DIRECTORY is empty.
Defaults to /run/modules.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
Add a boolean flag at setup time to let the user decide whether kmod should
consider the fallback MODULE_ALTERNATIVE_DIRECTORY or not.

If this flag is False, MODULE_ALTERNATIVE_DIRECTORY will be ignored.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
With the incoming MODULE_ALTERNATIVE_DIRECTORY double logic
where the same core function for each kmod tool will be called
twice, once on MODULE_DIRECTORY path and another time on
MODULE_ALTERNATIVE_DIRECTORY, we need to somehow catch the
error and print it only if both calls on these two directories
fail.

Otherwise we risk to make kmod very confusing for the user because
we might show an error first (MODULE_DIRECTORY missing), and then
the fallback (MODULE_ALTERNATIVE_DIRECTORY) successfully does its
job leaving command is successful.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
…issing

Currently if no module is found in MODULE_DIRECTORY, kmod will
just print "Module %s not found", but now that we are adding two
search paths, it is worth specifying which is the path that was
not found.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
Move code in do_static_nodes into two helpers: one to get the
dirname (MODULE_DIRECTORY/$kernel) and another to do the main
logic.

This is in preparation to perform the same two steps again for
the new MODULE_ALTERNATIVE_DIRECTORY option.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
If nothing is found in MODULE_DIRECTORY, perform the same exact steps
but with MODULE_ALTERNATIVE_DIRECTORY.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
nabled -- can be squashed

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
Move code in do_modinfo into two helpers: one to get the
dirname ($root/MODULE_DIRECTORY/$kernel) and another to do the main
logic.

The first helper, get_module_dirname(), is also always called to
figure the dirname path and to avoid providing NULL to the kmod_new()
API. This is because kmod_new() always defaults to MODULE_DIRECTORY when
the dirname path is empty, and won't work when MODULE_ALTERNATIVE_DIRECTORY
will be used.

This is in preparation to perform the same two steps again for
the new MODULE_ALTERNATIVE_DIRECTORY option.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
If nothing is found in MODULE_DIRECTORY, perform the same exact steps
but with MODULE_ALTERNATIVE_DIRECTORY.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
can be squashed

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
Using the macros developed in log.h, catch the error and
print it only if both MODULE_DIRECTORY and MODULE_ALTERNATIVE_DIRECTORY
call fail.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
Now that we have to repeat the same logic twice for MODULE_DIRECTORY
and MODULE_ALTERNATIVE_DIRECTORY, setting LOG priority to FATAL
causes modprobe to exit failure, without trying the second path.

Therefore increase the log priority to ERROR.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
Move code in do_modprobe into two helpers: one to get the
dirname ($root/MODULE_DIRECTORY/$kernel) and another to do the main
logic.

The first helper, get_module_dirname(), is also always called to
figure the dirname path and to avoid providing NULL to the kmod_new()
API. This is because kmod_new() always defaults to MODULE_DIRECTORY when
the dirname path is empty, and won't work when MODULE_ALTERNATIVE_DIRECTORY
will be used.

This is in preparation to perform the same two steps again for
the new MODULE_ALTERNATIVE_DIRECTORY option.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
If nothing is found in MODULE_DIRECTORY, perform the same exact steps
but with MODULE_ALTERNATIVE_DIRECTORY.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
…-- can be squashed

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
Using the macros developed in log.h, catch the error and
print it only if both MODULE_DIRECTORY and MODULE_ALTERNATIVE_DIRECTORY
call fail.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
Move code in do_insmod into two helpers: one to get the
dirname ($root/MODULE_DIRECTORY/$kernel) and another to do the main
logic.

The first helper, get_module_dirname(), is also always called to
figure the dirname path and to avoid providing NULL to the kmod_new()
API. This is because kmod_new() always defaults to MODULE_DIRECTORY when
the dirname path is empty, and won't work when MODULE_ALTERNATIVE_DIRECTORY
will be used.

This is in preparation to perform the same two steps again for
the new MODULE_ALTERNATIVE_DIRECTORY option.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
If nothing is found in MODULE_DIRECTORY, perform the same exact steps
but with MODULE_ALTERNATIVE_DIRECTORY.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
… can be squashed

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
Using the macros developed in log.h, catch the error and
print it only if both MODULE_DIRECTORY and MODULE_ALTERNATIVE_DIRECTORY
call fail.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
Move code in do_lsmod into two helpers: one to get the
dirname ($root/MODULE_DIRECTORY/$kernel) and another to do the main
logic.

The first helper, get_module_dirname(), is also always called to
figure the dirname path and to avoid providing NULL to the kmod_new()
API. This is because kmod_new() always defaults to MODULE_DIRECTORY when
the dirname path is empty, and won't work when MODULE_ALTERNATIVE_DIRECTORY
will be used.

This is in preparation to perform the same two steps again for
the new MODULE_ALTERNATIVE_DIRECTORY option.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
If nothing is found in MODULE_DIRECTORY, perform the same exact steps
but with MODULE_ALTERNATIVE_DIRECTORY.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
…can be squashed

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
Using the macros developed in log.h, catch the error and
print it only if both MODULE_DIRECTORY and MODULE_ALTERNATIVE_DIRECTORY
call fail.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
Move code in do_rmmod into two helpers: one to get the
dirname ($root/MODULE_DIRECTORY/$kernel) and another to do the main
logic.

The first helper, get_module_dirname(), is also always called to
figure the dirname path and to avoid providing NULL to the kmod_new()
API. This is because kmod_new() always defaults to MODULE_DIRECTORY when
the dirname path is empty, and won't work when MODULE_ALTERNATIVE_DIRECTORY
will be used.

This is in preparation to perform the same two steps again for
the new MODULE_ALTERNATIVE_DIRECTORY option.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
If nothing is found in MODULE_DIRECTORY, perform the same exact steps
but with MODULE_ALTERNATIVE_DIRECTORY.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
…can be squashed

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
Using the macros developed in log.h, catch the error and
print it only if both MODULE_DIRECTORY and MODULE_ALTERNATIVE_DIRECTORY
call fail.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
Esplicitly define MODULE_ALTERNATIVE_DIRECTORY in the docs
as fallback directory if MODULE_DIRECTORY is missing or
the module is not present there.

Signed-off-by: Emanuele Giuseppe Esposito <[email protected]>
@esposem
Copy link
Author

esposem commented Feb 3, 2025

Also added the optional build flag enable-alternative-dir to make this whole work completely optional at build time, and by default it's disabled.

I put the compile flag together with my solution for logs in separate commits, that can go squashed afterwards. I am waiting for reviews to see whether these options are useful or not :)

@esposem
Copy link
Author

esposem commented Feb 11, 2025

Hello maintainers, can you please give a look at this PR? I think it's in shape for a first pass :) @lucasdemarchi @evelikov

1 similar comment
@esposem
Copy link
Author

esposem commented Feb 17, 2025

Hello maintainers, can you please give a look at this PR? I think it's in shape for a first pass :) @lucasdemarchi @evelikov

@esposem
Copy link
Author

esposem commented Feb 25, 2025

Hello maintainers, can you please give a look at this PR? I think it's in shape for a first pass :) @lucasdemarchi @evelikov

@evelikov
Copy link
Collaborator

Hi @evelikov can you please answer my question? I will be happy to add manpage if you tell me what you want to see exactly

Sorry life got in the way, plus I've been sick as a dog the last month or so.

In my earlier comment, I requested that the manual pages are updated to summarise all the discussions so far. Basically, how things work and why the order is the way it was proposed.

Basically, the information you would expect if in 6 months time you have completely forgotten the topic and are working on systemd, dracut, the kernel. Sometimes it helps, to update a man page and pass it to ^^ developer (friend or colleague) who completely lacks the background and see if they can make sense of it.

Some questions from very quickly (literally a few seconds) glancing through the commits:

  • what's the difference of this PR instead of the one opened previously
    • why not take that work and fixup as needed - original adds ~200 loc, this one ~500
  • why are we changing the logger, instead of printing respective debug/error messages as needed
  • why do we add a build toggle which doesn't do anything
  • have you considered extending moduledir to take a list of folders ... NixOS people have something similar IIRC

Overall I am onboard with the idea and would review the patches, once Lucas is happy with how things should work at a high level.

@esposem
Copy link
Author

esposem commented Mar 14, 2025

In my earlier comment, I requested that the manual pages are updated to summarise all the discussions so far. Basically, how things work and why the order is the way it was proposed.

That's what I did :)

Some questions from very quickly (literally a few seconds) glancing through the commits:

* what's the difference of this PR instead of the one opened previously

The main change here is that if /lib/modules exists, nothing changes in kmod functionality. If, however, for some reasons it misses, /run/modules is used as search path.
The one previously was doing all the way around.

  * why not take that work and fixup as needed - original adds ~200 loc, this one ~500

See above, some commits are reused

* why are we changing the logger, instead of printing respective debug/error messages as needed

Because otherwise if the modules are in /run you get something like
modinfo xfat
module not found! # because it is not in default path
<then comes the module info found in /run>

* why do we add a build toggle which doesn't do anything

which toggle

* have you considered extending `moduledir` to take a list of folders ... NixOS people have something similar IIRC

I can look at that. But when you have time, can you give this PR a more than a few seconds look?

Overall I am onboard with the idea and would review the patches, once Lucas is happy with how things should work at a high level.

Nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants