Linux Kernel Module Basics and Hello World

Linux kernel is considered as mysterious and tough for many programmers, but it’s actually just another big code base with certain special rules and programming practices.

The loadable kernel module is something easier to start with, and one can still learn specifics about Linux kernel programming. This blog covers the very basics of the Linux kernel programming and how to write a hello world program.

Linux Kernel Module Intro

Linux kernel modules are a piece of software that can be loaded/unloaded upon request, either at system start up, or dynamically when the system is up and running.

Kernel modules extends the functionalities of the Linux kernel without re-compiling and rebooting the Linux system. Lots of Linux drivers are written as kernel modules and loaded when a compatible device is detected.

Linux kernel module exists as .ko file on Linux file system.

Linux Kernel Module Related Commands

Linux has several command line utilities for load, unload, and list kernel modules.

1. lsmod: list out a list of kernel modules, their sizes and the modules that depends on them.

2. modinfo <kernel module name or .ko file name>: show information about a kernel module. works for both loaded kernel program and .ko file. Below is a sample output of modinfo,

filename:       netfilter.ko

author:         Liu Feipeng/roman10

description:    linux-simple-firewall

license:        GPL

srcversion:     5CCF27A5D08DD9626B11EEA

depends:        

vermagic:       2.6.31-14-generic SMP mod_unload modversions 586 

3. insmod <.ko file name>and rmmod <kernel module name or .ko file name>: load a kernel module, and unload a loaded kernel module.

4. modprobe: load and unload modules from Linux kernel. It looks in the module directory /lib/modules/$(uname -r) for all modules and other files. This tool is more advanced and one can look up the man page for more detailed information.

Two basic usages are,

modprobe <module name>: load a module

modprobe -r <module name>: unload a module

To check if there’s any errors when loading/unloading a kernel module, using dmesg or tail -f /var/log/messages

Or you can add -v option to the utilities programs to enable verbose mode if they support it.

A Hello World Linux Kernel Module

The hello world kernel module can be written as below,

#include <linux/module.h>

#include <linux/kernel.h>

MODULE_LICENSE("GPL");

MODULE_DESCRIPTION("hello");

MODULE_AUTHOR("Liu Feipeng/roman10");

 

int init_module() {

    printk(KERN_INFO "initialize kernel modulen");

    printk(KERN_INFO "hello world!n");

    return 0;

}

 

void cleanup_module() {

    printk(KERN_INFO "kernel module unloaded.n");

}

The code above is the simplest program for a kernel module. Two functions are essential for every kernel module: init_module() is called when a kernel module is loaded, and cleanup_module() is called upon the removal of the module.

printk is a kernel version of printf. KERN_INFO is one of the log levels defined in kernel.h for printk.

To compile the kernel module, save the following makefile into Makefile, then type make to build the module,

obj-m += helloworld.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

 

Note that spaces in the Makefile matter, it is suggested that you copy and paste the makefile text above.

To load a kernel module, use lsmod helloworld.ko and then look into /var/log/messages by dmesg command, you’ll read two lines as below,

[21673.800709] initialize kernel module
[21673.800727] hello world!

unload the kernel module by rmmod helloworld, you’ll read a line by dmesg again,

[21715.889510] kernel module unloaded.

For a more detailed introduction to linux kernel module programming, please refer to the reference below.

Note:

This post is part of the tutorial: How to write a Linux Firewall in Less than 1000 Lines

Part 1: Overview

Part 2: Command Line Arguments Parsing in glibc

Part 3.1: Linux Kernel Module Basics and Hello World

Part 3.2: Linux Kernel Programming – Linked List

Part 3.3 Linux Kernel Programming – Memory Allocation

Part 4.1: How to Filter Network Packets using Netfilter – Part 1 Netfilter Hooks

Part 4.2 How to Filter Network Packets using Netfilter – Part 2 Implement the Hook Function

Part 5: Linux procfs Virtual File System

Part 6: Put Everything Together

References:

Linux kernel module programming guide: http://tldp.org/LDP/lkmpg/2.6/html/

0 thoughts on “Linux Kernel Module Basics and Hello World”

  1. Good post, thanks! I think where you wrote “To load a kernel module, use lsmod helloworld.ko” you should have written insmod instead of lsmod.

  2. Do you happen to know why I get this error when trying to compile I’ve looked all over

    scripts/selinux/genheaders/genheaders.c:13:22: error: classmap.h: No such file or directory
    scripts/selinux/genheaders/genheaders.c:14:35: error: initial_sid_to_string.h: No such file or directory
    scripts/selinux/genheaders/genheaders.c: In function ‘main’:
    scripts/selinux/genheaders/genheaders.c:61: error: ‘secclass_map’ undeclared (first use in this function)
    scripts/selinux/genheaders/genheaders.c:61: error: (Each undeclared identifier is reported only once

  3. 晴れてブログ開設 友達と共同展開していたサイトから離れ 自らで独立することにいたしました。

Leave a Reply

Your email address will not be published. Required fields are marked *