Introduction to Kernel Module Insertion with insmod
This tutorial delves into the insmod
command within Linux environments, a crucial tool for systemadmin tasks involving dynamic kernel modification. Specifically, insmod
facilitates the insertion of kernel modules into the currently running kernel. Kernel modules, often serving as device drivers or extending kernel functionalities, are self-contained code blocks that can be loaded and unloaded on demand. In this guide, you'll learn the process of compiling a basic kernel module and subsequently using insmod
to integrate it into the operating kernel.
This comprehensive guide covers the essentials: a detailed introduction to the insmod
command, the process of compiling a kernel module from source, and the practical application of insmod
for kernel module insertion. Note that compiling kernel modules requires the linux-headers-$(uname -r)
package, while the actual insertion is performed using the insmod
command.
Understanding the insmod Command
This section provides a thorough overview of the insmod
command in Linux, the primary utility for dynamically adding kernel modules to a live kernel. Kernel modules are essentially software components that enhance the kernel's capabilities or provide support for specific hardware devices. They can be added (and removed) without requiring a system reboot.
Let's begin by verifying the active kernel version and listing the presently loaded kernel modules:
uname -r
lsmod
Example output:
5.15.0-58-generic
Module Size Used by
nvme_core 102400 1
pci_stub 16384 1
vboxpci 24576 0
vboxnetadp 45056 0
vboxnetflt 49152 0
vboxdrv 1028096 3 vboxnetadp,vboxnetflt,vboxpci
The lsmod
command displays a list of kernel modules currently active within the kernel. The example output shows several modules associated with VirtualBox, a widely used virtualization application.
Now, let's construct a simple kernel module. Create a new file, named hello.c
, within the ~/project
directory. The file should contain the following C code:
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple kernel module");
int init_module(void) {
printk(KERN_INFO "Hello, kernel!\n");
return 0;
}
void cleanup_module(void) {
printk(KERN_INFO "Goodbye, kernel!\n");
}
This minimalist module will output "Hello, kernel!" upon loading and "Goodbye, kernel!" upon unloading, writing these messages to the system log.
Next, compile the hello.c
kernel module using the following commands:
sudo apt update
sudo apt-get install -y linux-headers-$(uname -r)
gcc -Wall -DMODULE -D__KERNEL__ -I/lib/modules/$(uname -r)/build/include -c hello.c
The linux-headers-$(uname -r)
package is essential for compiling kernel modules as it supplies the necessary header files corresponding to your running kernel. The gcc
command then compiles hello.c
, resulting in the object file hello.o
.
With the compiled hello.o
, you can now use insmod
to insert the module into the kernel:
sudo insmod hello.o
Example output:
Executing insmod
will load hello.o
into the kernel. Look for the "Hello, kernel!" message in the system logs, accessible through tools like dmesg
.
To confirm the module is loaded, execute the following command:
lsmod | grep hello
Example output:
hello 16384 0
If the hello
module is listed in the lsmod
output, it is successfully loaded into the kernel.
Kernel Module Compilation: A Practical Guide
This section provides a step-by-step guide on compiling a kernel module from its source code.
Begin by creating a new C source file named hello2.c
within the ~/project
directory. Populate it with the following content:
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Another simple kernel module");
int init_module(void) {
printk(KERN_INFO "Hello, from the second kernel module!\n");
return 0;
}
void cleanup_module(void) {
printk(KERN_INFO "Goodbye, from the second kernel module!\n");
}
This module mirrors the functionality of the previous one but utilizes different messages for identification.
Compile the hello2.c
source file using the gcc
compiler:
gcc -Wall -DMODULE -D__KERNEL__ -I/lib/modules/$(uname -r)/build/include -c hello2.c
The compilation process remains the same; however, you are now compiling hello2.c
instead of hello.c
.
Following successful compilation, the hello2.o
object file will be generated in the ~/project
directory.
Utilizing insmod for Kernel Module Insertion
This section focuses on the practical application of the insmod
command to insert a compiled kernel module into the running kernel.
First, verify that the hello2.o
module is correctly compiled and available within the ~/project
directory:
ls -l ~/project/hello2.o
Example output:
-rw-r--r-- 1 labex labex 16384 Apr 11 12:34 /home/labex/project/hello2.o
With the hello2.o
module ready, use insmod
to insert it into the kernel:
sudo insmod ~/project/hello2.o
Example output:
The insmod
command loads the hello2.o
module into the kernel. The message "Hello, from the second kernel module!" will be logged to the system logs and can be viewed using dmesg
.
Verify the successful insertion of the module with the following command:
lsmod | grep hello2
Example output:
hello2 16384 0
The presence of hello2
in the lsmod
output confirms that the module is successfully loaded.
Summary: Mastering Kernel Module Insertion with insmod
This tutorial provided a practical guide to the insmod
command in Linux for inserting kernel modules into the active kernel. We began by examining the current kernel version and loaded modules using uname -r
and lsmod
. Next, we developed a simple kernel module designed to print "Hello, kernel!" upon loading and "Goodbye, kernel!" upon unloading. This module was compiled using gcc
in conjunction with the linux-headers-$(uname -r)
package, providing the required kernel-specific header files. Finally, we successfully used the insmod
command to insert our custom module into the kernel, demonstrating the process of dynamically extending kernel functionality. This is a fundamental skill for any aspiring systemadmin dealing with the Linux operating system.