#
# ident "@(#)scvxinstall 1.2     03/03/05 SMI"
#
# Copyright (c) 2000-2001 by Sun Microsystems, Inc.
# All rights reserved.
#

#
# This script is activated during the processing initiated by scvxinstall.
#
# Scvxinstall provides automatic VxVM installation and root disk
# encapsulation for SunCluster, hiding the complex steps required and
# protecting the system from user mistakes which can severly impact cluster
# availability.
#
# This script will complete processing by reminoring the global filesystem
# entries for the rootdg.
#

#####################################################
#
# Global Constants
#
#####################################################

# The program name
SC_THIS_PROG=S74scvxinstall.sh                      # Program name

# file to indicate run is needed
SC_PROG=scvxinstall
SC_TMPDIR=/var/cluster/run
SC_VXDIR=${SC_TMPDIR}/${SC_PROG}
SC_RUNRC=${SC_VXDIR}/${SC_PROG}.runrc

# scconf error codes
SC_SCCONF_EBUSY=12

# If /var or /usr is a separate partition we will have to reboot
SC_REBOOT_AGAIN=N


######################################################################
######################################################################

scvx_reminor()
{
    install_log=`cat $SC_RUNRC`

    echo ${SC_THIS_PROG} Reminoring rootdg...
    if [ -f "${install_log}" ] ; then
	    echo ${SC_THIS_PROG} Reminoring rootdg... > ${install_log}
    fi
	
    #
    #  Set the new minor number as the node number times 50
    #

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

    /usr/sbin/umount /global/.devices/node@${node}
    if [ $? -ne 0 ]; then
            scvx_message="${SC_THIS_PROG}:  Unexpected error from umount"
	    echo ${scvx_message}
	    /usr/bin/logger -p daemon.err ${scvx_message}
	    if [ -f "${install_log}" ] ; then
		    echo ${scvx_message} > ${install_log}
            fi
            return 1
    fi

    new_minor=`/usr/bin/expr $node \* 50`
    /usr/sbin/vxdg reminor rootdg $new_minor
    if [ $? -ne 0 ]; then
	    scvx_message="${SC_THIS_PROG}:  Unexpected error from vxdg reminor"
	    echo ${scvx_message}
	    /usr/bin/logger -p daemon.err ${scvx_message}
	    if [ -f "${install_log}" ] ; then
		    echo ${scvx_message} > ${install_log}
            fi
            return 1
    fi

    VOLS="usr var"

    for VOL in $VOLS
    do
        if [ ! -b /dev/vx/dsk/"$VOL" ]
        then
            continue
        fi
        if [ ! -c /dev/vx/rdsk/"$VOL" ]
        then
            scvx_message="${SC_THIS_PROG}:  /dev/vx/dsk/${VOL} exists but /dev/vx/rdsk/${VOL} does not"
	    echo ${scvx_message}
	    /usr/bin/logger -p daemon.err ${scvx_message}
	    if [ -f "${install_log}" ] ; then
		    echo ${scvx_message} > ${install_log}
            fi
            continue
        fi

        MAJOR=`/usr/sbin/vxprint -v -l $VOL | /usr/bin/awk '/device:/ { print $3 }' | /usr/bin/awk -F= '{ print $2} ' | /usr/bin/awk -F/ '{ print $1 }'`

        MINOR=`/usr/sbin/vxprint -v -l $VOL | /usr/bin/awk '/device:/ { print $2 }' | /usr/bin/awk -F= '{ print $2}'`

        /usr/bin/rm -f /dev/vx/dsk/"$VOL"
        /usr/bin/rm -f /dev/vx/rdsk/"$VOL"
        /usr/bin/rm -f /dev/vx/dsk/rootdg/"$VOL"
        /usr/bin/rm -f /dev/vx/rdsk/rootdg/"$VOL"

        /usr/sbin/mknod /dev/vx/dsk/"$VOL" b $MAJOR $MINOR
        if [ $? -ne 0 ]; then
                scvx_message="${SC_THIS_PROG}:  Unable to create block special file /dev/vx/dsk/${VOL}"
	    	echo ${scvx_message}
	    	/usr/bin/logger -p daemon.err ${scvx_message}
	    	if [ -f "${install_log}" ] ; then
		    echo ${scvx_message} > ${install_log}
            	fi
            	return 1
        fi

        /usr/sbin/mknod /dev/vx/rdsk/"$VOL" c $MAJOR $MINOR
        if [ $? -ne 0 ]; then
               scvx_message="${SC_THIS_PROG}:  Unable to create character special file /dev/vx/rdsk/${VOL}"
	        echo ${scvx_message}
	    	/usr/bin/logger -p daemon.err ${scvx_message}
	    	if [ -f "${install_log}" ] ; then
		    echo ${scvx_message} > ${install_log}
            	fi
	        return 1
        fi

        /usr/sbin/mknod /dev/vx/dsk/rootdg/"$VOL" b $MAJOR $MINOR
        if [ $? -ne 0 ]; then
                scvx_message="${SC_THIS_PROG}:  Unable to create block special file /dev/vx/dsk/rootdg/${VOL}"
	    	echo ${scvx_message}
	    	/usr/bin/logger -p daemon.err ${scvx_message}
	    	if [ -f "${install_log}" ] ; then
		    echo ${scvx_message} > ${install_log}
            	fi
            	return 1
        fi

        /usr/sbin/mknod /dev/vx/rdsk/rootdg/"$VOL" c $MAJOR $MINOR
        if [ $? -ne 0 ]; then
                scvx_message="${SC_THIS_PROG}:  Unable to create character special file /dev/vx/rdsk/rootdg/${VOL}"
	        echo ${scvx_message}
	    	/usr/bin/logger -p daemon.err ${scvx_message}
	    	if [ -f "${install_log}" ] ; then
		    echo ${scvx_message} > ${install_log}
            	fi
            	return 1
        fi

	SC_REBOOT_AGAIN=Y
    done

    if [ ${SC_REBOOT_AGAIN} = Y ] ; then
	    #  Remove the file that triggers this function so we don't do it next time we boot
	    /usr/bin/rm $SC_RUNRC

	    # Get the boot token
	    printflg=0

	    while true
	    do
		/usr/cluster/lib/sc/scrconf -b
        	case $? in
		0)			# We got the boot token
			break
			;;
		
		${SC_SCCONF_EBUSY})	# Somebody else is booting
			if [ ${printflg} -eq 0 ]; then
				echo "${SC_THIS_PROG}:  Waiting for all nodes to re-join the cluster ..."
				if [ -f "${install_log}" ] ; then
					echo "${SC_THIS_PROG}:  Waiting for all nodes to re-join the cluster ..." > ${install_log}
				fi
				printflg=1
			fi
			/usr/bin/sleep 5
			;;

		*)			# Error
			scvx_message="${SC_THIS_PROG}:  Failed to get permission to re-boot."
			echo ${scvx_message}
	    		/usr/bin/logger -p daemon.err ${scvx_message}
	    		if [ -f "${install_log}" ] ; then
		    		echo ${scvx_message} > ${install_log}
            		fi
            		return 1
			;;
		esac
	    done

	    # Print message
	    echo "${SC_THIS_PROG}:  This node will now be re-booted."
	    if [ -f "${install_log}" ] ; then
		    echo "${SC_THIS_PROG}:  This node will now be re-booted." > ${install_log}
	    fi
	

	    # Re-boot in cluster mode
	    reboot
    fi

    #  Remove the file that triggers this function so we don't do it next time we boot
    /usr/bin/rm $SC_RUNRC
}

if [ -f $SC_RUNRC ] ; then
	scvx_reminor
fi

