Kernel Module Management
Introduction
Loadable Kernel Modules are object files with .ko extension which extends the capabilities of kernel. These modules
can be loaded while kernel is running and can be removed when no longer needed. Usually hardware drivers are provided as
kernel modules and loaded only those modules required by current machine’s hardware. modprobe
command is used for
loading and unloading of modules.
List Loaded and Installed Modules
List of currently loaded modules is found in /proc/modules
file. lsmod
command is wrapper around that file which
prints list of currently loaded modules.
$ lsmod
Module Size Used by
lp 20480 0
parport 49152 3 lp,parport_pc,ppdev
ip_tables 28672 0
x_tables 40960 1 ip_tables
# ... (Showing first 5 lines)
Each row prints information about a loaded kernel module. Module and Size column shows module name and size in bytes respectively. User by column list modules that depends upon this module.
lsmod
lists only loaded modules. All installed modules for currently running kernel can be found in
/lib/modules/$(uname -r)/kernel directory.
Those modules can be listed using following command:
$ find /lib/modules/$(uname -r)/kernel/ -type f -name .*ko
Details of an modules can be queried using modinfo
command:
$ modinfo <module_name>
$ modinfo rt2800pci
filename: /lib/modules/4.15.0-20-generic/kernel/drivers/net/wireless/ralink/rt2x00/rt2800pci.ko
license: GPL
firmware: rt2860.bin
description: Ralink RT2800 PCI & PCMCIA Wireless LAN driver.
version: 2.3.0
alias: pci:v00001814d0000539Fsv*sd*bc*sc*i*
depends: rt2x00lib,rt2800lib,rt2800mmio,rt2x00mmio,rt2x00pci,eeprom_93cx6
name: rt2800pci
vermagic: 4.15.0-20-generic SMP mod_unload
parm: nohwcrypt:Disable hardware encryption. (bool)
modinfo
lists various detailed information about the module. Some modules takes configurable parameters listed as
parm. This modules takes one bool parameter named nohwcrypt.
Load and Unload Modules
Kernel modules can be loaded and removed using modprobe
command. For loading a command following command is used:
$ sudo modprobe <module_name>
For loading with parameters we can execute modprobe
like below:
$ sudo modprobe rt2800pci nohwcrypt=1
modprobe
automatically finds all dependencies of the module and loads all required modules. modprobe
has an option
-n for dry running the command without actually changing anything. Along with -n we can add -v, verbose for
investigating what commands will modprobe
execute.
$ sudo modprobe -nv stp
insmod /lib/modules/4.15.0-20-generic/kernel/net/llc/llc.ko
insmod /lib/modules/4.15.0-20-generic/kernel/net/802/stp.ko
We can see that llc modules will also be loaded as dependency of stp.
We can remove a module using -r option in modprobe
.
$ sudo modprobe -r stp
Another command for installing kernel modules is insmod
and rmmod
for removing modules. But these command doesn’t
load dependencies. You have to manually resolve dependencies.
modprobe
uses /lib/modules/$(uname-r)/modules.dep.bin file for resolving dependences. There is a text files for
viewing dependencies named modules.dep. These files can be generated using depmod
command. We can re-generate the
dependency using following command:
$ sudo depmod
This will generate modules.dep.bin and modules.dep for currently running kernel.
Some symbol are provided by kernel and some are provided by other modules. If a modules didn’t find a symbol it assumes
kernel will provide it. This can create unresolved dependencies if kernel doesn’t provide the required symbol. We can
also check for unresolved dependencies using depmod
command. For that we will need a map for kernel provided symbol.
We can find kernel provided symbol in System.map file. For checking symbol errors with System.map file -eE option is used.
$ locate System.map
/boot/System.map-4.15.0-20-generic
$ sudo depmod -eE /boot/System.map-4.15.0-20-generic
modprobe Configuration
We can see the currently loaded configuration using command below:
$ modprobe -c
Configuration files are located in /etc/modprobe.d/
. modprobe
will load all files ending with .conf in this
directly.
Now create file /etc/modprobe.d/custom.conf with following lines:
# file: /etc/modprobe.d/custom.conf
option rt2800pci nohwcrypt=1
blacklist usbkbd
Lets assume that our hardware requires rt2800pci and usbkbd kernel modules to be loaded. Now if we start the system rt2800pci module will loaded with nohwcrypt=1 option. But usbkbd won’t be loaded as it is blacklisted. Modules is blacklisted if some other module provide better support then this module.
Tools Version
- kmod version 24 -XZ -ZLIB -EXPERIMENTAL
- find (GNU findutils) 4.7.0-git