zoukankan      html  css  js  c++  java
  • 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:
    下面是典型的初始化脚本:
    [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:
    当然在没有procfs的情况下,你也可以使用下面的形式。
    [1] mount -t sysfs sysfs /sys
    [2] sysctl -w kernel.hotplug=/sbin/mdev
    [3] mdev -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
    
    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之前,你需要挂在sysfs文件系统挂在在/sys目录;
        2. 接下来指定执行/sbin/mdev无论什么时候设备插入、拔除,设备节点
        都能够被创建或者销毁;
        3. 当系统跑起来之后,你可以在/dev下看到设备节点是否被创建。
    
    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.
    对于完整的设置,你需要确认/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][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:
    从前往后,最先匹配的到的那一行有效,之后系统会停止解析配置文件。
    如果最新的设备节点与配置文件中没有一行匹配,那么使用默认的值0:0 660。
    对于设置自己的默认值,只需要简简单单的加入如下配置就行了。
    
        .* 1:1 777
    
    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 0:3 660 =drives/
    This will move "hda" into the drives/ subdirectory.
    将"hda"设备节点放入"drives/"目录下。
        hdb 0:3 660 =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:
    ---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<---
    
    You can also prevent creation of device nodes with the 4th field as "!":
    也可以防止创建设备节点使用第四个域的值:"!"
        tty[a-z]. 0:0 660 !
        pty[a-z]. 0:0 660 !
    
    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 '
    ' is allowed),
    or if two seconds pass, mdev runs as usual, then it rewrites /dev/mdev.seq
    with SEQNUM+1.
    
    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 '
    ' to the file.
    NB: mdev recognizes /dev/mdev.seq consisting of single '
    ' 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      0:0  666
        zero      0:0  666
        grsec     0:0  660
        
        urandom   0:0  444
        
        console   0:5  600
        fd0       0:11 660
        hdc       0:6  660
        kmem      0:9  640
        mem       0:9  640
        port      0:9  640
        ptmx      0:5  660
        
        sda[0-9]  0:6  660
        sdb[0-9]  0:6  660
        hda[0-9]  0:6  660
        
        ttyS[0-9] 0:14 640
        tty[0-9]  0:0  660
        tty.+     0:5  660
  • 相关阅读:
    开通博客园
    ios关键字
    FirstDay
    An example for pysnmp
    remove debug symbols to a seperate file
    qemu下通过gdb调试内核时 遇到 evaluation of this expression requires the program to have a function "malloc" 错误的解决办法
    关于常识与知识的思考
    基于Qemu在ubuntu上构建linux学习环境
    How to download prebuilt toolchain
    诡异的打印异常BUG
  • 原文地址:https://www.cnblogs.com/zengjfgit/p/5133307.html
Copyright © 2011-2022 走看看