Adding Hello World Kernel Module
In this section, you are going to learn
How to write kernel module ?
How to write Makefile for kernel module ?
How to load the kernel module ?
How to unload the kernel module ?
How to check the output the kernel module ?
Let us answer few basic questions about kernel modules
What is Kernel ?
See Answer
Core component of OS that manages system resources.
Acts as a bridge between hardware and user level applications.
What is use of Kernel Module ?
See Answer
Kernel Module is a piece of code that can be dynamically loaded and unloaded into Linux Kernel.
How to load the kernel module into Linux Kernel ?
See Answer
insmod command is used to load the kernel module into linux kernel.
* sudo insmod <MODULE_NAME>
How to unload the kernel module from Linux Kernel ?
See Answer
rmmod command is used to unload the kernel module from linux kernel.
* sudo rmmmod <MODULE_NAME>
How to check whether the kernel module is loaded or not?
See Answer
lsmod is used to list out all the loaded modules in linux kernel.
How to check the output of the kernel module ?
See Answer
dmesg command is used to display all the kernel related message from kernel ring buffer.
sudo dmesg
What modinfo command does ?
See Answer
It provides about a kernel module, including its parameters, description, license, author.
modinfo <MODULE_NAME>
What is MODULE_LICENSE ?
See Answer
MODULE_LICENSE specifies the licensing terms for kernel module.
What is MODULE_AUTHOR ?
See Answer
MODULE_AUTHOR allows you to specify the author of kernel module.
What is MODULE_DESCRIPTION ?
See Answer
MODULE_DESCRIPTION provides description about the kernel module.
What is MODULE_VERSION ?
See Answer
MODULE_VERSION gives the version of the kernel module.
Let us now explore it in depth !
1#include <linux/init.h>
2#include <linux/module.h>
3
4MODULE_LICENSE("GPL");
5MODULE_AUTHOR("ABC");
6MODULE_DESCRIPTION("A simple hello world kernel module");
7MODULE_VERSION("0.1");
8
9static int __init hello_init(void)
10{
11 printk(KERN_INFO "Hello, World module loaded\n");
12 return 0;
13}
14
15static void __exit hello_exit(void)
16{
17 printk(KERN_INFO "Hello, World module unloaded\n");
18}
19
20module_init(hello_init);
21module_exit(hello_exit);
make -C /lib/modules/$(uname -r)/build M=$(PWD) modules
sudo insmod ./helloworld.ko
$ dmesg
[10472.955996] Hello, World module loaded
$ lsmod | grep helloworld
helloworld 16384 0
$ sudo rmmod ./helloworld
$ dmesg
[10516.629413] Hello, World module unloaded
1#include <linux/init.h>
2#include <linux/module.h>
3
4MODULE_LICENSE("GPL");
5MODULE_AUTHOR("ABC");
6MODULE_DESCRIPTION("A simple hello world kernel module");
7MODULE_VERSION("0.1");
8
9static int __init hello_init(void)
10{
11 printk(KERN_INFO "Hello, World module loaded\n");
12 return 0;
13}
14
15static void __exit hello_exit(void)
16{
17 printk(KERN_INFO "Hello, World module unloaded\n");
18}
19
20module_init(hello_init);
21module_exit(hello_exit);
1obj-m += helloworld.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5
6clean:
7 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
make
sudo insmod ./helloworld.ko
$ dmesg
[10472.955996] Hello, World module loaded
$ lsmod | grep helloworld
helloworld 16384 0
$ sudo rmmod ./helloworld
$ dmesg
[10516.629413] Hello, World module unloaded
Other topics of linux x86
Current Module
Previous Module
Next Module
Other Modules