Loading and Removing Kernel Module

Computer ProgrammingOperating SystemUnix

The Linux kernel modules can be loaded or removed from the kernel as required. This can be done without recompiling the kernel or rebooting the system and it enhances the functionality of the system.

Without the kernel modules, the operating system would have to include all the systems that provide all anticipated functionalities in the base kernel. This would lead to wastage of memory as most of those systems would not be used often. Also, the users would need to rebuild and reboot the base kernel every time they would require a new functionality.

The kernel modules have a .ko extension and they reside in the /lib/modules//kernel/ directory in the normal Linux system.

The major modules in the Linux kernel modules are: file system drivers, device drivers and system calls.

To insert module into kernel, use the insmod command. The syntax of this command is −

insmod <module_name>.ko

To remove module from kernel, use the rmmod command. A module that is used by a program cannot be removed. The syntax of the rmmod<.i> command is-

rmmod <module_name>.ko

The newly created module can be viewed using the lsmod command. This command displays the loadable kernel modules that are currently loaded in the system as shown below −

Creating and loading a Kernel Module

The steps to create a kernel module are given below −

 Let us first write a example.c program −

 First, add the header files − 

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

Next, add the description of the module and the author details. Do not forget to add the license −

MODULE_DESCRIPTION("Kernel module 1");
MODULE_AUTHOR("Harry");
MODULE_LICENSE("GPL");

Let us now see how to create a kernel module −

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

MODULE_DESCRIPTION("Kernel module 1 ");
MODULE_AUTHOR("Harry");
MODULE_LICENSE("GPL");

static int example_init(void)
{
   pr_debug("Welcome! Good to have you here!\n");
   return 0;
}

static void example_exit(void)
{
   pr_debug("Goodbye!\n");
}

module_init(example_init);
module_exit(example_exit);

The messages generated in the above program are not generated in the console but are saved in the specially reserved memory area. They can be recovered from this memory area by the logging daemon(syslog).

 Let us now see how to compile our kernel module −

 The standard compilation method is kbuild. It uses two files: a Makefile and a Kbuild file for the compilation process. Kbuild is the Linux Kernel Build System.

 For Makefile −

KDIR = home/dept/so2/linux
kbuild:
make -C $(KDIR) M=`pwd`
clean:
make -C $(KDIR) M=`pwd` clean

 As seen in the above example, calling make on the Makefile file results in the make invocation in the kernel source directory i.e. home/employee/so2/linux with reference to the current directory i.e M = `pwd`. This process eventually leads to reading the Kbuild file from the current directory and compiling the module as instructed in this file.

 Now to compile the module −

EXTRA_CFLAGS = -Wall -g

obj-m = example.o

To insert the module −

insmod example.ko

Now, the newly created module can be viewed using the lsmod command. The lsmod command displays the loadable kernel modules that are currently loaded.

$lsmod
demo
glue_helper
ssdhci_acpi
sdhci_cpi
ahci

The output obtained above can be viewed using the dmesg command. This command is used to write the kernel messages in Linux.

$dmesg | tail -2
Welcome! Good to have you here!
Goodbye!

After building the module, the following files are created −

  • example.o − This is the module object file before linking
  • example.mod.c − This file contains the module information
  • example.mod.o − This is the file created after the compilation and linking of example.mod.c
  • modules.order − This provides the order in which two or three modules get linked
  • modules.symvers −  This specifies the symbol versions, if there are any.
  • example.ko − This is the module kernel object file that is created after linking example.o and example.mod.o

Now let us see what we saw above.

Load a Kernel Module

To load the kernel module, the insmod command is used −

$insmod example.ko

Now, let us display the content of the module −

The dmesg command is used to display kernel messages.

# dmesg | tail -2
Welcome! Good to have you here!
Goodbye!
raja
Published on 10-Sep-2018 01:13:02
Advertisements