Repository navigation
Your first embedded LKM
Quentin Delhaye edited this page Apr 9, 2026
·
5 revisions
On your local computer, create a new directory from your project, then create a file with a simple 'Hello world' code.
/* hello.c */
#include <linux/module.h>
#include <linux/init.h>
/* Meta information */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("DLH");
MODULE_DESCRIPTION("Hello world kernel module");
/* __init tells the kernel this function is executed only once at the initialisation of the module. */
static int __init ModuleInit(void) {
/* printk writes to dmesg */
printk(KERN_NOTICE "Hello kernel!\n");
return 0;
}
/* __exit is the same as __init but for unloading the module.*/
static void __exit ModuleExit(void) {
printk(KERN_NOTICE "Goodbye, Kernel :'(\n");
}
/* Define entry point */
module_init(ModuleInit);
/* Define exit point */
module_exit(ModuleExit);Create a Makefile in the same directory:
obj-m += hello.o
KDIR = "/home/dlh/dev/lemon-image/build/6.12.y"
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) cleanKDIR is the path to the linux kernel you should have compiled. It contains the folders arch, block, certs, etc.
If you don't set this variable, make will assume you want to build a module for your local computer, which will not work on the target board.
Compile the module, then send it to the board:
$ make CROSS_COMPILE=arm-none-linux-gnueabihf- ARCH=arm
On the board, test the module:
citro@board:~/LKM$ modinfo hello.ko
filename: /home/citro/LKM/hello.ko
description: Hello world kernel module
author: DLH
license: GPL
depends:
name: hello
vermagic: 6.12.0-citronics-lemon SMP preempt mod_unload modversions ARMv7 p2v8
citro@board:~/LKM$ sudo insmod hello.ko
citro@board:~/LKM$ lsmod | grep hello
hello 12288 0
citro@board:~/LKM$ sudo rmmod hello
citro@board:~/LKM$ sudo dmesg | tail -n 3
[12101.011304] hello: loading out-of-tree module taints kernel.
[12101.011714] Hello kernel!
[12116.336846] Goodbye, Kernel :'(
→ Stuck ? Check the troubleshooting page.
→ More resources.
→ Suggest updates and new topics by submitting an issue.