Ubuntu16.04下的modules模塊編譯加載

1、首先編寫對應的驅動程序的相關內容:(最簡單的hello.c程序)html

 1 #include<linux/init.h>
 2 #include<linux/module.h>
 3 MODULE_LICENSE("Dual BSD/GPL");
 4 MODULE_AUTHOR("MDAXIA");
 5 
 6 static int __init hello_init(void)
 7 {
 8         printk(KERN_ALERT "Hello world!\n");
 9         return 0;
10 }
11 static void __exit hello_exit(void)
12 {
13         printk(KERN_ALERT "Goodbye,cruel world!");
14 }
15 module_init(hello_init);
16 module_exit(hello_exit);

2、編寫對應Makefile文件:(注意事項Makefile,首字母大寫M)linux

 1 ifeq ($(KERNELRELEASE),)
 2 KDIR:=/lib/modules/$(shell uname -r)/build
 3 PWD:=$(shell pwd)
 4 modules:
 5         $(MAKE) -C $(KDIR) M=$(PWD) modules
 6 modules_install:
 7         $(MAKE) -C $(KDIR) M=$(PWD) modules_install
 8 clean:
 9         rm -rf *.o *.ko .depend *.mod.o *.mod.c Module.* modules.* 
10 .PHONY:modules modules_install clean
11 else
12 obj-m :=hello.o
13 endif

3、使用make指令對程序進行編譯生成目標文件hello.koshell

sudo make

使用的是sudo make的指令來保證運行和文件的執行權限等等,編譯的結果以下所示:ubuntu

這裏成功生成了咱們須要的.ko文件vim

使用sudo make clean命令來清除相關的中間文件以及目標文件:post

sudo make clean

這樣就清除了全部的文件了~網站

4、安裝加載模塊,須要的是root權限:ui

sudo insmod ./hello.ko

這裏的路徑變了一下,是由於個人Ubuntu16.04的試題主機加載模塊的時候,須要數字簽名,可是數字簽名以後仍是不能正確的加載,以後就在個人虛擬機Ubuntu16.04上實驗了一下,這樣竟然成功了,所以路徑有所改變,可是驅動成功加載了。url

驅動加載成功的驗證方法:spa

cat /var/log/syslog | grep Hello

這樣就顯示驅動成功加載了

也可使用lsmod來查看模塊的加載:

lsmod | grep hello

使用rmmod指令來卸載驅動模塊:

sudo rmmod hello

這裏順便推薦一個linux相關指令的中文查詢網站:http://man.linuxde.net/

相關錯誤集合以下所示(確實不知道怎麼解決,最後歪打正着就解決了,不過回過頭來想,這些錯誤提示的分析也的確有很大的幫助吧):

Question1:這裏重點關注一下第三行,Error的修改每每是從上到下的修改方式,因此這裏提示的是-fstack-protector-strong not supported by compiler,意思很明確了,咱們當前的gcc編譯器版本不支持-fstack-protector-strong這個參數,這個參數的調用時在linux-headers-4.4.0-96-generic相關文件中的,既然不支持,那咱們就找個支持的gcc版原本編譯運行就能夠了,經過查閱相關資料獲得:‘-fstack-protector-strong’ 選項是gcc4.9之後的版本才加入的,也就是說須要安裝gcc4.9之後的版本才能夠編譯經過,因此直接安裝gcc5.4就好了,關於安裝多個版本的gcc的內容請參考個人這篇文章:Ubuntu16.04多個版本GCC編譯器的安裝和切換完成安裝以後直接make編譯就不會出現上述問題了。

 1 make -C /lib/modules/4.4.0-96-generic/build M=/home/ubuntu-mm/ARMTools/Code_arm/modulesprogram modules
 2 make[1]: Entering directory '/usr/src/linux-headers-4.4.0-96-generic'
 3 Makefile:702: Cannot use CONFIG_CC_STACKPROTECTOR_STRONG: -fstack-protector-strong not supported by compiler
 4 scripts/Makefile.build:44: /home/ubuntu-mm/ARMTools/Code_arm/modulesprogram/Makefile: No such file or directory
 5 make[2]: *** No rule to make target '/home/ubuntu-mm/ARMTools/Code_arm/modulesprogram/Makefile'.  Stop.
 6 Makefile:1423: recipe for target '_module_/home/ubuntu-mm/ARMTools/Code_arm/modulesprogram' failed
 7 make[1]: *** [_module_/home/ubuntu-mm/ARMTools/Code_arm/modulesprogram] Error 2
 8 make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-96-generic'
 9 makefile:7: recipe for target 'modules' failed
10 make: *** [modules] Error 2

Question2:這裏重點關注了第四行的描述,也就是第一個make[1]的錯誤提示:/home/ubuntu-mm/ARMTools/Code_arm/modulesprogram/Makefile: No such file or directory,仔細一看,發現這裏的文件名字是Makefile,回想起個人文件名是makefile,多是這個問題,因而就rename一下:mv makefile Makefile 結果成功編譯了~Cheers!因此說之後最好仍是用Makefile來寫吧~

 1 ubuntu-mm@ubuntu-mm:~/ARMTools/Code_arm/modulesprogram$ sudo make
 2 make -C /lib/modules/4.4.0-96-generic/build M=/home/ubuntu-mm/ARMTools/Code_arm/modulesprogram modules
 3 make[1]: Entering directory '/usr/src/linux-headers-4.4.0-96-generic'
 4 scripts/Makefile.build:44: /home/ubuntu-mm/ARMTools/Code_arm/modulesprogram/Makefile: No such file or directory
 5 make[2]: *** No rule to make target '/home/ubuntu-mm/ARMTools/Code_arm/modulesprogram/Makefile'. Stop.
 6 Makefile:1423: recipe for target '_module_/home/ubuntu-mm/ARMTools/Code_arm/modulesprogram' failed
 7 make[1]: *** [_module_/home/ubuntu-mm/ARMTools/Code_arm/modulesprogram] Error 2
 8 make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-96-generic'
 9 makefile:7: recipe for target 'modules' failed
10 make: *** [modules] Error 2

Question3:insmod在插入模塊的過程當中出現下面的問題,查閱相關博客是模塊數字簽名的相關問題,詳細參見:http://blog.csdn.net/hui872370036/article/details/69950869

insmod: ERROR: could not insert module ./hello.ko: Required key not available

內核從3.7後開始支持模塊簽名,這個功能使能之後,內核只容許安裝特定key簽名的模塊,內核配置項以下所示:

 CONFIG_MODULE_SIG=y 表示開啓了簽名機制,可是這時候模塊簽名或不簽名均可以使用。

 CONFIG_MODULE_SIG_FORCE=y 若是上述配置項使能,則模塊必須有正確的簽名才能正常使用。

 CONFIG_MODULE_SIG_ALL=y 內核在編譯的時候,並不會主動去給模塊簽名,除非你把上述配置項打開。 

根據下面的英文教程的相關提示,按照下面的步驟進行操做:

/usr/src/linux-headers-4.4.0-96-generic/scripts/sign-file sha512 /usr/src/linux-headers-4.4.0-96-generic/certs/signing_key.pem /usr/src/linux-headers-4.4.0-96-generic/certs/signing_key.x509 hello.ko

這裏顯示數字簽名註冊成功~註冊的方式是使用了對應ubuntu16.04的linux內核版本的sign-file文件,註冊使用的方式是 哈希表sha512格式  註冊的帳戶保存在signing_key.pem文件當中,對應的密鑰保存在signing_key.x509文件當中。

若是你在對應的certs目錄當中沒有找到對應的兩個文件,請用vim指令建立兩個文件,並在.pem文件中輸入以下的內容(這是pem文件的基本格式,因此要添上,不然會出現沒法識別pem文件問題):

-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----

這樣就完成了.pem文件的建立。

接下來就是加載對應的.ko模塊的文件:

結果仍是如此有待解決~

目前在虛擬機上直接插入和安裝是沒有問題的(虛擬機內核版本:4.4.0-83-generic,實體機內核版本:4.4.0-96-generic)

關於簽名證書的問題,如今也尚未很好的解決,後面再嘗試找到對應的解決辦法吧~

詳細的module數字簽名的英文文檔參照:https://wiki.gentoo.org/wiki/Signed_kernel_module_support#Enabling_module_signature_verification

完~

相關文章
相關標籤/搜索