What is a linux kernel modeule ?

Linux kernel module is a program that can be used to extend the functionality of the linux kernel without the need to rebuild and reboot the kernel everytime someone wants some new functionality. It can be loaded and unloaded into the kernel whenever the user wants.


How to look for current loaded modules in the kernel?

You can look for the current loaded module in the linux kernel by using the command

$ lsmod  

Under the hood it gets information by reading the file /proc/modules. You can also get the list of current loaded modules in the kernel by reading the /proc/modules

$ cat /proc/modules

Hello world! kernel module.

Today, we will code our own basic kernel module and load into the kernel.
As the modules are written in C language, you need to know the C language before getting started.

Code:

hello_world.c

#include <linux/module.h> 
#include <linux/kernel.h> 
#include <linux/init.h> 

#define DRIVER_AUTHOR "youhaveme"
#define DRIVER_DESC "Test Driver"
#define LICENSE "GPL"

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

static void __exit exitmod(void)
{
    printk(KERN_INFO "Goodbye\n");
}

module_init(initmod);
module_exit(exitmod);


MODULE_LICENSE(LICENSE);
MODULE_AUTHOR(DRIVER_AUTHOR); 
MODULE_DESCRIPTION(DRIVER_DESC);

Explaination:

#include <linux/module.h> -> this import is required by all the kernel modules #include <linux/kernel.h> -> this import is required for KERN_INFO
#include <linux/init.h> -> this import is for the __init and __exit macros

#define LICENSE "GPL" -> The line defines the license we are using for our drivers

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

The initmod function is called when the module is loaded into the kernel. You can name this function anything.
Notice that printk is used instead of printf. printk does not communicate information to the user, it is used to log information and give warnings.

static void __exit exitmod(void)
{
    printk(KERN_INFO "Goodbye\n");
}

The exitmod function is called when the module is unloaded from the kernel. Its return type must be void.

module_init(initmod);
module_exit(exitmod);

These macros are defined in linux/init.h and are used to call the functions at the time of loading and unloading the module in the kernel.

Compiling and loading the kernel module:

Kernel modules are not compiled like regular userspace C programs. A Makefile is required with compilation instructions to complie the module.

Makefile

#Makefile

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

After saving the Makefile run the command below to build the module

$ make

You will see a bunch of files created but our file of interest is hello_world.ko. It is the binary of our kernel module source file.

Now for loading the app into the kernel run the command as root:

$ insmod ./hello_world.ko

And boom you got your own first kernel module loaded into the kernel. Run the lsmod command to view the list of current loaded kernel modules, you will notice hello_world in the list.

For unloading the module from the kernel, run the command as root:

$ rmmod ./hello_world