linux-26-progress-20040412
[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=/etc/sysconfig/afs
62
63 # Gather up options and post startup script name, if present
64 if [ -f $SYSCNF ] ; then
65         . $SYSCNF
66 fi
67
68 CACHEINFO=${CACHEINFO:-/usr/vice/etc/cacheinfo}
69 CACHE=${CACHEDIR:-/usr/vice/cache}
70 AFS=${AFSDIR:-/afs}
71
72 # is_on returns 1 if value of arg is "on"
73 is_on() {
74         if  test "$1" = "on" ; then return 0
75         else return 1
76         fi
77 }
78
79 on_network() {
80     ADDRS=`LANG=C ifconfig -a | grep 'inet addr' | grep -v 127.0.0.1 | wc -l`
81     if [ "$ADDRS" = "" ]; then
82        echo afs: No interfaces with IP address 1>&2
83        return 1
84     elif [ $ADDRS = 0 ]; then
85        echo afs: No interfaces with IP address 1>&2
86        return 1
87     fi
88     return 0
89 }
90
91 # If choose_client can't correctly determine which client to use, set
92 # LIBAFS manually.
93 choose_client() {
94
95         # Use the second field of the uname -v output instead of just
96         # doing a match on the whole thing to protect against matching
97         # a timezone named SMP -- I don't know of one, but let's be
98         # paranoid.
99
100         set X `uname -v`; shift
101         case $2 in
102         SMP) MP=.mp ;;  # MP system
103         *)   MP= ;;     # SP system
104         esac
105
106         # For now, just use uname -r to get the module version. 
107         VERSION=`uname -r`
108
109         LIBAFS=libafs-$VERSION$MP.o
110 }
111
112 #
113 # Find prefix symbol to use with insmod.  We find the unregister_filesystem
114 # string from /proc/ksyms since we know it's there.  If /proc/ksyms does not
115 # exist, we print that info to the console and use the uname -v output to
116 # decide on a prefix.
117 # unregister_filesystem_Rsmp_b240cad8 is a typcial SMP version string from
118 # a kernel built from ftp.kernel.org
119 #
120
121 KSYMS_FILE=/proc/ksyms
122 SEARCH_STR="unregister_filesystem"
123 DEFAULT_SMP_PREFIX="smp_" # Redhat kernels need "smp" instead
124 PREFIX="" # none needed for UP with <= 1Gig memory
125
126 set_prefix()
127 {
128         h='[0-9a-fA-F]'
129         h8="$h$h$h$h$h$h$h$h"
130         prefix_set=0
131
132         set X `fgrep $SEARCH_STR $KSYMS_FILE 2> /dev/null`; shift
133         str=$2
134         case $str in
135         ${SEARCH_STR}_R$h8)
136                 # No prefix required
137                 ;;
138         $SEARCH_STR)
139                 # No versioning in kernel symbols
140                 ;;
141         ${SEARCH_STR}_R*$h8)
142                 suffix=${str#${SEARCH_STR}_R}
143                 PREFIX=${suffix%$h8}
144                 ;;
145         *)
146                 case $str in
147                 '')
148                         echo afsd: Cannot find \"$SEARCH_STR\" in file $KSYMS_FILE
149                         ;;
150                 *)
151                         echo afsd: Malformed kernel version symbol \"$str\"
152                         ;;
153                 esac
154
155                 echo Guessing prefix from output of uname -v
156                 set X `uname -v`; shift
157                 case $2 in
158                 SMP)
159                         PREFIX=$DEFAULT_SMP_PREFIX
160                         ;;
161                 esac
162                 ;;
163         esac
164 }
165
166 MODLOADDIR=/usr/vice/etc/modload
167 # load_client loads the AFS client module if it's not already loaded. 
168 load_client() {
169         # If LIBAFS is set, use it.
170         if [ -z "$LIBAFS" ] ; then
171                 # Try to determine the right client.
172                 choose_client
173         fi
174     
175         if [ ! -f "$MODLOADDIR/$LIBAFS" ] ; then
176                 echo AFS module $MODLOADDIR/$LIBAFS does not exist. Not starting AFS.
177                 exit 1
178         fi
179
180         if [ -f $KSYMS_FILE ]; then
181                 # use the prefix command if required
182                 set_prefix
183                 /sbin/insmod ${PREFIX:+-P $PREFIX} -f -m $MODLOADDIR/$LIBAFS > $MODLOADDIR/libafs.map 2>&1
184         else
185                 /sbin/insmod $MODLOADDIR/$LIBAFS > $MODLOADDIR/libafs.map 2>&1
186         fi
187 }
188
189 generate_cacheinfo() {
190     if [ "$CACHESIZE" = "AUTOMATIC" ]; then
191         LINE=`df -k $CACHE | tail -1`
192         PART=`echo $LINE | awk '{ if ( ($NF != "/usr")  && ($NF != "/") ) print $NF; else print "NONE";}'`
193         if [ "$PART" = "NONE" ]; then
194             echo "$CACHE or /usr/vice is not a separate partition"
195             echo "you have to change the cachesize in $SYSCNF by hand"
196             exit 1
197         else
198             PARTSIZE=`echo $LINE | awk '{print $2}'`
199             CACHESIZE=`echo $PARTSIZE | awk '{printf "%d",int(($1*.8)/1000)*1000}'`
200         fi
201     fi
202     if [ "x$CACHESIZE" != "x" ]; then
203         echo $AFS:$CACHE:$CACHESIZE >$CACHEINFO
204         chmod 0644 $CACHEINFO
205     else
206         CACHESIZE=`awk -F: '{print $3}' < $CACHEINFO`
207     fi
208 }
209
210 choose_afsdoptions() {
211         if [ -z "$OPTIONS" -o "$OPTIONS" = "AUTOMATIC" ]; then
212                 if [ $CACHESIZE -lt 131072 ]; then
213                         OPTIONS=$SMALL
214                 elif [ $CACHESIZE -lt 524288 ]; then
215                         OPTIONS=$MEDIUM
216                 elif [ $CACHESIZE -lt 1048576 ]; then
217                         OPTIONS=$LARGE
218                 elif [ $CACHESIZE -lt 2097152 ]; then
219                         OPTIONS=$XLARGE
220                 else
221                         OPTIONS=$XXLARGE
222                 fi
223         fi
224         AFSD_OPTIONS="$OPTIONS $VERBOSE"
225         if is_on $ENABLE_AFSDB; then
226                 AFSD_OPTIONS="$AFSD_OPTIONS -afsdb"
227         fi
228         if is_on $ENABLE_DYNROOT; then
229                 AFSD_OPTIONS="$AFSD_OPTIONS -dynroot"
230         fi
231 }
232
233 case "$1" in 
234   start)
235     if [ ! "$afs_rh" -o ! -f /var/lock/subsys/afs ]; then
236         if [ `echo "$OPTIONS" | grep -c dynroot` = 0 ]; then
237             on_network || exit 1
238         fi
239         # Load kernel extensions
240         if  load_client  ; then :
241         else
242                 echo Failed to load AFS client, not starting AFS services.
243                 exit 1
244         fi
245
246         echo "Starting AFS services..... "
247         # Start bosserver, it if exists
248         if  is_on $AFS_SERVER && test -x /usr/afs/bin/bosserver  ; then
249                 /usr/afs/bin/bosserver 
250                 test "$afs_rh" && touch /var/lock/subsys/afs
251         fi
252
253         # Start AFS client
254         if  is_on $AFS_CLIENT && test -x /usr/vice/etc/afsd  ; then
255                 generate_cacheinfo
256                 choose_afsdoptions
257                 /usr/vice/etc/afsd ${AFSD_OPTIONS}
258                 test "$afs_rh" && touch /var/lock/subsys/afs
259                 $AFS_POST_INIT
260         fi
261     fi
262         ;;
263
264   stop)
265     if [ ! "$afs_rh" -o -f /var/lock/subsys/afs ]; then
266         # Stop AFS
267         echo "Stopping AFS services..... "
268
269         if  is_on $AFS_CLIENT  ; then
270                 if [ -x /usr/vice/etc/killafs ] ; then
271                         runcmd "Sending all processes using /afs the TERM signal ..." /usr/vice/etc/killafs TERM
272                         runcmd "Sending all processes using /afs the KILL signal ..." /usr/vice/etc/killafs KILL
273                 fi
274                 umount /afs
275         fi
276
277         if  is_on $AFS_SERVER && test -x /usr/bin/bos ; then
278                 echo "Stopping AFS bosserver"
279                 /usr/bin/bos shutdown localhost -localauth -wait
280                 killall -HUP bosserver
281         fi
282
283         LIBAFS=`/sbin/lsmod | fgrep libafs`
284         if [ -n "$LIBAFS" ] ; then
285                 LIBAFS=`echo $LIBAFS | awk 'BEGIN { FS = " " } { print $1 }'`
286                 /sbin/rmmod $LIBAFS
287         fi
288
289         rm -f /var/lock/subsys/afs
290     fi
291         ;;
292
293   restart)
294         # Restart AFS
295         $0 stop
296         $0 start
297         ;;
298
299   *)
300         echo Usage: 'afs <start|stop|restart>'
301
302 esac
303
304 exit 0
305
306 action fool the Red Hat initscripts