Day05 - Kernel Module Hello World¶
Objective¶
- Build a Linux kernel module
- Load and unload module
- Observe kernel behavior via logs
Environment¶
- Board: Raspberry Pi 5
- OS: Debian (Raspberry Pi OS Lite)
- Architecture: arm64
Step 1 — Create Source Code¶
File: hello.c
#include <linux/module.h>
#include <linux/kernel.h>
static int __init hello_init(void)
{
printk(KERN_INFO "Hello, Kernel Module Loaded!\n");
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_INFO "Goodbye, Kernel Module Unloaded!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Chia-Wei Chien");
MODULE_DESCRIPTION("Simple Hello World Kernel Module");
/* End of File */
Step 2 — Create Makefile¶
obj-m += hello.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
# End of File
Step 3 — Build Module¶
Output:
hello.ko
Step 4 — Insert Module¶
Step 5 — Check Kernel Log¶
Expected:
Step 6 — Remove Module¶
Step 7 — Verify Removal¶
Expected:
Step 8 — Inspect Module¶
Observations¶
- Module is dynamically inserted into kernel
printkoutputs to kernel log- No reboot required
Key Learnings¶
- Kernel modules run in kernel space
.kois a loadable objectinsmod/rmmodcontrol module lifecycle- Debugging relies on
dmesg
Common Issues¶
1. Missing kernel headers¶
Error:
Solution:
2. Permission denied¶
Use:
Next Step¶
- Implement character device driver
- Add read/write interface
- Connect user space with kernel