Skip to content

Instantly share code, notes, and snippets.

@zenxedo
Created September 20, 2024 15:54
Show Gist options
  • Save zenxedo/4f9f302c146da73839c33b5487ad98c8 to your computer and use it in GitHub Desktop.
Save zenxedo/4f9f302c146da73839c33b5487ad98c8 to your computer and use it in GitHub Desktop.

ZFS Commands Cheatsheet

Devices and Pools

List all devices in the server

lsblk -S     # General Linux command
geom disk list   # FreeNAS specific

List all ZFS pools

zpool list

Clear device errors from a pool and attempt to rebuild it

zpool clear

Create a zpool called files in RAID 0 using /dev/sd[b,c,d]

zpool create -f files /dev/sdb /dev/sdc /dev/sdd

Optional Parameters

  • -m mountpoint
    Sets the mount point for the root dataset. The default is /pool or altroot/pool if altroot is specified. The mount point must be an absolute path, legacy, or none.
    For more information, see the zfs(8) man page.

Create a zpool called files in RAID 1 (Mirrored) using /dev/sda and /dev/sdb

zpool create files mirror /dev/sda /dev/sdb

Note: Use raidz instead of mirror for RAID 5 setups.

Change the base mountpoint of an existing ZFS Pool called $POOL to $NEWPATH

zfs set mountpoint=$NEWPATH $POOL

Destroy a pool named $POOL

zpool destroy $POOL

File Systems

ZFS file systems live 'inside' pools. By default, they are mounted under the parent pool.

For example:

zfs get mountpoint big
NAME  PROPERTY    VALUE                      SOURCE
big   mountpoint  /var/lib/snapd/hostfs/big  local

To create a filesystem:

zfs create big/docker   # docker is a filesystem inside the pool named big
zfs get mountpoint big/docker
NAME        PROPERTY    VALUE                             SOURCE
big/docker  mountpoint  /var/lib/snapd/hostfs/big/docker  inherited from big

As you can see, big is mounted in /var/lib/snapd/hostfs/big, and big/docker is mounted in /var/lib/snapd/hostfs/big/docker.

Change the mountpoint of big/docker to /var/lib/docker

zfs set mountpoint=/var/lib/docker big/docker
zfs get mountpoint big/docker

List file systems

  • zfs list shows all file systems
  • zfs list -r small shows all file systems under small
  • zfs list -t snapshot shows all snapshots

Snapshots

Snapshots capture a point in time of a file system. Snapshots can't be accessed directly, but you can clone them, back them up, and roll back to them.

Create a snapshot

zfs snapshot big/test@demo

List snapshots:

zfs list -t snapshot

Example output:

NAME                    USED  AVAIL  REFER  MOUNTPOINT
big/test@demo            0B      -    96K  -

Rollback a Snapshot

After making changes (e.g., creating a file):

touch /home/user/test/afile.txt
ls /home/user/test/
afile.txt

Rollback to the snapshot:

zfs rollback big/test@demo
ls /home/user/test/
<nothing is shown>

Clone a Snapshot

You can make a copy of a snapshot into a new filesystem.

Here we take the snapshot big/test@demo and create a new ZFS filesystem big/demobackup from it:

zfs clone big/test@demo big/demobackup

Note: Snapshots used to create cloned file systems can't be deleted until the cloned file system is destroyed.


Clone a Snapshot to Another Machine

You can also send your snapshot to another machine.

Here we're sending the big/test@demo snapshot to othermachine into the backup/test filesystem:

zfs send big/test@demo | ssh othermachine zfs recv backup/test

The receiving machine (othermachine) must have ZFS installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment