Compiling Linux kernel modules

Compiling kernel modules(this writing is with Ubuntu distro) ==============================
1) Getting Linux (simple way) get any Linux distribution(for beginners i recommend Ubuntu which is easy for install)
1.1) if you are running Windows and want learn Linux the best way is to begin with Virtualized linux with Vmawre player you can get Vmware player from http://www.vmware.com/vmwarestore/ by creating user account
1.2) download Vmware player(it is free) and install under windows. download linux image(.iso image) of your choice distribution; if you choose Ubuntu image it can downloaded from
http://www.ubuntu.com/download/ubuntu/download 1.3) install Linux with Vmware player once you install vmware player and run it you will see option shown in below picture; select option create new virtual machine to install Linux
once you choose create new virtual machine you find below screen

use the browse option and select the Linux imag
e downloaded(place where you stored it) if CD is available choose from CD; job done with install
2) Installing headers now you are done with getting Linux now you need to download Linux headers for compiling linux kernel modules using following command to get required headers for your kernel version sudo apt-get install kernel-headers-(kernel_version)
you can find your kernel version using command uname -r

3) Sample module

write a sample module (you can get sample example from any book or web)save it in some directory
just for example :
------------------------------------------------------------
shiv@ubuntu:~/test$ cat hello_user.c
#include "linux/init.h" /*(use angle brackets here if it doesn't work)*/
#include "linux/module.h"


MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
printk(KERN_ALERT "Hello good try keep it up\n");
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye .. take care\n");
}

module_init(hello_init);
module_exit(hello_exit);
---------------------------------------------------

4) Makefile
create a file, you need to save following lines into file name Makefile (called make file) both .c and Makefile must be in same directory to work below example

shiv@ubuntu:~/test$ cat Makefile
#--------------------------------------------------------------
obj-m += hello_user.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
#--------------------------------------------------------------
shiv@ubuntu:~/test$
5) Compiling
give the command make to compile the module of your
once you give make command you see following output

shiv@ubuntu:~/test$ make
make -C /lib/modules/2.6.38-8-generic/build M=/home/shiv/test modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.38-8-generic'
CC [M] /home/shiv/test/hello_user.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/shiv/test/hello_user.mod.o
LD [M] /home/shiv/test/hello_user.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.38-8-generic'

6) testing
you can test module by using insmod (to insert a module) and rmmod(to remove module from kernel) commands explained below
--------------------------------------------------------------
shiv@ubuntu:~/test$ *** After compilation ***
shiv@ubuntu:~/test$ ls
hello_user.c hello_user.mod.c hello_user.o modules.order
hello_user.ko hello_user.mod.o Makefile Module.symvers

***Now module is ready to test, it only possible by root ***
*** if not root and you know root password you can use sudo ***
*** lets test it by inserting module using insmod command ***
shiv@ubuntu:~/test$
shiv@ubuntu:~/test$ sudo insmod ./hello_user.ko
shiv@ubuntu:~/test$
*** done with running module o/p would have logged you can see
using dmesg command ***
*** lets see it is inserted or not ***

shiv@ubuntu:~/test$ lsmod | grep hello
hello_user 12448 0
( yes it is found so module is run successfully)
shiv@ubuntu:~/test$ dmesg | grep Hello
[13262.348015] Hello good try keep it up

shiv@ubuntu:~/test$ *** removing module from kernel ***
shiv@ubuntu:~/test$ sudo rmmod ./hello_user.ko
shiv@ubuntu:~/test$ dmesg | grep Goodbye
[13462.741548] Goodbye .. take care

shiv@ubuntu:~/test$ lsmod | grep hello
shiv@ubuntu:~/test$

-----------------------------------------------------------------



good reference
http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html

Comments

Popular posts from this blog

Kind of malware and Names for malicious code

Know tracers mostly available at Linux

cases of context switch