ZFS [HOW-TO] UFS to ZFS on a Live System

Since there is no tutorial how to migrate a UFS installation to ZFS, I'll give my 5-cent contribution. Done this procedure yesterday at my home server that I run several services like Nginx + PHP, Samba, Transmission, Plex, Bhyve VM's, etc.

You'll need a new disk that can hold all your data. The commands bellow are for single partition system, but can be changed to fit your needs. Remember that you must need run rsync for each partition you have!

STOP ALL RUNNING SERVICES (EXCEPT SSH)
UNMOUNT ALL "NON-SYSTEM" PARTITIONS


Code:
gpart create -s gpt da0
gpart add -a 4k -b 2048 -s 200M -t efi da0
gpart add -a 4k -s 16G -t freebsd-swap -l swap0 da0
gpart add -a 4k -t freebsd-zfs -l disk0 da0
newfs_msdos -F 32 -c 1 /dev/da0p1
mount -t msdosfs /dev/da0p1 /mnt
mkdir -p /mnt/EFI/BOOT
cp /boot/loader.efi /mnt/EFI/BOOT/BOOTX64.efi
umount /mnt
gnop create -S 4096 /dev/gpt/disk0
zpool create -o altroot=/mnt zfs0 /dev/gpt/disk0.nop
zfs set mountpoint=none zfs0
zfs set checksum=fletcher4 zfs0
zfs set compression=lz4 zfs0
zfs set atime=off zfs0
zfs create -o mountpoint=/ zfs0/root
zpool set bootfs=zfs0/root zfs0
rsync -axHAWXS --fileflags --force-change --numeric-ids --info=progress2 --exclude=/.snap --exclude=/.sujournal / /mnt/
cat << EOF >> /mnt/boot/loader.conf
zfs_load="YES"
vfs.root.mountfrom="zfs:zfs0/root"
kern.geom.label.disk_ident.enable=0
kern.geom.label.gpt.enable=1
kern.geom.label.gptid.enable=0
EOF
echo 'zfs_enable="YES"' >> /mnt/etc/rc.conf
echo '/dev/gpt/swap0 none swap sw 0 0' > /mnt/etc/fstab
zfs unmount -a
zpool export zfs0
gnop destroy /dev/gpt/disk0.nop
init 0

Replace the disks and boot the system. Done!

Remarks:
- The entire process can be done at a live system. Just remember to stop all running services before start.
- The destination ZFS disk can be attached through USB (3.2 preferably) or internally (SATA / NVMe connector). Since it will use GPT label to identify the partitions, it doesn't matter the drive assignment geom (ada, da, etc).
- Partition table can be changed to MBR, and boot partition to legacy (freebsd-boot) as your needs.
- New ZFS volumes can be created as your needs, including system partitions (/usr, /var, /tmp, etc).
 
Back
Top