+++
title = "dual boot with root on ZFS"
date = "2023-09-17T20:00:00+00:00"
author = "Gibheer"
+++

Some time ago I was experimenting on a server with dual booting FreeBSD
and Linux from the same ZFS pool.

The idea was, that I could allocate the space dynamically instead of using hard
partitions and wasting valuable disk space.

As of writing, ZFSonLinux and FreeBSD use different mechanisms to figure out
which partition to mount as the root partition. And this is what makes the dual
boot possible at the end.

The following description uses only EFI. I have no idea if this also possible
with other boot managers.
[systemd-boot](https://www.man7.org/linux/man-pages/man7/systemd-boot.7.html)
serves as the boot manager.

disk layout
-----------

The disk must contain a EFI system partition and the rest will be made available
for ZFS.

* /boot FAT32 512MB to 1GB
* zpool zfs

zpool settings
--------------

Some care must be taken with enabling features on a ZFS pool, because each
kernel must be able to understand the flags.
For example the ZFS version of a CentOS7 might not be able to use lzma
compression. In that case the ZFS driver will just not mount the partition
and you might end up with that distribution unbootable.

For FreeBSD to be able to boot, the partition must be set as the boot target
directly on the zpool like:

```
zpool set bootfs=rpool/ROOT/freebsd rpool
```

zfs partitions
--------------

I have used rpool/ROOT as the partition for all OS partitions, so I ended up
with:

  * rpool/ROOT/archlinux
  * rpool/ROOT/freebsd

To make things easier I set the following flags on `rpool/ROOT`:

  * compression=on
  * atime=off
  * acltype=posix
  * relatime=on
  * canmount=noauto

This way all OS partitions get the same attributes and I don't have to remember
to set it after creating a new OS tree.

systemd-boot
------------

As for the boot entries I have used systemd-boot and its easily to modify
loader entries.

For FreeBSD my loader entry looks like

```
title FreeBSD
efi /freebsd.efi
```

where I have copied the freebsd.efi file directly into /boot.
FreeBSDs actual /boot partition resides in its partition rpool/ROOT/freebsd.

For my archlinux I use the lts kernel to avoid breakage with new kernel releases
which results in the following entry

```
title Archlinux
linux /vmlinuz-linux-lts
initrd /initramfs-linux-lts.img
options zfs=rpool/ROOT/archlinux rw
```

installation process
--------------------

As for the installation process, I first installed archlinux. After that was
up and running, I installed FreeBSD via downloading the base.txz and unpacking
it into its own partition.

Apart from the default adjustments to /etc/rc.conf and setting a root password
nothing further was needed to properly boot the system.

documentation
-------------

There is some really good documentation for the details out there, for example

* [systemd-boot on Archlinux wiki](https://wiki.archlinux.org/title/systemd-boot)
* [the archzfs project](https://github.com/archzfs/archzfs)
* [the mfsbsd scripts](https://github.com/mmatuska/mfsbsd)
* [FreeBSD handbook](https://docs.freebsd.org/en/books/handbook/zfs/)
* [ZFS man pages](https://github.com/openzfs/zfs/tree/master/man)

And please do not forget to read the man pages. ZFS and systemd have some really
wonderful man pages with tons of knowledge put into them. In most cases there
are even some examples at the end.

Enjoy and have fun.
