Script for automatic mounting of USB flash drives (Linux)

Source: Internet
Author: User

I have tested this method myself.

Udev is configured to enable udev to automatically mount the disk after receiving the message from the kernel. First, make sure that your system supports udev, if you have set udev directly, port udev first. The porting method is at the end of this article.

Automatic mounting:

1. Create 11-usb-mount.rules in the/etc/udev/rules. d directory,

The content in 11-usb-mount.rules is as follows:

Action! = "Add", Goto = "farsight"
Kernel = "SD [A-Z] [0-9]", run + = "/sbin/mount-usb.sh % K"
Label = "farsight"

2. Create a mount-usb.sh script file in the/sbin/directory

The mount-usb.sh content is as follows:

#! /Bin/sh
/Bin/Mount-T vfat/dev/$1/tmp
Sync
Restart to automatically mount the disk to the/tmp directory in Linux.

Automatic uninstall:

1. Create 11-usb-mount.rules in the/etc/udev/rules. d directory. The content is as follows:

Action! = "Remove", Goto = "farsight"
Subsystem! = "Block", Goto = "farsight"
Kernel = "SD [A-Z] [0-9]", run + = "/sbin/umount-usb.sh"
Label = "farsight"

2. Create a umount-usb.sh script file in the/sbin/directory

The mount-usb.sh content is as follows:

Action! = "Remove", Goto = "farsight"
Subsystem! = "Block", Goto = "farsight"
Kernel = "SD [A-Z] [0-9]", run + = "/sbin/umount-usb.sh"
Label = "farsight"

. Udev porting
1. Download The udevsource code udev-080.tar.bz2or udev-100.tar.bz2 and decompress it.

Http://download.csdn.net/detail/sphone89/4092734

2. Cross-compilation.

Modify the makefile as follows:
Cross = arm-Linux-

Save and exit.
Execute the command: Make to compile

If there is no accident, nine tools, including udev, udevcontrol, udevd, udevinfo, udevmonitor, udevsettle, udevstart, udevtest, and udevtrigger, will be generated in the current directory. In the embedded system, we only need udevd and udevstart to make udev work well. Other tools help us to view udev information, capture events, or perform more advanced operations.

. And copy these files to the/sbin directory (this directory depends on your system directory ).

3. Added udev support

The following three methods have the same functions
(1) modify the etc/init. d/RCS script and add the following command:
/Bin/Mount-T sysfs/sys
(Create this directory first)
/Bin/Mount-T tmpfs/dev
/Bin/udevd -- daemon
/Bin/udevstart
(2) If linuxrc is a binary file (saved)
Rm/linuxrc
VI/linuxrc
Add the following content:
/Bin/Mount-T sysfs/sys
(Create this directory first)
/Bin/Mount-T tmpfs/dev
/Bin/udevd -- daemon
/Bin/udevstart
Exec/sbin/init
(3) Modify/etc/fstab
# Device Mount-point type options dump fsck order
Proc/proc defaults 0 0
Tmpfs/tmp tmpfs defaults 0 0
Sysfs/sys sysfs defaults 0 0
Tmpfs/dev tmpfs defaults 0 0

My fstab content is:

# <File system> <mount point> <type> <Options> <dump> <pass>
/Dev/root/auto defaults, errors = remount-ro 0 0
None/proc noauto 0 0
None/sys sysfs noauto 0 0
None/dev/PTS devpts noauto, gid = 5, mode = 620 0 0
None/dev/SHM tmpfs noauto 0 0

######################################## ######
Modify/etc/init. d/RCs and add the following content:
/Bin/udevd -- daemon
/Bin/udevstart
Write some of the above. After the system is restarted, our file system will have the function of automatically creating nodes.

Ii. automatic mounting of USB flash drives or SD cards

1. Create the udev directory under/etc

2. Under the/etc/udev directory rules. D and the file udev. conf

3. Add the following content to 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"

4. Create a rule file under rules. D.

For example, the USB flash drive is automatically mounted.
Vim 11-add-usb.rules

Add the following content:
Action! = "Add", Goto = "farsight"
Kernel = "SD [A-Z] [0-9]", run + = "/sbin/mount-usb.sh % K"
Label = "farsight"

In this file, action indicates what the event is, and kernel indicates what the device is, such as sda1 and mmcblk0p1, after the device is inserted, run % K indicates the program parameter. Here, % K = the value of kernel is sda1.

Create a mount-usb.sh file under/sbin/Add the following content
#! /Bin/sh
/Bin/Mount-T vfat/dev/$1/tmp
Sync

Modify file permissions to add executable permissions.

 

 

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.