# FreeBSD 9.0 boot from ZFS raidz1



## frijsdijk (Feb 17, 2012)

I've found that if you use the USB stick image to install FreeBSD, you can stick it in an existing FreeBSD machine somewhere, mount it, and add your own stuff to it. For instance an install-script.

In my case:

```
mount /dev/da8a /mnt
```

Then, from mostly here: http://www.aisecure.net/2011/11/28/root-zfs-freebsd9/ I took the steps to generate this script:

```
#!/bin/sh

# clear disks
dd if=/dev/zero of=/dev/ada0 bs=1m count=1
dd if=/dev/zero of=/dev/ada1 bs=1m count=1
dd if=/dev/zero of=/dev/ada2 bs=1m count=1

# gpt
gpart create -s gpt ada0
gpart create -s gpt ada1
gpart create -s gpt ada2
gpart add -b 34 -s 94 -t freebsd-boot ada0
gpart add -b 34 -s 94 -t freebsd-boot ada1
gpart add -b 34 -s 94 -t freebsd-boot ada2
gpart add -t freebsd-zfs -l disk0 ada0
gpart add -t freebsd-zfs -l disk1 ada1
gpart add -t freebsd-zfs -l disk2 ada2
gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ada0
gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ada1
gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ada2

# create the zroot
zpool create -f zroot raidz1 /dev/gpt/disk0 /dev/gpt/disk1 /dev/gpt/disk2
zpool set bootfs=zroot zroot
zfs set checksum=fletcher4 zroot
zfs set mountpoint=/mnt zroot
zpool export zroot
zpool import -o cachefile=/var/tmp/zpool.cache zroot

# create the 'partitions'
zfs create zroot/usr
zfs create zroot/home
zfs create zroot/var
zfs create -o compression=on -o exec=on -o setuid=off zroot/tmp
zfs create -o compression=lzjb -o setuid=off zroot/usr/ports
zfs create -o compression=off -o exec=off -o setuid=off zroot/usr/ports/distfiles
zfs create -o compression=off -o exec=off -o setuid=off zroot/usr/ports/packages
zfs create -o compression=lzjb -o exec=off -o setuid=off zroot/usr/src
zfs create -o compression=lzjb -o exec=off -o setuid=off zroot/var/crash
zfs create -o exec=off -o setuid=off zroot/var/db
zfs create -o compression=lzjb -o exec=on -o setuid=off zroot/var/db/pkg
zfs create -o exec=off -o setuid=off zroot/var/empty
zfs create -o compression=lzjb -o exec=off -o setuid=off zroot/var/log
zfs create -o compression=gzip -o exec=off -o setuid=off zroot/var/mail
zfs create -o exec=off -o setuid=off zroot/var/run
zfs create -o compression=lzjb -o exec=on -o setuid=off zroot/var/tmp

zfs create -V 2G zroot/swap
zfs set org.freebsd:swap=on zroot/swap
zfs set checksum=off zroot/swap

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

# install bsd
cd /usr/freebsd-dist
export DESTDIR=/mnt
for file in base.txz lib32.txz kernel.txz;
do (echo doing $file .. ; cat $file | tar --unlink -xpJf - -C ${DESTDIR:-/}); done

# copy zpool cache
cp /var/tmp/zpool.cache /mnt/boot/zfs/zpool.cache

# make sure zfs starts on boot
echo 'zfs_enable="YES"' >> /mnt/etc/rc.conf
echo 'zfs_load="YES"' >> /mnt/boot/loader.conf
echo 'vfs.root.mountfrom="zfs:zroot"' >> /mnt/boot/loader.conf

# otherwise bsd will complain
touch /mnt/etc/fstab

# correct mountpoints
zfs set readonly=on zroot/var/empty
zfs umount -af
zfs set mountpoint=legacy zroot
zfs set mountpoint=/tmp zroot/tmp
zfs set mountpoint=/usr zroot/usr
zfs set mountpoint=/var zroot/var

# done - reboot!
```

Adjust where you see fit. This could also easily be adjusted for a mirror configuration.

Copy this script to /mnt/bin/zfsroot.sh .. *umount* it, and stick it into something else and boot from it.
As soon as the install fires up, to to "Live CD". Then, simply type: 


```
sh /bin/zfsroot/sh
```

.. after a few minutes, it will be done, (hopefully without errors, although some may occur). Then type 'reboot', and in my case I now have a full-zfs-raidz1 (3*1TB disks for 1.8TB net space) server ready!


----------

