#!/bin/sh -xe
# Attempt to automate setting up RAIDframe on >2TB disks (abs@absd.org)
#
# Intended to be run from netbsd-7 booted USB disk image
# - Creates small 'BOOT' wedge on each disk to hold /netbsd 'boot[01]'
# - Remainder of each disk put into single large wedge 'raidNcomponent[01]'
# - Creates 32K slice RAID1 device from both large wedges 'raidN'
# - Within RAID1 creates root 'raidNa', 'swap', 'home' and 'media' wedges
#
# Issues:
# - raidframe does not understand NAME=, so $RAID.conf below uses /dev/dk?
# - installboot does not understand NAME=, so uses /dev/dk?
# - requires boot partitions as cannot boot /netbsd from raid1 in wedge
# 
DISK0=wd0
DISK1=wd1
BOOTSIZE=128M	# Needs to hold kernel & boot, but could put base.tgz if wanted
ROOTSIZE=64G
SWAPSIZE=16G
HOMESIZE=16G
RAID=raid0

umount -f /altroot || true
gpt destroy $RAID || true
raidctl -u $RAID || true
gpt destroy $DISK0 || true
gpt destroy $DISK1 || true

gpt create $DISK0
gpt create $DISK1
gpt add -a 64k -l boot0 -t ffs -s $BOOTSIZE $DISK0
gpt add -a 64k -l ${RAID}component0 -t raid $DISK0
gpt add -a 64k -l boot1 -t ffs -s $BOOTSIZE $DISK1
gpt add -a 64k -l ${RAID}component1 -t raid $DISK1
dkctl $DISK0 makewedges
dkctl $DISK1 makewedges
cat >$RAID.conf<<END
START array
1 2 0
START disks
/dev/dk1
/dev/dk3
START layout
64 1 1 1
START queue
fifo 100
END
cat > $RAID.conf <<END
START array
1 2 0
START disks
/dev/dk1
/dev/dk3
START layout
64 1 1 1
START queue
fifo 100
END
raidctl -C $RAID.conf $RAID
raidctl -I 1562315618 $RAID
raidctl -A root $RAID
gpt create $RAID
gpt add -a 64k -l $RAIDa -t ffs -s $ROOTSIZE $RAID
gpt add -a 64k -l swap -t swap -s $SWAPSIZE $RAID
gpt add -a 64k -l home -t ffs -s $HOMESIZE $RAID
gpt add -a 64k -l media -t ffs $RAID
dkctl $RAID makewedges
newfs -O2 -b32k NAME=boot0
newfs -O2 -b32k NAME=boot1
newfs -O2 -b32k NAME=$RAIDa
newfs -O2 -b32k NAME=home
newfs -O2 -b64k NAME=media
mount -o async NAME=$RAIDa /altroot
cd /altroot
mkdir -p home/media
for f in /$(uname -m)/binary/sets/[a-jl-z]*z /$(uname -m)/binary/sets/kern-GENERIC*z ; do
echo $f
tar xzpf $f
done
mount -o async NAME=boot0 /altroot/altroot
cp netbsd usr/mdec/boot /altroot/altroot
umount /altroot/altroot
mount -o async NAME=boot1 /altroot/altroot
cp netbsd usr/mdec/boot /altroot/altroot
umount /altroot/altroot
gpt biosboot -L boot0 $DISK0
gpt biosboot -L boot1 $DISK1
installboot /dev/dk0 /usr/mdec/bootxx_ffsv2
installboot /dev/dk2 /usr/mdec/bootxx_ffsv2
cd /altroot/dev
./MAKEDEV all
cat > /altroot/etc/fstab <<END
NAME=$RAIDa /           ffs rw,log 1 1
NAME=swap   swap        sw  sw     0 0
NAME=home   /home       ffs rw,log 2 2
NAME=media  /home/media ffs rw,log 3 3
ptyfs       /dev/pts    ptyfs   rw      0 0
tmpfs       /var/shm    tmpfs   rw,-m1777,-sram%25
END
cat >> /altroot/etc/rc.conf
rc_configured=YES
wscons=YES
sshd=YES
dhcpcd=YES
END
cd /
umount /altroot
raidctl -i $RAID
exit
