好记性不如铅笔头

linux, 操作系统

【转】内核与内核模块:depmod,lsmod,modinfo,insmod,rmmod,mdprobe

本文转自【 blog.chinaunix.net/uid-28216282-id-3380616.html 】,有删改。

CONTENTS

内核与内核模块的位置:

内核:/boot/vmlinuz或/boot/vmlinuz-version;
内核解压缩所需RAMDisk:/boot/initrd(/boot/initrd-version);
内核模块:/lib/modules/version/kernel或/lib/modules/$(uname -r)/kernel;
内核源码:/usr/src/linux 或 /usr/src/kernels;
如果内核顺利被加载了,会有以下几个信息记录:
内核版本:/proc/version
系统内核功能:/proc/sys/kernel

#作者本地的内容:
cstriker1407@cstriker1407-x64:/boot$ ls
abi-3.16.0-39-generic     config-3.19.0-22-generic      initrd.img-3.19.0-22-generic  memtest86+_multiboot.bin      vmlinuz-3.16.0-39-generic
abi-3.19.0-22-generic     config-3.19.0-23-generic      initrd.img-3.19.0-23-generic  System.map-3.16.0-39-generic  vmlinuz-3.19.0-22-generic
abi-3.19.0-23-generic     config-3.19.0-25-generic      initrd.img-3.19.0-25-generic  System.map-3.19.0-22-generic  vmlinuz-3.19.0-23-generic
abi-3.19.0-25-generic     config-3.19.0-26-generic      initrd.img-3.19.0-26-generic  System.map-3.19.0-23-generic  vmlinuz-3.19.0-25-generic
abi-3.19.0-26-generic     grub                          memtest86+.bin                System.map-3.19.0-25-generic  vmlinuz-3.19.0-26-generic
config-3.16.0-39-generic  initrd.img-3.16.0-39-generic  memtest86+.elf                System.map-3.19.0-26-generic

假如我有个新硬件,偏偏我的系统不支持,要这样做:
1 重新编译内核,并加入最新的硬件驱动程序源码;
2 将该硬件的驱动程序编译成为模块,在启动时加载该模块。

内核模块

一、内核模块与依赖性

#作者本地的内容:
cstriker1407@cstriker1407-x64:/lib/modules/3.19.0-26-generic/kernel$ ls
arch  crypto  drivers  fs  kernel  lib  mm  net  sound  ubuntu
cstriker1407@cstriker1407-x64:/lib/modules/3.19.0-26-generic$ ls
build          modules.alias.bin    modules.dep.bin  modules.symbols
initrd         modules.builtin      modules.devname  modules.symbols.bin
kernel         modules.builtin.bin  modules.order    updates
modules.alias  modules.dep          modules.softdep  vdso

基本上,内核模块放置处是在 /lib/modules/$(uname -r)/kernel当中,里面的几个目录主要为:
arch    与硬件平台有关的选项
crypto    内核所支持的加密的技术
drivers    一些硬件的驱动程序
fs    内核所支持的文件系统
lib    一些函数库
net    与网络有关的各项协议数据,还有防火墙模块等
sound    与音效有关的各项模块
还有文件/lib/modules/$(uname -r)/modules.dep ,记录了内核支持的模块的依赖性。那么如何创建该文件呢?可以使用depmod命令来更新依赖性。如下:

cstriker1407@cstriker1407-x64:/boot$ depmod --help
Usage:
	depmod -[aA] [options] [forced_version]

If no arguments (except options) are given, "depmod -a" is assumed

depmod will output a dependency list suitable for the modprobe utility.

Options:
	-a, --all            Probe all modules #全部处理
	-A, --quick          Only does the work if there's a new module #只处理新加入的
	-e, --errsyms        Report not supplied symbols  #显示出目前已加载的不可执行的模块名称。
	-n, --show           Write the dependency file on stdout only
	-P, --symbol-prefix  Architecture symbol prefix
	-C, --config=PATH    Read configuration from PATH
	-v, --verbose        Enable verbose mode
	-w, --warn           Warn on duplicates
	-V, --version        show version
	-h, --help           show this help

The following options are useful for people managing distributions:
	-b, --basedir=DIR    Use an image of a module tree.
	-F, --filesyms=FILE  Use the file instead of the
	                     current kernel symbols.
	-E, --symvers=FILE   Use Module.symvers file to check
	                     symbol versions.
  
例子:我做好一个网卡驱动程序a.ko(内核模块名以.ko结尾),该如何更新内核的依赖性?
	#cp a.ko /lib/modules/$(uname -r)/kernel/drivers/net
	#depmod

二、内核模块的查看

查看目前模块加载了多少的模块。
#lsmod

查阅每个模块信息
cstriker1407@cstriker1407-x64:/$ modinfo --help
Usage:
	modinfo [options] filename [args]
Options:
	-a, --author                Print only 'author'
	-d, --description           Print only 'description'
	-l, --license               Print only 'license'
	-p, --parameters            Print only 'parm'
	-n, --filename              Print only 'filename'
	-0, --null                  Use \0 instead of \n
	-F, --field=FIELD           Print only provided FIELD
	-k, --set-version=VERSION   Use VERSION instead of `uname -r`
	-b, --basedir=DIR           Use DIR as filesystem root for /lib/modules
	-V, --version               Show version
	-h, --help                  Show this help

cstriker1407@cstriker1407-x64:/$ modinfo rtl8192cu -d
Realtek 8192C/8188C 802.11n USB wireless

 三、内核模块的加载与删除

最好使用modprobe这个命令加载模块,因为modprobe会主动查找modules.dep的内容,解决了依赖性后,才决定需要加载的模块有哪些。insmod/rmmod则完全由用户自行加载一个完整文件名的模块,并不会主动分析模块依赖性。

cstriker1407@cstriker1407-x64:/$ insmod --help
Usage:
	insmod [options] filename [args]
Options:
	-V, --version     show version
	-h, --help        show this help

例子:尝试载入cifs.ko这个文件系统模块
#insmod /lib/modules/$(uname -r)/kernel/fs/cifs/cifs.ko
记住,一定要是完整的文件名。
cstriker1407@cstriker1407-x64:/$ rmmod --help
Usage:
	rmmod [options] modulename ...
Options:
	-f, --force       forces a module unload and may crash your
	                  machine. This requires Forced Module Removal
	                  option in your kernel. DANGEROUS
	-s, --syslog      print to syslog, not stderr
	-v, --verbose     enables more messages
	-V, --version     show version
	-h, --help        show this help

insmod与rmmod的问题是,你必须自行找到模块的完整文件名才行。所以,我们一般使用modprobe.

例子:加载cifs模块
#modprobe cifs
很方便,因为我们根本不用知道完整的模块文件名.

作者本地的modprobe命令帮助:
cstriker1407@cstriker1407-x64:/$ modprobe --help
Usage:
	modprobe [options] [-i] [-b] modulename
	modprobe [options] -a [-i] [-b] modulename [modulename...]
	modprobe [options] -r [-i] modulename
	modprobe [options] -r -a [-i] modulename [modulename...]
	modprobe [options] -c
	modprobe [options] --dump-modversions filename
Management Options:
	-a, --all                   Consider every non-argument to
	                            be a module name to be inserted
	                            or removed (-r)
	-r, --remove                Remove modules instead of inserting
	    --remove-dependencies   Also remove modules depending on it
	-R, --resolve-alias         Only lookup and print alias and exit
	    --first-time            Fail if module already inserted or removed
	-i, --ignore-install        Ignore install commands
	-i, --ignore-remove         Ignore remove commands
	-b, --use-blacklist         Apply blacklist to resolved alias.
	-f, --force                 Force module insertion or removal.
	                            implies --force-modversions and
	                            --force-vermagic
	    --force-modversion      Ignore module's version
	    --force-vermagic        Ignore module's version magic

Query Options:
	-D, --show-depends          Only print module dependencies and exit
	-c, --showconfig            Print out known configuration and exit
	-c, --show-config           Same as --showconfig
	    --show-modversions      Dump module symbol version and exit
	    --dump-modversions      Same as --show-modversions

General Options:
	-n, --dry-run               Do not execute operations, just print out
	-n, --show                  Same as --dry-run
	-C, --config=FILE           Use FILE instead of default search paths
	-d, --dirname=DIR           Use DIR as filesystem root for /lib/modules
	-S, --set-version=VERSION   Use VERSION instead of `uname -r`
	-s, --syslog                print to syslog, not stderr
	-q, --quiet                 disable messages
	-v, --verbose               enables more messages
	-V, --version               show version
	-h, --help                  show this help

 

发表评论

10 + 13 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据