+#!/bin/sh
+
+DESTDISK="ada0"
+DESTDIR="/mnt/zfs"
+ZPOOL="zroot"
+
+SCRIPTBASE=${PWD}
+
+# check if the zpool already exists
+
+ZPOOL_EXISTS=` zpool list -H | cut -f 1 | grep "${ZPOOL}"`
+if [ "${?}" -ne 1 ]; then
+ echo "One zpool called ${ZPOOL} already exists, you have to destroy it first:"
+ echo " # zpool destroy ${ZPOOL}"
+ exit 1
+fi
+
+# check if the disk is ok for writing
+
+echo checking sanity ${DESTDISK}
+echo =============
+gpart show ${DESTDISK} 1>/dev/null
+if [ "${?}" -eq 0 ]; then
+
+ gpart destroy -F ${DESTDISK}
+ if [ "${?}" -ne 0 ]; then
+ echo unable to reset ${DESTDISK}
+ exit 1
+ fi
+fi
+
+echo partitioning ${DESTDISK}
+echo =============
+
+gpart create -s gpt ${DESTDISK}
+gpart add -a 1m -t freebsd-boot -s 128k -l boot ${DESTDISK}
+gpart add -a 1m -s 32G -t freebsd-swap -l swap ${DESTDISK}
+gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ${DESTDISK}
+gpart add -a 1m -t freebsd-zfs -l syszfs ${DESTDISK}
+
+gpart show ${DESTDISK} 1>/dev/null
+
+sysctl vfs.zfs.min_auto_ashift=12
+
+echo set up zfs pool: ${ZPOOL} alt mount: ${DESTDIR}
+echo =================
+
+zpool create -m none -f -R ${DESTDIR} ${ZPOOL} ${DESTDISK}p3
+if [ "${?}" -ne 0 ]; then
+ echo "unable to create zpool"
+ exit 1
+fi
+
+
+for I in ROOT VAR HOME; do
+ zfs create -o mountpoint=none -o canmount=off ${ZPOOL}/${I}
+done
+
+zfs create -o atime=off -o mountpoint=/ ${ZPOOL}/ROOT/master
+zfs create -o mountpoint=/usr/obj ${ZPOOL}/ROOT/obj
+zfs create -o compression=off -o mountpoint=/usr/src ${ZPOOL}/ROOT/src
+# useless ? as far /tmp mountpoint will be mounted by tmpfs
+zfs create -o mountpoint=/tmp ${ZPOOL}/ROOT/tmp
+
+zfs create -o mountpoint=/usr/local ${ZPOOL}/LOCAL
+zfs create -o mountpoint=/usr/local/etc ${ZPOOL}/LOCAL/config
+zfs create -o copies=1 -o mountpoint=/usr/src -o compression=off ${ZPOOL}/src
+zfs create -o copies=1 -o mountpoint=/usr/doc -o compression=on ${ZPOOL}/doc
+zfs create -o copies=1 -o mountpoint=/usr/obj -o compression=on ${ZPOOL}/obj
+
+
+zfs create -o mountpoint=/var ${ZPOOL}/VAR/master
+zfs create -o mountpoint=/var/crash ${ZPOOL}/VAR/crash
+zfs create -o exec=off -o setuid=off -o mountpoint=/var/db ${ZPOOL}/VAR/db
+zfs create -o compression=lz4 -o exec=on -o setuid=off -o mountpoint=/var/db/pkg ${ZPOOL}/VAR/db/pkg
+zfs create -o compression=on -o exec=off -o setuid=off -o mountpoint=/var/mail ${ZPOOL}/VAR/mail
+zfs create -o compression=on -o exec=off -o setuid=off -o mountpoint=/var/log ${ZPOOL}/VAR/log
+zfs create -o exec=off -o setuid=off -o mountpoint=/var/run ${ZPOOL}/VAR/run
+zfs create -o exec=off -o setuid=off -o mountpoint=/var/tmp ${ZPOOL}/VAR/tmp
+
+zfs create -o mountpoint=/usr/home ${ZPOOL}/HOME/master
+
+
+chmod 1777 ${DESTDIR}/tmp ${DESTDIR}/var/tmp
+
+zfs list -r ${ZPOOL}
+
+cd ${DESTDIR}/tmp
+if [ "${?}" -ne 0 ]; then
+ echo zfs mountpoints arent ready
+ exit 1
+fi
+
+echo Fetching and Extracting base files into ${DESTDIR}
+echo =======================================
+
+
+for I in base.txz kernel.txz lib32.txz; do
+ fetch http://ftp.freebsd.org/pub/FreeBSD/snapshots/amd64/12.0-ALPHA8/${I}
+ tar --unlink -pJxf ${I} -C ${DESTDIR}
+done
+
+echo writing configuration files
+echo ==========================
+
+cat << EOF >> ${DESTDIR}/etc/fstab
+/dev/${DESTDISK}p2 none swap sw 0 0
+tmpfs /tmp tmpfs rw,mode=1777 0 0
+EOF
+
+cat << EOF >> ${DESTDIR}/boot/loader.conf
+zfs_load=YES
+tmpfs_load=YES
+EOF
+
+cat << EOF >> ${DESTDIR}/boot/loader.conf.local
+vfs.root.mountfrom="zfs:${ZPOOL}/ROOT/master"
+EOF
+
+cat << EOF >> ${DESTDIR}/etc/rc.conf
+sshd_enable="YES"
+sendmail_enable="NONE"
+kld_list="vmm cuse4bsd cpuctl filemon"
+ntpd_enable="YES"
+# Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
+dumpdev="AUTO"
+zfs_enable="YES"
+EOF
+
+cd ${SCRIPTBASE}
+for I in resolv.conf rc.conf.local; do
+ if [ -r "${I}" ]; then
+ echo installing ${I}
+ install -o root -g wheel -m 0644 ${I} ${DESTDIR}/etc/
+ fi
+done
+
+for I in loader.conf.local; do
+ if [ -r "${I}" ]; then
+ echo installing ${I}
+ install -o root -g wheel -m 0644 ${I} ${DESTDIR}/boot/
+ fi
+done
+
+echo installing rc.conf.d
+install -o root -g wheel -d -m 0755 ${DESTDIR}/etc/rc.conf.d
+if [ -d "./rc.conf.d" ]; then
+ echo installing files into rc.conf.d
+ install -o root -g wheel -m 0644 ./rc.conf.d/* ${DESTDIR}/etc/rc.conf.d/
+fi
+
+chroot ${DESTDIR} passwd
+chroot ${DESTDIR} adduser
+
+read -p "Do you wish to open a shell ?" yn
+case $yn in
+ [Nn]* ) exit 0;;
+ [Yy]* ) ;;
+ * ) echo "Please answer yes or no.";;
+esac
+chroot ${DESTDIR} tcsh
+exit 0
+