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,
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,
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.
This post is part of the tutorial: How to write a Linux Firewall in Less than 1000 Lines
Linux kernel module programming guide: http://tldp.org/LDP/lkmpg/2.6/html/