Sunday, October 26, 2008

"Hello World" kernel module

An Environment notes:
My kernel's module test environment is fedora core 9 with kernel version of
In order to compile my module I had to download the development package of this kernel version and install it on my build/debug machine :

Now, the module source code :

// Start of code

#include <linux/init.h>

#include <linux/module.h>

// The following macro's can be found at the kernel include file linux/module.h
// with some other as MODULE_INFO, MODULE_ALIAS, MODULE_VERSION ....

MODULE_LICENSE("DUAL BSD/GPL"); // license applies to its code
MODULE_AUTHOR("Ofer Koren"); // Code author
MODULE_DESCRIPTION("hello-world module"); // Module description

// My “started” print function

void hello_func(void)
printk (KERN_ALERT "Hello World strated :-]\n");

// Module's initialization function
// The __init mean that this function can be dropped from the memory
// after the module is loaded

static int __init start_hello(void)
return 0;

// Module's exit function
// The __exit mean that this code should be called upon module exit only
static void __exit stop_hello(void)
printk(KERN_ALERT "Hello-world was terminated\n");

// The next two macros are mandatory, this way we declare
// our init and exit functions

// End of code

Some notes regarding printk:

printk is actually the “printf” of the kernel space (which we cannot use since the kernel ir running without the C libs), For each printk calling we can declare the output priority using the following defines in the kernel.h header:

#define KERN_EMERG      "<0>"   /* system is unusable                   */

#define KERN_ALERT "<1>" /* action must be taken immediately */
#define KERN_CRIT "<2>" /* critical conditions */
#define KERN_ERR "<3>" /* error conditions */
#define KERN_WARNING "<4>" /* warning conditions */
#define KERN_NOTICE "<5>" /* normal but significant condition */
#define KERN_INFO "<6>" /* informational */
#define KERN_DEBUG "<7>" /* debug-level messages */
In order to compile the above code, Here's the Makefile:

#Start of Makefile

# The sourc file to compile
obj-m += hello.o
# The kernel header files
# Current directory
PWD= $(shell pwd)
# Make command

$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean

# Since the make start by changing the current directory to the KERNELDIR
# path, The M option cause the makefile to move back to our directory before
# its trying to build out module.

#End of Makefile

Module installation:

Since the only thing our module is (currently) doing is to print its message, we will try to install it and capture its outputs, this can be done using the dmesg function or going directly to the /var/log/messages file (I'm usually running the “tail -f /var/log/messages” command on separate console) , now when we will install the module (go to the file location and type “insmod hello.ko”), We will see the following output :
Hello World strated :-]

and while removing the module (“rmmod hello”):
Hello-world was terminated