Automount hint

Cort Tompkins rtompkin at
Sun Mar 17 11:53:05 PST 2002


I sent you an automount hint yesterday, but I just noticed a typo (/mnt/cdrom 
should be /mnt/cd) so here is the updated version.

Cort Tompkins

-------------- next part --------------
TITLE:		Setting up Automount
AUTHOR:		R. Cort Tompkins <rtompkin at>

	The mounting and unmounting of removable media is a tedious task, 
especially when it needs to be done by unprivileged users.  Automount is a 
utility that will automatically unmount specified devices after a given 
interval, and then remount them automatically upon subsequent access.  This 
makes the mount/unmount process relatively transparent to the end user.  

To get started you'll need a few things:
1) Automount support in the kernel.  This is compiled into the kernel by 
default unless you explicitly removed it during kernel configuration.  If this 
is the case, reconfigure your kernel (i.e. "make menuconfig" in your kernel 
source directory) and enable Automount v4 as a built-in feature under the 
"File Systems" heading.

2) The automount user utilities.  Download the latest version 3 utilities from (autofs-3.1.7.tar.bz2 at the time
of this writing).  Extract this archive and cd into it.  Before compilation
and installation, we'll take preemptive action to stop a compile-time error:

	$ cp modules/lookup_program.c modules/lookup_program.c.old
	$ sed "s/OPEN_MAX/FOPEN_MAX/" modules/lookup_program.c.old > \ 

	$ ./configure --prefix=/usr --sbindir=/sbin
	$ make
	$ make install

If you look in the sample subdirectory, you'll find rc.autofs, a startup script
designed to help automate the automounting process.  Use this if you wish, but 
I will give instructions for creating a slightly simpler script which should 
help you better understand the workings of automount.

First we'll create the script itself, as root:

$ cat > /etc/init.d/automount << "EOF"
# Begin /etc/init.d/automount
# Automount script by Cort Tompkins - rtompkin at, derived
# from ethnet script by Gerard Beekmans - gerard at

source /etc/init.d/functions

case "$1" in

	for mountspec in $(/bin/ls /etc/sysconfig/automount-config/*.auto)
		source $mountspec
		echo -n "Starting automount for group ${MOUNT_BASE##*/}  ..."
		/sbin/automount --timeout=${TIMEOUT} $MOUNTPOINT file \
	# assume all instances of automount were started by this script
		echo -n "Stopping automount ..."
		# Unmount everything mounted by automount
		/bin/killall -USR1 automount
		/bin/killall automount
		$0 stop
		sleep 1
		$0 start
		echo "Usage: $0 {start|stop|restart}"
		exit 1
# End /etc/init.d/automount

Give the script proper permissions:
$ chmod 754 /etc/init.d/automount

Since I use automount for network shares, I only want it to be running when
in a networkable state.  On the very rare occasion that you find yourself in
an unnetworked runlevel, you can always mount your removable devices manually.
In the commands below, replace "___" with numbers such that automount will be
started after your ethnet script, and killed before it (if you plan to use it
for network shares; otherwise just use your judgement).
$ cd /etc
$ for rl in $(seq 0 2; echo 6); do
  > cd rc${rl}.d 
  > ln -s ../init.d/automount K___automount
  > cd ..
  > done
$ for rl in $(seq 3 5); do
  > cd rc${rl}.d
  > ln -s ../init.d/automount S___automount
  > cd ..
  > done

Create the sysconfig directory that the script will use:
$ mkdir /etc/sysconfig/automount-config

Inside /etc/sysconfig/automount-config/, you'll create pairs of files for each 
group of devices you wish to automount.  The format of the files is as follows:
MOUNTNAME	-fstype=FSTYPE[,OPTIONS]	:/path/to/device
MOUNTNAME	-fstype=FSTYPE[,OPTIONS]	:/path/to/device

The format of the .auto files is arbitrarily determined by the workings of the 
automount script; more information on the format of the .map files can be 
found using "man 5 autofs".  The OPTIONS used in the .map file are the same 
options you would pass to mount with the -o flag.  Note that you can have 
multiple entries in a .map file, but they will all assume the same mountpoint 
and timeout specified in the corresponding .auto file of the same prefix.  The 
automount script can handle any number of .map/.auto pairs (so long as the 
pairs both have the same prefix).  Here are some examples:

cd	-fstype=iso9660,ro	:/dev/cdrom
The above pair will automount /dev/cdrom at /mnt/cd with a timeout of 5 
seconds.  This means that after five seconds of inactivity the cdrom device will
be automatically unmounted, allowing you to put in a new CD to be automatically
remounted upon subsequent access.  You can verify this after initializing

$ ls /mnt/cd; mount

You will see that the cdrom is mounted. Wait five seconds.

$ mount

If everything is working properly, automount will have unmounted the cdrom.
Subsequent access to /mnt/cd will cause it to be remounted.

Most CD drives lock their CD trays while mounted, preventing you from removing 
the media while the drive is mounted.  Floppy drives, on the other hand, have 
no such protection; it is best to keep their timeout value as small as possible:
flop	-fstype=auto	:/dev/fd0
This mounts the floppy drive at /mnt/flop.  Note that a timeout of 0 will 
disable the automatic unmounting altogether.  

Automount can also be used to mount network shares:
c -fstype=smbfs,username=samba,password=xxxx	://win2kbox/c
d -fstype=smbfs,username=samba,password=xxxx	://win2kbox/d
The two samba shares specified will be automounted at /smb/win2kbox/c and 

One final note of caution: nesting mountpoints does not seem to work.  Thus, the
following two .auto files (together with their corresponding .map files) will 
not achieve the desired results:

Feel free to e-mail me with questions or suggestions.

More information about the hints mailing list