Hello linux kernel-how to add a hello module to the linux kernel

How to add the first helloword module to the linux kernel

With excitement and sleepless night, I added the first hello_kernel module to the linux kernel. This is my first greeting to the linux kernel, hello kernel

As in the linux kernel driver, the first chapter does not have a very detailed description of how to load the kernel, learn from the website here

https://blog.csdn.net/wait_for_taht_day5/article/details/50404572

Linux kernel official download address

https://www.kernel.org

This is the official website of the linux kernel

My test environment is a ubuntu virtual machine. If you recompile the linux kernel, you need to pay attention. The kernel is compiled for about 20G. After two hours, luckily there is a kernel code tree in ubuntu. We don’t have to compile this process because we I saw the kernel tree is on my computer

First of all

ls /lib/modules

I saw that there are many release versions of the linux kernel on the computer

zhanglei@zhanglei-virtual-machine:~$ ls /lib/modules/5.4.0-
5.4.0-26-generic/ 5.4.0-37-generic/ 5.4.0-40-generic/ 5.4.0-45-generic/ 
5.4.0-33-generic/ 5.4.0-39-generic/ 5.4.0-42-generic/ 5.4.0-47-generic/

Here I have doubts, which directory should I choose?

uname -r 

Gave you the answer

The kernel tree is under /lib/modules/5.4.0-47-generic/build

Well, we start to write the code, we need to mount the module to the linux kernel, just write a hello_kernel.c

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

static __init int hello_init(void)
{
     printk(KERN_ALERT "HELLO,KERNEL\n");
    return 0;
 }

 static __exit void hello_exit(void)
 {
     printk(KERN_ALERT  "Goodbye, Kernel!\n");

 }

module_init(hello_init);
module_exit(hello_exit);

There are only two core mount functions

module_init(hello_init);
 module_exit(hello_exit);

There must be an explanation here that printk is defined in the kernel. The function is similar to printf, but it cannot seem to be unable to format floating point, because the kernel does not rely on any external C library when it is running. Modules can call printk because of After the insmod function is loaded, the module is connected to the kernel, and of course you can call this function (introduction to the linux device driver).

We need to write a make file

obj-m:=hello_kernel.o
        KERNELBUILD := /lib/modules/$(shell uname -r)/build
default:
        make -C $(KERNELBUILD) M=$(shell pwd) modules
clean:
        rm -rf *.o *.ko

obj-m means to compile the file test.o as a "module", it will not be compiled into the kernel, but a separate "test.ko" file will be generated

We can see the kernel module hello_kernel.ko

Well, finally we load the module and say hello to the kernel! !

zhanglei@zhanglei-virtual-machine:~$ sudo insmod hello_kernel.ko && tail -f /var/log/kern.log
Oct 28 23:42:51 zhanglei-virtual-machine kernel: [ 2277.644488] hello_kernel: module license 'unspecified' taints kernel.
Oct 28 23:42:51 zhanglei-virtual-machine kernel: [ 2277.644489] Disabling lock debugging due to kernel taint
Oct 28 23:42:51 zhanglei-virtual-machine kernel: [ 2277.644602] hello_kernel: module verification failed: signature and/or required key missing - tainting kernel
Oct 28 23:42:51 zhanglei-virtual-machine kernel: [ 2277.646193] HELLO, lingyun IoT Studio!
Oct 28 23:44:54 zhanglei-virtual-machine kernel: [ 2401.079667] Goodbye, I have found a good job!
Oct 28 23:45:22 zhanglei-virtual-machine kernel: [ 2428.281660] HELLO, lingyun IoT Studio!
Oct 28 23:45:34 zhanglei-virtual-machine kernel: [ 2440.247857] Goodbye, I have found a good job!
Oct 28 23:45:46 zhanglei-virtual-machine kernel: [ 2453.022348] HELLO, lingyun IoT Studio!
Oct 28 23:45:56 zhanglei-virtual-machine kernel: [ 2462.435602] Goodbye, I have found a good job!
Oct 28 23:56:02 zhanglei-virtual-machine kernel: [ 3068.900484] HELLO,KERNEL

Okay, we saw that we greeted the kernel

Let's say goodbye to the kernel again

zhanglei@zhanglei-virtual-machine:~$ sudo rmmod hello_kernel.ko && tail -f /var/log/kern.log
Oct 28 23:42:51 zhanglei-virtual-machine kernel: [ 2277.644489] Disabling lock debugging due to kernel taint
Oct 28 23:42:51 zhanglei-virtual-machine kernel: [ 2277.644602] hello_kernel: module verification failed: signature and/or required key missing - tainting kernel
Oct 28 23:42:51 zhanglei-virtual-machine kernel: [ 2277.646193] HELLO, lingyun IoT Studio!
Oct 28 23:44:54 zhanglei-virtual-machine kernel: [ 2401.079667] Goodbye, I have found a good job!
Oct 28 23:45:22 zhanglei-virtual-machine kernel: [ 2428.281660] HELLO, lingyun IoT Studio!
Oct 28 23:45:34 zhanglei-virtual-machine kernel: [ 2440.247857] Goodbye, I have found a good job!
Oct 28 23:45:46 zhanglei-virtual-machine kernel: [ 2453.022348] HELLO, lingyun IoT Studio!
Oct 28 23:45:56 zhanglei-virtual-machine kernel: [ 2462.435602] Goodbye, I have found a good job!
Oct 28 23:56:02 zhanglei-virtual-machine kernel: [ 3068.900484] HELLO,KERNEL
Oct 28 23:56:47 zhanglei-virtual-machine kernel: [ 3113.694009] Goodbye, Kernel!
^C
zhanglei@zhanglei-virtual-machine:~$ 

Since then we have completed our first meeting with the kernel, hello linux kernel, goodbye

Guess you like

Origin blog.csdn.net/qq_32783703/article/details/109348448
Recommended