Creating a ZFS mirrored vdev with Ubuntu 16.04 (Xenial)

I used raidz for a long time because names ending in “z” sound cool. I switched to a mirrored config because it’s more fault tolerant and reads are faster. (Here is a discussion about ZFS raidz vs. mirrored.) I use ZFS (and not BTRFS) because I’m a Kirk Mckusick fanboy.

Install the ZFS kernel modules and utilities.

The ZFS Linux port is now in the Ubuntu Xenial (Universe) release.

root@ashpool:~# apt-get install zfs-dkms zfsutils-linux

For Ubuntu 12.04 and Ubuntu 14.04, you can use the zfsonlinux PPA.

Create the pool

In this demonstration the three devices I’ll be using for the mirror vdev are all 8Gb disks: /dev/sdb, /dev/sdc, and /dev/sdd. You can use lshw(1) or “fdisk -l” if you’re not sure about disks and identifiers.

root@ashpool:~# zpool create -f z mirror sdb sdc sdd

The “-f” (force) option will create the required partition tables on the target disks. “z” is my choice for the name of the pool. “mirror” is the type of vdev to create.

root@ashpool:~# zpool status
pool: z
state: ONLINE
scan: none requested
z ONLINE 0 0 0
mirror-0 ONLINE 0 0 0
sdb ONLINE 0 0 0
sdc ONLINE 0 0 0
sdd ONLINE 0 0 0
errors: No known data errors

And the pool should already be mounted and available for use:

root@ashpool:~# df -h |grep /z
z 7.7G 0 7.7G 0% /z

I use the pool with a regular user account (“e”), so I fix permissions:

root@ashpool:~# chown -Rv e.e /z
changed ownership of ‘/z’ from root:root to e:e

Enable periodic snapshots

One of the wonderful things about ZFS is its snapshot functionality. Periodic snapshots aren’t configured by default, however, so you’ll need to install some cron-driven scripts for this. I use zfs-auto-snapshot, installed directly from github. More info here.

e@ashpool:~$ curl -L | tar xz -C /tmp/
e@ashpool:~$ pushd /tmp/zfs-auto-snapshot-master && sudo make install && popd && rm -rf /tmp/zfs-auto-snapshot-master
/tmp/zfs-auto-snapshot-master ~
install -d /etc/cron.d
install -d /etc/cron.daily
install -d /etc/cron.hourly
install -d /etc/cron.weekly
install -d /etc/cron.monthly
install -m 0644 etc/zfs-auto-snapshot.cron.frequent /etc/cron.d/zfs-auto-snapshot
install etc/zfs-auto-snapshot.cron.hourly /etc/cron.hourly/zfs-auto-snapshot
install etc/zfs-auto-snapshot.cron.daily /etc/cron.daily/zfs-auto-snapshot
install etc/zfs-auto-snapshot.cron.weekly /etc/cron.weekly/zfs-auto-snapshot
install etc/zfs-auto-snapshot.cron.monthly /etc/cron.monthly/zfs-auto-snapshot
install -d /usr/local/share/man/man8
install src/zfs-auto-snapshot.8 /usr/local/share/man/man8/zfs-auto-snapshot.8
install -d /usr/local/sbin
install src/ /usr/local/sbin/zfs-auto-snapshot

crond in Ubuntu doesn’t include /usr/local/sbin in its PATH, so I fixed it up for this case with a symlink:

e@ashpool:~$ sudo ln -s /usr/local/sbin/zfs-auto-snapshot /usr/sbin

After zfs-auto-snapshot completes a scheduled run, you will see the results in “/z/.zfs/snapshot”.

e@ashpool:~$ ls -l /z/.zfs/snapshot
total 0
dr-xr-xr-x 1 root root 0 Jun 5 17:28 zfs-auto-snap_frequent-2016–06–05–1815
dr-xr-xr-x 1 root root 0 Jun 5 17:28 zfs-auto-snap_frequent-2016–06–05–1845
dr-xr-xr-x 1 root root 0 Jun 5 17:28 zfs-auto-snap_frequent-2016–06–05–1900
dr-xr-xr-x 1 root root 0 Jun 5 17:28 zfs-auto-snap_hourly-2016–06–05–1817

You may cd into any snapshot directory and manipulate all files just as they were when the snapshot was taken.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.