aboutsummaryrefslogtreecommitdiffstats
path: root/samples/rc.autofs.in
blob: 92716a09754e6fd5dbea1e24268a31b5b43ecd12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/bin/bash
#
# $Id$
#
# rc file for automount using a Sun-style "master map".
# We first look for a local /etc/auto.master, then a YP
# map with that name
#
# On most distributions, this file should be called:
# /etc/rc.d/init.d/autofs or /etc/init.d/autofs
#

# For Redhat-ish systems
#
# chkconfig: 345 28 72
# processname: /usr/sbin/automount
# config: /etc/auto.master
# description: Automounts filesystems on demand

# This is used in the Debian distribution to determine the proper
# location for the S- and K-links to this init file.
# The following value is extracted by debstd to figure out how to
# generate the postinst script. Edit the field to change the way the
# script is registered through update-rc.d (see the manpage for
# update-rc.d!)
#
FLAGS="defaults 21"

#
# Location of the automount daemon and the init directory
#
DAEMON=@@sbindir@@/automount
prog=`basename $DAEMON`
initdir=@@initdir@@

#
# Determine which kind of configuration we're using
#
if [ -f /etc/debian_version ]; then
    system=debian
elif [ -f $initdir/functions ]; then
    system=redhat		# Or something sufficiently close to it...
else
    echo "$0: Unknown system, please port and contact autofs@linux.kernel.org" 1>&2
    exit 1
fi

if [ $system = redhat ]; then
    . $initdir/functions
fi

test -e $DAEMON || exit 0

if [ $system = debian ]; then
    thisscript="$0"
    if [ ! -f "$thisscript" ]; then
        echo "$0: Cannot find myself" 1>&2
        exit 1
    fi
fi

PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

#
# We can add local options here
# e.g. localoptions='rsize=8192,wsize=8192'
#
localoptions=''

# Daemon options
# e.g. --timeout 60
#
daemonoptions=''

#
# Check for all maps that are to be loaded
#
function getschemes()
{
    grep ^automount: /etc/nsswitch.conf | sed -e 's/^.*://' -e 's/\[.*\]/ /g'
}
function catnismap()
{
    if [ -z "$1" ] ; then
        map="auto_master"
    else
        map="$1"
    fi
    /usr/bin/ypcat -k "$map" 2> /dev/null | sed -e '/^#/d' -e '/^$/d'
}
function getfilemounts()
{
    if [ -f /etc/auto.master ] ; then
        cat /etc/auto.master | grep -v '^\+' | sed -e '/^#/d' -e '/^$/d'
        for nismap in `cat /etc/auto.master | grep '^\+' | sed -e '/^#/d' -e '/^$/d'`; do
            catnismap `echo "$nismap" | sed -e 's/^\+//'`
        done
    fi
}
function getnismounts()
{
    catnismap auto.master
}
function getldapmounts()
{
    @@autofslibdir@@/autofs-ldap-auto-master 2> /dev/null
}
function getrawmounts()
{
    for scheme in `getschemes` ; do
        case "$scheme" in
            files)
                if [ -z "$filescheme" ] ; then
                    getfilemounts
                    filescheme=1
                    export filescheme
                fi
                ;;
            nis)
                if [ -z "$nisscheme" ] ; then
                    getnismounts
                    nisscheme=1
                    export nisscheme
                fi
                ;;
            ldap*)
                if [ -z "$ldapscheme" ] ; then
                    getldapmounts
                    ldapscheme=1
                    export ldapscheme
                fi
                ;;
        esac
    done
}


#
# This function will build a list of automount commands to execute in
# order to activate all the mount points. It is used to figure out
# the difference of automount points in case of a reload
#
function getmounts()
{
	knownmaps=" "
	getrawmounts | (
	while read dir map options
	do
	    # These checks screen out duplicates and skip over directories
	    # where the map is '-'.
	    # We can't do empty or direct host maps, so don't bother trying.
	    if [ ! -z "$map" -a "$map" = "-hosts" ] ; then
		continue
	    fi
	    if [ ! -z "$dir" -a ! -z "$map" \
			-a x`echo "$map" | cut -c1` != 'x-' \
			-a "`echo "$knownmaps" | grep $dir/`" = "" ]
	    then
		# If the options include a -t or --timeout parameter, then
		# pull those particular options out.
		: echo DAEMONOPTIONS OPTIONS $daemonoptions $options
		startupoptions=
		if echo $options | grep -q -- '-t' ; then
		    startupoptions="--timeout $(echo $daemonoptions $options | \
		      sed 's/.*--*t\(imeout\)*[ \t=]*\([0-9][0-9]*\).*$/\2/g')"
		fi
		# Other option flags are intended for maps.
		mapoptions="$(echo "$daemonoptions $options" |\
		      sed   's/--*t\(imeout\)*[ \t=]*\([0-9][0-9]*\)//g')"
		# Break up the maptype and map, if the map type is specified
		maptype=`echo $map | cut -f1 -d:`
		# Handle degenerate map specifiers
		if [ "$maptype" = "$map" ] ; then
		    if [ -x "$map" ]; then
			maptype=program
		    elif [ -x "/etc/$map" ]; then
			maptype=program
			map=`echo /etc/$map | sed 's^//^/^g'`
		    elif [ -f "$map" ]; then
			maptype=file
		    elif [ -f "/etc/$map" ]; then
			maptype=file
			map=`echo /etc/$map | sed 's^//^/^g'`
		    elif [ "$map" = "hesiod" -o "$map" = "userhome" ] ; then
			maptype=$map
			map=
		    elif [ "$map" = "multi" ] ; then
			maptype=$map
			map=
		    else
			maptype=yp
			# Historical braindamage, use explicit yp: to override
			map=`basename $map | sed -e s/^auto_/auto./`
		    fi
		fi
		map=`echo $map | cut -f2- -d:`

		: echo STARTUPOPTIONS $startupoptions
		: echo DIR $dir
		: echo MAPTYPE $maptype
		: echo MAP $map
		: echo MAPOPTIONS $mapoptions
		: echo LOCALOPTIONS $localoptions

		echo "$DAEMON $startupoptions $dir $maptype $map $mapoptions $localoptions" | sed -e 's/	/ /g' -e 's/  / /g'

		: echo ------------------------
	    fi
	    knownmaps=" $dir/ $knownmaps"
	done
    )
}

#
# Status lister.
#
function status()
{
	echo $"Configured Mount Points:"
	echo  "------------------------"
	getmounts
	echo ""
	echo $"Active Mount Points:"
	echo  "--------------------"
	ps axwww|grep "[0-9]:[0-9][0-9] $DAEMON " | (
		while read pid tt stat time command; do echo $command; done
	)
}


#
# Redhat start/stop function.
#
function redhat()
{

#
# See how we were called.
#
case "$1" in
  start)
        # Make sure the autofs filesystem type is available.
        (grep -q autofs /proc/filesystems || /sbin/modprobe -k autofs || /sbin/modprobe -k autofs4) 2> /dev/null
        echo -n $"Starting $prog:"
        TMP=`mktemp /tmp/autofs.XXXXXX` || { echo $"could not make temp file" >& 2; exit 1; }
        getmounts | tee $TMP | sh
        RETVAL=$?
	if [ -s $TMP ] ; then
	    success "$prog startup" || failure "$prog startup"
            [ $RETVAL = 0 ] && touch /var/lock/subsys/autofs
	else
	    echo -n "" $"No Mountpoints Defined"
	    success "$prog startup"
	fi
	rm -f $TMP
        echo
	;;
  stop)
        echo -n $"Stopping $prog:"
	if [ -z "`pidofproc $prog`" -a -z "`getmounts`" ]; then
		success $"$prog shutdown"
		RETVAL=0
	else
        	killproc $DAEMON -USR2
        	RETVAL=$?
	fi
        count=0
        while [ -n "`/sbin/pidof $DAEMON`" -a $count -lt 8 ] ; do
	    killproc $DAEMON -USR2
            RETVAL=$?
	    [ $RETVAL = 0 -a -z "`/sbin/pidof $DAEMON`" ] || sleep ${count+1}
            count=`expr $count + 1`
        done
        umount -a -f -t autofs
        rm -f /var/lock/subsys/autofs
        echo
        ;;
  restart)
        redhat stop
        redhat start
	;;
  reload)
	if [ ! -f /var/lock/subsys/autofs ]; then
		echo $"$prog not running"
		RETVAL=1
		return
	fi
	echo $"Checking for changes to /etc/auto.master ...."
        TMP1=`mktemp /tmp/autofs.XXXXXX` || { echo $"could not make temp file" >& 2; exit 1; }
        TMP2=`mktemp /tmp/autofs.XXXXXX` || { echo $"could not make temp file" >& 2; exit 1; }
	getmounts > $TMP1
	ps axwww|grep "[0-9]:[0-9][0-9] $DAEMON" | (
	    while read pid tt stat time command; do
		echo "$command" >>$TMP2
		if ! grep -q "^$command" $TMP1; then
		    if ! echo "$command" | grep -q -e --submount; then
			kill -USR2 $pid
			echo $"Stop $command"
		    fi
		fi
	    done
	)
	cat $TMP1 | ( while read x; do
		if ! grep -q "^$x" $TMP2; then
			$x
			echo $"Start $x"
		fi
        done )
	rm -f $TMP1 $TMP2
	;;
  status)
	status
	;;
  condrestart)
	[ -f /var/lock/subsys/autofs ] && redhat restart
        RETVAL=0
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
	RETVAL=0
esac
}

#
# Debian start/stop functions.
#
function debian()
{
#
# See how we were called.
#
case "$1" in
    start)
	echo -n 'Starting automounter:'
	getmounts | while read cmd mnt rest
	do
		echo -n " $mnt"
		pidfile=/var/run/autofs`echo $mnt | sed 's/\//./'`.pid
		start-stop-daemon --start --pidfile $pidfile --quiet \
			--exec $DAEMON -- $mnt $rest
		#
		#	Automount needs a '--pidfile' or '-p' option.
		#	For now we look for the pid ourself.
		#
		ps ax | grep "[0-9]:[0-9][0-9] $DAEMON $mnt" | (
			read pid rest
			echo $pid > $pidfile
			echo "$mnt $rest" >> $pidfile
		)
	done
	echo "."
	;;
    stop)
	echo 'Stopping automounter.'
	start-stop-daemon --stop --quiet --signal USR2 --exec $DAEMON
	;;
    reload|restart)
	echo "Reloading automounter: checking for changes ... "
	TMP=/var/run/autofs.tmp
	getmounts >$TMP
	for i in /var/run/autofs.*.pid
	do
		pid=`head -n 1 $i 2>/dev/null`
		[ "$pid" = "" ] && continue
		command=`tail +2 $i`
		if ! grep -q "^$command" $TMP
		then
			echo "Stopping automounter: $command"
			kill -USR2 $pid
		fi
	done
	rm -f $TMP
	$thisscript start
	;;
    status)
	status
	;;
    *)
	echo "Usage: $initdir/autofs {start|stop|restart|reload|status}" >&2
	exit 1
	;;
esac
}

RETVAL=0
if [ $system = debian ]; then
	debian "$@"
elif [ $system = redhat ]; then
	redhat "$@"
fi

exit $RETVAL