ZFS Cheat Sheet
ZFS Cheat Sheet
Cheat Sheet
Serge Y. Stroobandt
Why ZFS?
The data integrity problem can be best described as follows:1
System administrators may feel that because they store their data
on a redundant disk array and maintain a well-designed tape-backup
regimen, their data is adequately protected. However, undetected
data corruption can occur between backup periods. Subsequently
backing up corrupted data will yield corrupted data when restored.
The Zeta File System (ZFS) features the capability of being self validating and
self healing from silent data corruption or data rot through continuous data
block checksumming.2 Each block write operation yields a 256‑bit block check-
sum. The resulting block checksum is not stored with the block, but rather
with its parent block.3 Hence, the blocks of a ZFS storage pool form a Merkle
tree in which each block contains the checksum of all of its children.4 This al-
1
lows the entire pool to continuously self validate its data with every operation
on both accuracy and correctness, ensuring data integrity.
Figure 1: How ZFS self heals from silent data corruption or data rot through continu-
ous block checksumming. Source: root.cz
2
ZFS server hardware
To run ZFS, a server needs to meet a set of stringent
hardware requirements. A separate article deals
with this matter in detail.
• ZFS only runs on 64-bit kernels because it contains 64-bit specific code.
• With deduplication switched off, ZFS runs on surprisingly modest
CPUs. Personally, I have ZFS running for several years on a 2010 vintage
Intel Atom D510 processor, clocked down to a mere 1 GHz clock speed to
save on the electricity bill.
• Install ZFS only on a system with lots of RAM. 1 GB is a bare minimum,
2 GB is better, 4 GB RAM would be preferred to start. Remember, ZFS
will use half of the available RAM for the ARC.
• Use ECC RAM when possible for scrubbing data in registers and
maintaining data consistency. The ARC is an actual read-only data cache
of valuable data in RAM.
• ZFS will be not happy running on a system without a swap partition.
I considered for a moment using CentOS for its ten years of support, compared
to a mere five years with Ubuntu Server LTS releases. However, in a home or
small office setting, one most probably expects a bit more from its server than
just file serving on the local network. The CentOS repository is extremely rudi-
mentary. Even for the most basic use cases, one grows dependent on third‑par-
ty repositories.
3
Installation
The Lawrence Livermore National Laboratory has
been working on porting the native Solaris ZFS
source to the Linux kernel as a kernel module. Fol-
low the installation instructions at zfsonlinux.org for
your specific GNU/Linux distribution.
$ dpkg -l zfs*
Virtual devices
A virtual device (VDEV) is a meta-device that can represent one or more de-
vices. ZFS supports seven different types of VDEV.
• File - a pre-allocated file
• Physical Drive (HDD, SDD, PCIe NVME, etc)
• Mirror - a standard RAID1 mirror
• ZFS software raidz1, raidz2, raidz3 ‘distributed’ parity based RAID
• Hot Spare - hot spare for ZFS software raid.
• Cache - a device for level 2 adaptive read cache (ZFS L2ARC)
• Log - ZFS Intent Log (ZFS ZIL)
A device can be added to a VDEV, but cannot be removed from it. For most
home or small office users, each VDEV usually corresponds to a single phys-
ical drive. During pool creation, several of these VDEVs are combined into
forming a mirror or RAIDZ.
• Use whole disks rather than partitions. ZFS can make better use of
the on-disk cache as a result. If you must use partitions, backup
the partition table, and take care when reinstalling data into the other
partitions, so you don’t corrupt the data in your pool.
• Do not mix disk sizes or speeds in VDEVs, nor storage pools. If VDEVs
vary in size, ZFS will favor the larger VDEV, which could lead to
performance bottlenecks.
• Do not create ZFS storage pools from files in other ZFS datasets. This will
cause all sorts of headaches and problems.
4
Zpool creation
A zpool is a pool of storage made from a collection of
VDEVs. One or more ZFS file systems can be created
within a ZFS pool, as will be shown later. In practical
examples, zpools are often given the names pool ,
tank or backup , preferably followed by a digit to
designate between multiple pools on a system.
$ lsblk -o NAME,SIZE,SERIAL,TYPE,MOUNTPOINT
sdb 931.5G WD-WCAV51264701 disk
$ ls -l /dev/disk/by-id/
lrwxrwxrwx 1 root root 9 Jul 8 16:25 ata-WDC_WD20EFRX-68EUZN0_WD-
WCC4M4DKAVF1 -> ../../sdb
5
-o ashift=12
The zpool create property -o ashift can only be set at pool creation time.
Its value corresponds to the base 2 logarithm of the pool sector size in kibibyte.
I/O operations are aligned to boundaries specified by this size. The default
9
value is 9, as 2 = 512, which corresponds to the standard sector size of operat-
ing system utilities used for both reading and writing data. In order to achieve
maximum performance from Advanced Format drives with 4 KiB boundaries,
12
the value should be set to ashift=12 , as 2 = 4096.
Here is a potential way of finding out the physical block size of your drives us-
ing the operating system. However, this method is not fail proof! Western Dig-
ital drives in particular may falsely report as being non-Advanced Format (see
inset below). Anyhow, by installing hdparm , one can query the microprocessor
on the printed circuit board of a hard drive. (Yes, hard drives are actually little
computers on their own.)
6
Figure 2: The label of a 2010 model EARS 1 TB Western Digital Green drive,
featuring Advanced Format. Source: tomshardware.com
-o autoexpand=on
The pool property -o autoexpand=on must be set on before replacing a first
drive in the pool with a larger sized one. The property controls automatic pool
expansion. The default is off . After all drives in the pool have been replaced
with larger drives, the pool will automatically grow to the new, larger drive
size.
-O compression=on
Always enable compression. There is almost certainly no reason to keep it dis-
abled. It hardly touches the CPU and hardly touches throughput to the drive,
yet the benefits are amazing. Compression is disabled by default. This doesn’t
make much sense with today’s hardware. ZFS compression is extremely cheap,
extremely fast, and barely adds any latency to the reads and writes. In fact, in
some scenarios, your disks will respond faster with compression enabled than
disabled. A further benefit is the massive space benefits.
-O dedup=off
Even if you have the RAM for it, ZFS deduplication is, unfortunately, almost
certainly a lose.5 So, by all means avoid using deduplication; even on a ma-
chine built to handle it. Unlike compression, deduplication is very costly on
the system. The deduplication table consumes massive amounts of RAM.
-f
The force option -f forces the use of the stated VDEVs, even if these appear
to be in use. Not all devices can be overridden in this manner.
7
Zpool mirror
$ cd /dev/disk/by-id/
$ ls
$ sudo zpool create -f -o ashift=12 -O compression=on -O dedup=off pool0
mirror scsi-SATA_WDC_WD10EARS-00_WD-WCAV56475795 scsi-
SATA_WDC_WD10EARS-00_WD-WCAV56524564
A mirrored storage pool configuration requires at least two disks, when pos-
sible, connected to separate controllers. Personally, I prefer running a three-
way mirror using three disks, even though this consumes 50% more electric
power. Here is the reason why. When one physical drive in a two-way mirror
fails, the remaining drive needs to be replicated —resilvered in ZFS speak—
to a new physical drive. Replication puts additional stress on a drive and it
is not inconceivable that the remaining drive would fail during its replication
process. “When it rains, it pours.” By contrast, a three-way mirror with one
failed disk maintains 100% redundancy. A similar argument exists in favour of
RAIDZ‑2 and RAIDZ‑3 over RAIDZ‑1.
Zpool RAIDZ
• http://www.openoid.net/zfs-you-should-use-mirror-vdevs-not-raidz/
• When considering performance, know that for sequential writes,
mirrors will always outperform RAID-Z levels. For sequential reads,
RAID-Z levels will perform more slowly than mirrors on smaller data
blocks and faster on larger data blocks. For random reads and writes,
mirrors and RAID-Z seem to perform in similar manners. Striped
mirrors will outperform mirrors and RAID-Z in both sequential, and
random reads and writes.
• Consider using RAIDZ-2 or RAIDZ-3 over RAIDZ-1. You’ve heard
the phrase “when it rains, it pours”. This is true for disk failures. If a disk
fails in a RAIDZ-1, and the hot spare is getting resilvered, until the data
is fully copied, you cannot afford another disk failure during the resilver,
or you will suffer data loss. With RAIDZ-2, you can suffer two disk
failures, instead of one, increasing the probability you have fully
resilvered the necessary data before the second, and even third disk fails.
8
ZFS file system creation
Turning access time writing off with -o atime=off can result in significant
performance gains. However, doing so might confuse legacy mail clients and
similar utilities.
• Avoid running a ZFS root file system on GNU/Linux for the time being.
It’s a bit too experimental for /boot and GRUB.
• However, do create datasets for /home/ , /var/log/ and /var/cache/ .
• For /home/ ZFS installations, setting up nested datasets for each user.
For example, pool0/home/atoponce and pool0/home/dobbs . Consider
using quotas on the datasets.
Scrubbing
Scrubbing examines all data to discover hardware
faults or disk failures, whereas resilvering examines
only that data known to be out of date. Scrubbing
ZFS storage pools does not happen automatically.
It is intiated manually. If possible, scrub consumer-
grade SATA and SCSI disks weekly and enterprise-
grade SAS and FC disks monthly.
Snapshot creation
Snapshot frequently and regularly. Snapshots are
cheap, and can keep a plethora of file versions over
time. Consider using something like
the zfs-auto-snapshot script.
To see the snapshot creation time, add the following zfs list options:
9
$ sudo zfs list -t snapshot -o name,used,refer,creation
NAME USED REFER CREATION
pool0/home@20120722 1.35M 373G Sun Jul 22 12:22 2012
cd /home/.zfs/snapshot/20120722
Automated snapshots
• https://opensource.com/life/16/7/sanoid
• Sanoid
• http://askubuntu.com/a/391970/164341
• zfs-auto-snapshot
$ screen -S backup
$ sudo su - root
$ zfs list -t snapshot
$ zfs send -R pool0/home@20160707 |zfs receive -F backup0/home
$ zpool export backup0
If the -F flag is specified when this stream is received, snapshots and file sys-
tems that do not exist on the sender side are destroyed on the receiver side.
10
Subsequent incremental local backups
If the -i or -I flags are used in conjunction with an additional snapshot argu-
ment and the -R flag, an incremental or delta replication stream is generated.
This is a real time saver. By specifying the -R replication flag, property values,
the current snapshot and file system names are updated when the stream is re-
ceived.
TODO: http://docs.oracle.com/cd/E19253-01/819-5461/gbchx/
Remote backup
The use of zfs send is preferred over using rsync , as the zfs send command
preserves dataset properties. Perform regular (at least weekly) backups of
the full storage pool.
A backup consists of multiple copies. Having only redundant disks, does not
suffice to guarantee data preservation in the event of a power failure, hard-
ware failure, disconnected cables or a viral infection.
• http://www.openoid.net/why-sanoids-zfs-replication-matters/
11
• Oracle Solaris ZFS Administration Guide
• simplesnap
• zrep
If the -F flag is specified when this stream is received, snapshots and file sys-
tems that do not exist on the sender side are destroyed on the receiver side.
Monitoring
In order to preserve maximum performance, it is es-
sential to keep pool allocation under 80% of its full
capacity. The following set of monitoring commands
help to keep an eye on this and other aspects of zpool
health.
12
For example, the file system may get heavily fragmented due to the copy-on-
write nature of ZFS. It might be useful to e-mail capacity reports monthly.
Use the zpool status command for status monitoring. Options are available
for verbose output or an automatic repeat after, for example, every five sec-
onds. Consult the Oracle Solaris documentation for details about
zpool status output. The second command line is a remote server example.
Mounting a backup
A backup drive will attempt to mount at the same point as its original. This re-
sults in a error similar to:
Changing the mountpoint for the backup resolves this issue. The backup zpool
will need to be exported and imported again for this mountpoint change to
take effect.
13
$ sudo zfs set mountpoint=/backup0/home backup0/home
$ sudo zpool export backup0
$ sudo zpool import backup0
Older snapshots of file systems on the backup drive are accessible through
the .zfs/snapshot/ directory within the root of the containing file system.
cd /backup0/.zfs/snapshot/home/20170803
Mounting as /home
Here is how to mount a ZFS file system as home:
SFTP server
Workstations use the SSHFS (Secure Shell Filesystem) client to access server
files through SFTP (Secure Shell File Transfer Protocol).
• Complete file permission transparency is the main reason for preferring
SFTP over the Windows™ style Server Message Block (SMB). This,
despite the fact that ZFS has been integrated with the GNU/Linux
implementation, called Samba.
• Furthermore, SFTP handles well changing the case of filenames. This
cannot be said about the latest Samba versions!
• SFTP significantly simplifies things. If the server is accessible over SSH,
SSHFS should also work.
• Running only an OpenSSH SFTP server significantly reduces exposure
as SFTP is inherently more secure than SMB and NFS.
• Eavesdropping on the (W)LAN is not an issue, since all file transfers are
encrypted.
• The only drawback are the slower file transfer speeds due to
the encryption overhead.
14
SSHFS clients
Here is the bash script that I wrote to mount the server through SSHFS on any
client computer. It gets executed at login, in my case by specifying the script
in Xubuntu’s Session and Startup → Application Autostart . However,
the script can also be run manually, for example after connecting to a mobile
network.
The optimisation parameters are from the following article and tests. As a req-
uisite, the administrator needs to create a /$server/$USER mount point direc-
tory for every user on the client system.
#!/usr/bin/env bash
mountpoint="/$server/$USER"
15
Unmounting /home
Certain Zeta pool and file system operations require prior unmounting of
the file system. However, the \home Zeta file system will refuse to unmount
because of being in use. Here is how to overcome this conundrum:
1. Physical access to the server will be required.
2. For security reasons, set both PermitRootLogin to no and
PasswordAuthentication to no in the /etc/ssh/sshd_config
configuration file, and leave it like that. Here are Ansible tasks to take
care of this:
---
- name: 'PermitRootLogin no'
lineinfile:
path: /etc/ssh/sshd_config
regexp: '^PermitRootLogin '
line: 'PermitRootLogin no' # Was: PermitRootLogin prohibit-password
notify: restart ssh
- meta: flush_handlers
---
- name: 'restart ssh'
service: name=ssh state=restarted
3. Since Ubuntu is being used, and if a root password was not defined yet,
one should do so now using $ sudo passwd root . There really is no
other way.
4. In /etc/dhcp/dhclient.conf , set timeout to 15 . Otherwise, the next
step will cause the boot time to increase with 5 minutes (300 seconds)!
---
- name: 'Lower DHCP timeout to 15s.'
lineinfile:
path: /etc/dhcp/dhclient.conf
regexp: '^timeout'
line: 'timeout 15' # Was: timeout 300
16
5. Reboot with the 8P8C Ethernet plug physically disconnected from
the RJ45 jack. This will ensure no other users are accessing the /home
file system.
6. Open a real text console with Ctrl + Alt + F1 .
7. Immediately log in as root .
8. Kill any X window manager services ~# service lightdm stop
9. List any open files ~# lsof |grep home on /home and kill any
pertaining processes when necessary.
10. Cautiously perform the exporting or mount point setting.
11. Switch to your normal user name as soon as possible for any other
tasks. This adds a level of fool proof safety to any shell commands.
Importing zpools
To import all known storage pools, simply type:
~# zpool import -a
Renaming a zpool
Here is an example where a zpool called tank0 is renamed to pool0 .
17
Attaching more drives
Before adding more drives to a zpool, it might be a good idea to
scrub the zpool first.
Doing so, will limit the resilvering process to a single drive. If the re-
silvering process does not run smoothly (e.g. slow data rate and/or
endless restarting), the drive to blame will be known. Should this
happen, simply detach the faulty drive and dispose of it in an envi-
ronmentally friendly way.
The command zpool attach is used to attach an extra drive to an existing dri-
ve in a zpool as follows:
$ cd /dev/disk/by-id/
$ ls
$ sudo zpool status
The last line is to monitor the resilvering process. If zpool attach is com-
plaining about the new drive being in use, and you know what you are doing,
simply add -f to force zpool attach into what you want it to do.
18
$ sudo zpool status
pool: pool0
state: ONLINE
status: The\ pool is formatted using an\ older on-disk format. The\ pool can
still be used, but some features are unavailable.
action: Upgrade the\ pool using 'zpool upgrade'. Once this is done, the
pool will no longer be accessible on older software versions.
All zpools can easily be upgraded while being online. Use the following com-
mand:
Troubleshooting
When failing to create a zpool
I once ran into a case where zpool creation by-id did not work. Using the sdx
device name did work, however. Exporting and reimporting the pool by-id
kept everything nice and neat.
$ sudo zdb
$ sudo zpool attach pool0 15687870673060007972 /dev/disk/by-id/scsi-
SATA_WDC_WD10EADS-00_WD-WCAV51264701
19
It looks tempting to later update such a backup using resilvering. However,
I strongly advise against this. Doing so leads to problems further down
the road.
Destroying a zpool
ZFS pools are virtually indestructible. If a zpool does not show up im-
mediately, do not presume too quickly the pool to be dead. In my
experience, digging around a bit will bring the pool back to life. Do
not unnecessarily destroy a zpool!
A zfs destroy can cause downtime for other datasets. A zfs destroy will
touch every file in the dataset that resides in the storage pool. The larger
the dataset, the longer this will take, and it will use all the possible IOPS out of
your drives to make it happen. Thus, if it take 2 hours to destroy the dataset,
that’s 2 hours of potential downtime for the other datasets in the pool.
20
Other cheat sheets
• https://www.csparks.com/ZFS%20Without%20Tears.html
• https://pthree.org/2012/04/17/install-zfs-on-debian-gnulinux/
• https://wiki.ubuntu.com/Kernel/Reference/ZFS
• http://www.datadisk.co.uk/html_docs/sun/sun_zfs_cs.htm
• http://lildude.co.uk/zfs-cheatsheet
• http://louwrentius.com/74tb-diy-nas-based-on-zfs-on-linux.html
• http://arstechnica.com/information-technology/2014/02/ars-
walkthrough-using-the-zfs-next-gen-filesystem-on-linux/1/
Real-world example
Here is a real-world ZFS example by Jim Salter of service provider Openoid,
involving reshuffling pool storage on the fly.
References
1. Michael H. Darden. Data integrity: The Dell|EMC distinction. 2002.
Available at: https://www.dell.com/content/topics/global.aspx/power/en/
ps2q02_darden.
2. Jim Salter. Bitrot and atomic COWs: Inside “next-gen” filesystems. 2014.
Available at: http://arstechnica.com/information-technology/2014/01/
bitrot-and-atomic-cows-inside-next-gen-filesystems/1/.
5. Jim Salter. ZFS dedup: Tested, found wanting. 2015. Available at:
http://www.openoid.net/zfs-dedup-tested-found-wanting/.
21
This work is licensed under a Creative Commons
Attribution‑NonCommercial‑ShareAlike 4.0 International License.
Other licensing available on request.
22