#!/bin/sh -e

mkdir -p fs/dev fs/root fs/tmp
ln -s rescue fs/bin
ln -s rescue fs/sbin
tar cCf / - rescue | tar xCf root -
cat <<"EOF" > fs/rc.init
#!/bin/sh
export PATH="/rescue"
mount -t ufs -r /dev/gpt/root /root # Or whatever needs to happen to get root mounted
EOF

makefs -t ffs -o version=2,label=mfsroot,minfree=0,optimization=space -s 5M image fs
mkuzip -s 65536 image
rm image
mv image.uzip mfsroot

cat <<"EOF" > loader.conf
# Make use of the mfsroot
mfsroot_load="YES"
mfsroot_name="/boot/mfsroot"
mfsroot_type="md_image"
init_script="/rc.init" # <- Must mount /root properly!
init_chroot="/root"
vfs.root.mountfrom="ufs:md0.uzip"
EOF