加载内核驱动的一般流程: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