Ce contenu n'est pas disponible dans la langue sélectionnée.
Chapter 3. Managing kernel modules
Kernel modules extend kernel functionality. Obtain module information and perform administrative tasks such as loading, unloading, and configuring modules at boot time.
3.1. Introduction to kernel modules Copier lienLien copié sur presse-papiers!
The Red Hat Enterprise Linux kernel can be extended with kernel modules, which provide optional additional pieces of functionality, without having to reboot the system. On Red Hat Enterprise Linux 10, kernel modules are extra kernel code built into compressed <replaceable>.ko.xz` object files.
- Loadable Kernel Modules (LKMs)
- LKMs can be dynamically loaded into and unloaded from the running Linux kernel. You can add device drivers or filesystem support without requiring a system reboot or recompiling the entire kernel.
The most common functionality enabled by kernel modules are:
- Device driver which adds support for new hardware
- Support for a file system such as GFS2 or NFS
- System calls
On modern systems, kernel modules are automatically loaded when needed. However, in some cases it is necessary to load or unload modules manually.
Similarly to the kernel, modules accept parameters that customize their behavior.
You can use the kernel tools to perform the following actions on modules:
- Inspect modules that are currently running.
- Inspect modules that are available to load into the kernel.
- Inspect parameters that a module accepts.
- Enable a mechanism to load and unload kernel modules into the running kernel.
3.2. Kernel module dependencies Copier lienLien copié sur presse-papiers!
Certain kernel modules sometimes depend on one or more other kernel modules. The /lib/modules/<KERNEL_VERSION>/modules.dep file contains a complete list of kernel module dependencies for the corresponding kernel version.
depmodThe dependency file is generated by the
depmodprogram, included in thekmodpackage. Many utilities provided bykmodconsider module dependencies when performing operations. Therefore, manual dependency-tracking is rarely necessary.WarningThe code of kernel modules executes in kernel-space in the unrestricted mode. Be cautious about the modules you are loading.
weak-modules-
In addition to
depmod, Red Hat Enterprise Linux provides theweak-modulesscript, which is a part of thekmodpackage. Theweak-modulesscript determines the modules that are kABI-compatible with installed kernels. While checking modules kernel compatibility,weak-modulesprocesses modules symbol dependencies from higher to lower release of kernel for which they were built. It processes each module independently of the kernel release.
3.3. Listing installed kernels Copier lienLien copié sur presse-papiers!
The grubby --info=ALL command displays an indexed list of installed kernels on BLS installs. With Boot Loader Specification (BLS), you can standardize the way of specifying boot entries. BLS is natively supported by systemd-boot and GRUB can also be configured to use BLS.
Procedure
List the installed kernels:
# grubby --info=ALL | grep titleThe list of all installed kernels is displayed:
title="Red Hat Enterprise Linux (6.12.0-55.9.1.el10_0.x86_64) 10.0" title="Red Hat Enterprise Linux (0-rescue-0d772916a9724907a5d1350bcd39ac92) 10.0"This is the list of installed kernels of grubby-8.40-17 from the GRUB menu.
3.4. Listing currently loaded kernel modules Copier lienLien copié sur presse-papiers!
Use the lsmod command to list currently loaded kernel modules.
Prerequisites
-
The
kmodpackage is installed.
Procedure
List all currently loaded kernel modules:
$ lsmodModule Size Used by fuse 126976 3 uinput 20480 1 xt_CHECKSUM 16384 1 ipt_MASQUERADE 16384 1 xt_conntrack 16384 1 ipt_REJECT 16384 1 nft_counter 16384 16 nf_nat_tftp 16384 0 nf_conntrack_tftp 16384 1 nf_nat_tftp tun 49152 1 bridge 192512 0 stp 16384 1 bridge llc 16384 2 bridge,stp nf_tables_set 32768 5 nft_fib_inet 16384 1 ...In this example:
-
The
Modulecolumn provides the names of currently loaded modules. -
The
Sizecolumn displays the amount of memory per module in KB. -
The
Used bycolumn shows the number, and optionally the names of modules that are dependent on a particular module.
-
The
3.5. Displaying information about kernel modules Copier lienLien copié sur presse-papiers!
You can use the modinfo command to display detailed information about the specified kernel module.
Prerequisites
-
The
kmodpackage is installed.
Procedure
Display information about any kernel module:
$ modinfo <pass:quotes[KERNEL_MODULE_NAME]>For example:
$ modinfo virtio_netfilename: /lib/modules/6.12.0-55.9.1.el10_0.x86_64/kernel/drivers/net/virtio_net.ko.xz license: GPL description: Virtio network driver rhelversion: 9.0 srcversion: 8809CDDBE7202A1B00B9F1C alias: virtio:d00000001v* depends: net_failover retpoline: Y intree: Y name: virtio_net vermagic: 6.12.0-55.9.1.el10_0.x86_64 SMP mod_unload modversions ... parm: napi_weight:int parm: csum:bool parm: gso:bool parm: napi_tx:boolYou can query information about all available modules, regardless of whether they are loaded. The
parmentries show parameters the user is able to set for the module, and what type of value they expect.NoteWhen entering the name of a kernel module, do not append the
.ko.xzextension to the end of the name. Kernel module names do not have extensions. However, their corresponding files do.
3.6. Loading kernel modules at system runtime Copier lienLien copié sur presse-papiers!
The optimal way to expand the functionality of the Linux kernel is by loading kernel modules. Use the modprobe command to find and load a kernel module into the currently running kernel.
The changes described in this procedure will not persist after rebooting the system. For information about how to load kernel modules to persist across system reboots, see Loading kernel modules automatically at system boot time.
Prerequisites
- You have root permissions on the system.
-
The
kmodpackage is installed. - The corresponding kernel module is not loaded. To ensure this, list the Listing currently loaded kernel modules.
Procedure
Select a kernel module you want to load.
The modules are located in the
/lib/modules/$(uname -r)/kernel/<SUBSYSTEM>/directory.Load the relevant kernel module:
# modprobe <pass:quotes[MODULE_NAME]>NoteWhen entering the name of a kernel module, do not append the
.ko.xzextension to the end of the name. Kernel module names do not have extensions; their corresponding files do.
Verification
Optionally, verify the relevant module is loaded:
$ lsmod | grep <pass:quotes[MODULE_NAME]>If the module is loaded correctly, you can display it:
$ lsmod | grep serio_rawserio_raw 16384 0See the
modprobe(8)man page on your system for more information.
3.7. Unloading kernel modules at system runtime Copier lienLien copié sur presse-papiers!
You can use the modprobe command to find and unload a kernel module at system runtime from the currently loaded kernel.
You must not unload the kernel modules that are active in the running system. This can lead to an unstable or non-operational system.
Unloading inactive kernel modules will not disable modules configured for automatic loading at boot. These modules will be automatically loaded again when the system restarts. For information about how to prevent this outcome, see Preventing kernel modules from being automatically loaded at system boot time.
Prerequisites
- You have root permissions on the system.
-
The
kmodpackage is installed.
Procedure
List all the loaded kernel modules:
# lsmodSelect the kernel module to unload.
If a kernel module has dependencies, unload those before unloading the kernel module. For details on identifying modules with dependencies, see Listing currently loaded kernel modules and Kernel module dependencies.
Unload the relevant kernel module:
# modprobe -r <pass:quotes[MODULE_NAME]>When entering the name of a kernel module, do not append the
.ko.xzextension to the end of the name. Kernel module names do not have extensions; their corresponding files do.
Verification
Optionally, verify the relevant module is unloaded:
$ lsmod | grep <pass:quotes[MODULE_NAME]>If the module is unloaded successfully, this command does not display any output.
See the
modprobe(8)man page on your system for more information.
3.8. Unloading kernel modules at early stages of the boot process Copier lienLien copié sur presse-papiers!
To temporarily block a kernel module from loading early in the boot process, use the boot loader. This is useful when a module causes the system to become unresponsive before you can permanently disable it.
You can edit the relevant boot loader entry to unload the required kernel module before the booting sequence continues.
The changes described in this procedure do not persist across system reboots. For information about how to add a kernel module to a denylist, see Preventing kernel modules from being automatically loaded at system boot time.
Prerequisites
- You have a loadable kernel module that you want to prevent from loading.
Procedure
- Boot the system into the boot loader.
- Use the cursor keys to highlight the relevant boot loader entry.
- Press the e key to edit the entry.
- Use the cursor keys to navigate to the line that starts with linux.
Append
modprobe.blacklist=module_nameto the end of the line.The
serio_rawkernel module illustrates a rogue module to be unloaded early in the boot process.- Press Ctrl+X to boot using the modified configuration.
Verification
After the system boots, verify that the relevant kernel module is not loaded:
# lsmod | grep serio_raw
3.9. Loading kernel modules automatically at system boot time Copier lienLien copié sur presse-papiers!
You can configure a kernel module to load automatically during the boot process.
Prerequisites
- Root permissions
-
The
kmodpackage is installed.
Procedure
Select a kernel module you want to load during the boot process.
The modules are located in the
/lib/modules/$(uname -r)/kernel/<SUBSYSTEM>/directory.Create a configuration file for the module:
# echo <MODULE_NAME> > /etc/modules-load.d/<MODULE_NAME>.confNoteWhen entering the name of a kernel module, do not append the
.ko.xzextension to the end of the name. Kernel module names do not have extensions; their corresponding files do.
Verification
After reboot, verify the relevant module is loaded:
$ lsmod | grep <MODULE_NAME>ImportantThe changes described in this procedure will persist after rebooting the system.
See the
modules-load.d(5)man page on your system for more information.
3.10. Preventing kernel modules from being automatically loaded at system boot time Copier lienLien copié sur presse-papiers!
Prevent the system from loading a kernel module automatically during the boot process by listing the module in the modprobe configuration file with a corresponding command.
Prerequisites
-
The commands in this procedure require root privileges. Either use
su -to switch to the root user or preface the commands withsudo. -
The
kmodpackage is installed. - Ensure that your current system configuration does not require a kernel module you plan to deny.
Procedure
List modules loaded to the currently running kernel by using the
lsmodcommand:$ lsmodModule Size Used by tls 131072 0 uinput 20480 1 snd_seq_dummy 16384 0 snd_hrtimer 16384 1 …In the output, identify the module you want to prevent from getting loaded.
Alternatively, identify an unloaded kernel module you want to prevent from potentially loading in the
/lib/modules/<KERNEL-VERSION>/kernel/<SUBSYSTEM>/directory, for example:$ ls /lib/modules/6.12.0-55.9.1.el10_0.x86_64/kernel/crypto/ansi_cprng.ko.xz chacha20poly1305.ko.xz md4.ko.xz serpent_generic.ko.xz anubis.ko.xz cmac.ko.xz…
Create a configuration file serving as a denylist:
# touch /etc/modprobe.d/denylist.confIn a text editor of your choice, combine the names of modules you want to exclude from automatic loading to the kernel with the
blacklistconfiguration command, for example:# Prevents <KERNEL-MODULE-1> from being loaded blacklist <MODULE-NAME-1> install <MODULE-NAME-1> /bin/false # Prevents <KERNEL-MODULE-2> from being loaded blacklist <MODULE-NAME-2> install <MODULE-NAME-2> /bin/false …Because the
blacklistcommand does not prevent the module from getting loaded as a dependency for another kernel module that is not in a denylist, you must also define theinstallline. In this case, the system runs/bin/falseinstead of installing the module. The lines starting with a hash sign are comments you can use to make the file more readable.NoteWhen entering the name of a kernel module, do not append the
.ko.xzextension to the end of the name. Kernel module names do not have extensions; their corresponding files do.Create a backup copy of the current initial RAM disk image before rebuilding:
# cp /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).bak.$(date +%m-%d-%H%M%S).imgAlternatively, create a backup copy of an initial RAM disk image which corresponds to the kernel version for which you want to prevent kernel modules from automatic loading:
# cp /boot/initramfs-<VERSION>.img /boot/initramfs-<VERSION>.img.bak.$(date +%m-%d-%H%M%S)
Generate a new initial RAM disk image to apply the changes:
# dracut -f -vIf you build an initial RAM disk image for a different kernel version than your system currently uses, specify both target
initramfsand kernel version:# dracut -f -v /boot/initramfs-<TARGET-VERSION>.img <CORRESPONDING-TARGET-KERNEL-VERSION>
Restart the system:
$ rebootImportantThe changes described in this procedure will take effect and persist after rebooting the system. If you incorrectly list a key kernel module in the denylist, you can switch the system to an unstable or non-operational state.
3.11. Compiling custom kernel modules Copier lienLien copié sur presse-papiers!
Build a sample kernel module as requested by various configurations at hardware and software level.
Prerequisites
You installed the
kernel-devel,gcc, andelfutils-libelf-develpackages.# dnf install kernel-devel-$(uname -r) gcc elfutils-libelf-devel- You have root permissions.
-
You created the
/root/testmodule/directory where you compile the custom kernel module.
Procedure
Create the
/root/testmodule/test.cfile with the following content.#include <linux/module.h> #include <linux/kernel.h> int init_module(void) { printk("Hello World\n This is a test\n"); return 0; } void cleanup_module(void) { printk("Good Bye World"); } MODULE_LICENSE("GPL");The
test.cfile is a source file that provides the main functionality to the kernel module. The file has been created in a dedicated/root/testmodule/directory for organizational purposes. After the module compilation, the/root/testmodule/directory will contain multiple files.The
test.cfile includes from the system libraries:-
The
linux/kernel.hheader file is necessary for theprintk()function in the example code. -
The
linux/module.hfile contains function declarations and macro definitions that are shared across several source files written in C programming language.
-
The
-
Follow the
init_module()andcleanup_module()functions to start and end the kernel logging functionprintk(), which prints text. Create the
/root/testmodule/Makefilefile with the following content.obj-m := test.oThe Makefile contains instructions for the compiler to produce an object file named
test.o. Theobj-mdirective specifies that the resultingtest.kofile is going to be compiled as a loadable kernel module. Alternatively, theobj-ydirective can instruct to buildtest.koas a built-in kernel module.Compile the kernel module:
# make -C /lib/modules/$(uname -r)/build M=/root/testmodule modulesmake: Entering directory '/usr/src/kernels/6.12.0-55.9.1.el10_0.x86_64' CC [M] /root/testmodule/test.o MODPOST /root/testmodule/Module.symvers CC [M] /root/testmodule/test.mod.o LD [M] /root/testmodule/test.ko BTF [M] /root/testmodule/test.ko Skipping BTF generation for /root/testmodule/test.ko due to unavailability of vmlinux make: Leaving directory '/usr/src/kernels/6.12.0-55.9.1.el10_0.x86_64'The compiler creates an object file (
test.o) for each source file (test.c) as an intermediate step before linking them together into the final kernel module (test.ko).After a successful compilation,
/root/testmodule/contains additional files that relate to the compiled custom kernel module. The compiled module itself is represented by thetest.kofile.
Verification
Optional: check the contents of the
/root/testmodule/directory:# ls -l /root/testmodule/total 152 -rw-r--r--. 1 root root 16 Jul 26 08:19 Makefile -rw-r--r--. 1 root root 25 Jul 26 08:20 modules.order -rw-r--r--. 1 root root 0 Jul 26 08:20 Module.symvers -rw-r--r--. 1 root root 224 Jul 26 08:18 test.c -rw-r--r--. 1 root root 62176 Jul 26 08:20 test.ko -rw-r--r--. 1 root root 25 Jul 26 08:20 test.mod -rw-r--r--. 1 root root 849 Jul 26 08:20 test.mod.c -rw-r--r--. 1 root root 50936 Jul 26 08:20 test.mod.o -rw-r--r--. 1 root root 12912 Jul 26 08:20 test.oCopy the kernel module to the
/lib/modules/$(uname -r)/directory:# cp /root/testmodule/test.ko /lib/modules/$(uname -r)/Update the modular dependency list:
# depmod -aLoad the kernel module:
# modprobe -v testinsmod /lib/modules/6.12.0-55.9.1.el10_0.x86_64/test.koVerify that the kernel module was successfully loaded:
# lsmod | grep testtest 16384 0Read the latest messages from the kernel ring buffer:
# dmesg[74422.545004] Hello World This is a test