The use method and principle of mdev and the realization of automatic mounting of U disk or SD card

Udev realizes the automatic mounting of U disk or SD card. Here we will talk about how to realize this function with mdev. The use of mdev has been detailed in the mdev.txt file in busybox.
 
mdev is a simplified version of a udev management program in busybox. It can also realize automatic creation of device nodes and automatic mounting of devices, but there are some differences in the implementation process. When hot plugging time occurs, mdev is Called directly by hotplug, at this time mdev uses the ACTION and DEVPATH in the environment variable to determine the action of the hot plug event and the directory in /sys that is affected. Then we will check if there is a "dev" attribute file in this directory, and if there is, use this information to create a device node file under /dev for this device.
Here is how to make our system support mdev.
 
1. When using busybox to make a root file system, choose to support mdev
Linux System Utilities  --->  
           [*] mdev     
           [*]   Support /etc/mdev.conf
           [*]     Support command execution at device addition/removal
2. Add the following content to the file system
Vim /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
The addition of these statements can be found in the mdev manual.
3. Add the response to the hot swap event to realize the automatic mounting of U disk and SD card.
Vim /etc/mdev.conf
        mmcblk[0-9]p[0-9] 0:0 666 @ /etc/sd_card_inserting
        mmcblk[0-9] 0:0 666 $ /etc/sd_card_removing
        sd[a-z] [0-9] 0:0 666 @ /etc/usb/usb_inserting
        sd[a-z] 0:0 666 $ /etc/usb/usb_removing
The red part is a script. The content of the script can be customized according to our needs, and can be mounted, unloaded or other functions.
The following is the script for automatic mounting and unmounting:
/etc/sd_card_inserting
        #!/bin/sh
        mount -t vfat /dev/mmcblk0p1 /mnt/sd
/etc/sd_card_removing
        #!/bin/sh
        sync
        umount /mnt/sd
 
This function is to be used in the project. When the U disk is inserted, it is required to automatically detect the insertion of the U disk and automatically start the application.
 
我的根文件系统是用busybox构建的,其中,设备文件的生成是使用busybox中的mdev生成。Mdev除了生成设备文件外,还能检测到设备的插入拨出,即热插拨检测。那么,自动检测U盘的插入,mdev本来就可以做到,不用再做研究了,问题是如何自动启动程序呢这就要使用mdev.conf文件了,此文件在根文件系统的/etc目录下。Mdev检测到设备插入后,会根据此文件中的规则自动做一些相关的事。于是,我就写了如下一条规则,sda[0-9] 0:0 600 @(/autostart) 让mdev在检测到U盘插入后,自动运行autostart脚本。做到这一步都没困难,busybox的文档就有详细说明,不多解释了。
 
但我要启动的是一个QT4 embedded的程序。大家都知道,运行这样的程序是要配置一些相关环境变量的,比如,我的程序要使用到tslib的一些环境变量,一般是在/etc/profile文件中进行配置。我也在此文件中配置了相关环境变量,但发现用手工方法启动QT程序,环境变量是起作用的,程序工作正常。但是我用autostart脚本启运此程序,却发现环境变量没起作用。百思不得其解,想到很多办法都没有解决。最后,在PC上的linux发行版中运行QT程序时,得到启发,我发现QT程序相关的环境变量只在当前的控制台中起作用。那么,mdev自动起动autostart是不是另起了一个控制台呢?于是在atutostart的脚本中加入QT的相关环境变量设置,问题得到了解决。
 
-----------------------------------------------------------------------------------------
 
mdev是busybox自带的一个简化版的udev,适合于嵌入式的应用埸合。其具有使用简单的特点。它的作用就是在系统启动和热插拔或动态加载驱动程序时,自动产生驱动程序所需的节点文件。在以busybox为基础构建嵌入式linux的根文件系统时,使用它是最优的选择。
mdev使用
mdev的使用在busybox中的mdev.txt文档已经将得很详细了。但作为例子,我简单讲讲我的使用过程:
(1)在编译时加上对mdev的支持(我是使用的是busybox1.10.1):
    Linux System Utilities  --->
    mdev          
    Support /etc/mdev.conf
    Support command execution at device addition/removal
 
(2)在启动时加上使用mdev的命令:
我在自己创建的根文件系统(nfs)中的/linuxrc文件中添加了如下指令:
#挂载/sys为sysfs文件系统
    echo "----------mount /sys as sysfs"
    /bin/mount -t tmpfs mdev /dev
    /bin/mount -t sysfs sysfs /sys
    echo "----------Starting mdev......"
    /bin/echo /sbin/mdev > /proc/sys/kernel/hotplug
    mdev -s
注意:是/bin/echo /sbin/mdev > /proc/sys/kernel/hotplug,并非/bin/echo /bin/mdev > /proc/sys/kernel/hotplug。
busybox的文档有错!!
 
(3)在你的驱动中加上对类设备接口的支持。
  在驱动程序的初始化函数中,使用下述的类似语句,就能在类设备目录下添加包含设备号的名为“dev”的属性文件。并通过mdev在/dev目录下产生gpio_dev0的设备节点文件。
  my_class = class_create(THIS_MODULE, "gpio_class");
  if(IS_ERR(my_class)) {
    printk("Err: failed in creating class.\n");
    return -1;
  }
 
  class_device_create(my_class, NULL, MKDEV(gpio_major_number, 0), NULL, "gpio_dev%d" ,0);
  在驱动程序的清除程序段,加入以下语句,以完成清除工作。
  class_device_destroy(my_class, MKDEV(gpio_major_number, 0));
  class_destroy(my_class);
  需要的头文件是linux/device.h,因此程序的开始应加入下句
  #include <linux/device.h>
  另外,my_class是class类型的结构体指针,要在程序开始时声明成全局变量。
  struct class *my_class;
  上述程序中的gpio_major_number是设备的主节点号。可以换成需要的节点号。gpio_dev是最终生成的设备节点文件的名子。%d是用于以相同设备自动编号的。gpio_class是建立的class的名称,当驱动程序加载后,可以在/sys/class的目录下看到它。
  上述语句也不一定要在初始化和清除阶段使用,可以根据需要在其它地方使用。
 
(4)至于/etc/mdev.conf文件,可有可无,不影响使用,只是添加了些功能。
关于mdev的使用方法,我在网上找到一篇中文版的。大家可以到我上传的资源中下载。    
 
要想真正用好mdev,适当知道一下原理是必不可少的。现在简单介绍一下mdev的原理:
 
执行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 下创建设备节点文件。
 
最后,附上我在工作中编写的一段简单的gpio控制驱动程序。此程序没有什么功能,主要是做一些测试用的。有兴趣的朋友可以用它测试一下上述的mdev的使用方法。我用的是友善公司的mini2440开发板。
 
补充:1
为mdev的运行准备环境
mdev需要改写/dev和/sys两个目录。所以必须保证这两个目录是可写的(一般会用到sysfs,tmpfs。所以要重新编译内核)。
然后在你的启动脚本文件中加入
/bin/mdev -s
补充2:
 ·/etc/fstab
这是mount -a要读取的文本。根据需要编写。

Guess you like

Origin blog.csdn.net/qq_26690505/article/details/80591307