#!/sbin/sh
#!Must be statically linked
#
# Andrei.Ryjov@LogicaCMG.COM aka aryzhov@yahoo Thu Feb  5 14:55:51 CET 2004
# CLI VxMirroring for OpForce @ LogicaCMG
#

exec >/dev/console 2>&1
CrashMe() { exec 2>&1; echo ERROR:; echo "$*"; echo Aborted; exit 1; }

#
# This should run right after encapsulation followed by reboot
# We presume that there's only one disk in rootdg with vanilla
# root-encapsulated structure
#
# Now, we need an intelligent guess what disk to use as a submirrort
# The criteria are:
# - Refuse to mirror to the same controller
# - Choose another controller with smallest number of disks;
#   if there are controllers with equal # of disks, pick the lowest controller ID
# - Pick the disk with lowest target ID on that bus
# - pick the lowest LUN
# This should work for shared D1000 at least :-)
#
## MIRRD=c2t0d0 ; [ X$1 != X ] && MIRRD=`basename $1 | cut -ds -f1`
## DISKS=`vxdisk list
ORIG_DISK=`vxprint -d -F '%device' rootdisk | cut -ds -f1`
ROOT_CTRL=`echo $ORIG_DISK | cut -dt -f1`
MIRRD=`vxdisk list | grep 'c[0-9]' | grep -v "^${ROOT_CTRL}t" | head -1 | cut -ds -f1`

/etc/vx/bin/vxdisksetup -i $MIRRD || CrashMe "Disk setup of $MIRRD failed"
vxdg -g rootdg adddisk rootmird=$MIRRD

VL=`vxprint -s -g rootdg -F'%offset %len %vol %name' | sort -n | grep  -v rootdisk-B0 | awk '{print $3}'`

for v in $VL ; do
  echo "\t"=== Mirroring $v ===
  vxassist mirror $v rootmird || CrashMe "Mirroring of $v failed"
done

for v in $VL ; do
  echo "\t"=== Removing original submirror of $v ===
  vxassist remove mirror $v !rootdisk || CrashMe "Submirror breaking failed"
done

echo Re-initializing the original disk

vxdg rmdisk rootdisk
/etc/vx/bin/vxdisksetup -i $ORIG_DISK || CrashMe "Re-init of $ORIG_DISK failed"
vxdg -g rootdg adddisk rootdisk=$ORIG_DISK || CrashMe "Adding $ORIG_DISK back to dg failed"

for v in $VL ; do
  echo "\t"=== Mirroring $v back to original disk ===
  vxassist mirror $v rootdisk  || CrashMe "Mirroring of $v failed"
done

/etc/vx/bin/vxbootsetup
echo === New eeprom settings ===
eeprom | grep vx

