Beginners guide to Kernel Module Configuration in Linux

The Linux kernel is loaded into memory by the boot loader. Kernel modules are dynamically loaded and unloaded on demand. They provide device drivers to allow the kernel to access new hardware, support for different file system types and generally extend the functionality of the kernel.

 

Listing the Loaded Kernel Modules

 

To list which kernel modules are currently loaded into the kernel, use the lsmod command. This command produces output by reading the /proc/modules file. Example:

# lsmod
Module                  Size  Used by
ip_tables              27115  5 iptable_security,iptable_filter,iptable_mangle,iptable_nat,iptable_raw
xfs                   985426  2 
libcrc32c              12644  1 xfs
... 

The Used by column gives the total number of processes that are using the module and the other modules that it depends on, followed by a list of those dependent modules. 

 

kmod Package

 

The lsmod command and other kernel module files and utilities such as modinfo, modprobe, depmod, insmod, and rmmod are provided by the kmod package:

# rpm -qf /sbin/lsmod 
kmod-20-9.el7.x86_64

 

To list all files provided by the kmod package, enter: 

# rpm -ql kmod
/etc/depmod.d
/etc/depmod.d/dist.conf
/etc/modprobe.d
/usr/bin/kmod
/usr/lib/modprobe.d
/usr/sbin/depmod
/usr/sbin/insmod
/usr/sbin/lsmod
/usr/sbin/modinfo
/usr/sbin/modprobe
/usr/sbin/rmmod
/usr/sbin/weak-modules
/usr/share/bash-completion/completions/kmod
/usr/share/doc/kmod-20
/usr/share/doc/kmod-20/COPYING
/usr/share/doc/kmod-20/NEWS
/usr/share/doc/kmod-20/README
/usr/share/doc/kmod-20/TODO
/usr/share/man/man5/depmod.d.5.gz
/usr/share/man/man5/modprobe.conf.5.gz
/usr/share/man/man5/modprobe.d.5.gz
/usr/share/man/man5/modules.dep.5.gz
/usr/share/man/man5/modules.dep.bin.5.gz
/usr/share/man/man8/depmod.8.gz
/usr/share/man/man8/insmod.8.gz
/usr/share/man/man8/kmod.8.gz
/usr/share/man/man8/lsmod.8.gz
/usr/share/man/man8/modinfo.8.gz
/usr/share/man/man8/modprobe.8.gz
/usr/share/man/man8/rmmod.8.gz

 

Listing Module Details

 

 The modinfo command displays detailed information about a specific kernel module. For example, to display information about the iptables kernel module, enter:

# modinfo ip_tables
filename:       /lib/modules/3.10.0-514.el7.x86_64/kernel/net/ipv4/netfilter/ip_tables.ko
description:    IPv4 packet filter
author:         Netfilter Core Team [coreteam@netfilter.org]
license:        GPL
rhelversion:    7.3
srcversion:     EDBAB32FC38D6942C83A4B1
depends:        
intree:         Y
vermagic:       3.10.0-514.el7.x86_64 SMP mod_unload modversions 
signer:         CentOS Linux kernel signing key
sig_key:        D4:88:63:A7:C1:6F:CC:27:41:23:E6:29:8F:74:F0:57:AF:19:FC:54
sig_hashalgo:   sha256 

 

here ,
filename: The absolute path of the kernel object file
description: The short description of the module
alias: The internal alias names for the module, if any
depends: A comma-separated list of modules that this module depends on, if any
parm: The parameter name and a short description

 

Modules are loaded from the /lib/modules/[kernel_version]/kernel directory. For example, to display the absolute path of the ip_tables kernel object file, :

 

Loading and unloading kernel modules

 

Loading modules

Kernel modules are loaded by using the modprobe command. The device manager for the Linux kernel, udev, uses modprobe to load drivers for automatically detected hardware. For example to load the kernel module ip_tables :

# modprobe ip_tables

 

To verify that the module has been loaded :

# lsmod | grep ip_tables
ip_tables              27115  5 iptable_security,iptable_filter,iptable_mangle,iptable_nat,iptable_raw

 

The dependent modules are loaded first. Use modprobe –v (verbose) to view the dependency resolution when loading a kernel module. For example :

# modprobe -v nfs
insmod /lib/modules/3.10.0-514.el7.x86_64/kernel/fs/fscache/fscache.ko 
insmod /lib/modules/3.10.0-514.el7.x86_64/kernel/fs/nfs/nfs.ko

 

Note : modprobe uses the insmod command to load the modules into the kernel. Do not use insmod, however, because this command does not resolve dependencies.

 

Unloading Modules

Unload kernel modules by using the modprobe –r command. You can also use the verbose option. For example, to unload the nfs kernel module, enter:

# modprobe -rv nfs
rmmod nfs
rmmod fscache

 

Note : The modprobe –r command uses rmmod to unload the modules. But similar to insmod, it is not recommended to use rmmod directly to unload kernel modules.

Modules are unloaded in the reverse order, with the nfs.ko kernel module being unloaded first followed by the modules it was dependent on. Modules being used by a process or modules needed by other loaded modules are not unloaded.

 

Kernel Module Parameters

 

Just as the kernel can accept boot time parameters to modify the behavior of the kernel, kernel modules can also accept parameters to modify their behavior. The syntax for passing parameters to a kernel module with modprobe is:

# modprobe [module_name] [parameter=value]

 

 Multiple parameter=value pairs can be passed by separating the pairs with spaces. Ensure that the module is not previously loaded, because modprobe does not reload the module.

 

 

 Configuration Directory for modprobe

 

 

 The configuration directory for modprobe is /etc/modprobe.d. Create *.conf files in /etc/modprobe.d to:

 

 – Specify options 

 – Create aliases 

 – Override normal modprobe behavior 

 – Blacklist kernel modules

 

 The format of these .conf files is one command per line. Valid commands to use in these files include the following:

 

alias, options, install, remove, blacklist

 

 Alias

 Use the syntax alias alias_name module_name to create alternative names for kernel modules. You can also use shell wildcards in alias names. Example:

alias usbdevfs usbcore

 

 Options

 Use the syntax options module_name option(s) to add options to module_name. Example:

options b43 nohwcrypt=1 qos=0

 

 Install

 Use the syntax install module_name command(s) to tell modprobe to run shell commands rather than inserting the module in the kernel. Example:

install net-pf-6 /bin/true

 

Remove

This is similar to the install command, except it is invoked when “modprobe –r” is run. Use the syntax remove module_name command(s) to tell “modprobe –r” to run shell commands rather than unloading the module from the kernel.

 

Blacklist

Use the syntax blacklist module_name to tell modprobe to ignore a module’s internal aliases. Internal aliases are those seen when using the modinfo [module_name] command. The blacklist keyword is typically used when the associated hardware is not needed, or when two or more modules support the same devices, or a module invalidly claims to support a device.

 

Refer to the modprobe.d man page for more information

# man modprobe.d

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)