Automatic mounting of USB flash drives

Source: Internet
Author: User

Some mainstream desktop systems (such as gnome, Kde, and xfce) Support automatic Mount USB disks. A widely spread saying is that Hal (Hardware Abstraction Layer) plays a role in automatic mounting, which is a misunderstanding. for the 2.6 kernel, udev is the main body that receives device connection or disconnects information directly from the kernel.
After udev obtains information from the kernel, it creates corresponding device nodes in the/dev directory according to some simple rules (Note: simple rules) and performs some related operations. what Hal does is add (or several) to the udev rule so that udev can pass the received information to Hal. next, Hal uses more complex rules to match and describe the currently connected hardware. for example, udev knows that the USB flash disk is connected and has a partition. Hal can further learn the volume name and other information of this partition.
The USB flash drives cannot be mounted automatically. GNOME and other desktop systems will start a daemon and get hardware information from Hal through D-bus, if you find that a USB flash drive is connected, mount it (actually calling pmount ).
The problem is, if you don't want to use the gnome, Kde, and xfce desktop systems, the USB flash drive automatic mounting function provided by them will become invalid. Is there a solution?
One of the solutionsLet udev do this!
The main function of udev is to create and delete device nodes in the/dev directory in real time, but it can also execute an additional program while creating nodes. the specific principles are not detailed here. Please read the article writing udev rules carefully if you have time. when writing rules, pay special attention to the differences between the singular and plural keywords such as kernel and subsystem. the plural (such as kernels) indicates that you want to use the attributes of the parent device to match. The singular (such as kernel) indicates that you want to match the attributes of the device.
Create the file 10_usbkey.rules in the/etc/udev/rules. d directory. The content of the file is as follows:
Kernel = "sda1", subsystem = "Block", run + = "/root/usbmount. sh % K"
Then, create the file usbmount. Sh in the/root directory, whose content is
#! /Bin/bash
Log =/var/log/USB-Hotplug. Log
Lap = lap: Action "> action =" add "]
Then
Mkdir $1
Mount-T vfat-O umask = 000, noatime, async, codePage = 936, iocharset = gb2312 \/dev/$1/Media/$1
Mount-t ntfs-O umask = 000, noatime, async, codePage = 936, iocharset = gb2312 \/dev/$1/Media/$1
Mount-T ext4-O umask = 000, noatime, async, codePage = 936, iocharset = gb2312 \/dev/$1/Media/$1
Elif [$ action = "Remove"]
Then
Umount-L/Media/$1
Rm-RF $1
Fi

And set the file property to executable, chmod A + x USB mount. Sh
Note that if locale on your Linux is a zh_CN.utf-8, you need to change the above iocharset = gb2312 to iocharset = utf8
This is a very simple but usable example. After the USB flash drive is inserted, it is automatically mounted to/Media/USB key, and automatically umount is pulled out. You can view/var/log/USB-Hotplug. Log, which contains some simple call records.

If you want to mount a USB flash drive in exfat format, you must download its support.

To mount an exfat USB flash drive:
Sudo apt-Get install Subversion
Sudo apt-Get install scons
Sudo apt-Get install libfuse-Dev
Sudo apt-Get install gcc
SVN Co http://exfat.googlecode.com/svn/trunk/ exfat-read-only
CD exfat-read-only
Scons
Sudo scons install
Rm-RF exfat-read-only
Add the following to the preceding script:
Mount-T exfat-fuse-O umask = 000, noatime, async, codePage = 936, iocharset = gb2312 \/dev/$1/Media/$1
After that, you can mount a USB flash drive in exfat format.

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.