#!/bin/sh

dev='ada0'
tank='zroot'
iface='em0'
host='vbox'
dom='home.lan'
ip='192.168.21.44'
mask='255.255.255.0'
gw='192.168.21.1'
dns='192.168.21.1'




# gpart, use 4K part
echo '--------- GPART ----------------'
gpart create -s GPT $dev
gpart add -b 40 -s 1024 -t freebsd-boot $dev
gpart add -b 2048 -s 512M -t freebsd-swap -l swap0 $dev
gpart add -s 40892376 -t freebsd-zfs -l data0 $dev
gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 $dev
gpart show $dev

echo 'press enter...'
read null



# create ZFS pool
echo '--------- ZFS ----------------'

sysctl kern.geom.debugflags=0x10

# load ZFS kernel modules
kldload /boot/kernel/opensolaris.ko
kldload /boot/kernel/zfs.ko
kldload /boot/kernel/geom_nop.ko


# ZFS block 4K align
gnop create -S 4096 /dev/gpt/data0
zpool create -o altroot=/mnt -o cachefile=/var/tmp/zpool.cache   $tank   /dev/gpt/data0.nop
zpool export $tank
gnop destroy /dev/gpt/data0.nop
zpool import -o altroot=/mnt -o cachefile=/var/tmp/zpool.cache   $tank

zpool set bootfs=$tank $tank

zfs set atime=off $tank
zfs set checksum=fletcher4 $tank


# create folders tree
zfs	create	-o	compression=lzjb	-o	exec=off	-o	setuid=off	$tank/share
zfs create	-o	compression=on		-o	exec=on		-o	setuid=off	$tank/tmp
zfs create 															$tank/usr
zfs create 															$tank/usr/home
zfs	create	-o	compression=off						-o	setuid=off	$tank/usr/ports
zfs	create	-o	compression=off	    -o	exec=off	-o	setuid=off	$tank/usr/ports/distfiles
zfs	create	-o	compression=off	    -o	exec=off	-o	setuid=off	$tank/usr/ports/packages
zfs	create	-o	compression=lzjb	-o	exec=off	-o	setuid=off	$tank/usr/src
zfs	create															$tank/var
zfs	create	-o	compression=lzjb	-o	exec=off	-o	setuid=off	$tank/var/crash
zfs	create							-o	exec=off	-o	setuid=off	$tank/var/db
zfs	create	-o	compression=lzjb	-o	exec=on		-o	setuid=off	$tank/var/db/pkg
zfs	create							-o	exec=off	-o	setuid=off	$tank/var/empty
zfs	create	-o	compression=lzjb	-o	exec=off	-o	setuid=off	$tank/var/log
zfs	create	-o	compression=gzip	-o	exec=off	-o	setuid=off	$tank/var/mail
zfs	create							-o	exec=off	-o	setuid=off	$tank/var/run
zfs	create	-o	compression=lzjb	-o	exec=on		-o	setuid=off	$tank/var/tmp


cd /mnt ; ln -s /usr/home home

chmod 1777 /mnt/tmp
chmod 1777 /mnt/var/tmp


zfs list

echo 'press enter...'
read null


# install FreeBSD
echo '--- install FreeBSD  ---'
cd /usr/freebsd-dist
export DESTDIR=/mnt

for file in base.txz doc.txz kernel.txz; do (echo $file ; cat $file | tar --unlink -xpJf - -C ${DESTDIR:-/}) ; done

#for file in base.txz doc.txz kernel.txz ports.txz src.txz; do (echo $file ; cat $file | tar --unlink -xpJf - -C ${DESTDIR:-/}) ; done
# for amd64
#cat lib32.txz | tar --unlink -xpJf - -C ${DESTDIR:-/}

echo 'press enter...'
read null


echo '--- install base configs  ---'
# install base configs
cat << EOF > /mnt/etc/rc.conf
zfs_enable="YES"
hostname="$host.$dom"
ifconfig_$iface="inet $ip netmask $mask"
sendmail_enable="NO"
syslogd_flags="-ss"
defaultrouter="$gw"

#firewall_enable="YES"
#firewall_quiet="YES"
#firewall_type="simple"

# cyrillic console fonts
#keymap=ru.koi8-r
#keychange="61 ^[[K"
#scrnmap=koi8-r2cp866
#font8x16=cp866b-8x16
#font8x14=cp866-8x14
#font8x8=cp866-8x8
EOF

echo "$ip  $host $host.$dom" >>  /mnt/etc/hosts

cat << EOF > /mnt/etc/resolv.conf
domain $dom
nameserver $dns
EOF




cat << EOF > /mnt/boot/loader.conf
zfs_load="YES"
vfs.root.mountfrom="zfs:$tank"
vm.kmem_size="512M"
vfs.zfs.prefetch_disable=1
EOF



cat << EOF > /mnt/etc/fstab
# Device          Mountpoint     FStype  Options         Dump    Pass#
/dev/gpt/swap0    none           swap    sw              0       0
procfs            /proc          procfs  rw              0       0
EOF


echo 'LOADER_ZFS_SUPPORT=YES' >> /mnt/etc/src.conf




echo '--- copy zpool cache  ---'
cp /var/tmp/zpool.cache /mnt/boot/zfs/zpool.cache


echo '--- set quotas, reservations and mountpoints ---'

export LD_LIBRARY_PATH=/lib 

cd /

# correct ZFS mount points and quotas
zfs unmount -af
zfs set mountpoint=legacy	$tank
zfs set mountpoint=/usr		$tank/usr

zfs set reservation=512M $tank/tmp
#zfs set quota=1G	$tank/tmp   
zfs set mountpoint=/tmp	$tank/tmp
zfs set reservation=512M $tank/var
#zfs set quota=1G	$tank/var  
zfs set mountpoint=/var	$tank/var

zfs set mountpoint=/share		$tank/share

# FIXME cannot mount /share : failed to create mountpoint
# property may be set but unable to remount filesystem...


# make DESTDIR=/mnt TARGET=amd64 TARGET_ARCH=amd64 obj depend all

#tzsetup
#cd /etc/mail && make all install 

# $Id: install.sh,v 1.4 2012/03/02 14:40:18 mechanoid Exp mechanoid $
