#!/sbin/sh
#
# Andrei.Ryjov@LogicaCMG.COM aka aryzhov@yahoo Tue Feb  3 21:44:27 CET 2004
# CLI VxInstall/VxEncap - trimming for OpForce @ LogicaCMG
#
# Derived from ARZ Jumpstart
#   http://arz.dyndns.org:8008/Pub/export/Jumpstart/.
# Not backportable as Jumpstart Chrooted Finish,
# since $ROOOPTS and $TARGET handling removed
#

exec >/dev/console 2>&1

#
# Install licenses first
#
echo "
  3JPU-RD63-MPBO-RPNH-B2MN-O6C # Foundation Suite HA 3.5
" | sed 's/#.*$//;/^ *$/d;s/ *//g' | /sbin/vxlicinst

VDSK=`basename \`df -ka / | awk '$NF=="/" {print $1}'\` | cut -ds -f1`
DISK=/dev/rdsk/${VDSK}s2
VTOC="`prtvtoc -h $DISK | sort -n`"
OLDSWAP=`/etc/swap -l | grep '^/dev/' | awk '{print $1}'`

#
# Scratch the Veritas tags, just in case
#
echo "$VTOC" | awk '$2!=14 && $2!=15 {print}' | fmthard -s - $DISK

[ -n "$OLDSWAP" ] && dumpadm -d $OLDSWAP
mkdir -p /dev/vx/dsk/rootdg /dev/vx/rdsk/rootdg
/usr/sbin/modload /kernel/drv/vxdmp
vxconfigd -k -m disable
vxdctl init
vxdg init rootdg
rm    /etc/vx/reconfig.d/state.d/*
touch /etc/vx/reconfig.d/state.d/install-db
sed 's/uadmin 2 1/ exit 0/'  </etc/init.d/vxvm-reconfig >/tmp/vxvm-reconfig

ENCAP="
  /etc/vx/bin/vxencap -c -g rootdg -N rootdisk=$VDSK || exit 0
  touch /etc/vx/reconfig.d/state.d/reconfig && echo '\n\n' | sh /tmp/vxvm-reconfig
"

VxMajRel=`pkginfo -l VRTSvxvm | awk '$1=="VERSION:" {print $2}' | cut -d. -f1`
if [ "$VxMajRel" -ge 4 ] ; then
  vxdctl defaultdg rootdg
  vxdctl bootdg    rootdg
  vxdctl enable
  eval "$ENCAP"
else
  eval "$ENCAP"
  vxdctl enable
fi

echo Looking if any slices got lost at encapsulation ...
VTOV="`prtvtoc -h $DISK | sort -n`"
[ "$VTOV"  =  "$VTOC" ] && {
  echo "
      WARNING: Partition table (VTOC) has not changed by vxencap
  "
  exit 0
}

# Take the current VTOC and add the slices from old VTOC that
# a. Have not "Veritas-known" tags
# b. Have inexistent slice numbers
#
BTG="2 3 5 7 14 15"
BNM="`echo \"$VTOV\" | awk '{print $1}'`"

echo Adding lost slices ...
(
  echo "$VTOV"
  echo "$VTOC" | while read Nm Tg Fl St Cn En Mp ; do
    if (for t in $BTG; do for n in $BNM; do [ $Tg -eq $t -o $Nm -eq $n ] && exit 1; done; done; exit 0) ; then
      echo $Nm $Tg $Fl $St $Cn $En
    fi
  done
) | sort -n | uniq | fmthard -s - $DISK


echo "
\n\n    LogicaCMG / Veritas VxVM trimming for OpForce `date`
\n\n    VxVM installed and configured, root disk encapsulated
\n\n
" >/etc/motd

swap -a /dev/vx/dsk/rootdg/swapvol
[ -n "$OLDSWAP" ] && dumpadm -d $OLDSWAP

##########################################################################################
exit 0
##########################################################################################
#
#
#!!!Under construction!!
#
VDSK=`basename \`df -ka / | awk '$NF=="/" {print $1}'\` | cut -ds -f1`
MPTs=`df -ka | egrep "dsk/${VDSK}s[0-7]" | awk '$NF!="/" {print $NF}'`
MPdl=`echo "$MPTs" | sort`
MPrl=`echo "$MPTs" | sort -r`

COMMANDS="
  cd /
  `for p in $MPrl; do echo /sbin/umount -f $p; done`
  /sbin/mount -O /
  `for p in $MPdl; do echo /sbin/mount     $p; done`
"
/sbin/sh -c "$COMMANDS"

COMMANDS="
  cd /
  /sbin/umount -f /var
  /sbin/umount -f /usr/local
  /sbin/umount -f /usr
  /sbin/mount  -O /
  /sbin/mount     /usr
  /sbin/mount     /usr/local
  /sbin/mount     /var
"
echo "$COMMANDS"
