Hello World Kernel Module


Writing a basic kernel module is actually pretty simple, and a good learning exercise. Here’s how to do it.

More information can be found in the official linux documentation.

https://www.kernel.org/doc/html/latest/kbuild/modules.html

Install necessary packages

Before you start, make sure that you have the necessary software installed. For a Debian-based system, you can install the essential packages with:

sudo apt-get update
sudo apt-get install build-essential linux-headers-$(uname -r)

for Fedora

sudo dnf install kernel-headers kernel-devel make

Write the module

Create a new file called hello.c and insert the following code.

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple Linux kernel module");
MODULE_VERSION("0.1");

static int __init hello_init(void) {
 printk(KERN_INFO "Hello, World!\\n");
 return 0;
}

static void __exit hello_exit(void) {
 printk(KERN_INFO "Goodbye, World!\\n");
}

module_init(hello_init);
module_exit(hello_exit);
  • MODULE_* macros are used to provide information about the module.
  • hello_init function is the entry point of the module, which is called when the module is inserted into the kernel.
  • hello_exit function is the exit point of the module, called when the module is removed.
  • module_init and module_exit macros define the module’s entry and exit points.

Write a Makefile

In order to compile the module, you need a Makefile. Create a new file called Makefile and insert the following:

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

This Makefile is set up to build a kernel module hello.o from your hello.c source file.

Build the module

You can now build the module by running make:

make

After running this command, several new files should appear in the directory, including hello.ko. This file is the loadable kernel module.

Load the module

Insert the module into the kernel with the insmod command:

sudo insmod hello.ko

You can check that the module has been loaded with the lsmod command:

lsmod | grep hello

And you can check the kernel log with the dmesg command:

dmesg

# example output
# [ 1816.454838] hello: loading out-of-tree module taints kernel.
# [ 1816.454845] hello: module verification failed: signature and/or required key missing - tainting kernel
# [ 1816.455466] Hello, world!

You should see “Hello, World!” in the kernel log.

Unload the module

You can remove the module from the kernel with the rmmod command:

sudo rmmod hello

If you check the kernel log again, you should see “Goodbye, World!”:

dmesg

# example output
# [ 1880.346527] Goodbye, world!

Clean up

Finally, you can clean up the build artifacts with:

make clean

Please note: Loading a faulty kernel module can crash your system. Always test in a controlled environment.

That’s it! You have written, built, and tested a simple “Hello, World!” kernel module. This module doesn’t do much, but it provides a starting point for developing more complex kernel modules.