Automatically Mounting USB, Camera, DVD, Hard Disk and CDROM

USB drives, DVDs, CDROMs and other drives must be mounted before they are used. This article explains how any drive can be mounted automatically when it is needed. For most Linux distributions, such as Fedora Core, automounting works without installing new software. Example configuration for most common devices is provided.

Benefits of method described here: works with or without graphical desktop, uses standard software, configuration works for many users out-of-the-box, no need to install new software.

(c) 2004 Tero Karvinen www.iki.fi/karvinen. Latest version of this document is at www.iki.fi/karvinen/mounttero/

Usage

For example, when user opens directory /mnt/auto/usb/, digital camera is automatically mounted and all the pictures shown in the directory. After four seconds of inactivity, device is unmounted and can be detached.

Configuration for USB mass storage, DVD, CDROM, IDE hard drives

Create the directories used by the automounter. The directory is the one mentioned in /etc/auto.master:

# mkdir -p /mnt/auto/autofs

Create auto.master to tell autofs daemon that /mnt/auto/autofs directory is handled according to auto.tero

# /etc/auto.master
# mountpoint		map	options	# see also: man 8 autofs
/mnt/auto/autofs	/etc/auto.tero  --timeout=4

List the actual mountpoints and devices in the automounter map

# /etc/auto.tero 
# http://iki.fi/karvinen/linux/doc/automatic-mounting-autofs.html
# mountpoint_key options location_device # man 5 autofs
cdrom	-fstype=iso9660,ro,nosuid,nodev,user    :/dev/cdrom
cdrom1	-fstype=auto,ro,nosuid,nodev,user       :/dev/cdrom1
dvd	-fstype=auto,ro,nosuid,nodev,user	:/dev/dvd	
usb	-fstype=auto,nosuid,nodev,noexec,user,gid=100,umask=000 :/dev/sda1
floppy          -fstype=auto,nosuid,nodev,noexec,user,gid=100,umask=000 :/dev/fd0

hda1            -fstype=auto,nosuid,nodev,user  :/dev/hda1
hda2            -fstype=auto,nosuid,nodev,user  :/dev/hda2
hda3            -fstype=auto,nosuid,nodev,user  :/dev/hda3
hda4            -fstype=auto,nosuid,nodev,user  :/dev/hda4

hdb1            -fstype=auto,nosuid,nodev,user  :/dev/hdb1
hdb2            -fstype=auto,nosuid,nodev,user  :/dev/hdb2
hdb3            -fstype=auto,nosuid,nodev,user  :/dev/hdb3
hdb4            -fstype=auto,nosuid,nodev,user  :/dev/hdb4

hdc1            -fstype=auto,nosuid,nodev,user  :/dev/hdc1
hdc2            -fstype=auto,nosuid,nodev,user  :/dev/hdc2
hdc3            -fstype=auto,nosuid,nodev,user  :/dev/hdc3
hdc4            -fstype=auto,nosuid,nodev,user  :/dev/hdc4

hdd1            -fstype=auto,nosuid,nodev,user  :/dev/hdd1
hdd2            -fstype=auto,nosuid,nodev,user  :/dev/hdd2
hdd3            -fstype=auto,nosuid,nodev,user  :/dev/hdd3
hdd4            -fstype=auto,nosuid,nodev,user  :/dev/hdd4

# Serial ATA (SATA) disks are IDE emulated in Linux 2.6
hde1            -fstype=auto,nosuid,nodev,user  :/dev/hde1
hde2            -fstype=auto,nosuid,nodev,user  :/dev/hde2
hde3            -fstype=auto,nosuid,nodev,user  :/dev/hde3
hde4            -fstype=auto,nosuid,nodev,user  :/dev/hde4

# (c) 2003, 2004-05-29 Tero.Karvinen atta iki.fi

# /etc/init.d/autofs restart

Now drives are automatically mounted when you try to access them. You can test it by inserting a cdrom, and cd /mnt/auto/autofs/cdrom. The CDROM is automatically mounted, and ls should show you contents of the cd. When you cd to another directory, such as home directory (cd), CDROM is umounted in four seconds and the eject button in the drive starts working.

To see which drives are mountable (have discs in drive), you can create symlinks (similar to shortcuts) to the mountpoints. You can create the symlinks manually for each drive, for example

# cd /mnt/auto/
# ln -s autofs/cdrom cdrom

Or, if you are lazy, you can use this script

#!/bin/sh

# autotero.sh - automatically create symlinks for auto.tero autofs map.
# (c) 2004-05-29 Tero.Karvinen atta iki.fi, http://www.iki.fi/karvinen
# http://www.iki.fi/karvinen/linux/doc/automatic-mounting-autofs.html

MNT="/mnt/"

if [ -z $MAP ]; then
        MAP=`cat /etc/auto.master |gawk '/^[^#]/{print $2}'`
fi

if [ "0" != "$id" ]; then
        echo "Warning: You are currently not root. Try -t for test in current dir."
fi

if [ "-t" == "$1" ]; then
                echo "Testing only, directories and symlinks will be created to current dir"
                MNT=`pwd`
fi

cd $MNT
mkdir -p auto/  # for symlinks
cd auto
mkdir -p autofs # mountpoint 

# Create drives mentioned in auto.tero
MOUNTPOINTS=`cat $MAP|gawk '/^[^#]/{print $1}'`
for MPOINT in $MOUNTPOINTS
do
        echo $MPOINT
        ln -s autofs/$MPOINT $MPOINT
done
# autotero.sh

Now ls /mnt/auto shows unmountable devices as red, broken symlinks and mountable devices with normal colors. If you use tab to complete filenames, it only offers you mountable drives. For example, cd /mnt/auto/c<tab> fills the name to cdrom1 if cdrom0 does not have a disc inside.

If you don't like copy-pasting, you can also dowload the scripts.

Well done, now you can enjoy moveable media without mounting them by hand.

Alternatives for Automatic Mounting

Gnome automounter, KDE automounter, mntd, automount.

Notes and Copyright

Todo: screenshot of automounted camera in nautilus or konqueror. Link autofs mini-howto. More explanations. Link alternatives mntd. Explain why gnome mounter is bad. autofs-tero-links.sh. Make an RPM of this whole thing.

Copyright Tero Karvinen www.iki.fi/karvinen 2003 (initial release of configuration files only) 2004-05-29 (html file with some explanations). GNU Free Document License with no Invariant Sections, with the Front-Cover Texts and Back-Cover Texts being "Contains material by Tero Karvinen http://www.iki.fi/karvinen".

XHTML Basic 1.0 - top