File rc.afs-fuse-client of Package openafs16

#! /bin/sh
# Copyright (c) 1995-2000 SuSE GmbH Nuernberg, Germany.
#
# Author: Peter Poeml <poeml@suse.de>, 2001
# Additions by Christof Hanke <biber@induhviduals.de>, 2009
#
# /etc/init.d/afs-server
#   and its symbolic link
# /usr/sbin/rcafs-server
#
# System startup script for OpenAFS
#
### BEGIN INIT INFO
# Provides:       afs-client
# Required-Start: $local_fs $named $network $remote_fs
# Should-Start:   $syslog $time kdc afs-server
# Should-Stop:    $null
# Required-Stop:  $null
# Default-Start:  3 5
# Default-Stop:   0 2 1 6
# Short-Description: Start OpenAFS Client
# Description: Starts the OpenAFS Client by loading the required kernel-module and starting afsd.   
### END INIT INFO


DAEMON="OpenAFS Client"
DAEMON_PIDFILE=
STARTPROC_LOGFILE=
SUPPORTS_HUP="no"
DAEMON_BIN=/usr/sbin/afsd.fuse

test -x $DAEMON_BIN || exit 5

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

. /etc/sysconfig/openafs-fuse-client

CACHEINFO=${CACHEINFO:-/etc/openafs/cacheinfo}
CACHE=${CACHEDIR:-/var/cache/openafs}
AFS=${AFSDIR:-/afs}
REGENERATE_CELL_INFO=${REGENERATE_CELL_INFO:-no}
REGENERATE_CACHE_INFO=${REGENERATE_CACHE_INFO:-yes}
MEMCACHE=${MEMCACHE:-yes}
VERBOSE=${VERBOSE:-no}
DYNROOT=${DYNROOT:-yes}
FAKESTAT=${FAKESTAT:-yes}
AFSDB=${AFSDB:-yes}
DATA_ENCRYPTION=${DATA_ENCRYPTION:-yes}
THIS_CELL_SERVER_NAME=${THIS_CELL_SERVER_NAME:-unknown}
SERVER_TIMEOUT=120

check_configfiles() {
	if [ ! -f /etc/openafs/ThisCell ]; then
	    echo "Invalid configuration. Cellconfiguration in /etc/openafs/ThisCell is missing."
	    rc_failed 1
            return
	fi
	if [ "$AFSDB" = "no" ] ; then
	    if [ ! -f /etc/openafs/CellServDB ]; then
		echo "No CellServDB found and AFSDB=no."
		echo "Either set AFSDB=yes in /etc/sysconfig/openafs [recommended] or"
		echo "populate the /etc/openafs/CellServDB file"
		rc_failed 1
		return
	    fi

	    if [ "`grep -c \>$THIS_CELL /etc/openafs/CellServDB`" != "1" ] ; then
		echo "Cannot find unique definition of your cell $THIS_CELL in /etc/openafs/CellServDB."
		echo "Consider setting AFSDB=yes in /etc/sysconfig/openafs or"
		echo "fix /etc/openafs/CellServDB."
		rc_failed 1
	    fi
	fi
}

generate_cellinfo() {
    if [ "$REGENERATE_CELL_INFO" = "yes" -a "x$THIS_CELL" != "x" ]; then
        rm -f /etc/openafs/ThisCell #it could be symlinks to server configuration
	echo "$THIS_CELL" > /etc/openafs/ThisCell
	
	if [ -f /etc/openafs/CellServDB -a ! -L /etc/openafs/CellServDB ]; then
	    if grep -q "^>$THIS_CELL\([[:space:]#].*\)\?$" /etc/openafs/CellServDB ; then
	        # CellServDB contains THIS_CELL, replace servers
		awk "
		    /^>$THIS_CELL([[:space:]#].*)?$/   { 
					    this_cell=1; 
					    print \$0;
					    print \"$THIS_CELL_SERVER #$THIS_CELL_SERVER_NAME\";
					    next
				      }
		    /^>/	      {
					    this_cell=0;
					    print \$0;
					    next
				      }
		    /^[^>[:space:]]+/ {
					    if (!this_cell) print \$0;
				      }
		    " /etc/openafs/CellServDB > /etc/openafs/CellServDB.new
	    else
		# add THIS_CELL to CellServDB
		echo ">$THIS_CELL #Cell name, generated from /etc/sysconfig/afs-client" >/etc/openafs/CellServDB.new
		echo "$THIS_CELL_SERVER #$THIS_CELL_SERVER_NAME" >>/etc/openafs/CellServDB.new
		cat /etc/openafs/CellServDB >> /etc/openafs/CellServDB.new    
	    fi
	    mv -f /etc/openafs/CellServDB.new /etc/openafs/CellServDB
	else
	    # new CellServDB
	    rm -f /etc/openafs/CellServDB #it could be symlinks to server configuration
	    echo ">$THIS_CELL #Cell name, generated from /etc/sysconfig/afs-client" >/etc/openafs/CellServDB
	    echo "$THIS_CELL_SERVER #$THIS_CELL_SERVER_NAME" >>/etc/openafs/CellServDB
	fi
    fi
}	

generate_cacheinfo() {
    if [ "$REGENERATE_CACHE_INFO" = "yes" -a "x$CACHESIZE" != "x" ]; then
	if [ "$CACHESIZE" = "AUTOMATIC" ]; then
    	    if [ "$MEMCACHE" = "yes" ]; then
		useCACHESIZE=`awk '/^MemTotal:/{ print int($2 / 8);}' /proc/meminfo`
	    else
		LINE=`df -k $CACHE | tail -1`
		PART=`echo $LINE | awk '{ if ( ($NF != "/usr")  && ($NF != "/") && ($NF != "/var") ) print $NF; else print "NONE";}'`
		if [ "$PART" = "NONE" ]; then
		    echo "$CACHE is not a separate partition"
		    echo "you have to change the cachesize in $SYSCNF by hand"
		    return 1
		else
		    PARTSIZE=`echo $LINE | awk '{print $2}'`
		    useCACHESIZE=`echo $PARTSIZE | awk '{printf "%d",int(($1*.8)/1024)*1024}'`
		fi
	    fi
        else 
            useCACHESIZE=$CACHESIZE
	fi
	echo $AFS:$CACHE:$useCACHESIZE >$CACHEINFO
	chmod 0644 $CACHEINFO
	mkdir -p $CACHE # to be sure
    else
	useCACHESIZE=`awk -F: '{print $3}' < $CACHEINFO`
    fi
    return 0
}

choose_afsdoptions() {
    if [ "x$OPTIONS" = "x" -o "x$OPTIONS" = "xAUTOMATIC" ]; then
	if [ $useCACHESIZE -lt 131072 ]; then
		OPTIONS=$SMALL
	elif [ $useCACHESIZE -lt 524288 ]; then
		OPTIONS=$MEDIUM
	elif [ $useCACHESIZE -lt 1048576 ]; then
		OPTIONS=$LARGE
	elif [ $useCACHESIZE -lt 2097152 ]; then
		OPTIONS=$XLARGE
	else
		OPTIONS=$XXLARGE
	fi
    fi
    AFSD_OPTIONS="$OPTIONS"
    if [ "$MEMCACHE" = "yes" ]; then
	AFSD_OPTIONS="$AFSD_OPTIONS -memcache"
        if [ "x$OPTIONS" != "x" -a "x$CACHESIZE" != "xAUTOMATIC" ];then
	    AFSD_OPTIONS="$AFSD_OPTIONS -blocks $useCACHESIZE"
        fi
    fi	
    if [ "$VERBOSE" = "yes" ]; then
	AFSD_OPTIONS="$AFSD_OPTIONS -verbose"
    fi	
    if [ "$DYNROOT" = "yes" ]; then
	AFSD_OPTIONS="$AFSD_OPTIONS -dynroot"
    fi	
    if [ "$FAKESTAT" = "yes" ]; then
	AFSD_OPTIONS="$AFSD_OPTIONS -fakestat"
    fi	
    if [ "$AFSDB" = "yes" ]; then
	AFSD_OPTIONS="$AFSD_OPTIONS -afsdb"
    fi	
}

killafs() {

    SIGNAL=$1
    
    PIDS=`lsof -Fp $AFS | sed -e 's/p//'` 
    
    if [ "x" != "x$PIDS" ]; then 
	kill -$SIGNAL $PIDS >/dev/null 2>&1
	sleep 3
    fi
}

wait_for_server() {
    TIMEOUT=$SERVER_TIMEOUT
    if [ -f /usr/sbin/bosserver ] && checkproc /usr/sbin/bosserver ; then
	# we run on the same machine as the server
	while /usr/sbin/bos status localhost -localauth |grep -q "salvaging file system"; do
	    echo
	    echo -n "Waiting for local AFS server to come up ..."
	    sleep 1
	    TIMEOUT=$(( $TIMEOUT - 1 ))
	    [ $TIMEOUT -eq 0 ] && return 1
	done
    fi
    return 0
}

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

case "$1" in
    start)
	echo "Starting $DAEMON "
	
	mkdir -p $AFS 

	if generate_cacheinfo; then 
	    generate_cellinfo
	    check_configfiles
	    choose_afsdoptions
	    if wait_for_server; then
		ps ax | grep -q '\[afsd\]' || $DAEMON_BIN $AFSD_OPTIONS || rc_failed 1
		if [ "$DATA_ENCRYPTION" = "yes" ]; then
		    /usr/bin/fs setcrypt on	
        	fi	
		if [ "x$SYSNAME" != "x" ]; then
		    /usr/bin/fs sysname $SYSNAME	
        	fi	
	    else
		#local server not running
		rc_failed 1
	    fi
	else
	    rc_failed 6
	fi
	# Remember status and be verbose
	rc_status -v
	;;
    stop)
	[ "x`whoami`" = xroot ] || exit 4 # do not allow regular user to kill all his processes
	echo -n "Shutting down $DAEMON "
	if ps ax | grep -q '\[afsd\]'; then
	    echo
	    echo "Sending all processes using $AFS the TERM signal ..." 
	    killafs TERM
	    echo "Sending all processes using $AFS the KILL signal ..." 
	    killafs KILL
    	    umount $AFS
	fi
	if grep -q ^$AFS_KERNEL_MODULE /proc/modules ; then
	    /sbin/rmmod $AFS_KERNEL_MODULE || rc_failed 1
	fi
        # rmtsys doesn't go away, so kill them all to be sure
        killall $DAEMON_BIN > /dev/null 2>&1 
	# Remember status and be verbose
	rc_status -v
	;;
    try-restart)
	## Stop the service and if this succeeds (i.e. the 
	## service was running before), start it again.
	$0 stop  &&  $0 start

	# Remember status and be quiet
	rc_status
	;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$0 start

	# Remember status and be quiet
	rc_status
	;;
    force-reload)
	## Signal the daemon to reload its config. Most daemons
	## do this on signal 1 (SIGHUP).
	## If it does not support it, restart.

	echo -n "Reload service $DAEMON"

	if [ "$SUPPORTS_HUP" = "yes" ] ; then 
		killproc -p $DAEMON_PIDFILE -HUP $DAEMON_BIN
		#touch $DAEMON_PIDFILE
		rc_status -v
	else
		$0 stop  &&  $0 start
		rc_status 
	fi
	;;
    reload)
	## Like force-reload, but if daemon does not support
	## signalling, do nothing (!)

	if [ "$SUPPORTS_HUP" = "yes" ] ; then
		# If it supports signalling:
		echo -n "Reload service $DAEMON"
		killproc -p $DAEMON_PIDFILE -HUP $DAEMON_BIN
		#touch $DAEMON_PIDFILE
		rc_status -v
	else
		## Otherwise if it does not support reload:
		rc_failed 3
		rc_status -v
	fi
	;;
    status)
	echo -n "Checking for $DAEMON: "
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0.

	# Status has a slightly different for the status command:
	# 0 - service running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running

	# NOTE: checkproc returns LSB compliant status values.

        ps ax | grep -q '\[afsd\]' || rc_failed 3

	rc_status -v
	;;
    probe)
	## Optional: Probe for the necessity of a reload,
	## give out the argument which is required for a reload.

	if [ "$DAEMON_CONF" -nt "$DAEMON_PIDFILE" ]; then 
		if [ "$SUPPORTS_HUP" = "yes" ]; then 
			echo reload
		else 
			echo restart
		fi
	fi
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit
openSUSE Build Service is sponsored by