一、首先編寫對應的驅動程序的相關內容:(最簡單的hello.c程序)
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);
二、編寫對應Makefile文件:(注意事項Makefile,首字母大寫M)
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
三、使用make指令對程序進行編譯生成目標文件hello.ko
sudo make
使用的是sudo make的指令來保證運行和文件的執行權限等等,編譯的結果如下所示:
這里成功生成了我們需要的.ko文件
使用sudo make clean命令來清除相關的中間文件以及目標文件:
sudo make clean
這樣就清除了所有的文件了~
四、安裝加載模塊,需要的是root權限:
sudo insmod ./hello.ko
這里的路徑變了一下,是因為我的Ubuntu16.04的實體主機加載模塊的時候,需要數字簽名,但是數字簽名之后還是不能正確的加載,之后就在我的虛擬機Ubuntu16.04上實驗了一下,這樣居然成功了,因此路徑有所改變,但是驅動成功加載了。
驅動加載成功的驗證方法:
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
完~