A Basic Linux Kernel Module

A very brief intro to creating, building, installing and removing a Linux kernel module, using Debian 8 x64.
From a clean Debian installation run the following to setup the correct environment for building a kernel module.

root@debian:~# apt-get install build-essential linux-source linux-headers-amd64

The Linux kernel is monolithic and modular, allows for loading and unloading of modules during the runtime life cycle of the kernel.
An example of one of the most basic kernel modules you can create is listed below, hwkm (Hello World Kernel Module).

File: hwkm.c

#include <linux/module.h>
#include <linux/kernel.h>

MODULE_DESCRIPTION("Just a test, nothing serious, just for fun!");

static int hwkm_init(void)
        /* kernel print function, provides a debug message, in /var/log */
        printk(KERN_INFO "Hello everybody peeps!n");
        return 0;

static void hwkm_cleanup(void)
        /* kernel print function, provides a debug message, in /var/log */
        printk(KERN_INFO "So long and thanks for all the fish!n");

/* register entry/exit points for module */

module_init, registers the modules entry point to the kernel.
module_exit, registers the modules clean up and exit point.

The build script for this module is as follows,
File: Makefile

obj-m := hwkm.o

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

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

The module can be built within the kernel source tree or in a project folder external to the kernel source. The external project is the option chosen for the example kernel module. At the top of the Makefile the line obj-m := dictates that the project is external. For a project created within the kernel source tree the same line would read obj-m +=.

Use the following to build and test the kernel module, the module should be built as root.

root@debian:/home/test/projects/hwkm# make
make -C /lib/modules/3.16.0-4-amd64/build M=/home/test/projects/hwkm 
make[1]: Entering directory '/usr/src/linux-headers-3.16.0-4-amd64'
  LD      /home/test/projects/basic-linux-kernel-module/built-in.o
  CC [M]  /home/test/projects/basic-linux-kernel-module/hwkm.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/test/projects/basic-linux-kernel-module/hwkm.mod.o
  LD [M]  /home/test/projects/basic-linux-kernel-module/hwkm.ko
make[1]: Leaving directory '/usr/src/linux-headers-3.16.0-4-amd64'

root@debian:/home/test/projects/hwkm# insmod hwkm.ko
root@debian:/home/test/projects/hwkm# tail -f /var/log/messages
Aug 28 09:21:24 debian kernel: [61271.123420] Hello everybody peeps!

root@debian:/home/test/projects/hwkm# rmmod hwkm.ko
root@debian:/home/test/projects/hwkm# tail -f /var/log/messages
Aug 28 09:21:59 debian kernel: [61305.925237] So long and thanks for all the fish!

Some man pages to look over for further information.

init_module, delete_module, create_module, query_module, insmod, rmmod, lsmod and modprobe.

Suffice to say that kernel mode development is quite different to what most are used to in user mode. I would like to explore these differences more fully sometime in the future in another post.

So that’s it for now, this was just a quick overview to lay some groundwork for further research and fun.

Link to github repo.

2 thoughts on “A Basic Linux Kernel Module

  1. Two issues:
    * make clean delete source files (asm-lkm.s)
    * when building: whis line pops up in the output:
    WARNING: could not find /home/…/basic-linux-kernel-module-in-asm/.asm-lkm.o.cmd for /home/…/basic-linux-kernel-module-in-asm/asm-lkm.o

    • Thank you for your comment, I have only tested this code on Debian Jessie and the code presented works as described. I suspect it may be an issue with a later kernel version and related tools.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s