11/20/2018

ZFS on Linux Quick Start

The point of this entry is to document how to use ZFS, and not all of the reasons why you might want to do so, or how it works. If you refer to the sources, then you'll find many compelling reasons to use ZFS, and a deeper understanding of how it works.
I recently had my Buffalo NAS device fail, and decided to try ZFS on mirrored disks in my computer as a replacement until a new device can be found. Over the last few days, I've used the following sources and my own experimentation to produce this entry.

Sources

https://www.open-zfs.org

https://zfsonlinux.org/

https://github.com/zfsonlinux/zfs/wiki/Fedora

https://github.com/zfsonlinux/zfs/

https://wiki.gentoo.org/wiki/ZFS

https://docs.joyent.com/private-cloud/troubleshooting/disk-replacement

https://www.thegeekdiary.com/how-to-backup-and-restore-zfs-root-pool-in-solaris-10


ZFS Basics

My reason for wanting to use ZFS is that offers all the advantages of acls, backup, deduplication, logical volume management, quotas, restore and software raid within an efficient and resilient filesystem. ZFS works by combining devices into pools which can be used to create filesystems (volumes) and snapshots.

The pool and devices are managed with the zpool command and the filesystems and snapshots with the zfs command. Devices (vdevs) can be used for write buffering (log) devices, read caching (cache) devices, spare devices, clones or as data devices in a mirrored (mirror) array or a RAID-like array with single (raidz1), double (raidz2), or triple (raidz3) parity. Both commands can be used to get or set properties which determine the configuration of the pool or the filesystem/snapshot.

The zfs command can make filesystems or snapshots created from the space available in the pool. It can also be used to send or receive snapshots.

Getting Started with ZFS on Linux

ZFS is not in the mainstream Linux kernel, as it licensed under CDDL, which is not compatible with GPL. However, the source of ZFS can be redistributed and compiled under Linux using dynamic kernel modules under Fedora (dkms) and other distributions such as: Arch, Debian, Gentoo, Ubuntu, etc, according to https://zfsonlinux.org/.

To install the repository configuration for zfsonlinux.org on Fedora:

# dnf install http://download.zfsonlinux.org/fedora/zfs-release$(rpm -E %dist).noarch.rpm

To install the zfs package, the kernel-devel, and dependencies to build the zfs kernel modules:

# dnf install kernel-devel zfs

To enable the necessary services for systemd, execute:

# systemctl preset zfs-import-cache zfs-import-scan zfs-import.target zfs-mount zfs-share zfs-zed zfs.target


It was necessary and recommended to reboot the system:

# systemctl reboot

Jumping in zPool

Depending on the number of disks that you want to use, you can create pools with single or multiple disks in mirrored (mirror) or raid-like (raidz?) configurations. The man page for the zpool command gives examples of many of these configurations.

The following zpool command creates a mirrored pool with two disks /dev/sda and /dev/sdb.

# zpool create ztank mirror /dev/sda /dev/sdb

It is best to use entire disks for maximum efficiency, although partitions can be used.

Common zpool Commands

zpool status - display status of pool(s)

zpool iostat - show io statistics for pool(s)

zpool list - show details for pool(s)

zpool add - add a new vdev to a pool for log and cache

zpool remove - remove a vdev to a pool for log and cache

zpool attach - attach a new vdev

zpool detach - detach a vdev

zpool online - active a vdev in a pool

zpool offline - deactivate a vdev in a pool


Other zpool Commands

zpool import - activate a ZFS pool

zpool export - deactivate a ZFS pool

zpool upgrade - show or upgrade a ZFS pool

zpool scrub - check and fix ZFS filesystems

zpool history - show command history of pool


History Example with zpool

# zpool history

History for 'ztank':

2018-11-18.17:20:02 zpool create ztank mirror /dev/sda /dev/sdb

2018-11-18.17:21:38 zfs create ztank/keith

2018-11-18.17:21:52 zfs create ztank/pattie

2018-11-18.17:22:01 zfs create ztank/chris

2018-11-18.17:22:34 zfs create ztank/gallery

2018-11-18.17:24:52 zfs set mountpoint=/var/zfs/gallery ztank/gallery

2018-11-18.17:26:57 zfs create ztank/backup

2018-11-18.17:34:11 zfs set dedup=verify ztank

2018-11-18.17:50:30 zpool add -f ztank log /dev/sdd1

2018-11-18.17:50:44 zpool add -f ztank cache /dev/sdd2

2018-11-18.18:49:58 zfs create ztank/isos

2018-11-18.19:51:52 zfs set logbias=throughput ztank

2018-11-18.20:41:08 zfs set compression=lz4 ztank

2018-11-18.20:48:28 zfs set mountpoint=none ztank/gallery

2018-11-18.20:50:53 zfs set mountpoint=/ztank/gallery ztank/gallery

2018-11-19.00:21:40 zfs snapshot -r ztank/keith@20181119-002133

2018-11-19.00:23:40 zfs snapshot -r ztank/pattie@20181119-002334

2018-11-19.00:23:53 zfs snapshot -r ztank/chris@20181119-002348

2018-11-19.00:24:06 zfs snapshot -r ztank/backup@20181119-002359

2018-11-19.01:15:26 zpool scrub ztank

2018-11-19.19:37:40 zpool import -c /etc/zfs/zpool.cache -aN

2018-11-19.20:26:54 zpool import -c /etc/zfs/zpool.cache -aN

2018-11-19.22:55:21 zpool import -c /etc/zfs/zpool.cache -aN

2018-11-19.23:37:44 zpool import -c /etc/zfs/zpool.cache -aN

2018-11-20.01:38:13 zfs create ztank/VMS

2018-11-20.10:45:52 zfs snapshot -r ztank@backup


Managing Volumes and Snapshots with zfs

The zfs command shows up repeatedly in the above zpool history output. As shown the first step after creating a pool is to create volumes:

# zfs create ztank/keith

# zfs create ztank/pattie

# zfs create ztank/chris

# zfs create ztank/gallery

# zfs create ztank/isos

# zfs create ztank/VMS



To create snapshots of volumes a pool/volume@snapshot syntax is used:


# zfs snapshot -r ztank/keith@20181119-002133

# zfs snapshot -r ztank/pattie@20181119-002334

# zfs snapshot -r ztank/chris@20181119-002348

# zfs snapshot -r ztank/backup@20181119-002359



The zfs snapshot -r option makes the snapshot recursive throughout the filesystem and its descendants.

To view filesystems, volumes and snapshots the zfs list -t all command can be used.

The zfs destroy command can be used to remove snapshots and volumes.

Managing Properties with zfs

It is a mistake to believe the man page for the default value of properties of a filesystem. I found that dedup and compression actual values disagreed with the default values in the man page. To view all the current values of a filesystem or snapshot use:

# zfs get all ztank/chris

NAME PROPERTY VALUE SOURCE

ztank/chris type filesystem -

ztank/chris creation Sun Nov 18 17:21 2018 -

ztank/chris used 1.11G -

ztank/chris available 1.39T -

ztank/chris referenced 1.11G -

ztank/chris compressratio 1.00x -

ztank/chris mounted yes -

ztank/chris quota none default

ztank/chris reservation none default...


Properties set on a parent are inherited by a child in ZFS, so for example, properties set on the pool will be inherited by the volumes in the pool unless overridden.

A few examples of setting properties to be inherited by all volumes:

# zfs set dedup=verify ztank

# zfs set logbias=throughput ztank

# zfs set compression=lz4 ztank


Properties can also be set on a volume or a snapshot. If you wanted to restrict usage on a volume, then a quota can be set. To insure that space will be allocated to a volume a reservation can be set:

# zfs set quota=4G ztank/chris

# zfs set reservation=2G ztank/chris

# zfs get all ztank/chris
 # verifies that the quota and reservation are updated

NAME         PROPERTY              VALUE                  SOURCE
ztank/chris  type                  filesystem             -
ztank/chris  creation              Sun Nov 18 17:21 2018  -
ztank/chris  used                  1.11G                  -
ztank/chris  available             2.89G                  -
ztank/chris  referenced            1.11G                  -
ztank/chris  compressratio         1.00x                  -
ztank/chris  mounted               yes                    -
ztank/chris  quota                 4G                     local
ztank/chris  reservation           2G                     local
...

Backing Up

To create a backup image of a pool or filesystem, first create a snapshot and then send that snapshot to a file or another host:

# zfs snapshot -r ztank@backup

# zfs send -v ztank@backup > /mnt/ztank.dump

To restore an image can be received:

# zfs receive ztank@backup < /mnt/ztank.dump

Maintenance and Repair

Maintenance and repair of a ZFS pool and filesystems should be automatic, but periodically running the following will search for corrupted blocks and repair them:

# zpool scrub ztank

No comments:

About Me - WrightRocket

My photo

I've worked with computers for over 30 years, programming, administering, using and building them from scratch.

I'm an instructor for technical computer courses, an editor and developer of training manuals, and an Android developer.