加載內核驅動的一般流程:ide
1.先將.ko文件拷貝到/lib/module/`uname -r`(內核版本號)/kernel/driver/...目錄下,post
根據具體用途的區別分爲net、ide、scsi、usb、video、parport、md、block、ata等等。測試
2.運行depmod -a,更新模塊依賴新,主要是更新modules.dep文件ui
3.運行modprobe加載內核模塊this
lsmod
depmod
modprobe
modinfo
insmod
rmmod
以上內容是參考man翻譯的,如有疑問請用man …查看原始文檔,翻譯可能有誤。spa
其它:命令行
(1)lsmod 顯示當前加載的全部模塊,至關於cat /proc/modules,
假設你沒有設定開機加載某個模塊,好比ntfs,那麼開機後執行lsmod,列表裏不會有ntfs這個模塊的,
這時你再執行 mount -t ntfs xxx後,執行lsmod後列表裏就會有ntfs這個模塊了。
還要注意的是lsmod顯示的是模塊名,而不是別名(alias)。
(2) modprobe與insmod
modprobe -l #顯示當前能夠加載的模塊
modprobe xxx.ko #加載某個模塊
modprobe -r xxx.ko #卸載某個模塊
經過了解modprobe的manpage咱們知道,我能夠經過modprobe -l來顯示能夠當前能夠加載的模塊,所謂當前能夠加載的模塊,
實際上就是modules.dep文件中包含的那些模塊,而不是manpage裏說的modprobe會加載/lib/modules/`uname -r`下的全部模塊(也許是我理解錯誤),下面咱們將會證實這一點.
insmod 與 modprobe 都是載入 kernel module,不過通常差異於 modprobe 可以處理 module 載入的相依問題。
比方你要載入 a module,可是 a module 要求系統先載入 b module 時,直接用 insmod 掛入一般都會出現錯誤訊息,不過 modprobe 卻是可以知道先載入 b module 後才載入 a module,如此相依性就會知足。
不過 modprobe 並非大神,不會厲害到知道 module 之間的相依性爲什麼,該程式是讀取 /lib/modules/2.6.xx/modules.dep 檔案得知相依性的。而該檔案是透過 depmod 程式所創建。
(3)上面(1)中提到modprobe加載某個模塊是根據/lib/modules/`uname -r`目錄下的modules.dep文件中的模塊列表,這個文件中有的模塊modprobe會正確加載,不然就會出錯。
咱們還拿ntfs這個模塊來舉例:
vi /lib/modules/`uname -r`/modules.dep
註釋掉/lib/modules/2.6.18-4-k7/kernel/fs/ntfs/ntfs.ko這一行,就是加個#號.
這個修改是即便生效的。
modinfo ntfs
modinfo: could not find module ntfs
modprobe ntfs
FATAL: Module ntfs not found.
重啓機器,執行一樣的命令會獲得一樣的結果,說明開機不會自動執行depmod的,而
locate ntfs.ko
/lib/modules/2.6.18-4-k7/kernel/fs/ntfs/ntfs.ko
證實咱們並無轉移ntfs模塊。
注意若是重啓機器以前進行mount仍是能夠的,重啓以後就會報錯了,而上邊的都是即時生效的。
還有若是modules.dep裏註釋掉了ntfs,那麼在/etc/modules裏寫上也是不起做用的,說明這個和mount同樣都是依賴 modprobe來完成加載模塊命令的。而insmod是能夠的,由於insmod後面跟的是絕對路徑,它和modules.dep沒什麼關係。 insmod比較重要的用途是用來測試模塊的正確性,加載通常都是依靠modprobe。(這個可能也不起做用了,都用modprobe吧)
這一切只是由於咱們註釋掉了modules.dep中關於ntfs.ko的那一行,而模塊並無刪除或轉移。既然modules.dep文件如此重要,那麼它是怎麼生成的呢?這就和下一個命令有關了,depmod。
(4)depmod
man depmoddepmod -- program to generate modules.dep and map files. Blank lines, and lines starting with a '#' (ignoring spaces) are ignored in modules.dep.depmod是一個用來產生modules.dep和map文件的程序。在modules.dep文件中空白行和以'#'開頭的行將被忽略.Linux kernel modules can provide services (called "symbols") for othermodules to use (using EXPORT_SYMBOL in the code). linux核心模塊能夠提供服務給其餘模塊,稱之爲"symbols"depmod creates a list of module dependencies, by reading each moduleunder /lib/modules/version and determining what symbols it exports, andwhat symbols it needs. depmod經過讀取/lib/modules/version目錄下的每個模塊來建立一個記錄模塊相依性的列表。這個列表就是/lib/modules/version目錄下的modules.dep。If a version is provided, then that kernel version's module directoryis used, rather than the current kernel version (as returned by "uname-r").若是給定version的話,那麼depmod會檢查這個version對應的modules目錄而不是當前運行的kernel對應的modules目錄。depmod will also generate various map files in this directory, for useby the hotplug infrastructure.depmod也會在/lib/modules/version目錄下建立許多map文件,這些文件將會被hotplug用到。OPTIONS:-a --all Probe all modules. This option is enabled by default if no file names are given in the command-line.檢查全部的模塊,這個命令是默認的若是你沒有指定模塊名字的話。-A --quick This option scans to see if any modules are newer than the modules.dep file before any work is done%3