浅谈linux中的模块加载

来源:互联网 发布:华为认证云计算工程师 编辑:程序博客网 时间:2024/05/04 16:34

浅谈linux中的模块加载

和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, andlines 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


原创粉丝点击