Hello students welcome to free linux kernel development course, in this lecture i will show you how to write the hello world linux kernel module.
hello world kernel module is the first linux kernel module tha we should write , just like any other c program hello world, so let us see how to wirte hello world linux kernel module.
Just like any c program have main() function linux kernel modules also have entry point and exit points, but in linux kernel modules we dont have main() function but instead we will use something called as initialization function and cleanup function() these functions are registered using the macros provided by the linux kernel.
Entry function and Exit function in linux kernel modules:
- Initialization function: Initialization function is the firs function that executes when a module is loaded into the kernel. Purpose of initialization function is to allocate resorces, initialize variables, register devices, print debug messages, and so on. return value of initialization function is integer value. 0 – success, non-zero – failure.
- Cleanup function – this function is called when the modules is removed from the kernel. its purpose is to free the allocated resources, unregister devices, cleanup kernel objects, return value of cleanup function is void, it does not return any value.
module_init() and module_exit() function:
| Macro | Purpose |
|---|---|
| module_init() | Registers initialization function |
| module_exit() | Registers cleanup function |
Example:
module_init(init_function);
module_exit(exit_function);
Module Licencing:
every kernel module we develop should use a licence MODUL_LICENSE() this tell what is the licence of the kernel module. below are the few examples of the linux kernel modules:
| License | Meaning |
|---|---|
| GPL | GNU Public License v2 or later |
| GPL v2 | GNU Public License Version 2 |
| Proprietary | Closed-source module |
| Dual BSD/GPL | Dual licensed module |
declaring the license information is mandatory in recent linux versions.
Required Header Files for linux kernel modules:
Just like any other c programming , kernel programming also needs few header files some of them are
- linux/module.h : this is used for modules macros information, module licensing information, module_init(), module_exit() functions.
- linux/kernel.h: used for kernel logging information, prink() function, log level macros.
as you got the basic information let us start writing our first kernel module that is helloworld kernel object- helloworld.ko
1_km.c
#include <linux/kernel.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
static int hello_init_fun(void)
{
printk(KERN_INFO "%s: Init function called\n", __func__);
return 0;
}
static void hello_exit_fun(void)
{
printk(KERN_INFO "%s: Exit function called\n", __func__);
}
module_init(hello_init_fun);
module_exit(hello_exit_fun);
hello world kernel object
Explanation of hello world kernel object:
#include <linux/kernel.h>
#include <linux/module.h>
these are the header files required for our hello world kernel object creation
module_init(hello_init_fun);
module_exit(hello_exit_fun);
above are the apis that are used to initialize and exit the modules so which function is passed as an input to it that function will be executed
MODULE_LICENSE("GPL");
the module is general GPL license.
Init function is always of return type int
exit function is always of void return type.
printk() : this is similar to printf() function but our kernel print function.
KERN_INFO: specifies the log level of the information.
__func__ prints the function name.
Makefile file for kernel module compilation:
Always remember kernel modules can not be compiled by normal gcc, instead to compile the linux kernel modules we should use the kernel build system, create a make file in same directory.
obj-m := 1_km.o
make file for compiling hello world kernel object
obj-m tells the kernel build system to build the lodable kernel modules.
1_km.ko is the module object file , for this lecture as the file name is 1_km.c i am creating object file as 1_km.o in next lecture i will show you how to create our kernel objects with our own name, instead of .c file name.
Kernel build system:
Linux kernel provides a special make file for module compilation, you can find the kernel build system information from
/lib/modules/$(uname -r)/build/Makefile
linux kernel build sytems. below is some sample output
-rw-r--r-- 1 root root 67889 Nov 20 /lib/modules/6.8.0-90-generic/build/Makefile
sample output. so by using the make file we can compile the kernel modules.
How to compile linux kernl modules
To compile the linux kernel modules you should use the linux build system, please follow below approach to comple your linux kernel modules.
make -C /lib/modules/$(uname -r)/build M=${PWD} modules
compiling linux kernel module.
| Option | Meaning |
|---|---|
| -C | Change directory |
| build | Kernel build directory |
| M=${PWD} | Current module directory |
| modules | Build loadable modules
|
Once your compilation is successfull you will see many files in your directory, few of them are
1_km.ko
1_km.o
1_km.mod
1_km.ko –> this is our hello world kernel object file. Great now you have successfully created hello world kernel object file. Now let us discuss how to check module information.
How to check module information
modinfo ./1_km.ko
modinfo command to check kernel module status, the above command shows the below infomation about the hello world linux kernel module, module name, lincence inforamtion of hello world kernel module, version of our hello world kernel object, dependencies of our hello world kernel module. Now we have generated our hello world kernel module file let us see how to load it.
Loading Kernel Module:
To load the kernel module into linux please use below commands.
sudo insmod ./1_km.ko
to load kernel module
lsmod | grep 1_km
to verify the module loading
ls /sys/module/1_km
you can also check our module presense in linux kernel after loading,
dmesg you can use dmesg to see the kernel logs of our module inserting.
Now you have successfully inserted your hello world linux kernel module, now let us discuss how to remove our hello world kernel module.
Removing the kernel module:
now i will show you steps to remove the linux kernel module
sudo rmmod 1_km
steps to remove linux kernel module
dmesg
you can see dmesg to see the kernel logs to see our module removing.
Now if you want to clean the build files which are generated using the build modules step, please run below command
make -C /lib/modules/$(uname -r)/build M=${PWD} clean
this will remove all the module files that are generated,
makefile:
obj-m := 1_km.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
just run make and it will compile and give all the modules, if you give make clean then it will remove all the modules
Secure boot error:
sometimes you may see errors when inserting the kernel modules, so incase if you see below error
insmod: ERROR: could not insert module: Key was rejected by service
this does not mean your module is wrong, it happens because secure boot is enabled in your system. so you can disable your secure boot and reboot the system and then try loading it again. becuase modren linux system enforce UEFI secure boot and only signed linux kernel modules are allowed, our .ko is unassigned so kernel rejects it. below is the solution to disable SECUREBOOT in BIOS:
1 Restart system
2 Press F2 (for Dell systems)
3 Enter BIOS Setup
4 Go to Boot Settings
5 Disable Secure Boot
reboot the system and the kenrel module will be isnerted successfully.
How to check kernel logs:
dmesg | tail
in dmesg you can see the kernel logs of our module init and exit functions
hello_init_fun: Init function called
hello_exit_fun: Exit function called
conclusion:
In this article we have learnt about
what is kernel module
how to create a kernel module
how to insert kernel module
init and exit functions in kernel module
how to remove kernel modules.
we have developed hello world linux kerne module and inserted and removed the kernel module. we have also used tools like lsmod and modinfo to see kernel module information.
Kernel Module Lifecycle
Write Module
|
v
+----------------+
| hello_km.c |
+----------------+
|
v
Compile Module
|
v
make -C /lib/modules/$(uname -r)/build M=${PWD} modules
|
v
+----------------+
| hello_km.ko |
+----------------+
|
v
Load Module
(insmod / modprobe)
|
v
+----------------------+
| module_init() runs |
| Initialization code |
+----------------------+
|
v
Module Active
(Running in Kernel)
|
v
Remove Module
(rmmod)
|
v
+----------------------+
| module_exit() runs |
| Cleanup code |
+----------------------+
|
v
Module Removed
simple kernel module structure
