02de249b56e01a930331d9bcf8f18996272e211d
[openafs.git] / src / packaging / Debian / openafs-client.init
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides:             openafs-client
4 # Required-Start:       $remote_fs $network $time
5 # Required-Stop:        $remote_fs $network
6 # Should-Start:         $syslog
7 # X-Stop-After:         autofs
8 # Default-Start:        2 3 4 5
9 # Default-Stop:         0 1 6
10 # Short-Description:    OpenAFS client
11 # Description:          Starts or stops the OpenAFS client, loading the
12 #                       OpenAFS kernel module as necessary.
13 ### END INIT INFO
14 #
15 # Modified by Sam Hartman <hartmans@mit.edu> for Debian
16 # Copyright 2000, International Business Machines Corporation and others.
17 # All Rights Reserved.
18
19 # This software has been released under the terms of the IBM Public
20 # License.  For details, see the LICENSE file in the top-level source
21 # directory or online at http://www.openafs.org/dl/license10.html
22
23 # This init script bears little resemblence to the regular upstream init
24 # script at this point and is therefore maintained as a Debian-specific file.
25 # The upstream init script assumes Red Hat paths and uses insmod to load the
26 # module directly rather than using modprobe.
27
28 PATH=/bin:/usr/bin:/sbin:/usr/sbin
29
30 CACHEINFO=${CACHEINFO:-/etc/openafs/cacheinfo}
31 MODULEROOT=${MODULEROOT:-/lib/modules/`uname -r`}
32 MODULEDIR=${MODULEDIR:-$MODULEROOT/fs}
33 DKMSDIR=${DKMSDIR:-$MODULEROOT/updates/dkms}
34
35 exec 3>/dev/null
36 exec </dev/null
37
38 # Gather up options and post startup script name, if present
39 if [ -f /etc/openafs/afs.conf ]; then
40     . /etc/openafs/afs.conf
41 fi
42
43 # Return 1 if the argument is "true".
44 is_on() {
45     if [ x"$1" = x"true" ] ; then
46         return 0
47     else
48         return 1
49     fi
50 }
51
52 # If choose_client can't correctly determine which client to use, set
53 # LIBAFS manually.
54 choose_client() {
55     # Use the second field of the uname -v output instead of just doing a
56     # match on the whole thing to protect against matching a timezone named
57     # SMP -- I don't know of one, but let's be paranoid.
58     set X `uname -v`; shift
59     case $2 in
60     SMP) MP=.mp ;;      # MP system
61     *)   MP=    ;;      # SP system
62     esac
63
64     # The Debian OpenAFS packages do not add the .mp by default as of 1.3, but
65     # modules obtained from other sources may.  If a module with the .mp
66     # exists and this is an SMP system, use it; otherwise, use the default
67     # value.  Unset $MP if not using it so that we can use it later in the
68     # call to modprobe.
69     #
70     # Assume that if we're using DKMS, we won't have a .mp and we'll always
71     # have a *.ko module name.
72     if [ -f "$DKMSDIR/openafs.ko" ] ; then
73         LIBAFS=openafs.ko
74     elif [ -n "$MP" -a -f "$MODULEDIR/openafs${MP}.o" ] ; then
75         LIBAFS=openafs${MP}.o
76     elif [ -n "$MP" -a -f "$MODULEDIR/openafs${MP}.ko" ] ; then
77         LIBAFS=openafs${MP}.ko
78     elif [ -f "$MODULEDIR/openafs.ko" ] ; then
79         MP=
80         LIBAFS=openafs.ko
81     else
82         MP=
83         LIBAFS=openafs.o
84     fi
85 }
86
87 # Load the AFS client module if it's not already loaded.  Set $MODULEDIR and
88 # $LIBAFS to override the default location and module name.  Also check before
89 # loading whether the module is listed in the module dependencies so that we
90 # can exit with a 0 status in that case.
91 load_client() {
92     if [ -z "$LIBAFS" ] ; then
93         choose_client
94     fi
95     if [ ! -f "$MODULEDIR/$LIBAFS" -a ! -f "$DKMSDIR/$LIBAFS" ] ; then
96         echo ""
97         cat <<EOF >&2
98 AFS module $MODULEDIR/$LIBAFS does not exist.
99 Not starting AFS.  Please consider building kernel modules using
100 instructions in /usr/share/doc/openafs-client/README.modules
101 EOF
102         # We must exit successfully here or openafs-client will fail on
103         # installation unless a module is installed.
104         exit 0
105     fi
106     sawdep=0
107     if grep -q openafs "$MODULEROOT/modules.dep" ; then
108         sawdep=1
109     fi
110     LOADED=`/sbin/lsmod | fgrep openafs`
111     if [ -z "$LOADED" ] ; then
112         modprobe openafs
113         status=$?
114         if [ $status = 0 ] ; then
115             echo -n " openafs"
116         fi
117
118         # We must exit successfully here if the openafs module just isn't
119         # listed in the dependency information for modprobe, which can happen
120         # if openafs-client and the module package are installed at the same
121         # time and the module hasn't been set up yet.
122         if [ $sawdep = 0 ] ; then
123             return 0
124         else
125             return $status
126         fi
127     fi
128     return 0
129 }
130
131 # Determine which afsd options to use.  /etc/openafs/afs.conf contains the
132 # settings that are checked here.
133 choose_afsd_options() {
134     if [ -z "$OPTIONS" ] || [ "$OPTIONS" = "AUTOMATIC" ] ; then
135         AFSD_OPTIONS="$VERBOSE"
136     else
137         AFSD_OPTIONS="$OPTIONS $VERBOSE"
138     fi
139
140     # These variables are from /etc/openafs/afs.conf.client and are managed
141     # automatically by debconf.
142     if is_on $AFS_AFSDB ; then
143         AFSD_OPTIONS="$AFSD_OPTIONS -afsdb"
144     fi
145     if is_on $AFS_DYNROOT ; then
146         AFSD_OPTIONS="$AFSD_OPTIONS -dynroot"
147     fi
148     if is_on $AFS_FAKESTAT ; then
149         AFSD_OPTIONS="$AFSD_OPTIONS -fakestat"
150     fi
151 }
152
153 # Start afsd.  Be careful not to start it if another one is already running,
154 # as that has a bad tendency to hang the system.  Earlier versions of the
155 # openafs-client package put afsd in /usr/sbin.
156 start_client() {
157     if pidof /sbin/afsd >/dev/null || pidof /usr/sbin/afsd >/dev/null ; then
158         echo "."
159     else
160         choose_afsd_options
161         echo " afsd."
162         start-stop-daemon --start --quiet --exec /sbin/afsd -- $AFSD_OPTIONS
163     fi
164
165     # From /etc/openafs/afs.conf.client, whether to enable fcrypt encryption.
166     if is_on $AFS_CRYPT ; then
167         fs setcrypt on
168     fi
169
170     # From /etc/openafs/afs.conf, set a sysname list if one was configured.
171     if [ -n "$AFS_SYSNAME" ] ; then
172         fs sysname $AFS_SYSNAME
173     fi
174 }
175
176 # Kill all processes that are accessing AFS.  Not enabled by default, and
177 # normally called via kill_all_afs.
178 kill_afs() {
179     signal=$1
180     mount=`grep ^'AFS ' /etc/mtab | awk '{ print $2 }'`
181     if [ -n "$mount" ] ; then
182         pids=`/usr/bin/lsof -Fp $mount | sed 's/p//'`
183         if [ -n "$pids" ] ; then
184             kill -$signal $pids > /dev/null 2>&1
185             sleep 1
186         fi
187     fi
188 }
189
190 # Repeatedly call kill_afs for a series of signals to give AFS-using processes
191 # a hope of shutting down cleanly if the system is shutting down.  Not enabled
192 # by default.  Enable this in /etc/openafs/afs.conf.
193 #
194 # Warns and does nothing if lsof is not installed.
195 kill_all_afs() {
196     runlevel=`runlevel | sed 's/^. //'`
197     if [ "$runlevel" -eq 0 ] || [ "$runlevel" -eq 6 ] ; then
198         if [ -x /usr/bin/lsof ] ; then
199             echo -n "Killing processes with AFS files open: "
200             kill_afs HUP
201             kill_afs TERM
202             kill_afs ABRT
203             kill_afs KILL
204             echo "done."
205         else
206             echo '/usr/bin/lsof not found, not killing processes' >&2
207         fi
208     fi
209 }
210
211
212 case "$1" in 
213 start)
214     if is_on $AFS_CLIENT && test -x /sbin/afsd ; then
215         echo -n "Starting AFS services:"
216         if load_client ; then
217             start_client
218             $AFS_POST_INIT
219         else
220             echo ""
221             echo "Failed to load AFS kernel module, not starting AFS" >&2
222             exit 1
223         fi
224     fi
225     ;;
226
227 force-start)
228     if test -x /sbin/afsd ; then
229         echo -n "Starting AFS services:"
230         if load_client ; then
231             start_client
232             $AFS_POST_INIT
233         else
234             echo ""
235             echo "Failed to load AFS kernel module, not starting AFS" >&2
236             exit 1
237         fi
238     fi
239     ;;
240
241 stop|force-stop)
242     $AFS_PRE_SHUTDOWN
243     echo -n "Stopping AFS services:"
244     if grep -q '^AFS ' /etc/mtab ; then
245         umount `grep ^'AFS ' /etc/mtab | awk '{ print $2 }'`
246         echo -n " afsd"
247     fi
248     if pidof /usr/sbin/afsd >/dev/null || pidof /sbin/afsd >/dev/null ; then
249          afsd -shutdown
250     fi
251
252     # If running with the -rmtsys option, afsd doesn't kill the rmtsys helper
253     # on afsd -shutdown.  Run start-stop-daemon to make sure that everything
254     # is dead.
255     start-stop-daemon --stop --quiet --name afsd
256
257     LIBAFS=`/sbin/lsmod | awk 'BEGIN { FS = " " } /openafs/ { print $1 }'`
258     if [ -n "$LIBAFS" ] ; then
259         /sbin/rmmod $LIBAFS
260         echo -n " openafs"
261     fi
262     echo "."
263     ;;
264
265 restart|force-reload)
266     "$0" stop
267     "$0" start
268     ;;
269
270 *)
271     echo Usage: \
272         'openafs-client {start|force-start|stop|restart|force-reload}' >&2
273     exit 1
274
275 esac