Wednesday, January 11, 2012

Basic Linux Hello World Kernel module

Step 1: vi hello.c


#include
#include
#include

static int hello_init(void)
{
printk(KERN_ALERT "Hello World");
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye World");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR(RICHIE);


Step 2: vi Makefile

obj-m := hello-kernel.o
hello-kernel-objs := hello.o

Step 3: make -C /lib/modules/`uname -r`/build M=`pwd`
You should see hello-kernel.ko in your current directory, by now.

Step 4: sudo /sbin/insmod hello-kernel.ko

Step 5: dmesg

You should see "Hello World" here.

Step 6: lsmod | grep hello-kernel

Step 7: modinfo hello-kernel.ko

Step 8: sudo /sbin/rmmod hello-kernel.ko

Step 9: dmesg

You should see "Goodbye World" here.

No comments: