#!/bin/sh
#
# Ralf S. Engelschall <rse@FreeBSD.org>
# Written: 2005-01-09, Last Modified: 2007-05-17
# Version: 2.2

# switch to /bin/sh as this script uses Bourne-Shell syntax
exec /bin/sh

# make step-by-step list more reusable by allowing the two disk devices
# and the resulting mirror device to be adapted to the local situation
d1=ad0; d2=ad1; gm=gm0

# make sure the first disk is treated as a really fresh one again
dd if=/dev/zero of=/dev/${d1} bs=512 count=79

# place a new PC MBR onto the first disk with a single FreeBSD slice
# /dev/${d1}s1 _exactly_ as large as the /dev/${d2}s1
size=`fdisk ${d2} | grep ', size ' |\
    head -1 | sed -e 's;^.*size \([0-9]*\).*$;\1;'`
(echo "p 1 165 63 $size"; echo "a 1") | fdisk -v -B -f- -i /dev/${d1}

# switch GEOM mirror to auto-synchronization and add first disk (with
# higher priority). The first disk is now immediately synchronized with
# the second disk content.
gmirror configure -a ${gm}
gmirror insert -p 1 ${gm} /dev/${d1}s1

# wait for the GEOM mirror synchronization to complete
while [ ".`gmirror list ${gm} | grep SYNCHRONIZING`" != . ]; do \
    gmirror list ${gm} | grep Synchronized: |\
    awk '{ printf("\r%s", $0); }'; sleep 5; \
done

# reboot into the final two-disk GEOM mirror setup
# (now actually boots with the MBR and boot stages on first disk
# as it was synchronized from second disk)
shutdown -r now

