tests-update-20080121
[openafs.git] / src / afsd / afs.rc.linux
1 #! /bin/sh
2 # Copyright 2000, International Business Machines Corporation and others.
3 # All Rights Reserved.
4
5 # This software has been released under the terms of the IBM Public
6 # License.  For details, see the LICENSE file in the top-level source
7 # directory or online at http://www.openafs.org/dl/license10.html
8
9 # AFS   Start and stop AFS components
10
11
12 # chkconfig: 35 60 20
13 # description:  AFS is a distributed file system which provides location
14 #               transparency, caching and secure authentication.
15 #               Additional configuration can be done in the /etc/sysconfig/afs
16 #               file. Read the documentation in that file for more information.
17 #
18 # The following section is used at least by SuSE insserv(8)
19 ### BEGIN INIT INFO
20 # Provides: afs
21 # Required-Start: $network
22 # Required-Stop: $network
23 # Default-Start: 3 5
24 # Default-Stop: 0 1 2 6
25 # Description:  AFS is a distributed file system which provides location
26 #              transparency, caching and secure authentication.
27 #              Additional configuration can be done in the /etc/sysconfig/afs
28 #              file. Read the documentation in that file for more information.
29 ### END INIT INFO
30 #
31 # Note that AFS does not use a pid file in /var/run. It is turned off by
32 # unmounting /afs.
33
34
35 # check for special bootup functions
36 if [ -f /etc/rc.d/init.d/functions ] ; then
37         . /etc/rc.d/init.d/functions
38         afs_rh=1
39 else
40         # special (RedHat) functions not available...
41         # so I provide neccessary replacements (e.g. for SuSE)
42
43         function echo_failure () { echo -n " - failed." ; }
44
45         function echo_success () { echo -n " - successful." ; }
46
47         # these are hopefully all functions I use...
48 fi
49
50 runcmd() {
51    echo -n "$1 "
52    shift
53    if [ "$BOOTUP" = "color" ]; then
54       $* && echo_success || echo_failure
55    else
56       $*
57    fi
58    echo
59 }
60
61 SYSCNF=${SYSCNF:-/etc/sysconfig/afs}
62 KERNEL_VERSION=`uname -r`
63
64 # Gather up options and post startup script name, if present
65 if [ -f $SYSCNF ] ; then
66         . $SYSCNF
67 fi
68
69 CACHEINFO=${CACHEINFO:-/usr/vice/etc/cacheinfo}
70 CACHE=${CACHEDIR:-/usr/vice/cache}
71 AFS=${AFSDIR:-/afs}
72 AFSD=${AFSD:-/usr/vice/etc/afsd}
73 BOSSERVER=${BOSSERVER:-/usr/afs/bin/bosserver}
74 BOS=${BOS:-/usr/bin/bos}
75 KILLAFS=${KILLAFS:-/usr/vice/etc/killafs}
76
77 # is_on returns 1 if value of arg is "on"
78 is_on() {
79         if  test "$1" = "on" ; then return 0
80         else return 1
81         fi
82 }
83
84 on_network() {
85     ADDRS=`LANG=C ifconfig -a | grep 'inet addr' | grep -v 127.0.0.1 | wc -l`
86     if [ "$ADDRS" = "" ]; then
87        echo afs: No interfaces with IP address 1>&2
88        return 1
89     elif [ $ADDRS = 0 ]; then
90        echo afs: No interfaces with IP address 1>&2
91        return 1
92     fi
93     return 0
94 }
95
96 # If choose_client can't correctly determine which client to use, set
97 # LIBAFS manually.
98 choose_client() {
99
100         # Use the second field of the uname -v output instead of just
101         # doing a match on the whole thing to protect against matching
102         # a timezone named SMP -- I don't know of one, but let's be
103         # paranoid.
104
105         set X `uname -v`; shift
106         case $2 in
107         SMP) MP=.mp ;;  # MP system
108         *)   MP= ;;     # SP system
109         esac
110
111         # For now, just use uname -r to get the module version. 
112         case $KERNEL_VERSION in
113           [1-2].[0-5].*)
114                 LIBAFS=libafs-$KERNEL_VERSION$MP.o
115                 ;;
116           *)
117                 LIBAFS=libafs-$KERNEL_VERSION$MP.ko
118                 ;;
119         esac
120 }
121
122 #
123 # Find prefix symbol to use with insmod.  We find the unregister_filesystem
124 # string from /proc/ksyms since we know it's there.  If /proc/ksyms does not
125 # exist, we print that info to the console and use the uname -v output to
126 # decide on a prefix.
127 # unregister_filesystem_Rsmp_b240cad8 is a typcial SMP version string from
128 # a kernel built from ftp.kernel.org
129 #
130 case $KERNEL_VERSION in
131   [1-2].[0-5].*)
132         KSYMS_FILE=/proc/ksyms
133         ;;
134   *)
135         KSYMS_FILE=/proc/kallsyms
136         ;;
137 esac
138 SEARCH_STR="unregister_filesystem"
139 DEFAULT_SMP_PREFIX="smp_" # Redhat kernels need "smp" instead
140 PREFIX="" # none needed for UP with <= 1Gig memory
141
142 set_prefix()
143 {
144         h='[0-9a-fA-F]'
145         h8="$h$h$h$h$h$h$h$h"
146         prefix_set=0
147
148         set X `egrep "\<$SEARCH_STR" $KSYMS_FILE 2> /dev/null`; shift
149         
150         case $KERNEL_VERSION in
151           [1-2].[0-5].*)
152                 str=$2
153                 ;;
154           *)
155                 str=$3
156                 ;;
157         esac
158         case $str in
159         ${SEARCH_STR}_R$h8)
160                 # No prefix required
161                 ;;
162         $SEARCH_STR)
163                 # No versioning in kernel symbols
164                 ;;
165         ${SEARCH_STR}_R*$h8)
166                 suffix=${str#${SEARCH_STR}_R}
167                 PREFIX=${suffix%$h8}
168                 ;;
169         *)
170                 case $str in
171                 '')
172                         echo afsd: Cannot find \"$SEARCH_STR\" in file $KSYMS_FILE
173                         ;;
174                 *)
175                         echo afsd: Malformed kernel version symbol \"$str\"
176                         ;;
177                 esac
178
179                 echo Guessing prefix from output of uname -v
180                 set X `uname -v`; shift
181                 case $2 in
182                 SMP)
183                         PREFIX=$DEFAULT_SMP_PREFIX
184                         ;;
185                 esac
186                 ;;
187         esac
188 }
189
190 MODLOADDIR=${MODLOADDIR:-/usr/vice/etc/modload}
191 # load_client loads the AFS client module if it's not already loaded. 
192 load_client() {
193         # If LIBAFS is set, use it.
194         if [ -z "$LIBAFS" ] ; then
195                 # Try to determine the right client.
196                 choose_client
197         fi
198     
199         if [ ! -f "$MODLOADDIR/$LIBAFS" ] ; then
200                 echo AFS module $MODLOADDIR/$LIBAFS does not exist. Not starting AFS.
201                 exit 1
202         fi
203
204         if [ -f $KSYMS_FILE ]; then
205                 # use the prefix command if required
206                 case $KERNEL_VERSION in
207                   [1-2].[0-5].*)
208                         set_prefix
209                         /sbin/insmod ${PREFIX:+-P $PREFIX} -f -m $MODLOADDIR/$LIBAFS > $MODLOADDIR/libafs.map 2>&1
210                         ;;
211                   *)
212                         /sbin/insmod $MODLOADDIR/$LIBAFS > $MODLOADDIR/libafs.map 2>&1
213                         ;;
214                 esac
215         else
216                 /sbin/insmod $MODLOADDIR/$LIBAFS > $MODLOADDIR/libafs.map 2>&1
217         fi
218 }
219
220 generate_cacheinfo() {
221     if [ "$CACHESIZE" = "AUTOMATIC" ]; then
222         LINE=`df -k $CACHE | tail -1`
223         PART=`echo $LINE | awk '{ if ( ($NF != "/usr")  && ($NF != "/") ) print $NF; else print "NONE";}'`
224         if [ "$PART" = "NONE" ]; then
225             echo "$CACHE or /usr/vice is not a separate partition"
226             echo "you have to change the cachesize in $SYSCNF by hand"
227             echo "AFS will be started with a VERY small cache of 8Mb."
228             CACHESIZE=8000
229         else
230             # Check to see if df has pretty-printed for long dev (i.e. LVM)
231             FCHAR=`echo $LINE | cut -c 1`
232             if [ "$FCHAR" = "/" ]; then
233                 PARTSIZE=`echo $LINE | awk '{print $2}'`
234             else
235                 PARTSIZE=`echo $LINE | awk '{print $1}'`
236             fi
237             CACHESIZE=`echo $PARTSIZE | awk '{printf "%d",int(($1*.8)/1000)*1000}'`
238         fi
239     fi
240     if [ "x$CACHESIZE" != "x" ]; then
241         echo $AFS:$CACHE:$CACHESIZE >$CACHEINFO
242         chmod 0644 $CACHEINFO
243     else
244         CACHESIZE=`awk -F: '{print $3}' < $CACHEINFO`
245     fi
246 }
247
248 choose_afsdoptions() {
249         if [ -z "$OPTIONS" -o "$OPTIONS" = "AUTOMATIC" ]; then
250                 if [ $CACHESIZE -lt 131072 ]; then
251                         OPTIONS=$SMALL
252                 elif [ $CACHESIZE -lt 524288 ]; then
253                         OPTIONS=$MEDIUM
254                 elif [ $CACHESIZE -lt 1048576 ]; then
255                         OPTIONS=$LARGE
256                 elif [ $CACHESIZE -lt 2097152 ]; then
257                         OPTIONS=$XLARGE
258                 else
259                         OPTIONS=$XXLARGE
260                 fi
261         fi
262         AFSD_OPTIONS="$OPTIONS $VERBOSE"
263         if is_on $ENABLE_AFSDB; then
264                 AFSD_OPTIONS="$AFSD_OPTIONS -afsdb"
265         fi
266         if is_on $ENABLE_DYNROOT; then
267                 AFSD_OPTIONS="$AFSD_OPTIONS -dynroot"
268         fi
269 }
270
271 case "$1" in 
272   start)
273     if [ ! "$afs_rh" -o ! -f /var/lock/subsys/afs ]; then
274         if [ `echo "$OPTIONS" | grep -c dynroot` = 0 ]; then
275             on_network || exit 1
276         fi
277         # Load kernel extensions
278         if  load_client  ; then :
279         else
280                 echo Failed to load AFS client, not starting AFS services.
281                 exit 1
282         fi
283
284         echo "Starting AFS services..... "
285         # Start bosserver, it if exists
286         if  is_on $AFS_SERVER && test -x $BOSSERVER  ; then
287                 $BOSSERVER 
288                 test "$afs_rh" && touch /var/lock/subsys/afs
289                 if is_on $WAIT_FOR_SALVAGE; then
290                         # wait for fileserver to finish salvaging
291                         sleep 10
292                         while $BOS status localhost fs 2>&1 | grep 'Auxiliary.*salvaging'; do
293                                 echo "Waiting for salvager to finish..... "
294                                 sleep 10
295                         done
296                 fi
297         fi
298
299         # Start AFS client
300         if  is_on $AFS_CLIENT && test -x $AFSD  ; then
301                 generate_cacheinfo
302                 choose_afsdoptions
303                 $AFSD ${AFSD_OPTIONS}
304                 test "$afs_rh" && touch /var/lock/subsys/afs
305                 $AFS_POST_INIT
306         fi
307     fi
308         ;;
309
310   stop)
311     if [ ! "$afs_rh" -o -f /var/lock/subsys/afs ]; then
312         # Stop AFS
313         echo "Stopping AFS services..... "
314
315         if  is_on $AFS_CLIENT  ; then
316                 if [ -x $KILLAFS ] ; then
317                         runcmd "Sending all processes using /afs the TERM signal ..." $KILLAFS TERM
318                         runcmd "Sending all processes using /afs the KILL signal ..." $KILLAFS KILL
319                 fi
320                 umount /afs
321         fi
322
323         if  is_on $AFS_SERVER && test -x $BOS ; then
324                 echo "Stopping AFS bosserver"
325                 $BOS shutdown localhost -localauth -wait
326                 killall -HUP bosserver
327         fi
328
329         LIBAFS=`/sbin/lsmod | fgrep libafs`
330         if [ -n "$LIBAFS" ] ; then
331                 LIBAFS=`echo $LIBAFS | awk 'BEGIN { FS = " " } { print $1 }'`
332                 /sbin/rmmod $LIBAFS
333         fi
334
335         rm -f /var/lock/subsys/afs
336     fi
337         ;;
338
339   restart)
340         # Restart AFS
341         $0 stop
342         $0 start
343         ;;
344
345   *)
346         echo Usage: 'afs <start|stop|restart>'
347
348 esac
349
350 exit 0
351
352 action fool the Red Hat initscripts