3 # $Id: rc.autofs.in,v 1.58 2005/04/11 11:30:54 raven Exp $
5 # rc file for automount using a Sun-style "master map".
6 # We first look for a local /etc/auto.master, then a YP
9 # On most distributions, this file should be called:
10 # /etc/rc.d/init.d/autofs or /etc/init.d/autofs
13 # For Redhat-ish systems
15 # chkconfig: 345 28 72
16 # processname: /usr/sbin/automount
17 # config: /etc/auto.master
18 # description: Automounts filesystems on demand
20 # This is used in the Debian distribution to determine the proper
21 # location for the S- and K-links to this init file.
22 # The following value is extracted by debstd to figure out how to
23 # generate the postinst script. Edit the field to change the way the
24 # script is registered through update-rc.d (see the manpage for
30 # Location of the automount daemon and the init directory
32 DAEMON=/usr/sbin/automount
33 prog=`basename $DAEMON`
36 test -e $DAEMON || exit 0
39 # Determine which kind of configuration we're using
44 PATH=/sbin:/usr/sbin:/bin:/usr/bin
48 # We can add local options here
49 # e.g. localoptions='rsize=8192,wsize=8192'
53 daemonoptions='--ghost '
56 # load custom settings
62 daemonoptions="$daemonoptions --timeout=$TIMEOUT"
66 # Check for all maps that are to be loaded
70 SOURCES=`grep ^automount: /etc/nsswitch.conf | \
71 sed -e 's/^.*://' -e 's/\[.*\]/ /g'`
73 if [ `echo $SOURCES | awk '{print NF}'` -gt 0 ]
88 /usr/bin/ypcat -k "$map" 2> /dev/null | sed -e '/^#/d' -e '/^$/d'
93 if [ -f /etc/auto.master ] ; then
94 cat /etc/auto.master | awk '{print $0}' | sed -e '/^#/d' -e '/^$/d' | (
95 while read auto_master_in
97 if [ "`echo $auto_master_in | grep '^+'`" = "" ]; then
100 for nismap in `cat /etc/auto.master | grep '^\+' |
101 sed -e '/^#/d' -e '/^$/d'`; do
102 catnismap `echo "$nismap" | sed -e 's/^\+//'`
112 YPMAP=`catnismap auto.master`
113 if [ -z "$YPMAP" ]; then
116 catnismap auto.master
122 if [ -x /usr/lib/autofs/autofs-ldap-auto-master ]; then
123 /usr/lib/autofs/autofs-ldap-auto-master 2> /dev/null
129 for scheme in `getschemes` ; do
132 if [ -z "$filescheme" ] ; then
139 if [ -z "$nisscheme" ] ; then
146 if [ -z "$ldapscheme" ] ; then
158 # This function will build a list of automount commands to execute in
159 # order to activate all the mount points. It is used to figure out
160 # the difference of automount points in case of a reload
168 while read dir map options
170 # These checks screen out duplicates and skip over directories
171 # where the map is '-'.
172 # We can't do empty or direct host maps, so don't bother trying.
174 # Strip trailing slashes from the dir line if it exists to aid
175 # in checking for duplicate maps
176 dir=`echo "$dir" | sed -e "s/\/*$//"`
178 if [ ! -z "$map" -a "$map" = "-hosts" ] ; then
182 if [ $DISABLE_DIRECT -eq 1 \
183 -a x`echo $dir | grep -E "^/-"` != 'x' ]
188 # Do not include a map if it is a duplicate, maps on top of
189 # another map or another map, maps on top of it.
190 for knownmap in $knownmaps
192 if [ "`echo $dir/ | grep ^$knownmap`" != "" \
193 -o "`echo $knownmap | grep ^$dir/`" != "" \]
199 if [ ! -z "$dir" -a ! -z "$map" \
200 -a x`echo "$map" | cut -c1` != 'x-' ]
202 # If the options include a -t or --timeout, a -g or --ghost,
203 # a -v or --verbose or a -d or --debug paramter, then pull
204 # those particular options out.
205 : echo DAEMONOPTIONS OPTIONS $daemonoptions $options
207 if echo "$options" | grep -qE -- '\B-(t\b|-timeout\b=)' ;
209 startupoptions="--timeout=$(echo $options |\
210 sed 's/.*-\(t[^0-9]*\|-timeout\)[ \t=]*\([0-9][0-9]*\).*$/\2/g')"
211 elif echo "$daemonoptions" | grep -q -- '-t' ;
213 # It's okay to be sloppy with DAEMONOPTIONS as there is no
214 # possibility of conflicting with mount or map options.
215 startupoptions="--timeout=$(echo $daemonoptions | \
216 sed 's/.*--*t\(imeout\)*[ \t=]*\([0-9][0-9]*\).*$/\2/g')"
219 # Check for the ghost option
220 if echo "$daemonoptions $options" | grep -qE -- '\B-(g\b|-ghost\b)' ;
222 startupoptions="$startupoptions --ghost"
224 # Dont even deal with conflicts between --ghost and [no]browse
225 # Its just insane to configure things like that.
226 if echo "$options" | grep -qE -- '\B-browse\b' ;
228 startupoptions="$startupoptions --ghost"
231 if echo "$daemonoptions $options" | \
232 grep -qE -- '\B-(v\b|-verbose\b)' ;
234 startupoptions="$startupoptions --verbose"
238 if echo "$daemonoptions $options" | \
239 grep -qE -- '\B-(d\b|-debug\b)' ;
241 startupoptions="$startupoptions --debug"
244 # Other option flags are intended for maps.
245 mapoptions="$(echo "$daemonoptions $options" |\
246 sed 's/-\(t[^0-9]*\|-timeout\)[ \t=]*\([0-9][0-9]*\)//g' |
247 sed 's/-\(g\b\|-ghost\b\)//g' |
248 sed 's/-\(v\b\|-verbose\b\)//g' |
249 sed 's/-\(d\b\|-debug\b\)//g' |
250 sed 's/-\b\(no\)\?browse\b//g')"
252 # Break up the maptype and map, if the map type is specified
253 maptype=`echo $map | cut -f1 -d:`
254 # Handle degenerate map specifiers
255 if [ "$maptype" = "$map" ] ; then
256 if [ "$map" = "hesiod" -o "$map" = "userhome" ] ; then
259 elif [ "$map" = "multi" ] ; then
262 # elif echo "$map" | grep -q '^!'; then
263 # map=`echo "$map"| sed -e 's/^!//g'`
264 elif `echo $map | grep -q "^/"` && [ -x "$map" ]; then
266 elif [ -x "/etc/$map" ]; then
268 map=`echo /etc/$map | sed 's^//^/^g'`
269 elif `echo $map | grep -q "^/"` && [ -f "$map" ]; then
271 elif [ -f "/etc/$map" ]; then
273 map=`echo /etc/$map | sed 's^//^/^g'`
276 if [ "$UNDERSCORETODOT" = "1" ] ; then
277 map=`basename $map | sed -e s/^auto_home/auto.home/ -e s/^auto_mnt/auto.mnt/`
279 map=`basename $map | sed 's^//^/^g'`
283 map=`echo $map | cut -f2- -d:`
285 : echo STARTUPOPTIONS $startupoptions
287 : echo MAPTYPE $maptype
289 : echo MAPOPTIONS $mapoptions
290 : echo LOCALOPTIONS $localoptions
295 if [ x"$m" = x"--" ]; then
296 NEWOPTIONS="$NEWOPTIONS $localoptions --"
298 NEWOPTIONS="$NEWOPTIONS $m"
301 mapoptions=$NEWOPTIONS
303 echo "$DAEMON $startupoptions $dir $maptype $map $mapoptions $localoptions" | sed -e 's/ / /g' -e 's/ */ /g'
305 : echo ------------------------
306 knownmaps=" $dir/ $knownmaps"
317 echo -e $"Configured Mount Points:\n------------------------"
320 echo -e $"Active Mount Points:\n--------------------"
321 ps axwww|grep "[0-9]:[0-9][0-9] $DAEMON " | (
322 while read pid tt stat time command; do echo $command; done
326 get_command_from_pid()
328 ps ax | grep "[0-9]:[0-9][0-9] $DAEMON " | (
329 while read pid tt stat time command; do
330 if [ "$pid" = "$1" ] ; then
331 echo `echo "$command" | \
332 sed 's/--pid-file.*\.pid/ /'`
340 # return true if at least one pid is alive
347 if kill -0 $i 2> /dev/null; then
356 # Find pids of process group leaders
360 ps -eo pid,pgrp,cmd|grep $DAEMON|\
361 awk '{print $1,$2}'|\
364 if [ $pid -eq $pgrp ]
372 # Signal each automount process group leader
377 pgrp_pids=`get_pgrp_pids`
378 for pid in $pgrp_pids
380 kill -USR2 $pid 2 > /dev/null
382 while ps ax|grep -v grep|grep $pid >/dev/null
385 count=$(expr $count + 1)
386 if [ $count -gt $DAEMON_EXIT_WAIT ]; then
392 if [ -n "`/sbin/pidof $DAEMON`" ] ; then
401 loops=`LC_ALL=C awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts`
402 automounts=`LC_ALL=C awk '!/^#/ && $1 ~ /^automount/ {print $2}' /proc/mounts`
406 # If a loop device is mounted into a subdir of the automount point,
410 match=`echo $l | grep -E "^$a[\$|/]" || true`
411 if [ -n "$match" ]; then
413 echo -n $"Unmounting loopback filesystem $match: "
414 loopdev=`LC_ALL=C grep $l /proc/mounts | awk '{print $1}'`
416 if [ $? -eq 0 ]; then
425 # If the loop device file exists under the automount point, umount
427 loopmnts=`LC_ALL=C awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts`
430 loopmnt=`losetup $l | awk -F\( '{print $2}' | awk -F\) '{print $1}'`
433 match=`echo $loopmnt | grep -E "^$a[\$|/]" || true`
434 if [ -n "$match" ]; then
436 echo -n $"Unmounting loopback filesystem $match: "
438 if [ $? -eq 0 ]; then
449 echo -n $"Stopping $prog: "
451 while [ -n "`pidof $DAEMON`" -a $count -lt 15 ] ; do
452 killall -TERM $prog >& /dev/null
454 [ $RETVAL = 0 -a -z "`pidof $DAEMON`" ] || sleep 3
455 count=`expr $count + 1`
457 if [ -n "`pidof $DAEMON`" ] ; then
469 echo -n 'Starting automounter:'
471 # make sure autofs4 is loaded
472 if ! grep -q autofs /proc/filesystems
474 # autofs filesystem support not loaded
475 echo -n ' loading autofs4 kernel module,'
476 modprobe autofs4 || true
477 elif ([ -f /proc/modules ] && lsmod) | grep -q autofs[^4]; then
478 # wrong autofs filesystem module loaded
480 echo "WARNING: autofs kernel module is loaded, autofs4 needed"
481 echo " for correct behaviour. You might experience mount failures."
484 # Check that maps exist
485 if [ -z "$(getrawmounts)" ]
487 echo " no automount maps defined."
491 # ensure pid file directory exists
492 if [ ! -e /var/run/autofs ]
494 mkdir /var/run/autofs
497 getmounts | while read cmd rest
499 mnt=`echo $rest | sed 's/^.* \(\/[^ ]*\) [A-Za-z].*$/\1/'`
500 pidfile=/var/run/autofs/`echo $mnt | sed 's,/,_,g'`.pid
502 start-stop-daemon --start --pidfile $pidfile --quiet \
503 --exec $DAEMON -- --pid-file=$pidfile $rest
510 echo " failed to start automount point $mnt"
517 echo -n 'Stopping automounter:'
522 for file in `ls /var/run/autofs/*.pid 2>/dev/null`
527 pid=`head -n 1 $file`
528 mnt=`ps -wwo 'cmd=' $pid | sed -e \
529 's,.* --pid-file=/var/run/autofs/\([^ ]*\)\.pid.*,\1,; s,_,/,g'`
530 dname=`basename $DAEMON`
532 start-stop-daemon --stop --quiet --pidfile $file --name $dname
541 echo -n -e "\n No process for automount $mnt"
545 echo -n -e "\n Couldn't stop automount for $mnt"
549 "\n Strange start-stop-daemon exit status: $ret"
558 echo "Reloading automounter: checking for changes ... "
560 TMP1=`mktemp autofs.XXXXXX`
561 TMP2=`mktemp autofs.XXXXXX`
565 for i in `ls /var/run/autofs/*.pid 2>/dev/null`
567 pid=`head -n 1 $i 2>/dev/null`
568 [ "$pid" = "" ] && continue
570 cmd=`get_command_from_pid $pid`
573 mnt=`ps -wwo 'cmd=' $pid | sed -e \
574 's,.* --pid-file=/var/run/autofs/\([^ ]*\)\.pid.*,\1,; s,_,/,g'`
576 if ! grep -q "$cmd" $TMP1; then
577 echo "Stopping automounter for: $mnt"
578 kill -USR2 $pid 2> /dev/null
580 echo "Reloading automounter map for: $mnt"
581 kill -HUP $pid 2> /dev/null
585 cat $TMP1 | (while read x; do
586 if ! grep -q "^$x" $TMP2; then
587 echo $x | while read cmd rest; do
589 mnt=`echo $rest | sed 's/^.* \(\/[^ ]*\) [A-Za-z].*$/\1/'`
590 pidfile=/var/run/autofs/`echo $mnt | sed 's,/,_,g'`.pid
592 start-stop-daemon --start --pidfile $pidfile --quiet \
593 --exec $DAEMON -- --pid-file=$pidfile $rest
600 echo " failed to start automount point $mnt"
602 echo "Started automounter: $mnt"
610 force-reload|restart)
624 echo "Usage: $initdir/autofs {start|stop|restart|reload|status|getmounts|active}" >&2