linux poison RSS
linux poison Email

Write your own kernel module and insert it into running kernel

So, you want to write a kernel module. You know C, you've written a few normal programs to run as processes, and now you want to get to where the real action is, to where a single wild pointer can wipe out your file system and a core dump means a reboot.

kernel 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. For example, one type of module is the device driver, which allows the kernel to access hardware connected to the system.

1) Check if you have all the required tools and lib (Linux Kernel headers)for building the kernel modules for this you need - kernel-headers, you can check if it's install or not by using command: # rpm -qa | grep kernel-headers, if installed, Typing the following command ...

# ls -d /lib/modules/$(uname -r)/build 
Output (OpenSuse 11.1) : /lib/modules/2.6.27.7-9-pae/build 

else install the kernel-header from your installation CD/DVD

2) Now, start with the famous "hello World" program, create c file call - hello.c

/*************************************************************/
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
    printk(KERN_INFO "Hello world.\n");
    return 0;
}
void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye world\n");
}

/*************************************************************/

3) Create the make file: vi Makefile

obj-m += hello.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

save and close

4) Now compile the module to create - hello.ko using command : # make and you should see something like ...

make -C /lib/modules/2.6.27.7-9-pae/build M=/root/kernel modules
make[1]: Entering directory `/usr/src/linux-2.6.27.7-9-obj/i386/pae'
make -C ../../../linux-2.6.27.7-9 O=/usr/src/linux-2.6.27.7-9-obj/i386/pae/. modules
  CC [M]  /root/kernel/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /root/kernel/hello.mod.o
  LD [M]  /root/kernel/hello.ko
make[1]: Leaving directory `/usr/src/linux-2.6.27.7-9-obj/i386/pae'

you should see lot of news files get created inside the directory, check it "ls" command

5) Load/insert our kernel module into the running kernel (hello.ko) using command: # insmod hello.ko

6) Now let check the information about our module (hello.ko) using command:
# modinfo hello.ko, you should see something like ...

filename:       hello.ko
srcversion:     A59CC2D814343F3CA40CADF
depends:        built-in
vermagic:       2.6.27.7-9-pae SMP mod_unload modversions 586

7) To list the module currently running inside the kernel : # lsmod

8) To remove the "hello.ko" module: # rmmod hello.ko

9) Check the output of our module by looking at the /var/log/message file, you should find the entries like ..
Jan 11 12:31:48 poison kernel: Hello world.
Jan 11 12:32:26 poison kernel: Goodbye world


2 comments:

Nitiket said...

stepwise instructions for conversion of .ko file from .o file using modpost in linux fedora

Nitiket
nitiket_m@rediffmail.com

Rishabh said...

i am using linux mint , but i cant find "message" in

/var/log/ .How can i chk the output of the kernel module inserted .

Post a Comment

Related Posts with Thumbnails