Ubuntu16.04下的modules模塊編譯加載
一、首先編寫對應的驅動程序的相關內容:(最簡單的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) modules6 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權限:
相關錯誤集合如下所示(確實不知道怎麽解決,最後歪打正著就解決了,不過回過頭來想,這些錯誤提示的分析也的確有很大的幫助吧):
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 [email protected]:~/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
內核在編譯的時候,並不會主動去給模塊簽名,除非你把上述配置項打開。
完~
Ubuntu16.04下的modules模塊編譯加載