2

In a minimal Busybox-based Linux system, which commands must be invoked as part of the init script to ensure all kernel modules for the current hardware are loaded?

2 Answers 2

0

Depending on the hardware there will be some calls to dbus to load modules. Most of this should be automated already. I assume via initramfs, udev/mdev, hald , init-system, and so on ...

One way to load modules manually is to call modprobe which will take care of all dependencies.

For example:

# modprobe snd_pcm

In my case this will load all modules needed to create sounds.

First modprobe will load all dependencies for snd_hda_core. This is cause snd_hda_core is part of the snd_pcm.

$ lsmod | grep ^snd_hda_core 
snd_hda_core          110592  5 snd_hda_codec_generic,snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec,snd_hda_codec_realtek

After that all snd_pcm dependencies are loaded by modprobe.

$ lsmod | grep ^snd_pcm
snd_pcm               135168  10 snd_hda_codec_hdmi,snd_hda_intel,snd_usb_audio,snd_hda_codec,soundwire_intel,snd_compress,snd_soc_core,snd_hda_core,snd_pcm_dmaengine

There is a folder called /etc/modules-load.d/ and in it are *.conf files to load some modules by default. This should take care of all you need, so there is no need to write an init-script...

$ cat /etc/modules-load.d/local.conf 
vfio_pci
asus-wmi-sensors
tun

I other words: I can load all modules with just one call or one file.

modprobe manpage ...

modprobe expects an up-to-date modules.dep.bin file as generated by the corresponding depmod utility shipped along with modprobe (see depmod(8)). This file lists what other modules each module needs (if any), and modprobe uses this to add or remove these dependencies automatically

depmod manpage ...

Linux kernel modules can provide services (called "symbols") for other modules to use (using one of the EXPORT_SYMBOL variants in the code). If a second module uses this symbol, that second module clearly depends on the first module. These dependencies can get quite complex.

depmod creates a list of module dependencies by reading each module under /lib/modules/version and determining what symbols it exports and what symbols it needs. By default, this list is written to modules.dep, and a binary hashed version named modules.dep.bin, in the same directory.

#####################

Addition

after pointing out I should explain the magic behind all this. I have to try but its a bit sketchy.

Lets take a look at one of my gpus.

The following example shows the modalias of my gpu and the alias in the module definition overlaps.

$ lspci
...
0b:00.0 VGA compatible controller: NVIDIA Corporation GP102 [GeForce GTX 1080 Ti] (rev a1)
...
$ cat /sys/bus/pci/devices/0000:0b:00.0/modalias
pci:v000010DEd00001B06sv00001462sd00003607bc03sc00i00
$ modinfo nvidia
...
alias:          pci:v000010DEd*sv*sd*bc03sc02i00*
alias:          pci:v000010DEd*sv*sd*bc03sc00i00*
depends:        i2c-core,drm
...

lspci manpage ...

Kernel module reporting that it is capable of handling the device

This part is a bit unknown to me and I had to guess a bit.

Anyway, cause of the information I gathered now I know two ways to setup the module depending on this information.

first method (at boot) ...

echo "alias $(cat /sys/bus/pci/devices/0000:0b:00.0/modalias) nvidia" >> /etc/modprobe.d/default.conf 

second method (runtime)...

echo "0000:0b:00.0" > /sys/bus/pci/drivers/nvidia/bind

The nvidia folder can be one of any module name, for example I could give it the vfio-pci folder instead.

echo "0000:0b:00.0" > /sys/bus/pci/drivers/nvidia/unbind
echo "0000:0b:00.0" > /sys/bus/pci/drivers/vfio-pci/bind

Keep in mind this is just an example and wont work in a real environment, cause gpu modules have to be loaded at boot time only.

4
  • Please revise your answer and don't assume any distro or other setup. This question is purely about a minimal setup involving just a kernel and an initramfs containing busybox and an init script located at /init . Devices can mounted either with devtmpfs or mdev (there is no udev in this setup) Dec 27, 2021 at 18:42
  • ok thx. maybe he uses mdev instead of udev. but this doesn't change the fact that he needs a way to load modules, like modprobe or insmod. I showed a way to load modules and pointed to the manpages of depmod. If he doesn't use depmod it will get really messy and we would need more intel on the hardware to approach this. The one and only alternative I know of is to put all modules into the initramfs
    – Mario
    Dec 27, 2021 at 18:57
  • modprobe doesn't automatically coldplug all the current hardware and still requires someone to hardcode which modules to load. What I am interested in is the missing "glue" that will cause the system to scan all devices in /sys and then automatically call modprobe only for those that were found. Dec 27, 2021 at 19:01
  • ok I updated my post, hope this will halp
    – Mario
    Dec 27, 2021 at 21:16
0

After going down the rabbit hole, assuming a minimal initramfs with some drivers built into the kernel and others present as kernel modules along with all relevant depmod-generated metadata, here is what I found:

Drivers built into the kernel are loaded before /init is invoked.

Drivers built as modules must be loaded by /init as follows:

  • first /sys and /proc must be mounted
  • then the existing hardware should be scanned and the relevant kernel modules should be loaded

The hardware scanning and module loading should normally be accomplished by a simple mdev -s invokation.

Unfortunately that doesn't work as it should. One must thereforce force this process to occur by invoking find /sys/ -name modalias | xargs sort -u | xargs -n 1 modprobe instead.

After that all the drivers for the current hardware (and their dependencies) will have been loaded and initialized.

1
  • That last pipeline command line doesn't make sense: neither modutils modprobe or busybox modprobe support loading a module based on a path to a modalias file in /sys.
    – JoostM
    Sep 11, 2023 at 7:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .