Monday, March 3, 2014

What is a Loadable Kernel Module?

Modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. 
Example: one type of module is the device driver, which allows the kernel to access hardware connected to the system. 
 Without modules, we would have to build monolithic kernels and add new functionality directly into the kernel image. Besides having larger kernels, this has the disadvantage of requiring us to rebuild and reboot the kernel every time we want new functionality.

C

  • kernel modules are object files that contain kernel code. 
  • They can be loaded and removed from the kernel during run time.
  • They contain unresolved symbols that are linked into the kernel when the module is loaded.
  • kernel modules can only do some of the things that built-in code can do , they do not have access to internal kernel symbols.dep
 Kernel Module Utilities:

lsmod      : lists the modules already loaded into kernel.

rmmod    : Removes or unloads a module one at a time.

insmod    : Insert or load a module.

depmod   : Creates the data base of module dependencies. This is created based         on the information present in /lib/modules/module.dep file.

modprob Inserts a module and its dependencies based on information from modules.depfile.

modinfo  : List the information about the module like author, version tag, parameters etc. 

Writing a simple kernel module:

Here I am explaining how to write a simple kernel module that doesn't have any functionality. 
Every Kernel modules must have at least two functions:
"start" (initialization) function called init_module()
It is called when the module is loaded using insmod into the kernel.

"end" (cleanup) function called cleanup_module()
It is called just before it is removed using rmmod. 


Kmod is a subsystem that allows the loading and unloading of modules.

Issues to be considered in writing a kernel module:
  • Module code should not invoke user space Libraries or API’s or System calls.
  • Modules are free to use kernel data types and GNU-C extensions for linux kernel.
  • Following path contains the list of header files that can be included in module programs./lib/modules/2.6.32.generic/build/include/linux.
Code for Test.c:

#include <linux/module.h>  /* Needed by all modules */
#include <linux/kernel.h>/* Needed for KERN_INFO */
#include <linux/init.h> 
#include <linux/version.h>

MODULE_LICENSE(“GPL”); 
MODULE_DESCRIPTION(“This is a basic test module); 
MODULE_AUTHOR(“xyz");

int init_module(void)
{
printk(KERN_INFO "My first test module loaded.\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO “Test module unloaded\n");
}

Note: Copying the code directly into your source.c file will also copies the invisible characters and finally you will left out with stray errors, i recommend you to type the code and that even becomes a practice.


module.h : module management subsystem, its an interface file for Kmod functions.
kernel.h   : resolves kernel symbol calls, it provides access to global symbol table.
init.h        : describes the sequence of initialization.
version.h : It binds a module to a particular version of kernel.
printk     : printk is printf for kernel programs, as said earlier modules can’t use stdlib due to user space/ kernel space issues. Most of C library is implemented in kernel. with in printk “KERN_INFO” is a macro found in kernel.h that defines the priority to printk logs. There are 8 such macros as shown below.

0- highest priority 7-lowest priority

KERN_EMERG "<0>" /* system is unusable */
KERN_ALERT "<1>" /* action must be taken immediately */
KERN_CRIT "<2>" /* critical conditions */
KERN_ERR "<3>" /* error conditions */
KERN_WARNING "<4>" /* warning conditions */
KERN_NOTICE "<5>" /* normal but significant condition */
KERN_INFO "<6>" /* informational */
KERN_DEBUG "<7>" /* debug-level messages */

MACROS :
      MODULE_LICENSE() : declares the module's licensing
      MODULE_DESCRIPTION() : to describe what the module does
      MODULE_AUTHOR() : declares the module's author
  • In modules the comments are achieved with macros so that information of module sits along with the code so that debugging becomes easy. i.e., comments are not stripped out.
  • License macro is mandatory even if it is free or proprietary.
  • Modules can comprise of any number of functions and data elements which form module body.
Building a Module:
Kernel source has two types of make files:
  1. src/Makefile called as top/primary Makefile
  2. Each branch in kernel source has a Makefile called as kbuild Makefile.
So to build a module we have to write a Makefile that follows the rules followed by kbuild. To learn more on how to compile modules, see file linux/Documentation/kbuild/modules.txt.

Makefile:
obj-m += test.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

from a technical point of view the first line is really necessary, the "all" and "clean" targets were added for convenience, and make sure that after "all:" give a tab and write the command as its a convention in writing Makefile
Now you can compile the module test.c using make command. Here is the list of command I am executing the corresponding output is show in below image highlighted with blue arrow.
Note: You should be the root user or should have requisite permissions to load and unload modules.
$ ls    (to list the files in the current directory)
$ make      ( to build the module, this will generate many files)
$ ls      (to list the files in the current directory)
$ insmod test.ko      ( loading the module)
$ dmesg     ( to see the messages printed by printk)
$ modinfo test.ko      (this display the module information, we have put in macros)
$ rmmod test     ( unloading the module)
$ dmesg      ( to see the messages printed by printk)

Finally you are done...! :-)

2 comments:

  1. I am glad to find amazing information from the blog. Thanks for sharing the information about the Linux kernel modules and more information Please go trough the link : Linux training in Hyderabad

    ReplyDelete
  2. Most powerful blog. Good informative blog. It's very useful site to all. Thanks for the who is posted the good information thanks.
    DevOps Training in Hyderabad

    ReplyDelete