Hello World Module
In this program, you are going to learn
How to write a sample kernel module ?
How to write Makefile to compile driver module ?
How to check if module is loaded successfully ?
How to check if module is unloaded successfully ?
How to check prints from kernel modules ?
How to use below APIs ?
Topics in this section,
init_module
function gets called on insertion of module using insmod commandcleanup_module
function gets called on removal of module using rmmod command
1#include <linux/module.h>
2#include <linux/kernel.h>
3
4MODULE_LICENSE("GPL");
5MODULE_AUTHOR("Linux_usr");
6
7int init_module(void)
8{
9 pr_info("Hello, world\n");
10
11 return 0;
12}
13
14void cleanup_module(void)
15{
16 pr_info("Good bye, world\n");
17}
In this Makefile,
obj-m
indicates the build system to create a separate loadable .ko modulemake -C
informs the make command to look for master make file in the given path$(shell uname -r)
replaces the output ofuname -r
command$(PWD)
replaces it with Present Working Directory
1obj-m += hello.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5clean:
6 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
1$ pwd
2basic_ldd/chapter1_basics/p1_basicModule
3
4
5$ ls
6hello.c Makefile README
7
8
9$ make all
10make -C /lib/modules/6.2.0-32-generic/build M=/home/linux/basic_ldd/chapter1_basics/p1_basicModule modules
11make[1]: Entering directory /usr/src/linux-headers-6.2.0-32-generic
12 CC [M] basic_ldd/chapter1_basics/p1_basicModule/hello.o
13 MODPOST basicl_ldd/chapter1_basics/p1_basicModule/Module.symvers
14 CC [M] basicl_ldd/chapter1_basics/p1_basicModule/hello.mod.o
15 LD [M] basicl_ldd/chapter1_basics/p1_basicModule/hello.ko
16 BTF [M] basicl_ldd/chapter1_basics/p1_basicModule/hello.ko
17make[1]: Leaving directory /usr/src/linux-headers-6.2.0-32-generic
1$ ls -l
2total 240
3-rwxrwxrwx 1 linux linux 233 Sep 11 16:00 hello.c
4-rw-rw-r-- 1 linux linux 108392 Sep 12 12:37 hello.ko
5-rw-rw-r-- 1 linux linux 134 Sep 12 12:37 hello.mod
6-rw-rw-r-- 1 linux linux 891 Sep 12 12:37 hello.mod.c
7-rw-rw-r-- 1 linux linux 93536 Sep 12 12:37 hello.mod.o
8-rw-rw-r-- 1 linux linux 16232 Sep 12 12:37 hello.o
9-rwxrwxrwx 1 linux linux 154 Sep 11 16:00 Makefile
10-rw-rw-r-- 1 linux linux 134 Sep 12 12:37 modules.order
11-rw-rw-r-- 1 linux linux 0 Sep 12 12:37 Module.symvers
12-rwxrwxrwx 1 linux linux 1876 Sep 12 13:12 README
1$ modinfo hello.ko
2filename: basic_ldd/chapter1_basics/p1_basicModule/hello.ko
3author: Linux_usr
4license: GPL
5srcversion: CC6A6FD6CE9DAF87AC2E72B
6depends:
7retpoline: Y
8name: hello
9vermagic: 6.2.0-32-generic SMP preempt mod_unload modversions
1$ sudo insmod ./hello.ko
1$ dmesg
2[67117.450399] Hello, world
1$ lsmod | grep hello
2hello 16384 0
1$ sudo rmmod hello
1$ dmesg
2[67136.900177] Good bye, world
API |
Learning |
---|---|
MODULE_LICENSE |
Tells the kernel what license our module is under |
MODULE_AUTHOR |
Declares the module’s author |
pr_info |
Prints an info-level message |
Command |
Learning |
---|---|
insmod |
Command to insert a kernel module |
rmmod |
Command to remove a kernel module |
lsmod |
Command to display the status of modules in the Linux kernel |
modinfo |
Command to display the information about a Linux Kernel module |
dmesg |
Command to display kernel-related messages |
Other topic
Next Chapter
Other Chapters