#!/bin/sh
#
# /secure/mirroring_init
#


## We assume we have two identical dsks, source = c0t0d0*, copy=c0t2d0*,
## slice s0 and s5 are active.
src_disk="c0t0d0";
dst_disk="c0t2d0";

echo "This SCRIPT WILL WIPE $dst_disk and set it up for mirroring"

#fs=`df -k -F ufs| awk '{print $1}'|egrep -v Filesystem`

# 1. Partition 
echo "\nPartition disks.."
/usr/sbin/prtvtoc /dev/rdsk/${src_disk}s2 | /usr/sbin/fmthard -n mirror -s - /dev/rdsk/${dst_disk}s2

# filesystems
echo "\nCreate filesystems..."
newfs ${dst_disk}s0
newfs ${dst_disk}s5

# or:
#dd if=/dev/rdsk/c0t0d0s2 of=/dev/rdsk/c0t2d0s2 bs=32768k
#fsck

# 2. setup /etc/vfstab
echo "\nsetup /etc/vfstab"
echo "/dev/dsk/${dst_disk}s0       /dev/rdsk/${dst_disk}s0      /newroot        ufs     1       no  logging" >> /etc/vfstab
echo "/dev/dsk/${dst_disk}s5       /dev/rdsk/${dst_disk}s5      /newroot/var    ufs     1       no noatime,nosuid" >> /etc/vfstab

touch /BOOT_MASTER

# 3. mount points
echo "\nmake mount points...."
mkdir /newroot 2>/dev/null; 
chmod 777 /newroot ; 
mount /newroot;
mkdir /newroot/var 2>/dev/null; 
chmod 777 /newroot/var; 
touch /newroot/BOOT_MIRROR
mount /newroot/var;
df -k -F ufs
umount /newroot/var;
umount /newroot;

# 4. setup /etc/vfstab.newroot
echo "\/etc/vfstab.newroot...."
cat > /etc/vfstab.newroot <<EOF
# /etc/vfstab.newroot
# device		device		mount		FS	fsck	mount	mount
#to mount	to fsck		point		type	pass	at boot	options
#
fd	-	/dev/fd	fd	-	no	-
/proc	-	/proc	proc	-	no	-
/dev/dsk/${dst_disk}s1	-	-	swap	-	no	-
swap - /tmp tmpfs - yes size=200m

/dev/dsk/${dst_disk}s0	/dev/rdsk/${dst_disk}s0	/	ufs	1	no	logging
/dev/dsk/${dst_disk}s5	/dev/rdsk/${dst_disk}s5	/var	ufs	1	no	noatime,nosuid

EOF

# 5. setup eeprom
echo "setup eeprom.newroot...."
eeprom boot-device="disk0 disk2"

# 6. Run first mirror boot
echo "Looks ok, check everything, then run:"
echo "     /secure/mirror_boot.sh"

