Introduction

An HDD for a rootFS of my FreeBSD machine has broken. I installed a new HDD and recovered the rootFS from ZFS snapshots.

Partitioning

The new HDD is recognized as /dev/da1. In this section, da1 is conformed to a broken disk.

# gpart destroy -F da1
da1 destroyed
# gpart create -s gpt /dev/da1
da1 created
# gpart add -s 64K -b 34 -t freebsd-boot -l /boot/boot0 /dev/da1
da1p1 added
# gpart add -s 1G -t freebsd-swap -l swap0 /dev/da1
da1p2 added
# gpart add -t freebsd-zfs -l disk0 /dev/da1
da1p3 added
# gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 /dev/da1
bootcode written to da1

Create zpool for rootFS

I named a new zpool rpool2. An original (broken) zpool was rpool. If a same name was used, several trouble would be occurred.

# zpool create -f -o altroot=/media rpool2 /dev/gpt/disk0

Recovery from snapshots

The snapshots have been saved in a zpool named tank.

# mkdir /tmp/tank
# zpool import -R /tmp tank

Then I recovered the data from the snapshots.

# zfs recv -F rpool2 < /tmp/tank/backup/[email protected]
# zfs recv -F rpool2/root < /tmp/tank/backup/[email protected]

Finalization

Rename zpool of rootFS

# zpool export rpool2
# zpool import rpool2 rpool

After input above commands, all zfs related commands will stop and give errors. This is no surprise since the system has two root mountpoints and they conflict.

Create zpool.cache

A zpool.cache is required by the OS for recognition of its storage pools.

# zpool import -f -o cachefile=/tmp/zpool.cache -o altroot=/media rpool
# cp -p /tmp/zpool.cache /media/root/boot/zfs/

Set properties to rootFS

# zpool set bootfs=rpool/root rpool
# zfs set mountpoint=/ rpool/root
# zfs set canmount=on rpool/root
# zfs set mountpoint=none rpool
# zfs set canmount=off rpool

Operation check

Finally, I rebooted the OS and confirmed that the system worked well. If the system couldn’t mount rootFS, the mountpoint may be wrong. One of the solutions is changing the mountpoint by

# zfs set mountpoint=legacy rpool/root

Remark

My solution is just an example. Please let me know other easy ways if you know.