1. 程式人生 > >Linux核心模組(Module)的單獨編譯

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