“Hello World” Loadable Kernel Module

A Loadable Kernel Module (LKM) allows modification or extension of a Unix-like operating system’s kernel without the need to recompile or reboot the machine. LKM functionality has been available to Linux users since 2.6, and similar functionality is available in BSD, OSX and most Unix variants.

The following tutorial outlines the process of writing, compiling and inserting a “Hello World” LKM into your running kernel. An archive of the source files for this post are available here. I performed the following steps on computers running Ubuntu 11.10 and 12.10, but the same process should work (with minor modifications) on any Linux box.

How to Create an LKM:

  1. Install and prepare module-assistant.
  2. Create hello.c and the Makefile (get both here).
  3. Compile the code.
  4. Insert the compiled module into the running kernel.
  5. Remove the module when you’re finished.

1. Install and prepare the kernel headers

Debian and Ubuntu both provide provide module-assistant, a convenient package that contains all you need to write your own LKM. Install and configure it with the following command:

module-assistant doesn’t do anything too fancy. It’s just a front-end that manages kernel source packages. The following command would also install the packages we need:

 2. Create the source and the Makefile for your LKM

Here is the C source code for the hello world LKM, hello.c:

The function given as an argument to module_init runs when the LKM is inserted into the kernel. Since it is given as the argument to module_exit, hello_cleanup() runs when the LKM is removed.

Here is the source of the Makefile:

 2. Compile the code

To compile the code, go to your project directory (available as this archive/the one containing hello.c and the Makefile) and type “make.”

 4. Insert the compiled module into the running kernel.

To insert your LKM into the running kernel, use insmod.

Our LKM uses printk(), which prints to the syslog. To see our message, tail the syslog:

On some systems, kernel messages are logged to a different file (i.e. /var/log/messages).

5. Remove the module when you’re finished.

Use rmmod to remove the LKM:

To verify that the LKM has been removed successfully, tail the syslog again:

Congratulations! You’ve just created and compiled a Loadable Kernel Module.

Further Reading

The Linux Kernel Module Programming Guide
Anatomy of Linux loadable kernel modules
Loadable Kernel Module Programming and System Call Interception
(nearly) Complete Linux Loadable Kernel Modules

Incoming search terms:

7 thoughts on ““Hello World” Loadable Kernel Module

  1. This guide is soooooo much better than the other ones I’ve found online so far. Plus you’ve got some great references that I didn’t see until now, except for the first one, The Linux Kernel Module Programming Guide. (By the way, there is a version that is 2 years newer at http://www.tldp.org/LDP/lkmpg/2.6/html/lkmpg.html)

    I wish this were the one that came up at the top, or at least top 5, when I Googled “Linux kernel module”. As it is I found my way here because someone mentioned it in a superuser.com thread.

  2. Great work Man!! Keep doing………Good for kernel 3+ version where it can easily run…Unlike other hello world modules what other persons wrote on other link…

  3. i am learning embedded linux my self   your tutorial is best for me.thanks you very much