Set Up Kernel Module Dev Environment
2026-05-06, Wed
Exmaple code in the venerable book Linux Device Drivers, Third Edition1, 2 could not run in current kernel kernel versions, hence this post for setting up a proper environment for module development.
1. Preparation
First, we need to install the kernel headers files needed
sudo dnf install kernel-devel
Then create a sample project directory and create file
.clangd3 with following content:
# ref: ~/Workspace/SampleProject/.clangd
# The content format is YAML
CompileFlags:
Add:
- "-I/usr/src/kernels/6.8.9-100.fc38.aarch64/include"
This file is used for LSP Server clangd to recognize additional header files.
The actual include path is /usr/src/kernel/$(uname -r)/include.
2. The Code
Now it's time to write down our first module:
// ref: hello_module.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("MEC");
MODULE_DESCRIPTION("A simple hello world module");
static int __init hello_init(void) {
printk(KERN_INFO "Hello, kernel world!\n");
return 0; // Success
}
static void __exit hello_exit(void) {
printk(KERN_INFO "Goodbye, kernel world!\n");
}
module_init(hello_init);
module_exit(hello_exit);
along with the Makefile:
obj-m += hello_module.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
To build this custom external module, run make.
3. The Module
Before we do anything further with this module, run sudo dmesg -w to
see live update of message output from printk from code. Now try
sudo insmod hello_module.koto install the module, andlsmod | grep hello_moduleto see its current status, andsudo rmmod hello_moduleto remove the module from kernel
4. References
See these docs for more info:
- Kernel Documentation: Building External Modules https://docs.kernel.org/kbuild/modules.html
Footnotes:
LWN.net: Linux Device Drivers, Third Edition https://lwn.net/Kernel/LDD3/
O'Reilly: Linux Deivce Drivers https://www.oreilly.com/openbook/linuxdrive3/book/
clangd doc: Configuration -> Files https://clangd.llvm.org/config.html#files