“Hello World” Kernel Module
“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:
Sample Makefile to Compile Kernel Module:
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:
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.
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.