modprobe 及 配置文件 /etc/modprobe.conf

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: if not, it silently
                 exits rather than regenerating the files.
只檢查那些比modules.dep文件裏記錄新的模塊的相依性,如果沒有則退出,並不重建modules.dep.

-e --errsyms
                 When  combined  with  the -F option, this reports any symbols
                 which a module needs which are not supplied by other  modules
                 or the kernel.  Normally, any symbols not provided by modules
                 are assumed to be provided by the  kernel  (which  should  be
                 true in a perfect world).
如果和下面的-F選項合用的話,將會報告模塊需要卻又不存在的symbols。通常,模塊不提供的symbols
會由kernel來提供。

-F --filesyms System.map
                 Supplied  with  the  System.map  produced when the kernel was
                 built, this allows the -e option to  report  unresolved  sym-
                 bols.
System.map是在kernel被創建的時候建立的,-F選項可以利用System.map文件,這時-e選項纔可以使用。

-n --dry-run
                 This  sends  the  resulting modules.dep, then the various map
                 files, to standard output, rather than writing them into  the
                 module directory.
只把結果顯示在屏幕上而不是寫到modules.dep中。

BACKWARDS COMPATIBILITY(向下兼容性)

This version of depmod is for kernels 2.5.48 and above.  If it detects a kernel with support for old style modules, or the version specified is before 2.5.48, it will attempt to run  depmod.modutils  in its place, so it is completely transparent to the user.

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

通常我們安裝一個新的模塊,先是編譯出相應的ko文件,然後移動/lib/modules/`uname -r`/目錄或者某個子
目錄下,locate xxx.ko確定該模塊確實在上面提到的目錄下面,執行depmod -aeF,depmod將會檢查
/lib/modules/`uname -r`/目錄及其子目錄中的所有模塊文件,並根據相依性生成新的modules.dep文件,這時我們執行modprobe xxx.ko,該模塊就會被正常加載了。
所以這時我們重新執行depmod -aeF會生成新的modules.dep,剛纔註釋掉了的那個#也消失了。
這裏我們還要提兩個文件:
1./etc/modules
vi /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.

loop

說明我們如果在這裏寫上模塊名字,比如ntfs(注意不要寫ntfs或者ntfs對應的alias),開機時就會自動加載,
即開機後lsmod後就會看到ntfs而不用等mount或者modprobe ntfs等命令。
man modprobe.conf
modprobe.conf -- Configuration file/directory for modprobe
modprobe.conf 傳遞給modprobe的配置文件或目錄。

Because  the  modprobe  command can add or remove extra more than one module, due to module dependen-
cies, we need a method of specifying what options are to  be  used  with  those  modules.   /etc/mod-
probe.conf  (or,  if  that  does  not exist, all files under the /etc/modprobe.d directory) specifies
those options, as required.  It can also be used to create convenient aliases: alternate names for  a
module.   Finally,  it can override the normal modprobe behavior altogether, for those with very spe-
cial requirements (such as inserting more than one module).
modprobe可以根據相依性來添加或刪除模塊,所以我們需要一種方法來指定特殊的選項給特定的某些模塊。
/etc/modprobe.conf(如果不存在就是在/etc/modprobe.d目錄下的所有文件)將會指定這些需要的選項。也可以
指定別名給一個模塊。

The  format  of  modprobe.conf and files under modprobe.d is simple: one command per line, with blank
lines and lines starting with # ignored (useful for adding comments).  A  at the end of a line causes
it to continue on the next line, which makes the file a bit neater.
格式:每行一個命令,空白行和行首有‘#’的將被忽略。行尾有A代表下一行是這行的繼續。

The syntax is a simplification of modules.conf, used in 2.4 kernels and earlier.
modules.conf只被2.4或更早的內核使用。


alias wildcard modulename
                 This  allows  you  to  give  alternate  names  for  a  module.   For example: "alias my-mod
                 really_long_modulename"  means  you  can  use  "modprobe  my-mod"  instead   of   "modprobe
                 really_long_modulename".   You  can  also  use  shell-style  wildcards,  so  "alias my-mod*
                 really_long_modulename" means that "modprobe my-mod-something" has the  same  effect.   You
                 can't  have aliases to other aliases (that way lies madness), but aliases can have options,
                 which will be added to any other options.
指定別名,也可以利用shell中的wildcard來指定。例子:
alias my-mod really_long_modulename
這意味着你可以用modprobe  my-mod"來代替"modprobe really_long_modulename.
不可以給別名指定別名。但是別名可以有選項。

Note that modules can also contain their own aliases, which  you  can  see  using  modinfo.
These  aliases  are used as a last resort (ie. if there is no real module, install, remove,
or alias       command in the configuration).

模塊可以有它們自帶的別名,你可以利用modinfo看到。這些別名將被作爲最後一着。配置文件裏的別名優先,如果
沒有指定的話,這些自帶的別名將被用到。

options modulename option...
                 This command allows you to add options to the module modulename (which might be  an  alias)
                 every  time it is inserted into the kernel: whether directly (using modprobe modulename, or
                 because the module being inserted depends on this module.

                 All options are added together: they can come from an option for the module itself, for  an
                 alias, and on the command line.


這個命令允許你加一些條件給模塊(模塊名或者是別名),當模塊被加載到內核中時。所有的條件選項可以被疊加。

install modulename command...
                 This is the most powerful primitive in modprobe.conf: it tells modprobe to run your command
                 instead of inserting the module in the kernel as normal.  The command can be any shell com-
                 mand: this allows you to do any kind of complex processing you might wish.  For example, if
                 the module "fred" worked better with the module "barney" already installed (but  it  didn't
                 depend  on  it,  so  modprobe  won't  automatically  load  it), you could say "install fred
                 /sbin/modprobe barney; /sbin/modprobe --ignore-install  fred",  which  would  do  what  you
                 wanted.   Note  the  --ignore-install,  which stops the second modprobe from re-running the
                 same install command.  See also remove below.
這是modprobe.conf中最原始最有效的命令:它告訴modprobe運行你的命令來取代modprobe通常的行爲(加載一個模塊到內核中)。
這個命令可以是shell命令,這將允許你做任何你西王做的複雜的事情。例子:
假定fred模塊和barney模塊一起工作時效果比較好,但是由於fred並不依賴於barney模塊,所以加載fred時並不會加載barney。
這是我們可以加如下的命令到配置文件中去。
install fred /sbin/modprobe barney; /sbin/modprobe --ignore-install  fred.
install fred 是指定modprobe在加載fred這個模塊之前要運行後面的這個命令來替代本來的動作(modprobe fred)。也就是:
/sbin/modprobe barney; /sbin/modprobe --ignore-install  fred 代替了modprobe fred
這裏--ignore-install表示加載時不要考慮配置文件中install選項,這是爲了避免又運行一遍同樣的shell命令。

                 You can also use install to make up modules which  don't  otherwise  exist.   For  example:
                 "install  probe-ethernet  /sbin/modprobe  e100  || /sbin/modprobe eepro100", which will try
                 first the e100 driver, then the eepro100 driver, when you do "modprobe probe-ethernet".
你可以組合模塊。例子:
install  probe-ethernet  /sbin/modprobe  e100  || /sbin/modprobe eepro100
當你下達modprobe probe-ethernet的命令時,將會先嚐試加載e100driver,如果不行再加載eepro100driver。

                 If you use the string "$CMDLINE_OPTS" in the command, it will be replaced  by  any  options
                 specified  on the modprobe command line.  This can be useful because users expect "modprobe
                 fred opt=1" to pass the "opt=1" arg to the module, even if there's an  install  command  in
                 the  configuration file.  So our above example becomes "install fred /sbin/modprobe barney;
                 /sbin/modprobe --ignore-install fred $CMDLINE_OPTS"

如果你在配置文件中的命令行裏使用了$CMDLINE_OPTS這個字符串,那麼你可以從shell命令行裏直接下達你希望傳遞給模塊
的條件,這個條件將取代配置文件中的這個$CMDLINE_OPTS.例子:
如果上邊的例子改爲:
install fred /sbin/modprobe barney;/sbin/modprobe --ignore-install fred $CMDLINE_OPTS
那麼當你下達modprobe fred opt=1時,這個opt=1參數將被傳遞給模塊,它將取代$CMDLINE_OPTS.

remove modulename command...
                 This is similar to the install command above, except it is invoked when  "modprobe  -r"  is
                 run.   The removal counterparts to the two examples above would be: "remove fred /sbin/mod-
                 probe -r --ignore-remove fred && /sbin/modprobe  -r  barney",  and  "remove  probe-ethernet
                 /sbin/modprobe -r eepro100 || /sbin/modprobe -r e100".
這個命令和install類似,只不過它只在modprobe -r時才起作用。上邊的例子對應:
"remove fred /sbin/modprobe -r --ignore-remove fred && /sbin/modprobe  -r  barney",  and
"remove  probe-ethernet/sbin/modprobe -r eepro100 || /sbin/modprobe -r e100".

blacklist modulename
                 Modules  can  contain  their  own aliases: usually these are aliases describing the devices
                 they support, such as "pci:123...".  These "internal" aliases can be overridden  by  normal
                 "alias"  keywords,  but  there  are  cases  where two or more modules both support the same
                 devices, or a module invalidly claims to support a device: the blacklist keyword  indicates
                 that all of that particular module's internal aliases are to be ignored.

模塊可以有自己的別名。通常這些別名描述了它們支持的設備,比如:pci:123...
這些內部別名會被通常我們指定的別名推翻。但是有些情況下,兩個或更多的模塊同時支持同一個設備,或者模塊實際不能支持它宣稱支持的設備:這時黑名單上的模塊意味着這些特殊模塊的內部別名將會被忽略。

Backwards Compatibility
       Although the syntax is similar to the older  /etc/modules.conf,  there  are  many  features  missing.
       There  are two reasons for this: firstly, install and remove commands can do just about anything, and secondly, the module-init-tools modprobe is designed to be  simple  enough  that  it  can  be  easily replaced.
雖然和老的/etc/modules.conf很像,但是老的/etc/modules.conf還是缺少很多的特性.
發佈了24 篇原創文章 · 獲贊 53 · 訪問量 100萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章