Understanding Linux Hardware Module

Let’s begin with explanation of what hardware module actually is. Then, we will discuss how to list installed modules, get module information, install new modules, and remove modules. So, let’s dive right into this motherfu*ker.

The Linux kernel communicates with devices (USB devices, keyboards, hard drives, magnetic tapes, all kinds of stuff). We know from Windows environment that a device needs driver to work. Similarly, kernel needs a device drivers to understand those devices. But unfortunately, compiling device drivers for all kinds of devices would be dumb. And, it was done like that in the past, when kernel was small and device support was small. Nowadays, as Linux conquers marketplace, it needs to add all those device drivers to its kernel binary file.

This is important to understand, let’s focus a bit here. To avoid compiling tons of code for supported device drivers, kernel can attach single module for specific device. That way, system uses only necessary modules for devices he understands and uses. Modules are published as source code or binary files. Knowing that, only then those files are ready to be inserted onto the kernel.

Standard location for storing module files is in /lib/modules/ directory. This is where insmod and modprobe look for module files by default. Inside this directory, each kernel has its own sub-directory for own modules. This allows you to create modules for each kernel version.

At boot time, kernel loads modules stored in /etc/modules file, one per line. Kernel module configurations are stored in /etc/modules.conf file. Some module depend on others, and they require dependent modules to be run first. These relationships are stored in /lib/modules/kernel-version/modules.dep file. Important thing to mention is that modules_install command is used to install modules. However, modules_install uses depmod utility to determine module dependencies. Once done, it creates modules.dep file. If we modify any modules after that, we should manually run depmod utility to update modules.dep file.

Listing installed modules

Listing modules is done with lsmod command. The lsmod will output all installed modules and their dependencies. The “Used by” header field explains dependency for the given module.

[aldin@arch ~]$ lsmod
Module                  Size  Used by
nvidia_drm             57344  8
nvidia_modeset       1216512  18 nvidia_drm
nvidia              27697152  1740 nvidia_modeset
snd_sof_pci            24576  0
snd_sof_intel_byt      20480  1 snd_sof_pci
snd_sof_intel_ipc      20480  1 snd_sof_intel_byt
snd_sof_intel_hda_common    90112  1 snd_sof_pci
snd_soc_hdac_hda       24576  1 snd_sof_intel_hda_common
snd_sof_xtensa_dsp     16384  2 snd_sof_intel_hda_common,snd_sof_intel_byt
snd_sof_intel_hda      20480  1 snd_sof_intel_hda_common
intel_rapl_msr         20480  0

Getting module information

To inspect each module a bit more, use modinfo command. It shows module location (filename: directive), where the module came from, module version, dependencies, description, and more…

[aldin@arch ~]$ modinfo ip_tables
filename:       /lib/modules/5.8.12-arch1-1/kernel/net/ipv4/netfilter/ip_tables.ko.xz
alias:          ipt_icmp
description:    IPv4 packet filter
author:         Netfilter Core Team <coreteam@netfilter.org>
license:        GPL
srcversion:     BB5C6B0520AD469887794CB
depends:        x_tables
retpoline:      Y
intree:         Y
name:           ip_tables
vermagic:       5.8.12-arch1-1 SMP preempt mod_unload 
sig_id:         PKCS#7
signer:         Build time autogenerated kernel key
sig_key:        65:CF:12:A1:9A:04:F6:A6:AE:BE:6F:43:5D:6E:A2:6A:31:CC:A2:13
sig_hashalgo:   sha512
signature:      A3:11:44:94:50:77:35:BF:00:04:7E:2B:5D:A6:31:11:87:53:7B:37:
		10:C7:5A:FF:E3:79:95:82:47:E0:25:F7:FA:75:BD:EB:6C:AA:E3:DE:
		55:76:6D:DE:54:FB:2D:13:54:E2:B1:7A:2B:8B:AD:DC:13:40:38:C7:

Installing new modules

To manually install module, use one of the two commands:

insmod

Requires to specify exact file to load. As we have seen before, kernel modules are stored in /lib/modules/ directory. Inside, you will find subdirectories for all kernel modules you can install. Each file has .ko extension, and to install it use the following command:

[aldin@arch ~]$ cd /lib/modules/5.8.12-arch1-1/build/security/selinux/Kconfig

Sadly, one downside to using insmod program is that cannot install dependencies for the module you want to install. For example, if a module needs dependency module, first install its dependencies modules, and then the module you want to install.

modprobe

In contrary, modprobe command will install all dependencies for specified module. Also, you do not have to specify full path within /lib/modules/ directory. The modprobe is smart enough to look for it recursively throughout the entire directory tree.

-a Insert all modules listed on the command line
-CSpecify different configuration file other than the default
-cDisplay current configuration used
-dSpecify root directory to use for installing modules
-fForce module installation, even if there are version issues
-iIgnore install and remove commands from configuration file
-nPerform fake module install, just to see if it works
-qQuiet mode (no error messages shown)
-rRemove module
-sSend any error messages to syslog facility
-VProgram version
-vVerbose messages

Removing modules

To mention, you can install modules if there is no device to support it – does no harm to the system. Kernel will just ignore unused module. Finally, to remove installed module, use rmmod command, and specify full path within /lib/modules/directory. However, modprobe will find module name if no full path is specified and remove it:

# modprobe -r Kconfig

Leave a Reply