#!/bin/ksh
#
# Copyright (c) 2001 by Sun Microsystems, Inc.
# All rights reserved.
#
#ident	"@(#)setup_nfs	1.9	01/03/07 SMI"
#
# Setup a HA NFS instance on a globally mounted filesystem

# Define command paths
SCRGADM=/usr/cluster/bin/scrgadm
SCSWITCH=/usr/cluster/bin/scswitch
LOGDIR=/var/cluster/spm

# Define NFS Resource information
RG=nfs-rg
R=nfs-rs

# 'mirror-1' is the first metaset created so let's use that one for NFS.
GLOBAL_DIR=/global/mirror-1

# Define the file which contains the logical hostname to use
LOGICAL_HOST_FILE=${LOGDIR}/logical_host

# First check that the proper package is installed.
pkginfo SUNWscnfs > /dev/null
if [ $? -eq 1 ]
then
	echo "HA NFS requires the SUNWscnfs package be installed."
	exit 1
fi

# Second check to make sure that we have a global filesystem created.
df -k | grep "mirror-1" > /dev/null
if [ $? -eq 1 ]
then
	echo "HA NFS requires the global filesystem \"${GLOBAL_DIR}\"."
	exit 2
fi

# Get the logical host from /etc/logical_host
LOGICAL_HOST=`cat $LOGICAL_HOST_FILE`

# NOw it looks like we can configure HS NFS.
echo "Starting to configure HA NFS..."

# Create a failover resource group to contain the NFS resource.
${SCRGADM} -a -g ${RG} -y Pathprefix=${GLOBAL_DIR}

# Add the desired logical hostname resource into the resource group.
${SCRGADM} -a -L -g ${RG} -l ${LOGICAL_HOST}

# Create the directory structure for the NFS config file.
/usr/bin/mkdir ${GLOBAL_DIR}/SUNW.nfs

# Create the dfstab file and set up the share options.
echo "share -F nfs -o rw ${GLOBAL_DIR}" >> ${GLOBAL_DIR}/SUNW.nfs/dfstab.${R}

# Register the NFS resource type.
${SCRGADM} -a -t SUNW.nfs

# Create the NFS resource in the logical resource group.
${SCRGADM} -a -j ${R} -g ${RG} -t SUNW.nfs

# Enable the resource and the resource monitor, manage the resource group,
# and switch the resource group into the online state.
${SCSWITCH} -Z -g ${RG}

# Remove the logical hostname so this script won't run again
rm -f ${LOGDIR}/setup_nfs_flag
