Module Management in RHEL / CentOS 7


The Linux kernel allows drivers and features to be compiled as modules rather than as part of the kernel itself. This means that users can often change features in the kernel or add drivers without recompiling, and that the Linux kernel doesn't have to carry a lot of unnecessary baggage. Want to learn how to manage your modules? It's easy to do, just keep reading.

In this tutorial, we'll walk through the steps of seeing what's already loaded in the running kernel, and adding and removing modules from the kernel.


What's Loaded

1. To see Loaded Kernel driver or module information for PCI components:

[root@localhost ~]# lspci -k
00:00.0 Host bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX Host bridge (rev 01)
        Subsystem: VMware Virtual Machine Chipset
        Kernel driver in use: agpgart-intel
00:01.0 PCI bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX AGP bridge (rev 01)
00:07.0 ISA bridge: Intel Corporation 82371AB/EB/MB PIIX4 ISA (rev 08)
        Subsystem: VMware Virtual Machine Chipset
00:07.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
        Subsystem: VMware Virtual Machine Chipset
        Kernel driver in use: ata_piix
        Kernel modules: ata_piix, pata_acpi, ata_generic
00:07.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
        Subsystem: VMware Virtual Machine Chipset
        Kernel modules: i2c_piix4
02:01.0 Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) (rev 01)
        Subsystem: VMware PRO/1000 MT Single Port Adapter
        Kernel driver in use: e1000
        Kernel modules: e1000
.
.
.


2. List currently loaded modules:

[root@localhost ~]# lsmod
Module                  Size  Used by
xt_CHECKSUM            12549  1
ipt_MASQUERADE         12678  3
nf_nat_masquerade_ipv4    13412  1 ipt_MASQUERADE
tun                    31740  1
devlink                48345  0
ip6t_rpfilter          12595  1
ipt_REJECT             12541  4
nf_reject_ipv4         13373  1 ipt_REJECT
ip6t_REJECT            12625  2
nf_reject_ipv6         13717  1 ip6t_REJECT
xt_conntrack           12760  12
.
.
.


You can also find loaded modules list in the below file:

[root@localhost ~]# cat /proc/modules
xt_CHECKSUM 12549 1 - Live 0xffffffffc0819000
ipt_MASQUERADE 12678 3 - Live 0xffffffffc0814000
nf_nat_masquerade_ipv4 13412 1 ipt_MASQUERADE, Live 0xffffffffc0805000
tun 31740 1 - Live 0xffffffffc080b000
devlink 48345 0 - Live 0xffffffffc07f8000
ip6t_rpfilter 12595 1 - Live 0xffffffffc07f3000
ipt_REJECT 12541 4 - Live 0xffffffffc07ee000
nf_reject_ipv4 13373 1 ipt_REJECT, Live 0xffffffffc07e9000
ip6t_REJECT 12625 2 - Live 0xffffffffc07e4000
nf_reject_ipv6 13717 1 ip6t_REJECT, Live 0xffffffffc07df000
xt_conntrack 12760 12 - Live 0xffffffffc07cd000
ip_set 45644 0 - Live 0xffffffffc07d2000
nfnetlink 14490 1 ip_set, Live 0xffffffffc07c1000
ebtable_nat 12807 1 - Live 0xffffffffc07bc000
.
.
.



Installing Modules and get information about them

What if you have a module you want to load into the kernel? You can do that with insmod or modprobe.

The preferred method is modprobe, because it will also load any modules that the requested module depends on.

Load a module "vfat":

[root@localhost ~]# lsmod | grep -i vfat

[root@localhost ~]# modprobe vfat

[root@localhost ~]# lsmod | grep -i vfat
vfat                   17461  0
fat                    65950  1 vfat

Modules dependencies are recorded to "modules.dep" file using "depmod" command. Whenever we run "modprobe" command to load a particular module it gets the dependency list from "modprob.dep" file to know which are all other modules needs to be loaded before the one you asked for.


Get information about "vfat" module:

[root@localhost ~]# modinfo vfat
filename:       /lib/modules/3.10.0-957.10.1.el7.x86_64/kernel/fs/fat/vfat.ko.xz
author:         Gordon Chaffee
description:    VFAT filesystem support
license:        GPL
alias:          fs-vfat
retpoline:      Y
rhelversion:    7.6
srcversion:     A3254796A3CD9815ABDDC94
depends:        fat
intree:         Y
vermagic:       3.10.0-957.10.1.el7.x86_64 SMP mod_unload modversions
signer:         CentOS Linux kernel signing key
sig_key:        17:EA:5F:B9:16:4B:C2:26:55:5C:00:43:FA:D4:E5:86:CC:E8:A2:05
sig_hashalgo:   sha256


Removing Modules temporarily

To unload "vfat" module:

[root@localhost ~]# modprobe -r vfat

[root@localhost ~]# lsmod | grep -i vfat

When we run "modprobe -r <module_name>" command, it unload the module as well as it dependent modules as well. Again it refers "modules.dep" file to do so.


Uses of modprobe.d directory and dep files:


Now a days old "modprobe.conf" file is replaced with module configuration directories:

/usr/lib/modprobe.d/ - Default modules needs to be loaded at the time of host boot-up
/etc/modprobe.d/ - Custom modules needs to be loaded at the time of host boot-up


Where do we find "modules.dep" file:

/usr/lib/modules/3.10.0-957.el7.x86_64/modules.dep - List of modules and their dependencies, generated by depmod. Contains Human readable format.
/usr/lib/modules/3.10.0-957.el7.x86_64/modules.dep.bin - Binary file generated by depmod listing the dependencies for every module. It is used by kmod tools like modprobe.

In case if this "modules.dep" file is removed or corrupted, you can face issues while loading new module. So to re-create the "modules.dep" file you can run:

[root@localhost ~]# depmod -a

Comments

Popular posts from this blog

Recover or restore initramfs file in RHEL or CentOS 7

Space reclamation / UNMAP on RHEL or CentOS 7

How to recover /boot partition on RHEL or CentOS 7?