#!/bin/ksh
#
# This script was developed to speed install times and reduce errors
# of 2 drive systems being loaded with jumpstart that required
# mirrored boot disks. It has been used extensively on Solaris 2.6, 7, 8,
# and 9 with minimal testing on betas of 10. Use at your own risk
# and make sure you have a backup first. No warranty express or implied.
#
# This script will setup disksuite to mirror one drive to another and
# setup boot aliases for them. rootdisk will be mirrored to mirrordisk.
# Slice 7 of the disk named in rootdisk will be used for the metadb
# and the slice must already exist. Disksuite must already be installed.
# A script is created and linked to /etc/rc3.d/S99md-setrootenv that
# runs after the reboot and then deletes its link. Please change
# the rootdisk, mirrordisk, and slices to get mirrored before running
# to match your system.
#
# author: Mike Trepanier
# email: mjtripper@yahoo.com
# 
# 2/11/02  cleanup and comments added
# 8/26/02  added Solaris 8 support
# 3/27/03  added Solaris 9 support
# 8/12/03  added sds-root and sds-mirror boot aliases
# 9/17/04  added logging to filesystems
# 11/21/04 added Solaris 10 support
# 2/4/05   submitted to bigadmin

TERM=vt100
export TERM
osrev=`uname -r`
# if rootmirror is yes the first metadevice will be d30
# if set to no the first metadevice will be d60
rootmirror=yes
vfstab=/etc/vfstab

echo "Starting Disksuite configuration script."

#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Change this to your primary boot drive disk name
rootdisk=c1t0d0
echo "rootdisk = $rootdisk"

#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Change this to your mirror disk name
mirrordisk=c1t1d0
echo "mirrordisk = $mirrordisk"

#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Change this to list the disk slices you want mirrored
slices="s0 s1 s3 s4 s5 s6"
echo "Slices to mirror = $slices"

#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Change this to the first name for the metadevice i.e.
# d60 metadevice of first slice listed will have 2 mirrors d40 and d50
# d61 metadevice of second slice listed will have 2 mirrors d41 and d51
# DO NOT change the 10,20, or 30 lines - these are need by the rest of
# the script for setting up the root mirror env
#
if [ $rootmirror == "yes" ]; then
	# don't change these, the rest of the script needs them for root
	pcount=10
	acount=20
	mcount=30
else
	# change these if you are mirroring additional non-root drives
        # and have changed the rootmirror var to no
	pcount=40
	acount=50
	mcount=60
fi

#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# uncomment the prtvtoc line ONLY if the drive being mirrored to
# doesn't have a partition table yet
slzero=s0
sltwo=s2
slseven=s7
prtvtoc /dev/rdsk/$rootdisk$sltwo | fmthard -s - /dev/rdsk/$mirrordisk$sltwo

if [ -f /usr/sbin/metastat ]; then
	cmdroot=/usr/sbin
else if [ -f /usr/opt/SUNWmd/sbin/metastat ]; then
	cmdroot=/usr/opt/SUNWmd/sbin
else
	echo "Can't find disksuite binaries."
	exit
fi
fi

# only run if a db doesn't exist already
if [ $rootmirror == "yes" ]; then
	$cmdroot/metadb -a -f -c2 /dev/dsk/$rootdisk$slseven /dev/dsk/$mirrordisk$slseven
fi

# make backup of /etc/vfstab
cp $vfstab $vfstab.backup
echo "Old $vfstab = "
cat $vfstab
echo 

# loop through vfstab to update mount info and add logging option
for dev in $slices
################
## START DO LOOP
################
do

# this won't attach the second mirror of /
$cmdroot/metainit -f d$pcount 1 1 $rootdisk$dev
$cmdroot/metainit d$acount 1 1 $mirrordisk$dev
$cmdroot/metainit d$mcount -m d$pcount
$cmdroot/metattach d$mcount d$acount

# setup different fields
F1=/dev/md/dsk/d$mcount
F2=/dev/md/rdsk/d$mcount
F3=`cat $vfstab|grep $rootdisk$dev |awk '{print $3}'`
F4=`cat $vfstab|grep $rootdisk$dev |awk '{print $4}'`
F5=`cat $vfstab|grep $rootdisk$dev |awk '{print $5}'`
F6=`cat $vfstab|grep $rootdisk$dev |awk '{print $6}'`
F7=`cat $vfstab|grep $rootdisk$dev |awk '{print $7}'`

# no raw device for swap
if [ "$F4" = swap ]; then
	F2=-
fi

# except for root, update the line
if [ `grep $rootdisk$dev $vfstab |wc -l` != 0 ]; then
	echo "Changing $rootdisk$dev in $vfstab\n"
	if [ $F3 == "/" ]; then
		# don't change device entries, let metaroot do it
		F1=`cat $vfstab|grep $rootdisk$dev |awk '{print $1}'`
		F2=`cat $vfstab|grep $rootdisk$dev |awk '{print $2}'`
		F7=logging
		# but do add logging as an option
	        echo ":%s/\/dev\/dsk\/$rootdisk$dev/#\/dev\/dsk\/$rootdisk$dev/g
:wq!" | vi $vfstab >/dev/null
	        printf "$F1\t$F2\t$F3\t$F4\t$F5\t$F6\t$F7\n" >>$vfstab
	else
		if [ $F7 == "-" ] && [ $F4 != "swap" ]; then
			F7=logging
		fi
		echo ":%s/\/dev\/dsk\/$rootdisk$dev/#\/dev\/dsk\/$rootdisk$dev/g
:wq!" | vi $vfstab >/dev/null
		printf "$F1\t$F2\t$F3\t$F4\t$F5\t$F6\t$F7\n" >>$vfstab
	fi
fi

((pcount=$pcount +1))
((acount=$acount +1))
((mcount=$mcount +1))

###############
## STOP DO LOOP
###############
done

echo "New $vfstab = "
cat $vfstab
echo

if [ $rootmirror == "yes" ]; then
	# finish setting up the mirrored root env
	$cmdroot/metaroot -v $vfstab d30
	case "$osrev" in
	'5.8' | '5.9' | '5.10' )
		/usr/sbin/dumpadm -d /dev/md/dsk/d31
		echo "set md:mirrored_root_flag=1" >> /etc/system
		;;
	'5.7')
                /usr/sbin/dumpadm -d /dev/md/dsk/d31
		;;
	*)
		;;
	esac
	setrootfile=/etc/init.d/md-setrootenv
	echo >$setrootfile
	echo "if [ `$cmdroot/metastat d30 |grep -i \"submirror of d30\" |wc -l` != 2 ]" >>$setrootfile
	echo "then" >>$setrootfile
	echo "$cmdroot/metattach d30 d20" >>$setrootfile
	echo "fi" >>$setrootfile
	echo "rootdevice=$rootdisk$slzero" >>$setrootfile
	echo "mirdevice=$mirrordisk$slzero" >>$setrootfile
	proot=`ls -l /dev/dsk/$rootdisk$slzero |awk '{print $11}' |awk '{FS="/"}{print "/"$4"/"$5"/"$6}'|sed "s/sd/disk/g"`
	pmirror=`ls -l /dev/dsk/$mirrordisk$slzero |awk '{print $11}' |awk '{FS="/"}{print "/"$4"/"$5"/"$6}'|sed "s/sd/disk/g"`
	echo "proot=$proot" >>$setrootfile
	echo "pmirror=$pmirror" >>$setrootfile
	echo "/usr/sbin/eeprom \"use-nvramrc?=true\"" >>$setrootfile
	echo "/usr/sbin/eeprom \"nvramrc=devalias sds-root \$proot devalias sds-mirror \$pmirror\"" >>$setrootfile
	echo "/usr/sbin/eeprom \"boot-device=sds-root\"" >>$setrootfile
	echo "rm /etc/rc3.d/S99md-setrootenv" >>$setrootfile
	ln -s $setrootfile /etc/rc3.d/S99md-setrootenv
	chmod 755 $setrootfile
fi

# dump the config to md.tab
case "$osrev" in
        '5.8' | '5.9' | '5.10' )
                $cmdroot/metastat -p >/etc/lvm/md.tab
                ;;
        *)
                $cmdroot/metastat -p >/etc/opt/SUNWmd/md.tab
                ;;
esac

lockfs -fa
sync;sync;sync;
init 6




##############################################################################
### This script is submitted to BigAdmin by a user of the BigAdmin community.
### Sun Microsystems, Inc. is not responsible for the
### contents or the code enclosed. 
###
###
### Copyright 2008 Sun Microsystems, Inc. ALL RIGHTS RESERVED
### Use of this software is authorized pursuant to the
### terms of the license found at
### http://www.sun.com/bigadmin/common/berkeley_license.html
##############################################################################


