Loading and Removing Kernel Module

Linux kernel modules are pieces of code that can be loaded into or removed from the kernel dynamically without recompiling the kernel or rebooting the system. This modular approach enhances system functionality while maintaining efficiency and flexibility.

Without kernel modules, the operating system would need to include all anticipated functionalities in the base kernel, leading to memory wastage as most systems would rarely be used. Users would also need to rebuild and reboot the kernel for new functionality, making the system less flexible.

Kernel modules have a .ko extension and reside in the /lib/modules/<kernel-version>/kernel/ directory. The major types include file system drivers, device drivers, and system calls.

Basic Module Commands

To insert a module into the kernel, use the insmod command −

insmod <module_name>.ko

To remove a module from the kernel, use the rmmod command. Note that a module currently in use cannot be removed −

rmmod <module_name>

To view currently loaded modules, use the lsmod command −

lsmod

Creating a Kernel Module

Let's create a simple kernel module called example.c. First, include the necessary header files −

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>

MODULE_DESCRIPTION("Kernel module 1");
MODULE_AUTHOR("Harry");
MODULE_LICENSE("GPL");

static int example_init(void)
{
    pr_debug("Welcome! Good to have you here!<br>");
    return 0;
}

static void example_exit(void)
{
    pr_debug("Goodbye!<br>");
}

module_init(example_init);
module_exit(example_exit);

The example_init() function runs when the module is loaded, while example_exit() runs when it's removed. Messages are stored in kernel log space and can be viewed using system logging tools.

Compiling the Module

Kernel modules use the kbuild system for compilation. Create a Makefile

KDIR = /lib/modules/$(shell uname -r)/build

all:
	make -C $(KDIR) M=$(PWD) modules

clean:
	make -C $(KDIR) M=$(PWD) clean

Create a Kbuild file to specify compilation details −

EXTRA_CFLAGS = -Wall -g
obj-m = example.o

Compile the module by running −

make

Loading and Testing the Module

Insert the compiled module −

sudo insmod example.ko

Verify the module is loaded −

lsmod | grep example
example                16384  0

View kernel messages −

dmesg | tail -2
Welcome! Good to have you here!

Remove the module −

sudo rmmod example
dmesg | tail -1
Goodbye!

Generated Files

After compilation, several files are created −

File Description
example.o Module object file before linking
example.mod.c Contains module metadata information
example.mod.o Compiled module metadata object
example.ko Final kernel module (linked object)
modules.order Module linking order specification
Module.symvers Symbol version information

Conclusion

Kernel modules provide a flexible way to extend Linux kernel functionality without system reboots. The process involves writing module code with proper init/exit functions, compiling with kbuild, and managing with insmod, rmmod, and lsmod commands.

Updated on: 2026-03-17T09:01:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements