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/
4 comments on “Linux Kernel Module Basics and Hello World”
Leave a Reply Cancel reply
40% Discount on My Book — Android NDK CookbookAndroid NDK Cookbook ebook 40% discount with promotion code MREANC40 at Packt Publishing The promotion code is valid until 15th June.
- Android Apps (18)
- Android Tutorial (26)
- Code Snippet (2)
- Coding Beyond Technique (18)
- Compilers and Related (2)
- ELF (2)
- Computer Languages (31)
- Data Structure & Algorithms (29)
- Database (1)
- SQLite (1)
- Digital Signal Processing (33)
- Distributed Systems (17)
- Linux Study Notes (40)
- Machinery (30)
- misc (1)
- My Ideas (1)
- My Project (3)
- My Publication (1)
- My Readings (1)
- Networking (15)
- Program for Performance (8)
- Uncategorized (1)
- Virtual Machine (2)
- Web Dev (8)
- web components (3)
- 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)