Linux kernels are monolithic by nature. They have a set of drivers already preinstalled on dispatch.
However there can be new devices such as a WiFi Adapter or Bluetooth Speaker which cannot be paired to the operating system. Now we have two solutions for this.
Either start from scratch, edit and compile the entire kernel. This can be time consuming as even a high configuration system can take 15 to 18 minutes just to debug. For a business this is not viable, unless paid by the hour.
The other is to write a specific module (driver) which can be inserted into the kernel, during runtime. The whole process will take less than 5 minutes. This module is known as Linux Kernel Modules or LKM.
Uses
LKM are used for creating new device drivers or file systems and network packet tracking. The latter is particularly useful in developing firewalls, Intrusion detection system (IDS) or Intrusion prevention system (IPS).
Prerequisite
To create a module, we need to install the Kmod package. This contains the needed libraries to execute
- make (compilation)
- insmod/modprobe (module insertion)
- rmmod (module removal).
All these commands run in privileged mode
Hello World
The beginner program is hello world. Open the text editor and save the file as .c extension. The code is as follows:
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE ("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("This is a test module");
static int start_init (void)
{
printk(KERN_INFO "Hello World\n");
return 0;
}
static void end_exit (void)
{
printk(KERN_INFO "Exiting module");
}
module_init(start_init);
module_exit(end_init);
Explanation
Here there are two functions. The start_init is used to insert the module while end_init remove the same from the kernel. The topmost code contains the header files and developer details.
The make command is used to compile the code and insmod to insert the same.
End Note
Another advantage of LKM is that even if there is an error within the module, we can separate it from the kernel. This prevents the whole system from shutting down due to a glitch.
Comments
Post a Comment