ZFS NFS shares with avahi zeroconf

How to announce ZFS NFS shares on your home network for clients to automatically discover them.

ZFS and NFS

ZFS1 is an exceptional filesystem but some parts are documented with a certain haze over them. For me, one of these was how to specify the network you want to share it with, but it turned out to be easy and straightforward, much like most things with ZFS.

Let's say we want to share a dataset, named media, sitting in our rpool.

Install NFS server

apt install nfs-kernel-server

Share the relevant ZFS datasets

zfs set sharenfs="rw=@192.168.0.1/24,insecure" rpool/media

and restart the service:

systemctl restart nfs-kernel-server

This will create an insecure, read-write to world share on the media dataset - consider putting it into read-only if that is needed.

Avahi

The steps above are enough to share, but not to let clients auto discovery it; for that, we need avahi and a configured avahi service.

Install avahi

apt install avahi-daemon avahi-utils

Create an avahi service for all NFS shares

To do that, here's a simple bash script:

#!/bin/bash

for export in $(exportfs | grep -vE "^\s" | awk '{print $1}'); do
# all hail the black magic of bash!
# 'tr' replaces all the / to _, then all the . to _ and in the end
# we replace the first occurance of _ - which had become a prefix
# so instead of /a/shared/nfs/path/, we get a_shared_nfs_path
avahifname=$(tr '/' '_'  <<< ${export} | tr '.' '_');
cat > /etc/avahi/services/${avahifname/_}.service << EOF
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
  <name replace-wildcards="yes">NFS ${export}</name>
  <service>
    <type>_nfs._tcp</type>
    <port>2049</port>
    <txt-record>path=${export}</txt-record>
  </service>
</service-group>
EOF
done

Reload avahi:

systemctl restart avahi-daemon

And you're good to go; the NFS shares should show up under Network in your Ubuntu/Mint/etc.

(Oh, by the way: this entry was written by Peter Molnar, and originally posted on petermolnar dot net.)