Build a sample kernel module as requested by various configurations at hardware and software level.
Before you begin
- You installed the
kernel-devel, gcc, and elfutils-libelf-devel packages.
# dnf install kernel-devel-$(uname -r) gcc elfutils-libelf-devel
- You have root permissions.
- You created the
/root/testmodule/ directory where you compile the custom kernel module.
Procedure
-
Create the
/root/testmodule/test.c file with the following content.
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{ printk("Hello World\n This is a test\n"); return 0; }
void cleanup_module(void)
{ printk("Good Bye World"); }
MODULE_LICENSE("GPL");
The test.c file is a source file that provides the main functionality to the kernel module. The file has been created in a dedicated /root/testmodule/ directory for organizational purposes. After the module compilation, the /root/testmodule/ directory will contain multiple files.
The test.c file includes from the system libraries:
- The
linux/kernel.h header file is necessary for the printk() function in the example code.
- The
linux/module.h file contains function declarations and macro definitions that are shared across several source files written in C programming language.
-
Follow the
init_module() and cleanup_module() functions to start and end the kernel logging function printk(), which prints text.
-
Create the
/root/testmodule/Makefile file with the following content.
The Makefile contains instructions for the compiler to produce an object file named test.o.
The obj-m directive specifies that the resulting test.ko file is going to be compiled as a loadable kernel module.
Alternatively, the obj-y directive can instruct to build test.ko as a built-in kernel module.
-
Compile the kernel module:
# make -C /lib/modules/$(uname -r)/build M=/root/testmodule modules
make: Entering directory '/usr/src/kernels/6.12.0-55.9.1.el10_0.x86_64'
CC [M] /root/testmodule/test.o
MODPOST /root/testmodule/Module.symvers
CC [M] /root/testmodule/test.mod.o
LD [M] /root/testmodule/test.ko
BTF [M] /root/testmodule/test.ko
Skipping BTF generation for /root/testmodule/test.ko due to unavailability of vmlinux
make: Leaving directory '/usr/src/kernels/6.12.0-55.9.1.el10_0.x86_64'
The compiler creates an object file (test.o) for each source file (test.c) as an intermediate step before linking them together into the final kernel module (test.ko).
After a successful compilation, /root/testmodule/ contains additional files that relate to the compiled custom kernel module. The compiled module itself is represented by the test.ko file.
Results
- Optional: check the contents of the
/root/testmodule/ directory:
# ls -l /root/testmodule/
total 152
-rw-r--r--. 1 root root 16 Jul 26 08:19 Makefile
-rw-r--r--. 1 root root 25 Jul 26 08:20 modules.order
-rw-r--r--. 1 root root 0 Jul 26 08:20 Module.symvers
-rw-r--r--. 1 root root 224 Jul 26 08:18 test.c
-rw-r--r--. 1 root root 62176 Jul 26 08:20 test.ko
-rw-r--r--. 1 root root 25 Jul 26 08:20 test.mod
-rw-r--r--. 1 root root 849 Jul 26 08:20 test.mod.c
-rw-r--r--. 1 root root 50936 Jul 26 08:20 test.mod.o
-rw-r--r--. 1 root root 12912 Jul 26 08:20 test.o
- Copy the kernel module to the
/lib/modules/$(uname -r)/ directory:
# cp /root/testmodule/test.ko /lib/modules/$(uname -r)/
- Update the modular dependency list:
- Load the kernel module:
insmod /lib/modules/6.12.0-55.9.1.el10_0.x86_64/test.ko
- Verify that the kernel module was successfully loaded:
- Read the latest messages from the kernel ring buffer:
[74422.545004] Hello World
This is a test