Managing Linux Kernel Modules


Learn how to manage kernel modules in your system. Dynamically loadable modules make things flexible within your environment.

Table of Contents

  1. Overview
  2. Modules-load.d
  3. Inserting Modules
  4. Removing Modules

Overview

In most Linux userspace distributions, modprobe is an available application that aides in loading dynamic kernel modules (KOs). Systemd can load these modules for a system based off a configuration file defining a list of available modules. The service utilizes modprobe for this.

This post teaches users:

  • The configuration file defining available modules
  • Manually inserting modules using modprobe/insmod
  • Manually removing modules

Modules-load.d

Under the “/etc” directory there should be another directory named “modules-load.d“. This is where configuration files are kept, which defines the list of modules that will be loaded.

/etc/modules-load.d/ 
├── 10-abc.conf 
├── 20-def.conf 
└── 30-xyz.conf

Within each .conf file is a newline separated list of module names that users are interested in loading when systemd starts.

10-abc.conf: 

hci_uart 
raspberrypi_hwmon 
...

The names of kernel modules can be found by looking at the “Makefile” within the driver’s directory. For example:

# Module 
rndis_host.ko 

# drivers/net/usb/Makefile 
obj-$(CONFIG_USB_NET_RNDIS_HOST) += rndis_host.o

By creating a .conf file, modules contained within the file will be loaded by default on device boot up by systemd. This avoids users from having to manually loading modules, or writing a separate startup script.

Inserting Modules

In case users want to manually load a kernel module, that can be done using either modprobe or insmod. Some differences between the two include:

  • modprobe
    • Looks for modules under a predefined directory, i.e. /lib/modules/$(uname -r)
    • No need to specify the .ko extension, just the module name is sufficient.
  • insmod
    • Module can be located anywhere in the filesystem.
    • Fille name and extension must be passed in.
    • Good for modules that aren’t built in, i.e. test modules.
# modprobe on a raspberry pi zero w 
root@raspberrypi0-wifi:/etc/modprobe.d# modprobe hci_uart

root@raspberrypi0-wifi:/etc/modprobe.d# 
[1730395.760177] Bluetooth: hci0: BCM: firmware Patch file not found, tried: ... 

# insmod of hci_uart 
root@raspberrypi0-wifi:/etc/modprobe.d# insmod /lib/modules/6.1.31+/kernel/drivers/bluetooth/hci_uart.ko 

root@raspberrypi0-wifi:/etc/modprobe.d# 
[1732213.870181] Bluetooth: hci0: BCM: firmware Patch file not found, tried: ...

Removing Modules

Part of removing modules will require users to first identify which modules are currently loaded. “lsmod” can be utilized to list all currently loaded modules in the system. The output module names are the strings that will be passed to the application which removes kernel modules.

root@raspberrypi0-wifi:/etc/modprobe.d# lsmod 
Module Size Used by 
hci_uart 35599 0 
algif_hash 5846 1 
aes_arm 4436 2 
aes_generic 28270 1 aes_arm 
ecb 2011 1 
cmac 3264 2 
algif_skcipher 4368 1 
af_alg 16671 6 algif_hash,algif_skcipher 
...

Now that users can find what modules can be unloaded, the “rmmod” command can be used to unload the specific module.

# check for loaded modules 
root@raspberrypi0-wifi:/etc/modprobe.d# lsmod 
Module Size Used by 
hci_uart 35599 0 
algif_hash 5846 1 
... 

# remove hci_uart module 
root@raspberrypi0-wifi:/etc/modprobe.d# rmmod hci_uart 

# hci_uart module unloaded 
root@raspberrypi0-wifi:/etc/modprobe.d# lsmod 
Module Size Used by 
algif_hash 5846 0 
aes_arm 4436 0 
...

Sometimes there are module dependencies, which would kick in if users attempted to remove a module that was referenced/used by another module. For example:

# attempt to remove aes_generic 
root@raspberrypi0-wifi:/etc/modprobe.d# lsmod 
Module Size Used by 
algif_hash 5846 0 
aes_arm 4436 0 
aes_generic 28270 1 aes_arm

# error when removing due to dependent module 
root@raspberrypi0-wifi:/etc/modprobe.d# rmmod aes_generic 
rmmod: ERROR: Module aes_generic is in use by: aes_arm 

# remove dependent modules first, and then desired module 
root@raspberrypi0-wifi:/etc/modprobe.d# rmmod aes_arm 
root@raspberrypi0-wifi:/etc/modprobe.d# rmmod aes_generic 

# both aes_arm and aes_generic removed 
root@raspberrypi0-wifi:/etc/modprobe.d# lsmod 
Module Size Used by 
algif_hash 5846 0 
ecb 2011 0 
cmac 3264 0


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *