Kernel Programming basics
Never interested in learning Kernel Programming until I found some useful material here.
1. This is a simple “Hello World” Kernel Module, we will see how we can load this module into Linux Kernel.
stephen@stephen-desktop:~$ cat hello.c
#include
/* Needed by all modules */
#include
/* Needed for KERN_INFO */
#include
/* Needed for the macros */
static int __init hello_2_init(void)
{
printk(KERN_INFO “Hello, world 2\n”);
return 0;
}
static void __exit hello_2_exit(void)
{
printk(KERN_INFO “Goodbye, world 2\n”);
}
module_init(hello_2_init);
module_exit(hello_2_exit);
2. Kernel Modules need to be compiled with certain gcc options, but we need not worry about those right now,
Here is the simple Makefile for our Kernel Module, we can use this to compile our “Hello World” module.
stephen@stephen-desktop:~$ cat Makefile
obj-m = hello.o
KVERSION = $(SHELL uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
3. Just do a make output would be something similar to this.
stephen@stephen-desktop:~$ make
make -C /lib/modules/2.6.20-15-generic/build M=/home/stephen modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.20-15-generic’
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/usr/src/linux-headers-2.6.20-15-generic’
4. If you list contents on the directory where you did make, you will find all these files
-rw-r–r– 1 stephen stephen 169 2007-08-18 22:55 Makefile
-rw-r–r– 1 stephen stephen 391 2007-08-18 22:56 hello.c
-rw-r–r– 1 stephen stephen 0 2007-08-18 22:56 Module.symvers
-rw-r–r– 1 stephen stephen 36408 2007-08-18 22:56 hello.o
-rw-r–r– 1 stephen stephen 660 2007-08-18 22:56 hello.mod.c
-rw-r–r– 1 stephen stephen 42628 2007-08-18 22:56 hello.mod.o
-rw-r–r– 1 stephen stephen 78024 2007-08-18 22:56 hello.ko
5. Before we load our Kernel Module, we need to login as super user (root).
stephen@stephen-desktop:~$ su -
Password:
root@stephen-desktop:~# id
uid=0(root) gid=0(root) groups=0(root)
6. We are all set to load our first Kernel Module, just do a insmod with our .ko file
root@stephen-desktop:/home/stephen# insmod hello.ko
7. How do we know that our Hello World module is attached with Kernel?
root@stephen-desktop:/home/stephen# lsmod | grep hello
hello 2432 0
(or)
root@stephen-desktop:/home/stephen# lsmod | less
Module Size Used by
hello 2432 0
binfmt_misc 12680 1
rfcomm 40856 0
l2cap 25728 5 rfcomm
bluetooth 55908 4 rfcomm,l2cap
ppdev 10116 0
Above is the snip of my lsmod command
8. We can also check the /var/log/messages file
root@stephen-desktop:/home/stephen# tail -f /var/log/messages
Aug 19 09:21:23 stephen-desktop gconfd (stephen-4866): Resolved address
“xml:readonly:/var/lib/gconf/defaults” to a read-only configuration source
at position 4
Aug 19 09:21:41 stephen-desktop gconfd (stephen-4866): Resolved address
“xml:readwrite:/home/stephen/.gconf” to a writable configuration source at
position 0
Aug 19 09:26:32 stephen-desktop syslogd 1.4.1#20ubuntu4: restart.
Aug 19 09:34:51 stephen-desktop kernel: [ 966.096866] hello: module
license ‘unspecified’ taints kernel.
Aug 19 09:34:51 stephen-desktop kernel: [ 966.111899] Hello, world 2
9. How do we remove the Hello World Module from kernel?
root@stephen-desktop:/home/stephen# rmmod hello
root@stephen-desktop:/home/stephen# lsmod | grep hello
root@stephen-desktop:/home/stephen# exit
stephen@stephen-desktop:~$
Wonderful right, if you are interested in this article then keep exploring Kernel Programming,
and try to build your own drivers !!!

Hi!!!! This is Stephen Rengan, did my schooling in St. Annes Matriculation HSC, then went ahead with my Bachelors degree with Computer Science in Crescent Engineering College, Anna University. This sums up 21 yeasrs of my life :D One open question is, still wondering whether all those years were spent useful or not? Very much debatable !!!
Had to move forward with IT Industry, spent 1.5 years in Bangalore HCL, then moved to Oracle Hyderabad. Worked for 1.5 years there in Hyd, such a good place. Got internal transfer to a new Team in Oracle Bangalore.