Linux内核模块(Module)的单独编译

  1. 模块文件

    /*
    * file name: hello.c
    */
    
    #include<linux/module.h>
    
    
    #include<linux/init.h>                                                          
    
    
    #include<linux/moduleparam.h>
    
    
    MODULE_AUTHOR("Kevin Taylor");
    MODULE_LICENSE("GPL");
    
    static int nbr = 10; 
    module_param(nbr, int, S_IRUGO);
    
    static int __init hello_init(void)
    {
       int i;
    
       printk(KERN_ALERT"Init hello mudule...\n");
    
       for(i=0;i<nbr;i++)
       {   
           printk(KERN_ALERT"Hello, how are you? %d\n", i); 
       }   
    
       return 0;
    }
    
    static void __exit hello_exit(void)
    {
       printk(KERN_ALERT"Exit hello mudule...\n");
       printk(KERN_ALERT"I come from hello's module, I have been unload.\n");
    }
    
    module_init(hello_init);
    module_exit(hello_exit);
    
    MODULE_DESCRIPTION("A Simple Hello World");
    MODULE_ALIAS("A simplest module");
  2. Makefile

    
    #makefile for the hello.c                                                       
    
    
    obj-m := hello.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:
       rm *.ko
       rm *.o
  3. 编译模块

    make

    编译成功会输出如下信息:

    make -C /usr/src/linux-headers-4.15.0-29-generic M=/home/yumo/Learn/driver/modules modules
    make[1]: Entering directory '/usr/src/linux-headers-4.15.0-29-generic'
     CC [M]  /home/yumo/Learn/driver/modules/hello.o
     Building modules, stage 2.
     MODPOST 1 modules
     CC      /home/yumo/Learn/driver/modules/hello.mod.o
     LD [M]  /home/yumo/Learn/driver/modules/hello.ko
    make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-29-generic'
  4. 加载模块到内核中

    sudo insmod ./hello.ko
  5. 验证模块是否加载成功

    lsmod # 查看所有已加载的模块
  6. 加载模块时传递参数

    sudo insmod hello.ko nbr=4

    查看最近的8条日志信息:dmesg | tail -8

    [16331.599330] Hello, how are you? 9
    [18297.411389] Exit hello mudule...
    [18297.411394] I come from hello's module, I have been unload.
    [18330.125440] Init hello mudule...
    [18330.125450] Hello, how are you? 0
    [18330.125453] Hello, how are you? 1
    [18330.125456] Hello, how are you? 2
    [18330.125459] Hello, how are you? 3

    在加载模块时动态设置了模块文件中的变量nbr(在模块文件中默认值为10),从打印的日志信息可以看出,初始化函数中循环只执行了4次,说明加载模块时传递的参数已成功。

  7. 查看模块信息

    sudo modinfo hello.ko

    输出的模块信息

    filename:       /home/yumo/Learn/driver/modules/hello.ko
    alias:          A simplest module
    description:    A Simple Hello World
    license:        GPL
    author:         Kevin Taylor
    srcversion:     A9C1413760EC5E7C4FD9DF6
    depends:        
    retpoline:      Y
    name:           hello
    vermagic:       4.15.0-29-generic SMP mod_unload 
    parm:           nbr:int
  8. 查看内核的日志信息

    dmesg # 查看当前所有的内核日志信息
    dmesg | tail -12  # 查看内核最近输出的12条日志信息

    最近的12条内核日志信息

    [15645.089201] I come from hello's module, I have been unload.
    [16331.599309] Init hello mudule...
    [16331.599316] Hello, how are you? 0
    [16331.599318] Hello, how are you? 1
    [16331.599320] Hello, how are you? 2
    [16331.599321] Hello, how are you? 3
    [16331.599323] Hello, how are you? 4
    [16331.599324] Hello, how are you? 5
    [16331.599326] Hello, how are you? 6
    [16331.599327] Hello, how are you? 7
    [16331.599329] Hello, how are you? 8
    [16331.599330] Hello, how are you? 9

    dmesg小结

    1. 执行dmesg命令用于查询内核的日志信息,方括号中的内容为time stamp,该时间戳默认是系统从开机到输出该条日志信息时的运行时间,以秒为单位。

    2. 以当前时间的为时间戳显示时间信息 :dmesg -T

      [Wed Aug  1 10:17:34 2018] Init hello mudule...
      [Wed Aug  1 10:17:34 2018] Hello, how are you? 0
      [Wed Aug  1 10:17:34 2018] Hello, how are you? 1
    3. 显示系统运行时间以及打印两条日志信息的时间间隔

      [16331.599309 <  686.510108>] Init hello mudule...
      [16331.599316 <    0.000007>] Hello, how are you? 0
      [16331.599318 <    0.000002>] Hello, how are you? 1
      [16331.599320 <    0.000002>] Hello, how are you? 2
    4. 显示最近输出的n条日志信息:dmesg | tail -n

    5. 以上参数可以混合使用,如:

      [Wed Aug  1 10:06:19 2018 < 5352.292092>] I come from hello's module, I have been unload.
      [Wed Aug  1 10:17:45 2018 <  686.510108>] Init hello mudule...
      [Wed Aug  1 10:17:45 2018 <    0.000007>] Hello, how are you? 0
      [Wed Aug  1 10:17:45 2018 <    0.000002>] Hello, how are you? 1
      [Wed Aug  1 10:17:45 2018 <    0.000002>] Hello, how are you? 2
      [Wed Aug  1 10:17:45 2018 <    0.000001>] Hello, how are you? 3
      [Wed Aug  1 10:17:45 2018 <    0.000002>] Hello, how are you? 4
      [Wed Aug  1 10:17:45 2018 <    0.000001>] Hello, how are you? 5
      [Wed Aug  1 10:17:45 2018 <    0.000002>] Hello, how are you? 6
      [Wed Aug  1 10:17:45 2018 <    0.000001>] Hello, how are you? 7
      [Wed Aug  1 10:17:45 2018 <    0.000002>] Hello, how are you? 8
      [Wed Aug  1 10:17:45 2018 <    0.000001>] Hello, how are you? 9

      即打印最近的12条日志信息,同时输出打印日志信息的当前时间以及相邻两条日志信息的时间间隔。

  9. 从内核中移除模块

    sudo rmmod hello.ko
  • 10
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

羽墨志

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值