This blog gives idea to develop simple kernel module. Information contained
here, is based on linux foundation tutorial. This tutorial introduces about
basic kernel module, to write a simple kernel module, compiling the kernel
module and injecting the module in to the kernel. In this tutorial,
we will be dealing with the following set of commands.
*) lsmod
lsmod command is used to list all the kernel modules that are loaded.
*) insmod <modulename>
insmod command will insert the module into kernel.
*) dmesg
dmesg command is used to obtain the kernel logs.
*) modinfo <modulename>
modinfo provides information about the module such as author, license,
version and description.
*) rmmod <modulename>
rmmod command removes the specified command from the kernel.
Now its time to write your simple kernel module which says
*) "Loaded test module into kernel successfully" upon loading
*) "Unloaded test module from kernel successfully" upon unloading.
Open your favorite editor (vim is my ravorite)
and type in the following code:
/**
* Simple kernel module program, which outputs some string when
* loaded into kernel.
* Copyright (C) 2013 Sagar Gugwad <saagar.gugwad@gmail.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* opinion) any later version.
*
* Derived from usb_tutorial driver
* Copyright (C) 2013 Greg Kroah-Hartman, Linux Foundation @ linux foundation.org
*
* V0.1 (mh) Initial version
*/
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/module.h>
static int __init gotemp_init(void)
{
printk(KERN_INFO "Loaded test module into kernel successfully");
return 0;
}
static void __exit gotemp_exit(void)
{
printk(KERN_INFO "Unloaded test module from kernel successfully");
return 0;
}
module_init(gotemp_init);
module_exit(gotemp_exit);
MODULE_AUTHOR("Saagar Gugwad");
MODULE_DESCRIPTION("Test device driver");
MODULE_LICENSE("GPL");
The function gotemp_init will be called whenever the module is loaded into
kernel. The corresponding message passed for printk method will be logged.
KERN_INFO is used to debug the moduke by printing messages. We can use
other possible macros which are:
*) KERN_EMERG
*) KERN_ALERT
*) KERN_CRIT
*) KERN_ERR
*) KERN_WARNING
*) KERN_NOTICE
*) KERN_DEBUG
Similarly, gotemp-exit will be called, whenever the module is exited from the
kernel. All the logged messages can be obatained by dmesg command.
Save the file with <yourfile>.c.
Now it is time to compile our code. Kernel module compilation is done
using the make utility. The make command defines part of the kernel module to
be re-compiled. Open your editor type in the following code:
obj-m := <yourfile>.o
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KERNELDIR) M=$(PWD)
obj-m specifies the kernel module to be recompiled. We specify the make command
with kernel directory to be used to re-compile and current module directory as
arguments. Save the file as Makefile.
Now type in the make command which run the instructions specified within the
Makefile. It will generate yourfile.ko kernel object which can be loaded in
to the kernel. You can load your module into kernel using
$ sudo insmod yourfile.ko
To see the all the loaded modules you can type in
$ sudo lsmod
You should be able to see the your module name.
$ dmesg
will show the logs by kernel, upon successfully inserting the module, you should be
able to see "Loaded test module into kernel successfully".
Now to remove the module inserted, we can run
$ sudo rmmod yourfile.ko
which will unload the module, within the kernel module you should be able to see
the message "Unloaded test module from kernel successfully".
Thats it you just wrote your first kernel module !
No comments:
Post a Comment