Automount hint (once again)

Cort Tompkins rtompkin at cs.odu.edu
Mon Mar 18 20:04:22 PST 2002


Hello,

Updated version of automount hint attached.

I caught another typo (left the c off the end of the patch instructions), and 
discovered that naming my startup script automount meant bad things happened 
when the script itself did a "killall automount".  Imagine that.

Hopefully this will be the last update for a few more hours at least.

Cort Tompkins
-------------- next part --------------
TITLE:		Setting up Automount
LFS VERSION:	Any
AUTHOR:		R. Cort Tompkins <rtompkin at cs.odu.edu>

SYNOPSIS:
	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.  

HINT:
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 
ftp://ftp.kernel.org/pub/linux/daemons/autofs (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 > \ 
	  modules/lookup_program.c

	$ ./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/auto_mount << "EOF"
#!/bin/bash
# Begin /etc/init.d/auto_mount
#
# Automount script by Cort Tompkins - rtompkin at cs.odu.edu, derived
# from ethnet script by Gerard Beekmans - gerard at linuxfromscratch.org

source /etc/init.d/functions

case "$1" in
	start)

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

Please resist the temptation to name the startup script "automount."  This
means that the script will get the same kill signals we send to automount
proper.

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

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/auto_mount K___auto_mount
  > cd ..
  > done
$ for rl in $(seq 3 5); do
  > cd rc${rl}.d
  > ln -s ../init.d/auto_mount S___auto_mount
  > 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:

xxxx.auto:
MOUNTPOINT=/path/to/mountdir
TIMEOUT=999

xxxx.map:
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 
auto_mount 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 
auto_mount script can handle any number of .map/.auto pairs (so long as the 
pairs both have the same prefix).  Here are some examples:

--
cdrom.auto:
MOUNTPOINT=/mnt
TIMEOUT=5

cdrom.map:
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
automount:

$ 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:
--
floppy.auto:
MOUNTPOINT=/mnt
TIMEOUT=1

floppy.map:
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:
--
samba.auto:
MOUNTPOINT=/smb/win2kbox
TIMEOUT=300

samba.map:
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 
/smb/win2kbox/d

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:
--
floppy.auto:
MOUNTPOINT=/mnt
TIMEOUT=1

samba.auto:
MOUNTPOINT=/mnt/samba
TIMEOUT=300
--

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



More information about the hints mailing list