#!/bin/sh
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# ident	"@(#)did_update_vfstab	1.7	05/01/20 SMI"

# is SMF present ?
INCLUDE_FILE="/lib/svc/share/smf_include.sh"
ERROR_CODE=0
if [ -f $INCLUDE_FILE ]
then
	. $INCLUDE_FILE
	ERROR_CODE=$SMF_EXIT_ERR_CONFIG
	svcprop -q -p system/reconfigure system/svc/restarter:default
	if [ $? -eq 0 ]
	then
		_INIT_RECONFIG=set
	fi
fi


# Convert the /global/.devices/node@ partition to mount on the
# appropriate DID device.

filename="/etc/rcS.d/S68did_update_vfstab"

awkp=/usr/bin/awk
sedp=/usr/bin/sed
tmpfile=/tmp/vfstab.new.$$

# Only continue on a reconfig reboot.
if [ -z "$_INIT_RECONFIG" ]; then
        exit 0
fi

# Check if we're in clustered mode.
/usr/sbin/clinfo > /dev/null 2>&1
if [ $? != 0 ] ; then
	exit 0
fi

nodeid=`/usr/sbin/clinfo -n`

# If /global/.devices already uses a DID device, exit.
if [ `grep -c "^[^#].*did.*/global/\.devices/node@$nodeid" /etc/vfstab` -gt 0 ]
then
	rm -f $filename
	exit 0
fi

line=`grep "^[^#].*[ 	]/global/.devices/node@$nodeid[ 	]" /etc/vfstab`
if [ $? -ne 0 ]
then
	rm -f $filename
	exit 0
fi

dev=`echo $line | $awkp '{ print $1 }'`
rdev=`echo $line | $awkp '{ print $2 }'`
mountp=`echo $line | $awkp '{ print $3 }'`
rest=`echo $line | $awkp '{ for (i = 4; i <= NF; i++) { print $i } }'`

case $dev in
/dev/dsk/*)
	device=`echo $dev|$sedp -e 's,..*/,,' -e 's,..$,,'`
	if [ -z "$device" ]; then
		rm -f $filename
		exit 0
	fi


	i=`/usr/cluster/bin/scdidadm -l -o inst -o path | grep $device | $awkp '{ print $1 }'`
	if [ -z "$i" ]; then
		rm -f $filename
		exit 0
	fi

	disk=d`echo $i|$sedp 's, ,,g'`
	if [ -z "$disk" ]; then
		rm -f $filename
		exit 0
	fi

	slice=`echo $dev|$sedp -e 's,..*\(..\)$,\1,'`
	if [ -z "$slice" ]; then
		rm -f $filename
		exit 0
	fi

	newline=`echo "/dev/did/dsk/$disk$slice /dev/did/rdsk/$disk$slice " \
		"$mountp $rest"`
	;;
*)
	newline="$line"
esac

cat /etc/vfstab| while read dev rdev mountp rest
do
	case $mountp in
	/global/.devices/node@$nodeid)
		case $dev in
		/dev/dsk/*)
			echo $newline
			;;

		*)
			echo "$dev	$rdev	$mountp	$rest"
		esac
		;;
	*)
		echo "$dev	$rdev	$mountp	$rest"
	esac
done > $tmpfile

if [ -s $tmpfile ]; then
	mv $tmpfile /etc/vfstab
fi

# Unlink this file.
rm -f $filename
exit 0
