ClearChain

Booting FreeBSD from a root zfs pool using a standard MBR and partition table

This page documents the current state of play for booting the root filesystem (/) off a zfs zpool under FreeBSD, using a standard master boot record (MBR) and a standard partition table. The aim was to be able to have a dual boot system for my laptop using the standard FreeBSD quick selection boot loader.

Note, this does not cover using the gpt based partition tables. If you want to use these, please refer to the following page: http://lulf.geeknest.org/blog/freebsd/Setting_up_a_zfs-only_system/ or booting zfs as root using a small ufs boot partition as provided by the instructions at: http://wiki.freebsd.org/ZFSOnRoot.

Below is the steps required to be able to setup the root zpool

  1. Download a FreeBSD -current fixit cdrom snapshot later than 200901, as these have loader ZFS support
  2. Burn the CD
  3. Boot the CD
  4. Setup any partitions you want – note you must setup the ‘a’ partition to cover the entire device as the loader will use this.
  5. Select Fixit from the menu, and use the CDrom as a source
  6. Create the pool and install the loader (see below)
  7. Copy the required files to boot (see below)

Creating the root zpool and installing the Loader

The fixit cd has everything required to create a zpool, however by default none of the required modules are loaded. Hence they need to be loaded first:

cd /mnt2/boot/kernel
kldload ./opensolaris.ko

kldload ./zfs.ko

Once the modules have been loaded all the zfs tools (zpool,zfs,zdb) should now work. Let assume you want to install FreeBSD to /dev/ad4s2 (second partition on a sata disk). You can do this using:

    zpool create somename /dev/ad4s2

Where somename is the name of the pool you want to create. This creates a single zfs filesystem and a zfs pool of storage. To install the boot loader you need to do:

    # dd if=/mnt2/boot/zfsboot of=/dev/da0s1 count=1
    # dd if=/mnt2/boot/zfsboot of=/dev/da0s1 skip=1 seek=1024

The first line installs boot1, the second line installs boot2.  However, boot2 is responsible for loading boot3 (aka the loader – found in /boot/loader). Hence that must be put in place.

Copying the required files to boot

The easiest way to get things to the point where things are ready to boot is to copy all the files from /dist  – the live distribution. Before you do this, you might like to take advantage of zfs and create some subfilesystems so you can snapshot, monitor space, etc.

For instance creating a /usr and /var filesystem is often very handy:

    #zfs create somename/usr
    #zfs create somename/var

Now you can copy the base system:

    cp -a /dist/* /somename

This will install among other things:

At this point you need to replace the loader with one that understands zfs. You can download the loader from: (To be advise – see cavet below)
And if you have a usb stick copy it in place using:

   mount_msdos /dev/da0s1 /mnt
   cp /mnt/loader /somename/boot

Finally you have to tell FreeBSD where to mount filesystems on a standard boot:

   zfs set mountpoint=/var somename/var
   zfs set mountpoint=/usr somename/usr

At this point any command you type will now probably fail indicating it’s missing some shared library. This is because /usr has now changed. You can get around this by telling the loader where to find valid libraries:

   export LD_LIBRARY_PATH=/mnt2/lib

Next we need to build the zfs cache. This is used by zfs mount to automatically mount zfs filesystems by /etc/rc.d/zfs at boot time. It’s also used to determine if a filesystem is local to the system or belongs to an exported pool.

   mkdir /boot/zfs
   mkdir /somename/boot/zfs
   cd /
   zfs export somename
   zfs import -f somename
   cp /boot/zfs/zfs.cache /somename/boot/zfs/

Finally we tell the loader where we want to boot from and set the init scripts to automatically start all zfs filesystems:

    echo 'zfs_enable="YES"' > /somename/etc/rc.conf
    echo 'zfs_load="YES"' > /somename/boot/loader.conf
    echo 'vfs.root.mountfrom="zfs:somename"' >> /somename/boot/loader.conf

And set the root filesystem to a legacy mountpoint (so zfs mount -a won’t try and mount an already mounted filesystem)

     zfs set mountpoint=legacy somename

At this point you can reboot and things should now boot!
Update: 20090809

There has been a lot of updates to instructions along the way. There are now official ZFS on Root instructions available using GPT/MBR/other available at: http://wiki.freebsd.org/RootOnZFS

Exit mobile version