Fundamentals 13 min read

Why Write Linux Kernel Modules? A Step‑by‑Step Guide to Building Your First LKM

This tutorial explains why kernel modules are needed, walks through preparing a Linux environment, installing required headers, creating and compiling a simple "Hello, World" module, extending it to a character device driver, and shows how to load, test, and unload the module safely.

21CTO
21CTO
21CTO
Why Write Linux Kernel Modules? A Step‑by‑Step Guide to Building Your First LKM

1. Why develop kernel modules

Modifying the Linux kernel directly is risky because a fault can crash the whole system; a kernel module provides a safer way to add hardware‑level functionality or privileged operations without rebuilding the entire kernel.

2. Preparation

You need a Linux machine (Ubuntu 16.04 LTS is used in the example), preferably a virtual machine to avoid data loss, and a solid understanding of C, since kernel code must be written in pure C.

3. Install development environment

sudo apt-get install build-essential linux-headers-`uname -r`

This installs the compiler, make, and kernel headers required for building modules.

4. First module

Create a working directory and a source file lkm_example.c with the following minimal code:

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

MODULE_LICENSE("GPL");
MODULE_AUTHOR("abin");
MODULE_DESCRIPTION("A simple example Linux module.");
MODULE_VERSION("0.01");

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

static void __exit lkm_example_exit(void)
{
    printk(KERN_INFO "Goodbye, World!
");
}

module_init(lkm_example_init);
module_exit(lkm_example_exit);

Compile it with a simple Makefile and load it using insmod. The messages appear in the kernel log, viewable with dmesg.

5. General module

To interact with user space, expose a character device. The full example defines DEVICE_NAME, implements device_open, device_read, device_write, and device_release, registers the device with register_chrdev, and cleans up with unregister_chrdev.

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

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Robert W. Oliver II");
MODULE_DESCRIPTION("A simple example Linux module.");
MODULE_VERSION("0.01");

#define DEVICE_NAME "lkm_example"
#define EXAMPLE_MSG "Hello, World!
"
#define MSG_BUFFER_LEN 15

static int major_num;
static int device_open_count = 0;
static char msg_buffer[MSG_BUFFER_LEN];
static char *msg_ptr;

static struct file_operations file_ops = {
    .read = device_read,
    .write = device_write,
    .open = device_open,
    .release = device_release
};

static int __init lkm_example_init(void)
{
    strncpy(msg_buffer, EXAMPLE_MSG, MSG_BUFFER_LEN);
    msg_ptr = msg_buffer;
    major_num = register_chrdev(0, DEVICE_NAME, &file_ops);
    if (major_num < 0) {
        printk(KERN_ALERT "Could not register device: %d
", major_num);
        return major_num;
    }
    printk(KERN_INFO "lkm_example module loaded with device major number %d
", major_num);
    return 0;
}

static void __exit lkm_example_exit(void)
{
    unregister_chrdev(major_num, DEVICE_NAME);
    printk(KERN_INFO "Goodbye, World!
");
}

module_init(lkm_example_init);
module_exit(lkm_example_exit);

After building, create the device node with mknod /dev/lkm_example c <MAJOR> 0, then read the message using cat /dev/lkm_example or dd. The module can be unloaded with rmmod.

6. Conclusion

Even a simple example demonstrates the basic structure of a Linux kernel module; you can extend it to implement complex functionality, but always allocate extra time for debugging because kernel bugs can compromise system stability.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxC programmingMakefiledevice driverKernel ModuleUbuntu
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.