MDEV Primer
/**************************************************************************
* MDEV Primer
* 说明:
* 本文内容来自busybox的文档,以前一直想搞清楚热插拔的本质是什么,
* 设备节点是怎么创建,权限该如何给,如何存储设备是如何挂载的,等等信息。
*
* 2016-1-15 深圳 南山平山村 曾剑锋
*************************************************************************/ -------------
MDEV Primer
------------- 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的人来说,这篇文章显得是多余的。对于其他的人来说,
像一个黑夹子,不知道她是怎么工作的,根本没有头绪。这就是这份文档的意义所在:
基础教程。 -----------
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:
下面是典型的初始化脚本:
[] mount -t proc proc /proc
[] mount -t sysfs sysfs /sys
[] echo /sbin/mdev > /proc/sys/kernel/hotplug
[] mdev -s Alternatively, without procfs the above becomes:
当然在没有procfs的情况下,你也可以使用下面的形式。
[] mount -t sysfs sysfs /sys
[] sysctl -w kernel.hotplug=/sbin/mdev
[] mdev -s Of course, a more "full" setup would entail executing this before the previous
code snippet:
当然,如果你想要一个更加全面的设置如下,那么在前面的代码执行之前,你可以加入
以下的内容。
[] mount -t tmpfs -o size=64k,mode= tmpfs /dev
[] mkdir /dev/pts
[] mount -t devpts devpts /dev/pts The simple explanation here is that [] you need to have /sys mounted before
executing mdev. Then you [] 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 [] seed /dev with all the device nodes that were created
while the system was booting.
综上所述:
. 首先在执行mdev之前,你需要挂在sysfs文件系统挂在在/sys目录;
. 接下来指定执行/sbin/mdev无论什么时候设备插入、拔除,设备节点
都能够被创建或者销毁;
. 当系统跑起来之后,你可以在/dev下看到设备节点是否被创建。 For the "full" setup, you want to [] 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 [] mount the devpts filesystem on it.
对于完整的设置,你需要确认/dev下是一个tmpfs文件系统。同时你想要创建
/dev/pts挂载点,并将devpts文件系统挂在在上面。 -------------
MDEV Config (/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
permissions.
当系统需要除了默认的root/root 666的用户、分组、权限之外,Mdev有一个
操作配置文件用于设置设备节点的用户、分组、权限。 The file has the format:
文件格式如下:
[-][envmatch]<device regex> <uid>:<gid> <permissions>
or
[envmatch]@<maj[,min1[-min2]]> <uid>:<gid> <permissions>
or
$envvar=<regex> <uid>:<gid> <permissions> For example:
hd[a-z][-]* : The config file parsing stops at the first matching line. If no line is
matched, then the default of : is used. To set your own default, simply
create your own total match like so:
从前往后,最先匹配的到的那一行有效,之后系统会停止解析配置文件。
如果最新的设备节点与配置文件中没有一行匹配,那么使用默认的值0: 。
对于设置自己的默认值,只需要简简单单的加入如下配置就行了。 .* : You can rename/move device nodes by using the next optional field.
你可以重命名/移动设备节点通过如下的操作域。 <device regex> <uid>:<gid> <permissions> [=path] So if you want to place the device node into a subdirectory, make sure the path
has a trailing /. If you want to rename the device node, just place the name.
这样如果你想要将一个设备节点放入子目录下,确保目录后面有一个"/"尾巴,
如果你想要重命名设备节点,仅仅需要写上名字就行了。
hda : =drives/
This will move "hda" into the drives/ subdirectory.
将"hda"设备节点放入"drives/"目录下。
hdb : =cdrom
This will rename "hdb" to "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下创建一个软链
接到设备节点上。 For example:
---<---
# block devices
([hs]d[a-z]) root:disk >disk/%/
([hs]d[a-z])([-]+) root:disk >disk/%/%
mmcblk([-]+) root:disk >disk/mmc/%/
mmcblk([-]+)p([-]+) root:disk >disk/mmc/%/%
# network devices
(tun|tap) root:network >net/%
---<--- You can also prevent creation of device nodes with the 4th field as "!":
也可以防止创建设备节点使用第四个域的值:"!"
tty[a-z]. : !
pty[a-z]. : ! If you also enable support for executing your own commands, then the file has
the format:
如果你想当设备节点生成的时候执行你自己安排的命令,格式如下:
<device regex> <uid>:<gid> <permissions> [=path] [@|$|*<command>]
or
<device regex> <uid>:<gid> <permissions> [>path] [@|$|*<command>]
or
<device regex> <uid>:<gid> <permissions> [!] [@|$|*<command>] 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"。同时在心里一定要记住热插拔的标准输入、标准输出、标准错误
都被重定向到"/dev/null"。 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的环境变量$MDEV被设置为设备名,如果设备"hdc"匹配上了,
MDEV将被设置为"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系统接口加载固件到内核,在内核中抽取的文件是硬编码,所以看
一下你是否需要知道怎么在用户空间命名。 ------------
SEQUENCING
------------ Kernel does not serialize hotplug events. It increments SEQNUM environmental
variable for each successive hotplug invocation. Normally, mdev doesn't care.
This may reorder hotplug and hot-unplug events, with typical symptoms of
device nodes sometimes not created as expected. 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+. IOW: this will serialize concurrent mdev invocations. 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. Example(https://git.busybox.net/busybox/tree/examples/mdev.conf):
# Note: the first field is a regex matcher
# Syntax: %s %d:%d %s
# devices user:group mode null :
zero :
grsec : urandom : console :
fd0 :
hdc :
kmem :
mem :
port :
ptmx : sda[-] :
sdb[-] :
hda[-] : ttyS[-] :
tty[-] :
tty.+ :
MDEV Primer的更多相关文章
- mdev USB disk auto mount demo
/********************************************************************* * mdev USB disk auto mount de ...
- 关于热插拔usb hotplug /proc/sys/kernel mdev udev b...
转:http://www.360doc.com/content/10/0527/18/9922_29835045.shtml 这篇文章说的很好http://blog.chinaunix.net/u1/ ...
- linux中的热插拔和mdev机制
mdev手册(自己翻译的留着看) mdev实现U盘或SD卡的自动挂载 mdev的使用以及mdev.conf的规则配置--busybox linux中的热插拔和mdev机制 关于实现udev/mdev自 ...
- Linux设备模型(热插拔、mdev 与 firmware)【转】
转自:http://www.cnblogs.com/hnrainll/archive/2011/06/10/2077469.html 转自:http://blog.chinaunix.net/spac ...
- C Primer Plus 学习体会
本月刚刚开始学习<C primer plus>,之前课上草草学过一遍,讲到指针就结束了.现在重新开始看感觉难度不大只是刚开始接触有些语言细节比较琐碎.学习这一周的体会如下: 诸多前辈推荐的 ...
- C++ Primer Plus 第六版笔记
C++ Primer Plus 第六版笔记 关于对象声明的思考 转自:http://www.cnblogs.com/weiqubo/archive/2009/11/02/1930042.html C+ ...
- 《3D Math Primer for Graphics and Game Development》读书笔记2
<3D Math Primer for Graphics and Game Development>读书笔记2 上一篇得到了"矩阵等价于变换后的基向量"这一结论. 本篇 ...
- 《3D Math Primer for Graphics and Game Development》读书笔记1
<3D Math Primer for Graphics and Game Development>读书笔记1 本文是<3D Math Primer for Graphics and ...
- 再读《C++ Primer》——变量和基本类型
刚上大学那时,几个室友一块买了本<C++ Primer>第4版,看了一遍后就没怎么碰了,偶尔拿出来翻翻,当作工具书使用.后来知道有第5版了,一直觉得内容差不多吧.直到最近,再读其中的一些内 ...
随机推荐
- Matlab计算两集合间的海明距离
一.问题描述 B1[1 2 3 4 5 6 7 8 9] B2[12 13 14 21 31 41 51 1 1 81 1 1] 两个十进制矩阵,行数不一样,分别是n1和n2,列数必须一致,为nwo ...
- 精华阅读第 10 期 |解开阿尔法狗(AlphaGo)人工智能的画皮
谷歌用一个变了身的古老「穷举算法」,披上「神经网络」的画皮,假装「跨时代」的黑科技,忽悠广大「膜拜者」,「狮仙」我实在看不下去了,来揭一揭这只幺蛾子小狗的画皮. 本期是移动开发精英俱乐部的第10期推荐 ...
- Chp12: Testing
What the Interviewer is Looking for: Big Picture Understanding Knowing How the Pieces Fit Together O ...
- HDU3068 最长回文 Manacher算法
Manacher算法是O(n)求最长回文子串的算法,其原理很多别的博客都有介绍,代码用的是clj模板里的,写的确实是异常的简洁,现在的我只能理解个大概,下面这个网址的介绍比较接近于这个模板,以后再好好 ...
- ***解决PHP输出多余的空格或换行
用CI框架写APP后台接口的时候,返回的JSON前面有多余的2哥换行,首先排查的是BOM,结果问题依旧 再就是排查<?php ?> 标签外没有多余的回车.换行,结果发现确实有多余的换行,去 ...
- 01 - 编译链接第一个wxWidgets3.0例子
1. preprocessor #define __WXMSW__#define WXUSINGDLL 2. vc10中设置Include dir, lib dir, dll path VC++平台编 ...
- Oracle - 位图索引的适用条件
位图索引的适用条件 位图索引适合只有几个固定值的列,如性别.婚姻状况.行政区等等,而身份证号这种类型不适合用位图索引. 位图索引适合静态数据,而不适合索引频繁更新的列. 举个例子,有这样一个字段bus ...
- [转]Openstack Havana Dashboard测试和使用
转贴一篇陈沙克老师的文章:http://www.chenshake.com/openstack-havana-dashboard-to-test-and-use/ Openstack Havana D ...
- cogs 自己出的题目 题解报告
第一题很简单嘛,就是裸的动态树分治嘛 对于每一层的重心维护子树路径的信息和子树到上一层重心的点的信息 空间复杂度O(nlogn) 对于每一层我们按dis排序,之后记录军队数量的前缀和 查询的时候我们只 ...
- lintcode:Number of Islands 岛屿的个数
题目: 岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], ...