“Hello World” Module:
This is simple kernel module, which when inserted to the Linux kernel, will print the message as “hello world”. And on removing it will print the message as “bye bye world!”. These are the two functions we will support in the “hello world” kernel module.
Sample Example Code for Kernel Module:
static int __init hello_init(void)
printk(KERN_INFO "Hello world\n");
static void __exit hello_exit(void)
printk(KERN_INFO "bye bye world\n");
Sample Makefile to Compile Kernel Module:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Steps to Compile the Kernel Module:
Copy both of the code snippet above to the same directory in the build host as shown below:
Execute the make command as shown below and we will see the logs as below:
make -C /lib/modules/4.15.0-163-generic/build/ M=/home/cienauser/hello-world modules
make: Entering directory '/usr/src/linux-headers-4.15.0-163-generic'
CC [M] /home/cienauser/hello-world/hello-world.o
Building modules, stage 2.
MODPOST 1 modules
LD [M] /home/cienauser/hello-world/hello-world.ko
make: Leaving directory '/usr/src/linux-headers-4.15.0-163-generic'
From the logs above, we can see that we have executed the make command and we will get some files created in the same directory where we have C and Makefile. “hello-world.ko” is the file we should be looking at. This is the kernel object file. This is the module which we will inserting into the Linux kernel.
Steps to Add the Hello-world Module to the Linux Kernel:
Insmod command can be used to insert the kernel module to the Linux kernel. The following is the log which shows the process of using the insmod.
Steps to See the Message Printed by Hello-world:
“dmesg” is the command can be used to see the output of the hello world kernel module. The following is the log which shows the process of using the dmesg command.
[10500712.434672] Hello world
Above is the process of adding the kernel module dynamically. It is compiled outside of the kernel image and at runtime gets added to the kernel image. This way while the kernel is running, we can expand the functionality of the kernel.
Linux provides this way of extending the functions of kernel without bringing down the system. User should be very careful while writing such modules. While unloading the kernel module, user should carefully release all the resource which were allocated when the module was loaded.
Hello World module can also be added as a part of kernel image. If user wants to add the hello world application as a part of kernel image.
The following steps can be followed to achieve it:
- Identify the path in the kernel tree where this needs to be added.
- Modify the KConfig to introduce the new module.
- Modify the Makefile to add the new module.
- Update the config file if the module will be added based on some config file.
How to Compile the Module Which was Added in These 4 Steps.
Kernel Makefile defines the “modules” as build target which can be used to build all the modules in the Linux Kernel. Once user issues the “make modules”, Our hello world/New module also gets compiled and we get the .ko file of the module.
This way of compilation is easy when we have a need to build the full kernel and our module we want to add in the system as loadable module.
We have discussed the simple hello world kernel module and different ways to compile the module.
We have also discussed the ways to add the module to the Linux kernel. We also referred to the example code and makefile for the compilation. Also, we have demonstrated the concepts with the experimental logs.