Kernel Development

The Debian system includes the kernel header files required for kernel module development and provides the toolchain needed to compile kernel modules. Users can directly develop and build new kernel modules on the device.

Environment Preparation

sudo apt update
sudo apt install -y make

Write the Source Code

helloworld.c File

The contents of the helloworld.c file are as follows:

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

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

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

static void __exit helloworld_exit(void) {
        printk(KERN_INFO "Goodbye!\n");
}

module_init(helloworld_init);
module_exit(helloworld_exit);

Makefile File

The contents of the Makefile are as follows:

obj-m := helloworld.o

KERNELDIR ?= /lib/modules/$(shell uname -r)/build

PWD := $(shell pwd)

all:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) clean

install:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

help:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) help

.PHONY: all clean install help

Build

Build command:

LD_LIBRARY_PATH=/opt/qcom/lib:$LD_LIBRARY_PATH PATH=/opt/quectel/bin:$PATH make LLVM=1

Run and Test

Load the module:

insmod helloworld.ko

Check the result:

lsmod | grep helloworld

Unload the module:

rmmod helloworld

Check the kernel log:

dmesg | grep "Hello World"