Mdev/Automount USB

This article describes how to implement automounting of USB devices on a machine using mdev as the device manager. There are many links on the web describing how to implement automounting on embedded devices, running busybox with mdev. They all seem to assume only one user, namely root. It's more of a challenge to allow regular users to automount and unmount, without compromising overall system security. Unless otherwise specified, all steps below are to be done by root. This example is done on Gentoo Linux, but it is hopefully portable to any Linux distro.

Constraints (or "why do it this way?")

 * 1) Why use pmount?
 * Because pmount supports mounting FAT-based filesystems with desired permissions and assigns user root and group plugdev.
 * 1) Why does automounting use ?
 * Because pmount is currently hard-coded to automount in . It would require a change to the source code of pmount to use a different directory.
 * 1) Why are scripts launched with   rather than  ?
 * If a machine is running mdev as the device manager, it's guaranteed to have busybox present. Although this is currently implemented on Gentoo Linux, it is hopefully portable to other Linux distros, and embedded systems. There may be lightweight distros that do not have bash loaded.
 * 1) Why is the main automount script under ?
 * Because that's where mdev already puts some scripts under Gentoo. Keeping mdev-related scripts in the same area makes things easier to keep track of.

Preliminary Setup

 * This page assumes a Linux distro using busybox's mdev device manager as described in the mdev page.
 * Create the directory
 * Regular user accounts that need to access FAT-formatted USB keys must be added to group "plugdev".
 * Install and, if they have not been installed yet.
 * In create a file  (if it doesn't exist).  To the file add a line like:

Replace "USERID" and "HOSTNAME" with the actual regular userid and the actual hostname. If you have 2 or more users that need to unmount USB devices in, add a separate line for each one.

Scripts for use for unmounting
Create a file containing the following lines:

Then, as local user for each user that needs to unmount USB devices that have been automounted, create an executable script "um" in the user's directory containing the lines:

Theoretical background
For more information on mdev scripts, see Busybox's mdev primer. For the purposes of this page, here are the critical details:
 * On a machine running mdev, the pseudo-file should contain a string with the full path and filename of the hotplug handler. On Gentoo linux this is
 * When the system boots up, mdev is invoked as  to populate . This invocation supplies a very minimal environment, only the device name. When a USB device is inserted or removed, there can be one or more events. The insertion/removal events have additional environmental variables available. Testing for these variables allows a script to determine whether the event is device-population at bootup, or insertion/removal later on.
 * The file is a list of patterns that is consulted by mdev. If a pattern is matched, certain attributes can be set for the device (user:group, permissions, symlinks). But most important for our purposes, commands can be executed. This is how auto(un)mounting is accomplished.

Config files and scripts
sd[a-z].* root:disk 660 */lib/mdev/usbdisk_link and replace it with the line sd[a-z].* root:disk 660 */lib/mdev/autousb/automount
 * Create directory amd executable file
 * mkdir /lib/mdev/autousb
 * touch /lib/mdev/autousb/automount
 * chmod 744 /lib/mdev/autousb/automount
 * copy the contents of the automount script, listed at /automount into your file
 * In file make the following change. Comment out the line

Usage notes

 * Rebooting is not required. Automounting should now work.
 * When a USB device with recognizable partition(s) is inserted, a corresponding subdirectory is created in, e.g. , and mounted with filesystem type "auto"
 * If script files and sudo permissions have been set up as described above, a local user can unmount with the command
 * The subdirectory will not be deleted until the USB device is physically removed
 * Note that all unmounted subdirectories in will be removed anytime an external USB device is removed. Do not manually create create subdirectories in /media

External resources

 * Busybox's mdev documentation