Outreachy – 0x02

This is an interesting story. We all need to start from somewhere. It doesn’t matter how long or short our experience is or how far it goes or shallow the waters we have dived are, it’s always new space to learn.

This is one of the most important learning points that everybody should have as part of their life story. Here is my story. Re-visiting the basics and creating a hello world app but this time not as a toy printf() program, C/C++. No nothing like that. A real kernel C program that greets the world with happiness, exciting and new baby life!

I am very proud of my baby and I want to present it to you. It may seem silly to the more experienced of you but actually it’s a very important stone for everyone who wants to go deeper into kernel programming and it’s teaching one or two (or many more if you have the perspective lessons).

Without further ado here it is!

/* hello.c - simplest of all the kernel modules */

#include <linux/module.h> /* every module needs that */
#include <linux/kernel.h> /* printk KERN_INFO needs this */

int init_module(void) {
printk(KERN_INFO "Happy kernel days!\n");
/* non 0: init_module failed; module can't be loaded */
return 0;

void cleanup_module(void) {
printk(KERN_INFO "Sad Kernel Days\n");

Kernel modules must have at least two functions: a “start” (initialization) function called init_module() which is called when the module is insmoded into the kernel, and an “end” (cleanup) function called cleanup_module() which is called just before it is rmmoded.

To compile this you will need a Makefile similar to the following:

obj-m += hello.o 

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

That’s the minimal Makefile ready to compile your module. After that you only need to run make and behold! Here’s your first amazing module.

The $(shell uname -r) is a useful call to return the current kernel build version — this ensures a degree of portability for the Makefile. The -C option switches the directory to the kernel directory before performing any make tasks. The M=$(PWD) variable assignment tells the make command where the actual project files exist. The modules target is the default target for external kernel modules. An alternative target is modules_install which would install the module (the make command would have to be executed with superuser permissions and the module installation path is required).

This should generate a .ko file. To load it into the kernel you run insmod hello.ko When you want to remove this module just run rmmod hello.ko

That was it right? Pretty easy and sweet you would say but this can teach you many lessons that you will find useful in the future (currently it helps me understand how to approach functions inside the Hypervisor Kernel Driver, understand the compilation, load and use printk to debug a kernel driver!)

Good luck and ping me if I can help some how 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s