加載內核驅動的通常流程:
1.先將.ko文件拷貝到/lib/module/`uname -r`(內核版本號)/kernel/driver/...目錄下,
根據具體用途的區別分為net、ide、scsi、usb、video、parport、md、block、ata等等。
2.運行depmod -a,更新模塊依賴新,主要是更新modules.dep文件
3.運行modprobe加載內核模塊
lsmod
depmod
modprobe
modinfo
insmod
rmmod
以上內容是參考man翻譯的,若有疑問請用man …查看原始文檔,翻譯可能有誤。
其它:
(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 depmod
depmod -- 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 other
modules to use (using EXPORT_SYMBOL in the code).
linux核心模塊可以提供服務給其他模塊,稱之為"symbols"
depmod creates a list of module dependencies, by reading each module
under /lib/modules/version and determining what symbols it exports, and
what symbols it needs.
depmod通過讀取/lib/modules/version目錄下的每一個模塊來創建一個記錄模塊相依性
的列表。這個列表就是/lib/modules/version目錄下的modules.dep。
If a version is provided, then that kernel version's module directory
is 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 use
by 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