现在的位置: 首页 > 综合 > 正文

实现udev/mdev自动挂载与卸载(linux) linux嵌入式系统中mdev的配置

2017年11月24日 ⁄ 综合 ⁄ 共 11235字 ⁄ 字号 评论关闭

转自:http://blog.csdn.net/liuqz2009/article/details/7230692

转自:http://blog.csdn.net/czh52911/article/details/6652791

       在网上有很多关于讲mdev的自动挂载基本上都是一个版本,经过测试自动挂载确实可行,但是关于自动卸载mdev似乎不能很好的支持,经过修改已经可以做到与udev的效果相似。不能在挂载的目录中进行热插拔,否则会出现问题,不过此问题在下次插入U盘时不会造成影响,可能对U盘有损坏。

本文介绍了mdev与udev两种方法来实现自动挂载,读者可根据需要任选其一即可。

        首先介绍一下mdev与udev之间的关系:

        mdev是busybox中的一个udev管理程序的一个精简版,他也可以实现设备节点的自动创建和设备的自动挂载,只是在实现的过程中有点差异,在发生热插拔时间的时候,mdev是被hotplug直接调用,这时mdev通过环境变量中的 ACTION 和 DEVPATH,来确定此次热插拔事件的动作以及影响了/sys中的那个目录。接着会看看这个目录中是否有“dev”的属性文件,如果有就利用这些信息为这个设备在/dev 下创建设备节点文件。

 

 

/***********************************************************************************************************************************************************************************/

1.mdev支持

①用busybox制作根文件系统的时候,要选择支持mdev机制

Linux System Utilities  --->   
           [*] mdev      
           [*]   Support /etc/mdev.conf
           [*]     Support command execution at device addition/removal

②在文件系统/etc/init.d/rsC文件中添加如下内容

Vi  /etc/init.d/rcS
        mount -t tmpfs mdev /dev 
        mount -t sysfs sysfs /sys
        mkdir /dev/pts
        mount -t devpts devpts /dev/pts

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

这些语句的添加在busybox的/doc/mdev.txt中可以找到。

 

③添加对热插拔事件的响应,实现U盘和SD卡的自动挂载。

Vi /etc/mdev.conf
       sd[a-z][0-9]      0:0 666        @/etc/mdev/udisk_insert                        

         sd[a-z]                   0:0 666          $/etc/mdev/udisk_remove

红色部分,是一个脚本,脚本内容可以根据我们的需要定制,可以实现挂载,卸载或其他一些功能。

注:@表示是在插入(创建设备结点)后执行后面的脚本,$表示在拔出(删除设备结点)前执行后面的脚本。

如下是自动挂载和卸载的脚本名称及其内容:

#!/bin/sh

if [ -d /sys/block/*/$MDEV ]  ; then

  mkdir -p /media/$MDEV

  mount /dev/$MDEV /media/$MDEV 

fi

根文件系统中的etc/mdev/udisk_remove文件内容:
        #!/bin/sh
        umount -l /media/$MDEV
        rm -rf /media/$MDEV
 
        #!/bin/sh
        umount -l /media/sd*
        rm -rf /media/sd*

修改为红色部分后能够自动挂载

以上两个脚本需要可执行权限:chmod +x  /etc/mdev/udisk_insert

               chmod +x etc/mdev/udisk_remove

/***********************************************************************************************************************************************************************************/

2.udev支持 

        linux传统上使用静态设备创建的方法,在dev下创建了大量的节点,而不管这些节点相应的硬件设备是否存在。采用udev的方法,系统检测到设备才会去创建这些设备对应的节点。

        这里我们简单的说一下udev的工作原理:

        udev是依赖于sysfs的,当系统中添加一个新的设备后,内核检测到后就会产生一个hotplug event并查找/proc/sys/kernel/hotplug去找出管理设备连接的用户空间程序,若udev已经启动,内核会通知udev去检测sysfs中关于这个新设备的信息并创建设备节点。如/dev/vcs,在/sys/class/tty/vcs/dev存放的是”7:0”,既/dev/vcs的主次设备号。并且udev还会根据/etc/udev/rules.d中的规则文件实现一些相应的功能。

下面我们介绍一下如何实现设备节点的自动创建及u盘或sd卡的自动挂载。

因为文件系统中默认是没有对udev进行支持的,所以我们移植一个udev。

1.下载udev源码udev-100.tar.bz2,并解压

网址:http://www.us.kernel.org/pub/linux/utils/kernel/hotplug

2.交叉编译。

修改makefile,具体修改如下:

        cross = arm-linux-

保存退出。

然后执行命令:make 进行编译,然后执行arm-linux-strip udev udevd udevstart udevinfo udevtest,并拷贝这些文件到目标板根文件/bin目录下面。

3.添加udev的支持

下面三种方法功能相同

        (1)并修改etc/init.d/rcs脚本,然后添加如下命令:

        /bin/mount -t sysfs sysfs /sys 

        /bin/mount -t tmpfs tmpfs /dev

        /bin/udevd --daemon

        /bin/udevstart

        (2)如果linuxrc是二进制文件的话 

        rm /linuxrc

        vi /linuxrc

        添加如下内容

        /bin/mount -t sysfs sysfs /sys

        /bin/mount -t tmpfs tmpfs /dev

        /bin/udevd --daemon

        /bin/udevstart

        exec /sbin/init

        (3)修改/etc/fstab为

        #device mount-point type options dump fsck order

        proc /proc proc defaults 0 0

        tmpfs /tmp tmpfs defaults 0 0

        sysfs /sys sysfs defaults 0 0

        tmpfs /dev tmpfs defaults 0 0

        修改/etc/init.d/rcs,添加如下内容

/bin/udevd --daemon

/bin/udevstart

        重新启动系统,文件系统就能够自动创建节点。

 

4.在/etc下创建udev目录

5.在/etc/udev下穿件目录rules.d和文件udev.conf

6.在udev.conf中添加如下内容

# udev.conf

        # the initial syslog(3) priority: "err", "info", "debug" or its

        # numerical equivalent. for runtime debugging, the daemons internal

        # state can be changed with: "udevcontrol log_priority=<value>".

        udev_log="err"

7.在rules.d下创建规则文件

如实现u盘自动挂载

        vim 11-add-usb.rules

添加如下内容

        action!="add",goto="farsight"

        kernel=="sd[a-z][0-9]",run+="/sbin/mount-usb.sh %k"

        label="farsight"

这个文件中action后是说明是什么事件,kernel后是说明是什么设备比如sda1,mmcblk0p1等,run这个设备插入后去执行哪个程序%k是传入这个程序的参数,这里%k=kernel的值也就是sda1等http://www.woaidiannao.com。

在/sbin/下创建mount-usb.sh文件添加如下内容 计算机

        #!/bin/sh

        /bin/mount -t vfat /dev/$1 /tmp

        sync

修改文件权限为其添加可执行的权限。

这样就实现了u盘的自动挂载,下面附上u盘的卸载规则文件和sd卡的文件

usb卸载

11-add-remove.rules

        action !="remove",goto="farsight"

        subsystem!="block",goto="farsight"

        kernel=="sd[a-z][0-9]",run+="/sbin/umount-usb.sh"

        label="farsight"

umount-usb.sh

        #!/bin/sh

        sync

        umount /tmp/

sd卡挂载

12-add-sd.rules

action!="add",goto="farsight"

        kernel=="mmcblk[0-9]p[0-9]",run+="/sbin/mount-sd.sh %k"

        label="farsight"

mount-sd.sh

        #!/bin/sh

        /bin/mount -t vfat /dev/$1 /tmp

        sync

 

sd卸载

12-remove-sd.rules

        action !="remove",goto="farsight"

        subsystem!="block",goto="farsight"

        kernel=="mmcblk*",run+="/sbin/umount-sd.sh"

        label="farsight"

umount-sd.sh

        #!/bin/sh

        sync

        /bin/umount /tmp/

=============================================================================================================================================

linux嵌入式系统中mdev的配置

转自:http://blog.csdn.net/guojiuming/article/details/6141978

1、mdev是什么。

busybox的mdev是一个精简了的udev管理程序,而udev是linux2.6内核引入的一种新的设备文件管理机制,用于取代老的devfs.udev最大的有点就是可以动态的管理/dev目录下的设备文件,而不用再系统初始化时就将可能要用到的设备都创建起来,还可以根据设备具体信息命名设备节点,而不是有内核统一分配。

 

2、如何在嵌入式系统中开启mdev。

  1)在busybox中添加对mdev的支持。

 

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

  #在/sys中挂载sysfs文件系统,它是mdev运行的基础条件之一

  mount -t sysfs sysfs /sys

 

  #设置系统的hotplug程序为mdev

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

  

  #mdev程序检索/sys和/proc中文件系统的设备信息,在/dev中生成相应的设备节点

  mdev –s

 

 

 

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

  CONFIG_PROC_FS=y

  CONFIG_PROC_SYSCTL=y

  CONFIG_HOTPLUG=y

  CONFIG_NET=y

 

  注:根据udev官方文件的介绍(http://git.kernel.org/?p=linux/hotplug/udev.git;a=blob;hb=HEAD;f=README),编译udev需要配置如下选项;最好采用2.6.27及其以后版本。

  CONFIG_HOTPLUG=y

  CONFIG_UEVENT_HELPER_PATH=""
  CONFIG_NET=y
  CONFIG_UNIX=y
  CONFIG_SYSFS=y
  CONFIG_SYSFS_DEPRECATED*=n
  CONFIG_PROC_FS=y
  CONFIG_TMPFS=y
  CONFIG_INOTIFY_USER=y
  CONFIG_SIGNALFD=y
  CONFIG_TMPFS_POSIX_ACL=y (user ACLs for device nodes)
  CONFIG_BLK_DEV_BSG=y (SCSI devices)
 
  4)建立/etc/uedv/udev.conf文件,一般定义如下几个参数:

udev_root:udev 产生的设备所存放的目录,默认值是 /dev/。建议不要修改该参数,因为很多应用程序默认会从该目录调用设备文件。
udev_db:udev 信息存放的数据库或者所在目录,默认值是 /dev/.udev.tdb。 
udev_rules:udev 规则文件的名字或者所在目录,默认值是 /etc/udev/rules.d/。 
udev_permissions:udev 权限文件的名字或者所在目录,默认值是 /etc/udev/permissions.d/。 
default_mode/ default_owner/ default_group:如果设备文件的权限没有在权限文件里指定,就使用该参数作为默认权限,默认值分别是:0600/root/root。
udev_log:是否需要 syslog记录 udev 日志的开关,默认值是 no。

  

  5)在/etc/udev/rules.d/目录下建立需要的规则文件*.rules,mdev会自动搜索这些文件内容,最终得到设备节点的名称、权限等。关于规则文件的如何撰写,请参见“跟我一起写udev规则” 一文。

 

      

 

  

 

执行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.txt

-------------
 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.

-----------
 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.

Here's a typical code snippet from the init script:
[0] mount -t proc proc /proc
[1] mount -t sysfs sysfs /sys
[2] echo /bin/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=/bin/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 /bin/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.

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 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
660 permissions.

The file has the format:
    <device regex>       <uid>:<gid> <octal permissions>
 or @<maj[,min1[-min2]]> <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
create your own total match like so:
    .* 1:1 777

You can rename/move device nodes by using the next optional field.
    <device regex> <uid>:<gid> <octal 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.
    hdb 0:3 660 =cdrom
This will rename "hdb" to "cdrom".

Similarly, ">path" renames/moves the device but it also creates
a direct symlink /dev/DEVNAME to the renamed/moved device.

If you also enable support for executing your own commands, then the file has
the format:
    <device regex> <uid>:<gid> <octal permissions> [=path] [@|$|*<command>]
    or
    <device regex> <uid>:<gid> <octal permissions> [>path] [@|$|*<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.

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".

----------
 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.

------------
 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+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 '\n' to the file.
NB: mdev recognizes /dev/mdev.seq consisting of single '\n' characher
as a special case. IOW: this will not make your first hotplug event
to stall for two seconds.

抱歉!评论已关闭.