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 (mounttero is an autofs map), configuration works for many users out-of-the-box, no need to install new software. Fedora RPM available.

(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.

Automatic Configuration with RPM

If you want it to just work, su - to root, install the rpm and start autofs automounting daemon:

# rpm -Uvh http://www.iki.fi/karvinen/linux/doc/automatic-mounting-autofs-files/mounttero-0.4-4.i386.rpm
# /etc/init.d/autofs restart
# chkconfig autofs on

On some systems you must specifically allow normal users to use /mnt/auto:

# chmod -R a+rx /mnt/auto

Insert a cdrom in drive and cd /mnt/auto/cdrom. That's it, you just installed mounttero.

Manual Configuration

The rest of this document describes manual configuration of mounttero. If you installed the rpm, it did all this configuration automatically and you don't need manual configuration. The scripts below contain some latest and greatest version 0.5 improvements that have not made it to rpm yet, namely more usb partitions.

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=auto,ro,nosuid,nodev,user	:/dev/cdrom
cdrom1	-fstype=auto,ro,nosuid,nodev,user	:/dev/cdrom1
usb		-fstype=auto,nosuid,nodev,noexec,user,gid=100,umask=000	:/dev/sda1
# second and third partitions in usb device:
usb2	-fstype=auto,nosuid,nodev,noexec,user,gid=100,umask=000 :dev/sda2
usb3	-fstype=auto,nosuid,nodev,noexec,user,gid=100,umask=000 :dev/sda3
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, 2004-09-19 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 dowload the scripts. A beta quality RPM is available.

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

Links

Sundaram 2002: Automount mini-Howto

Autofs is the kernel based automounter included in many distributions, such as Fedora. Many automounters actually use autofs: mounttero, auto-autofs and mkautosmb. fm pop 2,50%, vit 0,01% rat 7,81

auto-autofs perl script to create autofs map. fm pop 0,98%, vit 0,02%, rat nr

Submount kernel module for automounting.

Mntd "is an automount daemon and shared library written in C for automatically mount hotplug devices like USB sticks, Compact Flash cards or other mountable devices on linux 2.6.x kernels." GPL. fm pop 0,12%, vit 0,00%, rat nr

Magic Mounter is a user space automounter that uses volume names as mountpoints. fm pop 0,15%, vit 0,00%, rat nr

Usb-mount adds icons of usb devices on desktop when hotplug notices them.

Amount "mounts all the partitions mentioned in a command line, runs the command, and then unmounts them again."

Virtualfs virtual file system mounter with plugins for various things. GPL. pop 0,19% vit 0,00%, rat nr

Supermount kernel patch for 2.0, 2.2 and 2.4. fm. As a competing program, autofs, comes with most distributions, recompiling the kernel looks like too much trouble.

am-utils fm

Gnome automounter. Can pop up a window when a medium is inserted to drive. Implemented on window manager/desktop, so that it does not work on text only mode. Does not work if you are not using Gnome. For me, it (gnome, nautilus?) sometimes refuses to eject cdrom even though no program seems to use the drive: "Device busy". Fixed with lsof|grep /mnt/cdrom and then kill -9 123456 the process number.

KDE automounter.

To automount samba network neighbourhood, there are SMB for Fuse (fm) and mkautosmb (fm)

AVFS - a virtual filesystem allows mounting of compressed files and remote volumes (sf). FUSE is part of AVFS. LUFS is similar tool with less plugins.

Ribeiro: Autofs no Slackware. Fix for Slackware 9 problem with rc.autofs "Unknown system, please port and contact autofs@linux.kernel.org".

Mounttero on the Press

Fresmeat record on mounttero, published 2004-05-30.

Notes and Copyright

Todo: screenshot of automounted camera in nautilus or konqueror. Change cdrom fstype from iso9660 to auto in rpm too, or dvds won't work. Remove "dvd" entry that duplicates cdrom or cdrom1 unnecessarily. Recompile rpm to include more usb partitions.

Thanks to Lauri Sandelin for usb partitions fix and Kosti Kartano for bugreport. Thanks to alias "xlynx" for Slackware 9 autofs fix.

Copyright Tero Karvinen www.iki.fi/karvinen 2003 (initial release of configuration files only) 2004-05-29 (html file, explanations, RPM) 2004-05-30 (Quick RPM installation instructions, links to tutorial and alternative automounters) 2004-09-19 (more usb partitions, link to Slackware 9 fix). GNU Free Document License with no Invariant Sections, with the Front-Cover Texts and Back-Cover Texts being "Contains a document by Tero Karvinen http://www.iki.fi/karvinen".

XHTML Basic 1.0 - top