zoukankan      html  css  js  c++  java
  • 内核模块编写示例

    一、简介

    linux支持编写内核模块,通过insmod命令插入,进一步丰富内核功能。

    二、C文件

    初步写一个c文件 hello_module.c:

    // 内核模块相关的头文件
    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/init.h>
    
    // 加载函数
    static int __init hello_init(void)
    {
        printk("Hello World. Life is short, we need passion.\n");
        return 0;
    }
    
    // 卸载函数
    static void __exit hello_exit(void)
    {
        printk("Goodbye, honey.");
    }
    
    // 模块调用
    module_init(hello_init);
    module_exit(hello_exit);
    // 许可证
    MODULE_LICENSE("GPL");
    
    

    三、编译文件

    内核模块编译,需要使用makefile文件,使用文件名Makefile,make命令会默认查找对应文件:

    obj-m:=hello_module.o
    CURRENT_PATH := $(shell pwd)
    LINUX_KERNEL := $(shell uname -r)
    LINUX_KERNEL_PATH := /usr/src/linux-headers-$(LINUX_KERNEL)
    
    all:
            make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
    clean:
    
            make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean
    

    makefile有固定的格式,东西较多,后续系统学习。

    四、编译过程

    1、将上面两个文件hello_module.c和Makefile放在同一个文件夹中,执行命令make:

    root@ubuntu:/home/drivet_test# make
    make -C /usr/src/linux-headers-4.15.0-47-generic M=/home/drivet_test modules
    make[1]: Entering directory '/usr/src/linux-headers-4.15.0-47-generic'
      CC [M]  /home/drivet_test/hello_module.o
      Building modules, stage 2.
      MODPOST 1 modules
      CC      /home/drivet_test/hello_module.mod.o
      LD [M]  /home/drivet_test/hello_module.ko
    make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-47-generic'
    root@ubuntu:/home/drivet_test# ll
    total 160
    drwxr-xr-x 3 root root  4096 Dec 24 00:27 ./
    drwxr-xr-x 4 root root  4096 Dec 23 23:38 ../
    -rw-r--r-- 1 root root 52895 Dec 24 00:27 .cache.mk
    -rw-r--r-- 1 root root   339 Dec 24 00:13 hello_module.c
    -rw-r--r-- 1 root root  3992 Dec 24 00:27 hello_module.ko
    -rw-r--r-- 1 root root   245 Dec 24 00:27 .hello_module.ko.cmd
    -rw-r--r-- 1 root root   596 Dec 24 00:27 hello_module.mod.c
    -rw-r--r-- 1 root root  2584 Dec 24 00:27 hello_module.mod.o
    -rw-r--r-- 1 root root 29776 Dec 24 00:27 .hello_module.mod.o.cmd
    -rw-r--r-- 1 root root  3408 Dec 24 00:27 hello_module.o
    -rw-r--r-- 1 root root 29668 Dec 24 00:27 .hello_module.o.cmd
    -rw-r--r-- 1 root root   271 Dec 24 00:09 Makefile
    -rw-r--r-- 1 root root    41 Dec 24 00:27 modules.order
    -rw-r--r-- 1 root root     0 Dec 24 00:27 Module.symvers
    drwxr-xr-x 2 root root  4096 Dec 24 00:27 .tmp_versions/
    root@ubuntu:/home/drivet_test#
    
    

    编译成功后,会生成hello_module.ko。

    五、插入ko

    执行命令:insmod hello_module.ko
    可以通过lsmod命令查看ko是否插入成功,并在/var/log/kern.log日志中,查看ko加载过程的init函数是否执行:

    root@ubuntu:/home/drivet_test# insmod hello_module.ko
    root@ubuntu:/home/drivet_test#
    root@ubuntu:/home/drivet_test# lsmod | grep hello
    hello_module           16384  0
    root@ubuntu:/home/drivet_test#
    root@ubuntu:/home/drivet_test# tail /var/log/kern.log
    Dec 24 00:32:53 ubuntu kernel: [ 3631.352417] Hello World. Life is short, we need passion.
    
    

    六、删除ko

    执行命令:rmmod hello_module.ko
    同样可以通过lsmod查询ko是否存在,以及在/var/log/kern.log日志中,查看ko卸载过程的exit函数是否执行:

    root@ubuntu:/home/drivet_test# rmmod hello_module.ko
    root@ubuntu:/home/drivet_test#
    root@ubuntu:/home/drivet_test# lsmod | grep hello
    root@ubuntu:/home/drivet_test#
    root@ubuntu:/home/drivet_test# tail /var/log/kern.log
    Dec 24 00:32:53 ubuntu kernel: [ 3631.352417] Hello World. Life is short, we need passion.
    Dec 24 00:36:52 ubuntu kernel: [ 3815.830910] Goodbye, honey.
    
    
  • 相关阅读:
    R: 聚类分析
    R: 主成分分析 ~ PCA(Principal Component Analysis)
    R: 关于 ggplot2 的初探
    R: 字符串处理包:stringr
    R: 用 R 查看、管理文件(夹)
    R: 关于文件 文件夹的处理:file.show() dir.create().....
    R: 绘图 pie & hist
    R: 绘图 barplot
    R: 对向量中的每个元素,检查其是否包含某个“单词”
    R: 一页显示多张图的方法
  • 原文地址:https://www.cnblogs.com/HZL2017/p/15725790.html
Copyright © 2011-2022 走看看