| by Arround The Web | No comments

“Hello World” Kernel Module

This is the article for the person who is just starting with the Linux kernel modules. We will employ the sample example code for this module and the makefile to be used to compile the kernel module. Later, we will go through the steps to execute the kernel module and to view the output from the “hello world” 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:

#include <linux/module.h>  
#include <linux/kernel.h>  
#include <linux/init.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Hello world!");

static int __init hello_init(void)
{
    printk(KERN_INFO "Hello world\n");
    return 0;
}

static void __exit hello_exit(void)
{
    printk(KERN_INFO "bye bye world\n");
}

module_init(hello_init);
module_exit(hello_exit);

Sample Makefile to Compile Kernel Module:

obj-m = hello-world.o
all:
    make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
clean:
    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:

sushilrathore-2:~/hello-world$ ls
hello-world.c  Makefile
sushilrathore-2:~/hello-world$

Execute the make command as shown below and we will see the logs as below:

sushilrathore-2:~/hello-world$ make                                                                    
make -C /lib/modules/4.15.0-163-generic/build/ M=/home/cienauser/hello-world modules
make[1]: 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
  CC      /home/cienauser/hello-world/hello-world.mod.o
  LD [M]  /home/cienauser/hello-world/hello-world.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-163-generic'
sushilrathore-2:~/hello-world$

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.

sushilrathore-2:~/hello-world$ sudo insmod hello-world.ko
sushilrathore-2:~/hello-world$

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.

sushilrathore-2:~/hello-world$ sudo dmesg | grep -i hello
[10500712.434672] Hello world
sushilrathore-2:~/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:

  1. Identify the path in the kernel tree where this needs to be added.
  2. Modify the KConfig to introduce the new module.
  3. Modify the Makefile to add the new module.
  4. 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.

Conclusion

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.

Share Button

Source: linuxhint.com

Leave a Reply