写Linux Kernel Module

转载:  https://wenyuangg.github.io/posts/linux/simple-linux-kernel-module.html

1. 先創建一個資料夾 並寫個簡單的 kernel module (hello.c)

mkdir helloworld

cd helloworld

vim hello.c
  • hello.c 的 code 如下:
/* hello.c */
#include <linux/init.h>
#include <linux/module.h>
  
MODULE_DESCRIPTION("Hello_world");
MODULE_LICENSE("GPL");
  
static int hello_init(void)
{
 printk(KERN_INFO "Hello world !\n");
 return 0;
}
  
static void hello_exit(void)
{
 printk(KERN_INFO "ByeBye !\n");
}
  
module_init(hello_init);
/* When u use insmod, it will enter hello_init function */

module_exit(hello_exit);
/* When u use rmmod, it will enter hello_exit function */

2. 接著在同個目錄下寫個 Makefile

vim Makefile
  • Makefile 的內容如下:
PWD := $(shell pwd) 
KVERSION := $(shell uname -r)
KERNEL_DIR = /usr/src/linux-headers-$(KVERSION)/
 
MODULE_NAME = hello
obj-m := $(MODULE_NAME).o
 
all: 
 make -C $(KERNEL_DIR) M=$(PWD) modules
clean: 
 make -C $(KERNEL_DIR) M=$(PWD) clean

3. 最後就可以使用 sudo make 指令來編譯

將他編譯成 .ko 檔

sudo make

4. 使用 insmod 來載入 module

sudo insmod hello.ko

# 載完後用 dmesg 來觀看 kernel 訊息

dmesg

可以看到成功 print 出 hello world ! 字串

你也可以使用 lsmod 指令來列出目前在使用的 module

# grep "hello" 用來濾出我們的 hello module 

lsmod | grep "hello"

最後想要移除掉模組的話 要使用 rmmod

sudo rmmod hello.ko

# 再觀察一次 kernel 訊息

dmesg

可以看到 print 出了 ByeBye ! 字串

转载:https://wenyuangg.github.io/posts/linux/simple-linux-kernel-module.html

猜你喜欢

转载自blog.csdn.net/shanandqiu/article/details/111992767