Jun 20, 2024 by Thibault Debatty | 1018 views
In a previous blog post, I presented how to build your own Linux kernel. This time I will show how to create, compile and load a very simple kernel module…
To compile your kernel module, you’ll need a compile toolchain:
sudo apt install -y build-essential libncurses-dev flex bison libelf-dev libssl-dev
And you’ll also need the headers of the target kernel. If you are using the stock Ubuntu kernel, and want to compile the module for yourself, you can install these headers with:
sudo apt install linux-headers-`uname -r`
These headers are installed in /lib/modules/$(shell uname -r)/build/
.
We can now directly create the source code of your simple module testmod.c
:
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual MIT/GPL");
static int __init testmod_init(void)
{
printk(KERN_INFO "Hi there!\n");
return 0;
}
static void __exit testmod_exit(void)
{
printk(KERN_INFO "Exit!\n");
}
module_init(testmod_init);
module_exit(testmod_exit);
In this simple example:
MODULE_LICENSE
is required;module_init
and module_exit
macros to define the init and exit methods of your module;printk
to output a kernel message.The kernel source tree already contains a Makefile that allows to compile kernel modules (make modules
). This Makefile is also part of the kernel headers (which we installed in the Prerequisites). So we will create a simple Makefile
that:
-C $(HEADERS)
M=$(PWD)
obj-m
variable to only compile our moduleHere is the content of our Makefile
:
PWD := $(shell pwd)
HEADERS := /lib/modules/$(shell uname -r)/build/
obj-m := testmod.o
modules:
make -C $(HEADERS) M=$(PWD) modules
clean:
make -C $(HEADERS) M=$(PWD) clean
We can now compile the module with:
make modules
We can now try to load our module with
sudo insmod testmod.ko
If all went well, the “init” message of our module will appear at the end of dmesg
:
sudo dmesg
Keep in mind that you can only load a kernel module if it was compiled against the headers of the exact same kernel (same version, and same configuration options). If you wish to create an out-of-tree kernel module, you must
You can also unload the module with
sudo rmmod testmod
This is very simple example, but I hope it will help you understand how the Linux kernel works. I will probably supplement this topic with other blog posts about module auto-loading, DKMS and others…
This blog post is licensed under CC BY-SA 4.0