Skip to main content

Linux Kernel Module - Kernel Edited

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 kernelThe 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

Popular posts from this blog

Man In The Middle Attack - EavesDropping in the Digital World

    This is a term used to describe a cyber attack where a hacker acts as middleman, during data transfer between user and an application. The app can be a financial or an E commerce website, Software as a Service (SaaS) etc. The hacker can intercept data passed via the browser to the website, and collect sensitive information such as account and credit card details, user login etc. To put it into context, it is like a postman who reads all the letters addressed to you, then reseals the same, on delivery. This way no one suspects a foul play.   Interception This is the first phase of the attack. Here the hacker establishes a connection with the target device whether a PC, Laptop or Smart phone. This is done   through various methods   like   Free WiFi  : Here a WiFi hot spot is set up  which is not password protected. When a user logs in to the network, they are in essence connecting to the hacker's system. Now any data transfer occurs via this co...

X86 vs ARM - The PC War

X86 Vs ARM A Hype?  Recently laptops powered by ARM processors have taken industry by storm. Touted as being highly power efficient, providing 18 hours backup, on as single charge. That too while running multiple program instances. Would this spell an end to the x86 architecture, where Intel and AMD rule. Or will the tables turn around? What's the Difference? Let's start with an x86 based CPU. They feature a single processing unit which can be integrated to other external peripherals like Memory, GPU etc. Here each peripheral has a separate controller, knitted together by address and data BUS. When we look at ARM all the above components (Memory, GPU) are integrated within the main chip. Known commonly as SOC (System on a Chip) . This helps in reduced footprint. But they do come with a drawback. Cannot expand by adding extra peripherals. Coming to complexity, x86 provides a simpler set up, at least for a developer. Here most of the core tasks such as Memory Read/Write, Arithmet...

The Brain within an Embedded System-Difference between Microprocessors and Microcontrollers

To understand the working of an Integrated Circuit, we need to first understand what an Embedded System is.  It is basically a conjunction of Hardware and Software parts. Embedded System is a stripped-down version of a mainstream computer, specializing in a single task. They are connected either as a standalone device, or as part of a larger electrical or mechanical system. Mainly in consumer electronics like fridges or microwaves, they have low power rating and, is economical to build. Embedded system performs complex mathematical calculations at any specific time. This is done thru a Central Processing Unit (CPU) , mostly a Microcontroller or at times, a Microprocessor. A Sensor is used to collect data from the external environment Eg: Temperature, atmospheric pressure etc. This is then fed into an Analog to Digital (ADC) converter. The digital signal thus obtained is stored in memory and, decoded by the CPU.  The output of the operation is then fed to a Digital to Analog...