Udev and autofs rules for automounting removable devices

From Devpit
Jump to: navigation, search

Disclaimer: This how-to may only work on Debian. Debian uses the udev and sysfs device assignment convention in the 2.6.12 and later Linux kernels. This how-to assumes you have a udev and sysfs enabled system.

Introduction

This how-to article highlights a method for auto-mounting removable block storage device like iPods, usbkeys, and digital camera when they are accessed from a ubiquitous mount point.

The user creates a permanent mount point in /mnt or /media, e.g. /mnt/usbkey similar to what is often done for CD drives. When this directory is accessed the device it is configured to access is automatically mounted with the proper user permissions. When the directory is left and is no longer needed a timeout value directs the OS to automatically umount the device.

Device hotplug background

The general difficulty involving removable devices in Linux is that you usually aren't guaranteed the same /dev mount point each time you plug in a particular device. When a device is attached to an arbitrary USB port the kernel assigns a /dev/ entry based upon the location to which it was plugged in and/or the number of devices already attached. This can be further complicated by usb hubs.

e.g. a usbkey could show up in /dev/sda1, /dev/sdb2, /dev/sda2, etc depending on whether a usb hub is attached to one of the usb ports.

In order to access the device as a volume you have to mount it. This is usually achieved via a mount point in /mnt for each device, e.g. /mnt/usbkey.

Udev

A special udev rule can be created for each removable device such that the kernel can query the hotplugged devices vendor and product information and create a symlinked ubiquitous /dev/ entry to the actual device entry.

e.g. a CENTON usbkey plugged into usb port /dev/sda can have a symlink created for /dev/usbkey

Provided a properly created udev rule when a CENTON usbkey is plugged into any usb port on the system the kernel will provide access to it through /dev/usbkey. This is much better than searching through /dev/ for the correct device.

Mounting the device

There are a few methods for mounting the device. One is to provide an entry in /etc/fstab which can give the mount direction for mounting /mnt/usbkey when /dev/usbkey appears. The mount is generally required to be manual.

An alternative is to use autofs for auto mounting.

Autofs

Using autofs to auto mount a device, when the device is plugged into the system autofs makes note of it. When the user accesses the predefined mount point for the device in /mnt autofs will automatically mount the device if it is available.

Autofs has a --timeout feature that instructs autofs to automatically umount the device after a period of inactivity (define as the operating system holding zero references to the mounted directory).

You will need autofs installed to use autofs to auto mount the device. Autofs is controlled by a daemon controlled via /etc/init.d/autofs. This daemon watches device hotplug events and if it recognizes a rule for an exposed device entry then it will attempt to auto mount the device to the particular mount point based upon the rules that are provided.

When using autofs the actual device mount points are controlled by autofs and exist in /var/autofs/. The device entries you create in /mnt/ should point to the autofs managed mount points in /var/autofs/removable.

Examples