Linux – Sequence and Priority of Loading Kernel Modules on Startup

kernel-moduleslinuxstartup

I have a Linux kernel module which I compiled dynamically. How is it added to startup? There are lot of .ko files in /lib/modules. How is priority set for loading these modules?

Best Answer

They are not loaded automatically at start-up or any other time, although a lot of them do end up being loaded during boot. There are three different mechanisms by which this happens:

  • Userspace request: Which covers everything from init services to udev to the command-line. Init or udev are probably the most straightforward means if you want to load a specific module at boot time.

  • Hotplugged device: When you connect something to, e.g., USB or PCI, the kernel detects this and requests an appropriate module based on how the device identifies itself.

  • Needed protocol or other implementation: When the kernel needs to do something, such as read a filesystem, and discovers it lacks the knowledge to do so, it will request a module.

Notice that for the last two I used the phrase "request a module" -- this is because the kernel actually loads via a userspace daemon, kmod which executes /sbin/modprobe. According to Wolfgang Mauerer in Linux Kernel Architecture, there are only ~100 different points in the 2.6 kernel where it calls an internal request_module() function.

modprobe uses a database of installed MODULE_ALIAS's. These are specified in the module source code explicitly, or derived from it's MODULE_DEVICE_TABLE, which is a list of OEM device IDs that the module services.

Related Question