Automatic mounting of USB flash drives and SD cards in Linux Embedded Systems

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_5f8665e701016le6.html

In LINUX embedded systems, we often use USB flash drives and SD card mounting. It is very difficult to manually mount or detach each time. We can use the following methods to automatically mount or detach USB flash drives and SD cards.
This requires mdev.
The procedure is as follows:
1. Add the following statement to/etc/init. d/RSC:
Echo/sbin/mdev>/proc/sys/kernel/hotplug

2. The file "medv. conf" under "/etc/" contains the following content:
SD [A-Z] [0-9] 0:0 666 @ (/etc/hotplug/insert. Sh $ mdev $ subsystem)
SD [A-Z] 0: 0 $666 $ (/etc/hotplug/remove. Sh $ mdev $ subsystem)
UB [A-Z] [0-9] 0: 0 666 @ (/etc/hotplug/insert. Sh $ mdev $ subsystem)
UB [A-Z] 0: 0 $666 $ (/etc/hotplug/remove. Sh $ mdev $ subsystem)
Mmcblk [0-9] P [0-9] 0: 0 666 @ (/etc/hotplug/insert. Sh $ mdev $ subsystem)
Mmcblk [0-9] 666 $ (/etc/hotplug/remove. Sh $ mdev $ subsystem)

3. Create the following folder hotplug under/etc/, and create the following file under the hotplug directory, including the following content
Insert. sh:
If [-n "$1"]; then
If [-B/dev/$1]; then
If [! -D/media]; then
Mkdir-P/Media
Fi
If [! -D/Media/$1]; then
Mkdir-P/Media/$1
Fi
Mount/dev/$1/Media/$1
If [$? -Ne 0]; then
Rm-RF/Media/$1
Fi
Fi
Fi
Remove. sh:
Mounts = $ (Mount | grep $1 | cut-d'-F3)
Umount $ mounts
Rm-RF $ mounts
(Note: The above code can be directly copied in actual use, but pay attention to line breaks)

Insert a USB flash drive, SD, and other devices to create a folder in the/Media Directory of the root directory and mount the disk files used by the peer.

The principle is:
Mdev is a simplified udev that comes with busybox and is suitable for embedded applications. It is easy to use. It automatically generates the node files required by the driver when the system starts and hot swapping or dynamically loads the driver. It is the best choice to build an embedded linux root file system based on busybox.

The mdev.txt document of mdevs used in busyboxhas been described in detail. But as an example, I will briefly talk about my use process:
(1) added mdev support during compilation:
Linux system utilities --->
Mdev

Support/etc/mdev. conf
Support Command Execution At device addition/removal
(2) Add the mdev command at startup:
I added the following command to the/linuxrc file in the self-created root file system (NFS:
# Mount/sys as sysfs File System
Echo "---------- Mount/sys as sysfs"
/Bin/Mount-T tmpfs mdev/dev
/Bin/Mount-T sysfs/sys
Echo "---------- starting mdev ......"
/Bin/ECHO/sbin/mdev>/proc/sys/kernel/hotplug
Mdev-S
Note: It is/bin/ECHO/sbin/mdev>/proc/sys/kernel/hotplug, not/bin/ECHO/bin/mdev>/proc/sys/kernel/hotplug.

(3) add support for Class Device interfaces to your driver.
In the initialization function of the driver, you can use the following similar statements to add an attribute file named "Dev" containing the device number under the category Device directory. And through mdev
Generate the gpio_dev0 device node file in the/dev directory.
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, mkdev (gpio_major_number, 0), null, "gpio_dev % d", 0 );
In the driver's Purge section, add the following statement to complete the cleanup.
Class_device_destroy (my_class, mkdev (gpio_major_number, 0 ));
Class_destroy (my_class );
The required header file is Linux/device. H. Therefore, add the following statement to the beginning of the program:
# Include
In addition, my_class is a struct pointer of the class type, which must be declared as a global variable at the beginning of the program.
Struct class * my_class;
Gpio_major_number in the above program is the master node number of the device. You can change it to the required node number. Gpio_dev is the name of the final generated device node file. % D is automatically numbered with the same device. Gpio_class is the name of the created class. After the driver is loaded, you can see it in the/sys/class directory. The preceding statements do not have to be used in the initialization and clearing phases. They can be used elsewhere as needed.
(4) As for the/etc/mdev. conf file, it is optional and does not affect usage, but only adds some functions.
Run mdev-S:
Call mdev written in the/sbin directory with '-S' as the parameter (in fact, it is a link to pass the parameter to the busybox program in the/bin directory and call it ), mdev scans all the class device directories in/sys/class and/sys/block. If the directory contains a file named "Dev" and the file contains a device number, then mdev uses this information to create a node file for the device under/dev. Generally, "mdev-s" is executed only once at startup ".
Hot swapping event:
Because the command echo/sbin/mdev>/proc/sys/kernel/hotplug is run at startup, when a hot swapping event is generated, the kernel will call mdev in the/sbin directory. At this time, mdev uses the action and devpath in the environment variable (these two variables are included in the system) to determine the action of the hot swapping event and the directory in/sys. Check whether the "Dev" attribute file exists in the directory. If yes, use this information to create a device node file for the device under/dev.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.