🔎 Linux Kernel Module - Introduction
How to create a basic Linux Kernel Module.
A LKM is like a .so
library, and can run kernel-land.
Writting your own kernel module will help you to understand how the system works and make you think about malloc
ing in the kernel twice before running the code.
Code
Here is the bare minimum you have to code to get a working LKM:
|
|
Compile
The Makefile I use for my modules:
|
|
Then just run make
. A new my_module.ko
should be here, this is your kernel module.
Install
|
|
In dmesg
you should have a new line containing MODULE loaded!
. Your module is loaded in the kernel.
Uninstall
|
|
In dmesg
you should have a new line containing MODULE unloaded!
. Your module is deleted form the kernel.
MISC
dmesg -C
will clear the dmesg bufferwatch 'dmesg | grep MODULE'
will append the new messages of the module so you don’t have to re-run the command
Afterwords
Starting to create a kernel module is pretty easy, you just have to be careful with the functions you call and how you code them. There are a lot of specific functions for the kernel only, and you must check them out before using them.
In the future I will discuss more about the internals of Linux and why using a kernel module is insanely powerfull and at the same time a pain to code.