Wednesday 3 May 2017

Creating / installing a simple Linux kernel module

This tutorial will explain how we can create a simple Linux kernel module (hello world style) and how it can be installed.

When building Linux kernel modules you require the kernels header files. Header files in general define the how functions in the source are defined - you do not require the actual implementation of the functions (as this would introduce a lot unneeded code) - rather the function signature is provided - that is the return value and parameters of the function.

For example this might be:

int myFunction(char param1[], char param2[]);

Headers are typically used when compiling device drivers - while (obviously) you'd require the full sources if you were compiling a kernel.

Let's start by firstly installing the kernel headers for our current kernel:

sudo yum install kernel-devel kernel-headers

And then let's create a simple 'hello world' module (credit to tldp.org):

/*
 *  hello-1.c - The simplest kernel module.
 */
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */

int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");

/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}

void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}

And then creating a makefile for it:

vi Makefile

and adding:

obj-m += hello-1.o

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

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

and then compiling with:

make all

You should see something like the following:

make -C /lib/modules/4.10.5-200.fc25.x86_64/build M=/home/limited/test modules
make[1]: Entering directory '/usr/src/kernels/4.10.5-200.fc25.x86_64'
  Building modules, stage 2.
  MODPOST 1 modules
make[1]: Leaving directory '/usr/src/kernels/4.10.5-200.fc25.x86_64'

Locate the .ko (Kernel Object) file - this is the module that we will load into the kernel - we can view information about it with:

modinfo hello-1.c

This is useful for verifying that the module has been built for the correct kernel (by inspecting the 'vermagic' output.)

We can install the module with:

insmod /home/limited/test/hello-1.ko

* Warning * At the time of writing Fedora currently has a bug report open which I ran into while attempting to install the module -see: https://bugzilla.redhat.com/show_bug.cgi?id=1426741

[ 4192.599748] hello_1: loading out-of-tree module taints kernel.
[ 4192.599750] hello_1: module license 'unspecified' taints kernel.
[ 4192.599750] Disabling lock debugging due to kernel taint
[ 4192.599784] hello_1: module verification failed: signature and/or required key missing - tainting kernel
[ 4192.600029] Hello world 1.

You should see the hello world message - but also you might encounter a message complaining 'module verification failure' - this is an option on the kernel that requires any additional modules that are installed are signed - otherwise they will fail to load.

Sources

The Linux Kernel Module Programming Guide: http://www.tldp.org/LDP/lkmpg/2.6/html/x121.html

0 comments:

Post a Comment