关于热插拔usb hotplug /proc/sys/kernel mdev udev busybox

来源:互联网 发布:佳博打印软件 编辑:程序博客网 时间:2024/04/29 07:36
busybox的mdev是一个精简了的udev管理程序,它可以间接的以hotplug方式被内核调用,进而在arm平台上/dev目录下动态创建设备节点,因为hotplug会被内核发送uevent之前调用,所以如果动态可插拔设备很多,很频繁,那么每次内核都会调用elf格式的用户空间的hotplug,生成一个独立的hotplug程序来处理,所以hotplug是一个非常笨重的实现方法,但是在我们的arm开发板上,可插拔设备并没有想象中的频繁
目前还没有必要移植完全版的udev,所以完全可以在不复杂的嵌入式设备上使用mdev来管理动态节点,
方法也很简单,就是在rcS脚本中加入下面一句,将用户空间的应用程序路径传递给内核,
echo /sbin/mdev > /proc/sys/kernel/hotplug
之后kernel会在每次设备出现变动时调用上面一句传递进去的用户空间应用程序/sbin/mdev来处理对应的信息,进而mdev应用程序操作/dev目录下的设备,进行添加或删除,可以参考添加hotplug函数到内核,监控uevent信息
  mdev的另外一个很有用的地方是,他可以通过proc和sys文件系统来动态整理出注册的设备,进而到/dev/下生成他们对应的节点,
使用该功能之前必须mount上proc和sys文件系统,然后简单的执行下面的一句就弯成了:

  mdev -s




1, busybox 添加mdev支持


2, 在/etc/rc.d/rcS中添加

#mount filesystem

/bin/mount -a

#start mdev

#设置系统的hotplug 程序为mdev

echo /sbin/mdev > /proc/sys/kernel/hotplug  

#扫描并创建节点,这一步可不能少

mdev –s


/etc/fstab的内容如下:

proc    /proc   proc    defaults    0       0

none    /tmp    ramfs   defaults 0       0

mdev    /dev    ramfs   defaults 0       0

sysfs   /sys    sysfs   defaults    0       0


执行mdev -s :以‘-s’为参数调用位于 /sbin目录写的mdev(其实是个链接,作用是传递参数给/bin目录下的busybox程序并调用它),mdev扫描 /sys/class 和 /sys/block中所有的类设备目录,如果在目录中含有名为“dev”的文件,且文件中包含的是设备号,则mdev就利用这些信息为这个设备在/dev下创建设备节点文件。一般只在启动时才执行一次 “mdev -s”。


热插拔事件:由于启动时运行了命令:echo /sbin/mdev > /proc/sys/kernel/hotplug ,那么当有热插拔事件产生时,内核就会调用位于 /sbin目录的mdev。这时mdev通过环境变量中的 ACTION 和DEVPATH,来确定此次热插拔事件的动作以及影响了/sys中的那个目录。接着会看看这个目录中是否有“dev”的属性文件,如果有就利用这些信息为这个设备在/dev 下创建设备节点文件。


解决使用mdev时“cannot create /proc/sys/kernel/hotplug :nonexistent directory”错误

确保编译内核时编译如下选项:

CONFIG_PROC_FS=y

CONFIG_PROC_SYSCTL=y

CONFIG_HOTPLUG=y

CONFIG_NET=y

如果CONFIG_HOTPLUG和CONFIG_NET不选或没全选上的话,/proc/sys/kernel下将不会创建hotplug文件.(参见kernel/sysctl.c)

////////////////////////////////////////////////////////

这里有一段翻译的:

 

MDEV 入门(转)

Busybox-1.7.0/docs/mdev.txt
翻译:tekkamanninja Email:tekkamanninja@163.com - 1 -

-------------
MDEV Primer
MDEV 入门
-------------
For those of us who know how to use mdev, a primer might seem lame. For
这份文档对于那些知道如何使用 mdev 的人看来可能有些肤浅。
everyone else, mdev is a weird black box that they hear is awesome, but can't
但对于其他人,mdev 可能是一个神秘的黑匣子,以至让人敬畏。
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
mdev 有两个主要的应用:初始化对象和动态更新。
require sysfs support in the kernel and have it mounted at /sys. For dynamic
两个应用都需要内核 sysfs 的支持,且必须挂载到 /sys 。为了实现动态更新,
updates, you also need to have hotplugging enabled in your kernel.
你还必须在内核配置时增加热插拔支持(hotplugging)。
Here's a typical code snippet from the init script:
以下是系统初始化脚本中一个典型的使用mdev 的代码片段:
    [1] mount -t sysfs sysfs /sys
    [2] echo /bin/mdev > /proc/sys/kernel/hotplug
    [3] mdev -s

Of course, a more "full" setup would entail executing this before the previous
当然,一个对mdev 更完整的安装还必须在以上代码片段前执行下面的命令:
code snippet:
    [4] mount -t tmpfs mdev /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
简单说明一下上面的代码:[1]你必须在执行mdev 前挂载 /sys 。
executing mdev. Then you [2] instruct the kernel to execute /bin/mdev whenever
随后你 [2] 命令内核在增删设备时执行 /bin/mdev ,
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
最后你 [3] 设置mdev,让它在系统启动时创建所有的设备节点。
while the system was booting.
For the "full" setup, you want to [4] make sure /dev is a tmpfs filesystem
而对mdev 更完整的安装,你必须[4]确保 /dev 是 tmpfs 文件系统
(assuming you're running out of flash). Then you want to [5] create the
(假设文件系统在 flash 外运行)。 而且你必须 [5] 创建

-------------
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:
这个文件的格式如下:
    <device regex> <uid>:<gid> <octal permissions>
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
那么就使用默认的 0:0 660 。 你也可以通过在最后创建如下的全匹配
create your own total match like so:
行,来设置你自己的默认设置:
    .* 1:1 777
If you also enable support for executing your own commands, then the file has
如果你想 mdev 在找到匹配行时可以执行自定义的命令,那么文件格式如下:
the format:
    <device regex> <uid>:<gid> <octal 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
这些命令是通过系统函数(system())执行的(也就是说你在对shell 下命令)
command to the shell), so make sure you have a shell installed at /bin/sh.
,所以请确保你已在 /bin/sh 安装了shell。
For your convenience, the shell env var $MDEV is set to the device name. So if
为了方便,shell 的环境变量 $MDEV 会被设置成设备名。 例如
the device 'hdc' was matched, MDEV would be set to "hdc".
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
/lib/firmware/ 目录。 在运行时,内核将会按固件文件名调用 mdev ,
filename of the firmware which mdev will load out of /lib/firmware/ and into
之后 mdev 会通过 sysfs 接口将固件从 /lib/firmware/
the kernel via the sysfs interface. The exact filename is hardcoded in the
装载到内核。 确定的文件名被固化在内核中,
kernel, so look there if you need to want to know what to name the file in
如有必要,你必须知道如何在用户空间命名这个文件。
userspace.



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 不小心吃了蛆怎么办 一氧化二氢中毒怎么办 被家养松鼠咬了怎么办 碳酸碱在空气中怎么办 养殖水硬度太低怎么办 dnf主线任务没了怎么办 强制执行2年过了怎么办 绣球一天就蔫了怎么办 水插绣球蔫了怎么办 小孩吃到防潮剂怎么办 狗吃了防腐潮剂怎么办 新买的鲜花蔫了怎么办 咖啡粉受潮结坨怎么办 网购兰花不开花怎么办? 月季移植后蔫了怎么办 月季叶子掉光了怎么办 红鹦鹉鱼变白了怎么办 红鹦鹉鱼生病了怎么办 泡鱿鱼干没有碱怎么办 彩虹6号买了68怎么办 r6买了乞丐版怎么办 狙击精英3闪退怎么办 踩到地雷怎么办 知乎 蛋糕海绵灌胶了怎么办 蝴蝶的翅膀断了怎么办 培乐多彩泥干了怎么办 ps4被ban机了怎么办 塔吊离建筑物8米怎么办 手表带掉边缘皮怎么办 脸皮肤过敏痒肿怎么办 脸过敏发红发肿怎么办 皮肤过敏又红又肿怎么办 春天脸过敏发红痒怎么办 皮卡车后斗生锈怎么办 没带卸妆的东西怎么办 审车尾气过不了怎么办 违章停车条丢了怎么办 停车被城管贴条怎么办 违停告知单丢了怎么办 交通事故责任认定书不服怎么办 对交通事故认定书有异议怎么办