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 module\n");
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 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/
4 comments on “Linux Kernel Module Basics and Hello World”
Leave a Reply Cancel reply
40% Discount on My Book — Android NDK Cookbook
Android NDK Cookbook ebook 40% discount with promotion code MREANC40 at Packt Publishing The promotion code is valid until 15th June.Categories
- Android Apps (18)
- Android Audio Editor (1)
- TS 2 (3)
- Video Converter Android (8)
- Video2Gif (1)
- Android Tutorial (26)
- Android Dev Tools (1)
- API illustrated (8)
- Multimedia API (3)
- ffmpeg on Android (4)
- NDK (6)
- UI (5)
- Animation (1)
- Code Snippet (2)
- Coding Beyond Technique (18)
- a word, a world (4)
- Bug Rectified (4)
- Programming Habit (1)
- Software as a Career (1)
- Software as User Experience (1)
- Compilers and Related (2)
- ELF (2)
- Computer Languages (31)
- C/C++ (13)
- Java (9)
- JavaScript (2)
- PHP (1)
- Python (8)
- Data Structure & Algorithms (29)
- Bits (1)
- Data Structure (5)
- Integers (10)
- BigInteger (1)
- Prime (4)
- Search (3)
- Sorting (5)
- Strings (5)
- Database (1)
- SQLite (1)
- Digital Signal Processing (33)
- Distributed Systems (17)
- Apache Cassandra (6)
- Apache Hadoop (8)
- Apache Avro (3)
- Apache Nutch (3)
- Apache Solr (1)
- Linux Study Notes (40)
- crontab (1)
- Linux Kernel Programming (8)
- Linux Programming (12)
- IPC (2)
- Linux Network Programming (5)
- Linux Signals (2)
- Linux Shell Scripting (1)
- ssh (3)
- Machinery (30)
- misc (1)
- My Ideas (1)
- My Project (3)
- Mobile Caching (1)
- Selective Decoding (2)
- My Publication (1)
- My Readings (1)
- Networking (15)
- Program for Performance (8)
- Uncategorized (1)
- Virtual Machine (2)
- Web Dev (8)
- web components (3)
- Android Apps (18)
Recent Comments
Archives
- May 2013 (1)
- April 2013 (1)
- March 2013 (4)
- December 2012 (2)
- November 2012 (6)
- October 2012 (6)
- September 2012 (3)
- August 2012 (13)
- July 2012 (15)
- June 2012 (3)
- May 2012 (8)
- April 2012 (4)
- March 2012 (13)
- February 2012 (19)
- January 2012 (9)
- December 2011 (11)
- November 2011 (12)
- October 2011 (4)
- September 2011 (12)
- August 2011 (16)
- July 2011 (15)
- June 2011 (6)
- May 2011 (10)
- April 2011 (13)
- March 2011 (20)
- February 2011 (4)
- November 2010 (2)
- May 2010 (1)
- April 2010 (1)
- February 2010 (1)





Thanks for the post, it helps a lot for the programmer.
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.
Thank you! It’s a typo.
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