Kernel modules are programs that run inside the kernel. They also can be understood as device drivers.
Kernel modules can included in the kernel file
/usr/src/linux/arch/i386/boot/bzImage
or be separate files found in /lib/modules
.
As separate files, they have to be loaded on request, manually or at startup using
/etc/conf.d/modules
.
Not having the kernel modules in the kernel file
/usr/src/linux/arch/i386/boot/bzImage
gives some advantages:
Whats loaded can be seen via lsmod
Kernel module parameters and options can be passed
Kernel modules (if not running) can be unloaded rmmod, without requiring to reboot the system
Built into the kernel hides the modules, but /lib/modules/$(uname -r)/
has some text files. cat /lib/modules/$(uname -r)/modules.builtin will show what modules are built in.
It is also possible to pass module parameters to modules included in the kernel. The equivalent of modprobe usbcore blinkenlights=1 can be passed as kernel parameter in the form usbcore.blinkenlights=1
Module might require other modules to run those dependencies can be found in: /lib/modules/
<kernel version>
/modules.dep
Unfortunately different Linux distributions use different ways how the modules get loaded. Sometimes they make use of more than one way.
This can result that passing parameters to the module will not work since the module is already loaded.
In OpenRC the file /etc/conf.d/modules
holds the modules including their parameters to be loaded during the init startup process.
modules_3="${modules_3} saa7134 module_saa7134_args_3="card=2 tuner=69"
When the system is running and the devices are plugged in then also this event can trigger to load the missing modules.
On the low level they use modprobe, so configuring modeprobe to have parameters passed to a module.
So create a file /etc/modprobe.d/tv.conf
with the line inside to be used together with modprobe.
options saa7134 card=2 tuner=69
To verify if the system has taken the parameter do
cat /sys/module/saa7134/parameters/tuner
It is also possible to prevent a module being loaded
/etc/modprobe.d/blacklist
blacklist saa7134
Kernel modules in can be inserted and removed by a user using the console.
insmod inserts module to the kernel (version of kernel and module do have to match, -f force does ignore version) uname -r shows the kernel version.
modprobe does the same and more and is more convenient to be used. It has it configuration directory /etc/modprobe.d
where default parameters for the modules can be set.
rmmod => removes the module
lsmod => lists modules present (Formats just cat /proc/modules )
lsmod
Module Size Used by
radeon 104064 1
drm 65620 2 radeon
The number under used shows how many applications (references) using it currently
modprobe -s -k char-major-63 loads the kernel module mapped to the character device with the major number 63.
/var/log/messages
shows eventually problems
during loading of the module.
modinfo <module>
give information about loaded kernel modules
For not loaded modules the path needs to be added
modinfo /lib/modules/$(uname -r)/kernel/sound/core/oss/snd-pcm-oss.ko
modinfo /lib/modules/$(uname -r)/video/fglrx.ko
Modules depend from each other /lib/modules
contains /lib/modules/
showing how. If a module depends on others then
the whole dependency tree of modules will be loaded. depmod produces this file.<kernel version>
/modules.dep
cat /proc/ioports shows the area in the IO memory space occupied by the kernel modules.
cat /proc/devices shows the major numbers assigned to the kernel modules.
Opening or loading a kernel module creates a link between /dev
file and the kernel module itself. This is done via the major number. The major number is a
constant value in the kernel module. Obviously, there might be more than one kernel module per major number.
/dev
file as well have a constant major number as seen by
ls /dev -l. However this is not enough.
Filenames use - as separator character (snd-pcm-oss.ko
), whereas lsmod shows them with _ separation
character (snd_pcm_oss
).
Some useful commands:
find /lib/modules/`uname -r`/ -name "pwc*.ko*"
depmod -a
modinfo /lib/modules/$(uname -r)/kernel/drivers/usb/media/pwc/pwc.ko
A device driver might be created in a packet completely separated from the kernel sources. Those device drivers will also be installed under /lib/modules
and are often found in /lib/modules/
. If there is common interest, then the device drivers might get adopted by the kernel sources. When this happens, it can happen that multiple versions of the same device driver are under <kernel version>
/misc/lib/modules
but inside different directories. ls -lR /lib/modules/<kernel version>
shows that.
Open Source means source code that needs to be compiled to get binary. However Linux can handle also binaries. The gentoo kernel source comes with executable binaries as in /usr/src/linux-4.12.12-gentoo/firmware/radeon
. Those binaries are used for the radeon graphic cards.
Other binaries are usually added to /lib/firmware
. This is done by installing a package containing firmware as under gentoo emerge linux firmware.
Further, when creating a kernel, firmware can be included
dmesg | grep 'firmware' will show if the firmware is loaded and used. There are some option in the kernel to have more debug messages. However if the kernel starts it looks into various places for the firmware and can produce error messages even if it later finds the firmware and loads it.
[ 10.421498] dvb-usb: found a 'Hauppauge Nova-T Stick' in cold state, will try to load a firmware
[ 10.433650] usb 2-4: loading /lib/firmware/updates/4.12.12-gentoo/dvb-usb-dib0700-1.20.fw failed with error -2
[ 10.433658] usb 2-4: loading /lib/firmware/updates/dvb-usb-dib0700-1.20.fw failed with error -2
[ 10.441880] usb 2-4: loading /lib/firmware/4.12.12-gentoo/dvb-usb-dib0700-1.20.fw failed with error -2
[ 10.477139] dvb-usb: downloading firmware from file 'dvb-usb-dib0700-1.20.fw'
[ 10.682581] dib0700: firmware started successfully.