分类: LINUX
2013-12-27 17:11:55
------------- MDEV 入门
-------------
For those of us who know how to use mdev, a primer might seem lame. For everyone else, mdev is a weird black box that they hear is awesome, but can't seem to get their head around how it works. Thus, a primer.
对于知道如何使用mdev的人来说,这份文档是比较肤浅的。但对于其他人,mdev可能是一个神秘的黑匣子,以至让人敬畏。而这份文档不足以让他们知道mdev是如何工作的。因此这是一份入门文档。
----------- Basic Use
基本使用方法
-----------
Mdev has two primary uses: initial population and dynamic updates. Both require sysfs support in the kernel and have it mounted at /sys. For dynamic updates, you also need to have hotplugging enabled in your kernel.
Mdev有两个主要的应用:整体初始化和动态更新。它们都需要内核的sysfs支持,并且是挂载在/sys下。动态更新还需要内核支持热插拔功能。
Here's a typical code snippet from the init script: [0] mount -t proc proc /proc [1] mount -t sysfs sysfs /sys [2] echo /sbin/mdev > /proc/sys/kernel/hotplug [3] mdev –s
以下是系统初始化脚本中使用mdev的典型代码片段:
[0] mount –t proc proc /proc
[1] mount –t sysfs sysfs /sys
[2] echo /sbin/mdev > /proc/sys/kernel/hotplug
[3] mdev –s
Alternatively, without procfs the above becomes: [1] mount -t sysfs sysfs /sys [2] sysctl -w kernel.hotplug=/sbin/mdev [3] mdev –s
另外,没有procfs的话,上面得改成这样
[1] mount –t sysfs sysfs /sys
[2] sysctl –w kernel.hotplug=/sbin/mdev
[3] mdec –s
Of course, a more "full" setup would entail executing this before the previous code snippet: [4] mount -t tmpfs -o size=64k,mode=0755 tmpfs /dev [5] mkdir /dev/pts [6] mount -t devpts devpts /dev/pts
当然,一个完整的安装程序,在执行前面的代码片段之前执行下面的命令:
[4] mount –t tmpfs –o size=64k, mode=0755 tmpfs /dev
[5] mkdir /dev/pts
[6] mount –t devpts devpts /dev/pts
The simple explanation here is that [1] you need to have /sys mounted before executing mdev. Then you [2] instruct the kernel to execute /sbin/mdev whenever a device is added or removed so that the device node can be created or destroyed. Then you [3] seed /dev with all the device nodes that were created while the system was booting.
简单的解释一下上面的代码:
[1]在执行mdev之前要先挂载sys。
[2]随后告诉内核在添加或删除设备时执行/sbin/mdev,增加或设备节点。
[3]然后,设置mdev,让它在系统启动时创建所有的设备节点。
For the "full" setup, you want to [4] make sure /dev is a tmpfs filesystem (assuming you're running out of flash). Then you want to [5] create the /dev/pts mount point and finally [6] mount the devpts filesystem on it.
对于完整的mdev配置,必须确保/dev是一个tmpfs文件系统(假设在flash上运行)[4]。然后创建/dev/pts [5],然后把devpts文件系统挂载到/dev/pts上。
------------- MDEV Config (/etc/mdev.conf)
------------- MDEV配置 (/etc/mdev.conf)
-------------
Mdev has an optional config file for controlling ownership/permissions of device nodes if your system needs something more than the default root/root 660 permissions.
如果你的系统需要一个不是默认的root/root 660权限的话,Mdev有一个可选的配置文件,用来控制设备节点的所有者和权限。
The file has the format:
For example: hd[a-z][0-9]* 0:3 660
文件格式:
<设备的正则表达式>
或者:
@
The config file parsing stops at the first matching line. If no line is matched, then the default of 0:0 660 is used. To set your own default, simply create your own total match like so: .* 1:1 777
You can rename/move device nodes by using the next optional field.
配置文件在第一个匹配行处停止解析。如果没有匹配行,使用默认0:0 660。在配置文件的最后一行,设置如.* 1:1 777来创建自己的默认值。你可以重命名或者删除设备节点,通过使用下一个域。
<设备正则表达式>
所以如果你想重新命名设备节点,只要设置名字。
Had 0:3 660 =driver/
这样”had”将移动到”driver”目录下。
Hdb 0:3 660 =cdrom
Hdb将被重命名为cdrom。
Similarly, ">path" renames/moves the device but it also creates a direct symlink /dev/DEVNAME to the renamed/moved device.
类似,”>path”重命名/移动的设备,但是也创建了符号链接/dev/DEVNAME到重命名/移动的设备
If you also enable support for executing your own commands, then the file has the format:
如果你时能支持执行命令,那么文件的格式为:
或者
For example:
---8<--- # block devices ([hs]d[a-z]) root:disk 660 >disk/%1/0 ([hs]d[a-z])([0-9]+) root:disk 660 >disk/%1/%2 mmcblk([0-9]+) root:disk 660 >disk/mmc/%1/0 mmcblk([0-9]+)p([0-9]+) root:disk 660 >disk/mmc/%1/%2 # network devices (tun|tap) root:network 660 >net/%1
---8<---
例如:
([hs]d[a-z]) root:disk 660 >disk/%1/0 ([hs]d[a-z])([0-9]+) root:disk 660 >disk/%1/%2 mmcblk([0-9]+)
The special characters have the meaning: @ Run after creating the device.
$ Run before removing the device.
* Run both after creating and before removing the device.
特殊字符意思:
@:在创建设备后运行
$:在移除设备前运行
*:在创建设备后和移除设备前都运行。
The command is executed via the system() function (which means you're giving a command to the shell), so make sure you have a shell installed at /bin/sh. You should also keep in mind that the kernel executes hotplug helpers with stdin, stdout, and stderr connected to /dev/null.
命令是通过system()函数调用执行的,也就是说使用的命令时shell,所以要确保安装了/bin/sh。
For your convenience, the shell env var $MDEV is set to the device name. So if the device "hdc" was matched, MDEV would be set to "hdc".
为了方便,shell的环境env变量$MDEV被设置为设备名。所以,如果设备”hdc”匹配,MDEC将被设置为”hdc”。
---------- FIRMWARE
---------- 固件
----------
Some kernel device drivers need to request firmware at runtime in order to properly initialize a device. Place all such firmware files into the /lib/firmware/ directory. At runtime, the kernel will invoke mdev with the filename of the firmware which mdev will load out of /lib/firmware/ and into the kernel via the sysfs interface. The exact filename is hardcoded in the kernel, so look there if you need to know how to name the file in userspace.
一些内核设备驱动在运行时需要一些固件,实现设备正确的初始化。把所有固件文件都放在/lib/firmware/目录下。在运行时,内核将会按固件文件名调用 mdev ,之后 mdev 会通过 sysfs 接口将固件从 /lib/firmware/装载到内核。确定的文件名固化在内核中,如有必要,你必须知道如何在用户空间命名这个文件。
------------ SEQUENCING
------------
Kernel does not serialize hotplug events. It increments SEQNUM environmental variable for each successive hotplug . Normally, mdev doesn't care. This may reorder hotplug and hot-unplug events, with typical symptoms of device nodes sometimes not created as expected.
内核没有串行化热插拔事件。当正确调用哪个热插拔后,它增加了环境变量SENUM的值。通常情况下,mdev不关心这个。这将重新排列热插和热拔事件,典型的的设备节点有时创建的不是所期望的。
However, if /dev/mdev.seq file is found, mdev will compare its contents with SEQNUM. It will retry up to two seconds, waiting for them to match. If they match exactly (not even trailing '\n' is allowed), or if two seconds pass, mdev runs as usual, then it rewrites /dev/mdev.seq with SEQNUM+1.
然而,如果/dev/mdev.seq文件找到,mdev将会比较SEQNUM。它将重试最多两秒钟,等待它们匹配。如果精确匹配(不包括尾巴’\n’),或者如果超过2秒,mdev普通运行,然后重新写/dev/mdev.seq的SEQNUM+1
IOW: this will serialize concurrent mdev invocations.
IOW:这将序列号调用mdev
If you want to activate this feature, execute "echo >/dev/mdev.seq" prior to setting mdev to be the hotplug handler. This writes single '\n' to the file. NB: mdev recognizes /dev/mdev.seq consisting of single '\n' character as a special case. IOW: this will not make your first hotplug event to stall for two seconds.
如果你想激活这个特性。执行”echo>/dev/mdev.seq”,然后设置mdev。写’\n’到文件。注意:mdev识别 /dev/mdev.seq,以’\n’作为特殊字符。IOW:这不会使你的第一个热拔插事件拖延两秒钟。