#!/sbin/sh -
#
#   $Id: setupmirroring.sh,v 1.4 2004/10/07 17:50:07 dgregor Exp $ 
# 
# Copyright (c) 1999 Daniel J. Gregor, Jr., All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
# 	This product includes software developed by Daniel J. Gregor, Jr.
# 4. The name of Daniel J. Gregor, Jr. may not be used to endorse or promote
#    products derived from this software without specific prior written
#    permission.
# 
# THIS SOFTWARE IS PROVIDED BY DANIEL J. GREGOR, JR. ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL DANIEL J. GREGOR, JR. BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# See if DiskSuite is installed.  If it isn't, kill ourselves and exit.

if [ ! -d /usr/opt/SUNWmd ]
then
	echo "setupmirroring: DiskSuite is not installed.  Exiting." >&2
	rm -rf /mirroring
	rm /etc/rcS.d/S71setupmirroring.sh
	exit 0
fi

PATH=$PATH:/usr/opt/SUNWmd/sbin
export PATH

if [ -f /mirroring/finishmirrorsetup ]
then
	/mirroring/finishmirrorsetup
	rm -rf /mirroring
	rm /etc/rcS.d/S71setupmirroring.sh
	exit 0
fi

rootdisk="`sed 's/#.*//' /etc/vfstab | awk '$3 == "/" { print $1, $2 }'`"
if [ x"$rootdisk" = x"" ]
then
	echo "setupmirroring: could not find root device" >&2
	rm -rf /mirroring
	rm /etc/rcS.d/S71setupmirroring.sh
	exit 0
fi

# We should try to intelligently pick a disk on a different controller and
# verify that it's the same size as the root disk.

otherdisk="`sed 's/#.*//' /var/sadm/system/data/vfstab.unselected | awk '{ print $1, $2 }' | grep '^/' | head -1`"
if [ x"$otherdisk" = x"" ]
then
	echo "setupmirroring: could not find free device for 2nd submirror" >&2
	rm -rf /mirroring
	rm /etc/rcS.d/S71setupmirroring.sh
	exit 0
fi

disk0="`echo $rootdisk | sed -e 's/s[0-9] .*//'`"
rdisk0="`echo $rootdisk | sed -e 's/.* //' -e 's/s[0-9]$//'`"

disk1="`echo $otherdisk | sed -e 's/s[0-9] .*//'`"
rdisk1="`echo $otherdisk | sed -e 's/.* //' -e 's/s[0-9]$//'`"

disk0cyl="`prtvtoc ${rdisk0}s2 | grep 'accessible cylinders' | awk '{ print $2 }'`"
disk1cyl="`prtvtoc ${rdisk1}s2 | grep 'accessible cylinders' | awk '{ print $2 }'`"

if [ $disk0cyl -ne $disk1cyl ]
then
	"setupmirroring: root device and free device (${rdisk1}s2) are not of the same size ($disk0cyl vs. $disk1cyl)" 2>&1
	rm -rf /mirroring
	rm /etc/rcS.d/S71setupmirroring.sh
	exit 0
fi

prtvtoc ${rdisk0}s2 | sed 's/\*.*//;/^$/d' | \
	awk '$5 != 0 && $2 != 5 { print $1 }' | sort \
	> /mirroring/setupmirroring.goodparts

sed 's/#.*//;/^$/d' /etc/vfstab | grep "^$disk0" | sed 's/[ 	].*//;s/.*s//' \
	| sort > /mirroring/setupmirroring.usedparts

slice="`diff /mirroring/setupmirroring.goodparts \
	/mirroring/setupmirroring.usedparts | grep '<' | awk '{ print $2 }' | \
	head -1`"

rm /mirroring/setupmirroring.goodparts /mirroring/setupmirroring.usedparts

if [ x"$slice" = x"" ]
then
	echo "setupmirroring: could not find a free slice for the metadbs" >&2
	rm -rf /mirroring
	rm /etc/rcS.d/S71setupmirroring.sh
	exit 0
fi

/mirroring/mirrorit -a /mirroring/finishmirrorsetup \
	$disk0 $rdisk0 $disk1 $rdisk1 $slice

eeprom "nvramrc=devalias diskmirror `ls -l ${disk1}s0 | sed 's/.* -> \.\.\/\.\.\/devices//;s/sd@/disk@/'`"
eeprom "boot-device=disk diskmirror net"

eeprom "use-nvramrc?=true"

reboot

