Linux Kernel

Adding Module To The Kernel

We will go through the process or steps required to add a kernel module while the kernel is running. We will also discuss the few useful Linux utilities available. These utilities can be employed to get info on the module already present in the Linux kernel, add a new module, and get the info on the provided module.

Description

Let’s start with the kernel module; the kernel module is the logical separate functionality which can be added at a later stage in the running kernel. This extends the Linux kernel features/functions.

These modules are files with the extension as “.ko” , which means kernel objects. If we come across any file with “.ko” as an extension, it clearly indicates that it is a kernel module.

A few example names of modules are : i2c-modules.ko , hello-world.ko, etc.

The process of adding a kernel module to the kernel is also known as the loading of a kernel module. The kernel module can only be loaded if it is compatible with the kernel source tree. This means the kernel module is not platform-independent. These can be loaded only on the kernel with the same source tree and build configuration.

Adding/Loading Of Kernel Module

Linux provides a utility known as “insmod”. This is the utility which can be used to load the kernel module at the running kernel.

To load the kernel module, just execute the command as insmod followed by the module file name.

Let us take an example of hello-world.ko , a module which is the classic example and just prints the hello world message. To load the hello-world kernel module, below is the command which can be used :

A sample snapshot is attached below; highlighted file is the kernel module:

$ sudo insmod hello-world.ko

Example snapshot:

With the above command’s successful execution, our module is added to the Linux kernel, and we will see the print message from the hello-world kernel module in the printk buffer. To check the message from the hello-world, use the dmesg command. If the command results in no error message, then it can be considered module addition is successful. As we are extending the kernel functions, so it requires super user privileges to execute the “insmod”. Example output of the dmesg command is as below:

sushilrathore-2:~/hello-world$ sudo insmod hello-world.ko

sushilrathore-2:~/hello-world$ dmesg

[10500712.434672] Hello world

cienauser@haxv-srathore-2:~/hello-world$

Confirming The Module Is Loaded

To confirm if the module is loaded and present among the other modules in the Linux kernel. The “lsmod” command can be used to list all the kernel modules. Issue the command at the Linux shell, and we will see the complete list of the loaded modules in the Linux kernel. Issuing the lsmod on my system, I see the below output at the console:

sushilrathore-2:~/hello-world$ lsmod

Module Size Used by

hello_world 16384 0

nf_conntrack_ipv6 20480 0

nf_defrag_ipv6 20480 1 nf_conntrack_ipv6

ip6table_filter 16384 0

ip6_tables 28672 1 ip6table_filter

nf_conntrack_ipv4 16384 0

nf_defrag_ipv4 16384 1 nf_conntrack_ipv4

xt_recent 20480 0

xt_conntrack 16384 0

nf_conntrack 135168 3 xt_conntrack,nf_conntrack_ipv6,nf_conntrack_ipv4

cpuid 16384 0

iptable_filter 16384 1

ipmi_devintf 20480 0

ipmi_msghandler 53248 1 ipmi_devintf

As we can see in the logs above, there are many modules listed, and our module hello-world is also on the list; and I have highlighted it for easy spotting.

So we confirmed that our module is loaded in the kernel successfully.

Removing/Unloading The Kernel Module

To remove or unload the kernel module, we can use the Linux command “rmmod”. This is the command which is used to remove the loaded kernel module and listed by lsmod. This operation also requires superuser privileges. Going back to our hello-world example, if we wish to remove the hello-world kernel module which we have loaded previously. We need to issue the below command:

sushilrathore-2:~/hello-world$ sudo rmmod hello_world

sushilrathore-2:~/hello-world$

After the command execution, if nothing is seen on the console, i.e. there is no error message. This means the unloading/removal of the kernel module is successful.

Confirming The Removal/Unloading Of The Module

Again to confirm if the module is unloaded successfully, we can use the lsmod command. After the removal of the kernel module, we should not see the module present in the list of modules provided by “lsmod”.

Following is the example output from my system:

sushilrathore-2:~/hello-world$ lsmod

Module Size Used by

nf_conntrack_ipv6 20480 0

nf_defrag_ipv6 20480 1 nf_conntrack_ipv6

ip6table_filter 16384 0

ip6_tables 28672 1 ip6table_filter

nf_conntrack_ipv4 16384 0

nf_defrag_ipv4 16384 1 nf_conntrack_ipv4

xt_recent 20480 0

xt_conntrack 16384 0

nf_conntrack 135168 3 xt_conntrack,nf_conntrack_ipv6,nf_conntrack_ipv4

cpuid 16384 0

iptable_filter 16384 1

ipmi_devintf 20480 0

ipmi_msghandler 53248 1 ipmi_devintf

vmw_vsock_vmci_transport 32768 1

vsock 36864 2 vmw_vsock_vmci_transport

binfmt_misc 20480 1

intel_rapl_perf 16384 0

joydev 24576 0

input_leds 16384 0

vmw_balloon 20480 0

serio_raw 16384 0

shpchp 36864 0

vmw_vmci 69632 2 vmw_balloon,vmw_vsock_vmci_transport

In the above list, if we check, we will not find the hello-world kernel module. This double confirm that the module is removed from the system.

There is one more very important utility offered, which can be used to get the info of the kernel module file. “modinfo” is the command provided to get the details of the already present kernel module.

Executing the “modinfo” with the hello-world kernel module we get the below output:

sushilrathore-2:~/hello-world$ modinfo hello-world.ko

filename: /home/sushilrathore/hello-world/hello-world.ko

description: Hello world!

license: GPL

srcversion: BBD5A60063B15C8D80048FD

depends:

retpoline: Y

name: hello_world

vermagic: 4.15.0-163-generic SMP mod_unload modversions

cienauser@haxv-srathore-2:~/hello-world$

The above information is the details of the kernel module. The important information to note is it provides you with the srcversion and vermagic. This information can be used to identify the kernel with which this module is compatible and can be loaded into. If we try to add the kernel module compiled for other Linux sources or kernel, then we will get the incompatible error from the insmod command.

Below is the sample code and Makefile code we have used in our discussion above:

hello-world.c

#include <linux/module.h>

#include <linux/kernel.h>

#include <linux/init.h>
   
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Hello world!");

static int __inithello_init(void)
{
    printk(KERN_INFO "Hello world\n");
    return 0;
}

static void __exit hello_exit(void)
{
    printk(KERN_INFO "Goodbye world\n");
}

module_init(hello_init);
module_exit(hello_exit);

Makefile

obj-m = hello-world.o

all:

make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules

clean:

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Conclusion

So far, we have discussed about the loadable kernel modules process/steps to add the kernel module. Insmod is the command we discussed for loading the kernel module. To confirm if the module is loaded successfully, we can use the lsmod command and finally, to remove the rmmod command can be used. There is one more utility modinfo which can be used to print the details of the kernel module.

About the author

Sushil Rathore

Sushil Rathore is having hands-on experience in Linux Platform SW. He's an expert of Linux on ARM/X86 Boards. He has very good understanding on Bootloaders and other platform softwares. He has good industrial experience and have worked in reputed Organizations. Currently he is associated with a reputed firm in the networking domain.