Thursday, 25 June 2015

Linux Kernel Hello World module Easy Steps

I have observed people like me struggled very much while writing their first Linux kernel module. Most of them leave in the middle due to a lot of hectics. For them, I am writing in simple english how to write you first LKM i.e. Linux Kernel Module.

Following is the prerequisite of  LKM:

For Fedora/CentOS:

Before starting the kernel compilation, just make sure the kernel version you are having. 

# uname -r
3.17.4-301.fc21.x86_64

Now, with the same result append it with following command like:
# yum install kernel-devel-3.17.4-301.fc21.x86_64

and this will install the package for your current kernel version.

For Ubuntu/Debian (On experimentation basis):

# apt-get install build-essential linux-headers-$(uname -r)


additional packages, you require:

For Fedora/Redhat/CentOS

# yum install gcc           - To  compile the make file
# yum install rsyslog     -  To see logs of result

For Ubuntu/Debian
# apt-get install gcc          
# apt-get install rsyslog


Remember to do Kernel related operation most possibly as non-root user.

Create a directory, say, LKP

$ cd Documents/LKP/

$ touch hello.c

The hello.c should be like this:

 #include <linux/module.h>    /* Needed by all modules */  
 #include <linux/kernel.h>    /* Needed for KERN_LOGLEVEL_MESSAGES */  
 #include <linux/init.h>     /* Needed for the macros */  
 MODULE_LICENSE("GPL");  
 MODULE_AUTHOR("Mayur Patil");  
 MODULE_DESCRIPTION("Hello World module");  
 MODULE_VERSION("v0.1");  
 static int __init hello_start(void){  
 printk(KERN_INFO "Loading hello module...\n");  
 printk(KERN_INFO "Hello world\n");  
 return 0;  
 }  
 static void __exit hello_end(void){  
 printk(KERN_DEBUG "End of Hello World Kernel Module\n");  
 printk(KERN_DEBUG "DEBUG IS SUCCESSFUL\n");  
 }  
 module_init(hello_start);  
 module_exit(hello_end);  



Be careful regarding the "Makefile" (yes, the name must be as it is as I typed)

Next Important thing is give tabs rather than spaces.

ifneq ($(KERNELRELEASE),) 
obj-m := hello.o
else
<tab>KERNEL_SOURCE := /usr/src/kernels/3.17.4-301.fc21.x86_64 
<tab>PWD := $(shell pwd)

default: 
<tab>${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=$(PWD) modules  
clean:
<tab>${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=$(PWD) clean

endif


and its contents should be copy paste from here and only change the path of Linux Kernel source tree.

Now to see kernel messages we need the log levels.

KERN_INFO       "6"      Informational message
                                    e.g. startup information at driver initialization    




KERN_DEBUG    "7"     Debug messages



Now login as root user, give following command:

# echo "7" > /proc/sys/kernel/printk
# cat /proc/sys/kernel/printk
7          4         1                7

Meaning of it is:

7          4         1                7
current    default       minimum    boot-time-default

Insert module
#insmod hello.ko

Whether module loaded or not check with:

# lsmod | less

Now check in logs whether message is appeared,

# cat /var/log/messages
or
# tail -f /var/log/messages

If you want to only debug related messages;

#dmesg

To clear,

# dmesg -c


Now, time to remove your module i.e. unloading

# rmmod hello


Check again for message

# dmesg

Hey...... You have taken a step towards Linux Kernel Development !

Thanks !!

No comments:

Post a Comment