淺談linux中的模組載入

來源:互聯網
上載者:User
和linux中載入模組有關的幾個程式分別如下:
lsmod,modprobe,depmod
lsmod顯示當前載入的所有模組,相當於 cat /proc/modules,假設你沒有設定開機載入某個模組,比如ntfs,那麼開機後執行lsmod,列表裡不會有ntfs這個模組的,這時你再執行 mount -t ntfs xxx後,執行lsmod後列表裡就會有ntfs這個模組了。
還要注意的是lsmod顯示的是模組名,而不是別名(alias)。
這裡最重要的是modprobe.
man modprobe
節選如下:

modprobe -- program to add and remove modules from the Linux Kernel
從linux核心中添加或刪除模組。

modprobe intelligently adds or removes a module from the Linux kernel:  note  that  for  convenience,there  is  no  difference  between  _  and - in module names.  modprobe looks in the module directory /lib/modules/`uname -r` for all the modules and  other  files,  except  for  the  optional  /etc/modprobe.conf configuration file and /etc/modprobe.d directory (see modprobe.conf(5)).  All files in the /etc/modprobe.d/arch/ directory are ignored.

為了一致性,模組名字中包含的_和-是沒有任何區別的。modprobe會檢查/lib/modules/`uname -r`下的所有模組,除了/etc/modprobe.conf設定檔和/etc/modprobe.d目錄以外。所有/etc/modprobe.d/arch/目錄下的檔案將被忽略。

Note that this version of modprobe does not do anything to the module itself: the work  of  resolving symbols  and  understanding  parameters  is  done  inside the kernel.  So module failure is sometimes accompanied by a kernel message: see dmesg(8).

值得注意的是現在modprobe不會對模組本身進行操作,解析symbols和理解參數的工作都交由kernel來作,所以模組載入等錯誤有時將會包含在核心資訊中,利用dmesg可以查看到。

modprobe expects an up-to-date modules.dep file, as generated by depmod (see depmod(8)).   This  file
lists  what  other  modules each module needs (if any), and modprobe uses this to add or remove these
dependencies automatically.  See modules.dep(5)).

modprobe會根據modules.dep來添加或者刪除模組。

If any arguments are given after the modulename, they are passed to the kernel (in  addition  to  any
options listed in the configuration file).
如果指定模組名稱的話,這些模組將會被傳到核心中,當然還有它們對應的參數(記錄在設定檔中).

OPTIONS
-l --list List all modules matching the given wildcard (or "*" if no wildcard is given).  This option
                 is provided for backwards compatibility: see find(1) and basename(1) for  a  more  flexible
                 alternative.
用來列出所有模組或者符合指定條件的所有模組,可以使用wildcard。

-r --remove
刪除模組。

BACKWARDS COMPATIBILITY(向下相容性)
This  version  of  modprobe is for kernels 2.5.48 and above.  If it detects a kernel with support for old-style modules (for which much of the work was done in userspace), it will  attempt  to  run  modprobe.modutils in its place, so it is completely transparent to the user.

現在版本的modprobe只支援2.5.48及以上的核心,如果它發現核心支援老的模組或者核心本身就低於2.5.48,它將嘗試運行modprobe.modutils來代替自己。

通過瞭解modprobe的manpage我們知道,我可以通過modprobe -l來顯示可以當前可以載入的模組,所謂
當前可以載入的模組,實際上就是modules.dep檔案中包含的那些模組,而不是manpage裡說的modprobe會載入/lib/modules/`uname -r`下的所有模組(也許是我理解錯誤),下面我們將會證明這一點.
modprobe xxx.ko        #載入某個模組
modprobe -r xxx.ko     #卸載某個模組
上面提到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。

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   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.