#!/bin/sh
#
#ident	"@(#)nfs_start_daemons.sh	1.15	06/03/28 SMI"
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# cluster/src/cmd/ha-services/nfs/nfs_start_daemons.sh
#
# Called to start statd, lockd or nfsd.
# Computes the correct way to start them with the
# right arguments, propogates status to caller.
# Called with one argument statd, lockd, nfsd
# or mountd
#
# The output of this command is redirected into a file
# useful only for debugging. So we are free to use things
# like "echo" etc. The user never sees the output. The caller
# (presumably some HANFS RT method) uses the exit code from
# here to syslog() some stuff anyways.
#
# This script is called while holding the HA-NFS state lock
# so we need not worry about it changing from under us.
#
# We need not worry about the processes in question which we
# are attempting to start, the caller has already killed them
# (with SIGKILL). You can only do so much double checking...
#
# Most of the code here is copied from HA1.3
#

HANFS_DIR=/var/run/.hanfs

if [ $# -ne 1 ]
then
	echo invalid argument list $*
	exit 1
fi

daemon=$1
hanfs_state_file=$HANFS_DIR/nfs.state
statd_args_file=$HANFS_DIR/statd.args

# To make this script to be idempotent,
# make sure hanfs_state_file exists
/usr/bin/touch $hanfs_state_file

if [ ! -r $hanfs_state_file ]
then
	echo Can not read $hanfs_state_file
	exit 1
fi

# See which daemon needs to be started up
case $daemon in
	'statd')
		# Need to assemble the -p and -a options
		rm -f $statd_args_file
		# First assemble the -p options

		awk ' {print $3} ' < $hanfs_state_file |
		sort | uniq |
		while read path
		do
        		if [ -d $path ]
        		then
                		echo "-p $path \c" >> $statd_args_file
				else
						echo invalid statmon path $path
						exit 1
        		fi
		done

		# Now the -a options. These are the ipaddresses passed to
		# statd via the -a options. The code basically parses out
		# the first 3 fields (RG, R, StatmonPath) from the state
		# File, what is left is the ipaddresses.
		#

		awk ' { for(i=4; i <= NF; i++) print $i} ' < $hanfs_state_file |
		sort | uniq |
		while read ipaddr
		do
        		echo "-a $ipaddr \c" >> $statd_args_file
		done

		# The statd.args file is created IFF there are
		# additional arguments to statd
		if [ -r $statd_args_file ]
		then
			echo statd args are `cat $statd_args_file`
        		/usr/lib/nfs/statd `cat $statd_args_file`
			rc=$?
		else
			echo No additional statd arguments
        		/usr/lib/nfs/statd
			rc=$?
		fi

		exit $rc

		;;	# End of statd case

	'nfsd')
		# The comment from HA1.3 code is copied verbatim here for
		# reference.  Note that we no longer start nfsd explicitly
		# in the TS scheduling class in order to co-exist with SRM.

		#
		#
		# Search the /etc/init.d/nfs.server command file for the line
		# that starts up the nfsd, in case the system administrator
		# has customized that line, for example, by raising the
		# number of daemon threads or by using the other nfsd
		# switches, such as what network protocols to use.
		# If there are multiple nfsd lines in the nfs.server
		# command file, just use the first.
		# Thus, if the system administrator has increased the
		# number of servers in /etc/init.d/nfs.server, we'll use
		# that value.
		# According to SMCC NFS Server Performance and Tuning Guide:
		# "The default setting, 16, in Solaris 2.4 software environments
		# results in poor NFS response times.  Scale the setting with
		# the number of processors and networks.  Increase the number
		# of NFS server threads by editing the invocation of nfsd in
		# etc/init.d/nfs.server."
		# That is, the manual recommends to the administrator that the
		# way to tune the nfsd is to edit the /etc/init.d/nfs.server
		# script, thus, HA-NFS tries to honor any such edits.
		#

		DEFAULT_NFSDCMD="/usr/lib/nfs/nfsd"
		if [ -f /etc/init.d/nfs.server ]; then
			NFSDCMD="`egrep '^[^#]*/usr/lib/nfs/nfsd' \
				/etc/init.d/nfs.server \
				2>/dev/null | head -1`"
		fi
		if [ -z "$NFSDCMD" ]; then
			NFSDCMD="$DEFAULT_NFSDCMD"
		fi
		echo nfsd command is $NFSDCMD
		$NFSDCMD
		rc=$?
		if [ $rc -ne 0 ]
		then
			echo nfsd start failed with $rc
			exit 1
		fi
		exit 0

		;; # End of nfsd case

	'lockd')
		# Do the same as in nfsd case by extracting the lockd startup
		# command in /etc/init.d/nfs.client
		# Strip the last '&' from the command line using sed.

		# nfs.client file removed from Solaris 10.
		if [ -f /etc/init.d/nfs.client ]; then
			LOCKDCMD="`egrep '^[^#]*/usr/lib/nfs/lockd' \
				/etc/init.d/nfs.client \
				2>/dev/null | tail -1 | sed 's/ &/ /'`"
		fi
		if [ -z "$LOCKDCMD" ]; then
			LOCKDCMD="/usr/lib/nfs/lockd"
		fi

		echo lockd command is $LOCKDCMD
		eval $LOCKDCMD
		rc=$?
		if [ $rc -ne 0 ]
		then
			echo lockd start failed with $rc
			exit 1
		fi
		exit 0

		;; # End of lockd case

	'mountd')
		# No customization is needed in this case
		# simply call mountd

		/usr/lib/nfs/mountd
		rc=$?
		if [ $rc -ne 0 ]
		then
			echo mountd start failed with $rc
			exit 1
		fi
		exit 0

		;; # End of mountd case

	*)
		echo Unknown daemon $daemon
		exit 1

		;;
esac

echo Why are we here
exit 1

