Share ZFS datasets with NFS

tags: bsd · tutorials

Thanks to Mark Johnston <markj@FreeBSD.org> for recommending this configuration. The article is also mirrored on the FreeBSD Wiki.

Host

Add the following lines to /etc/rc.conf:

nfs_server_enable="YES"
mountd_enable="YES"
mountd_flags="-n"
rpc_lockd_enable="YES"
rpc_statd_enable="YES"
rpcbind_enable="YES"

For some reason the NFS server needs to be restarted the first time after boot. A simple way to do this automatically is:

# echo "service nfsd restart" >> /etc/rc.local

Set the sharenfs property to the dataset you want to share. Replace the IPs and pool/dataset* with your desired values. ZFS properties are documented in zfsprops(8).

We’re going to share 2 datasets, one with read-write and one with read-only access:

# chmod -R 777 /pool/dataset_rw
# zfs set sharenfs='-network=192.168.1.0/24,-alldirs' pool/dataset_rw
# zfs set sharenfs='ro=192.168.1.0/24,-alldirs' pool/dataset_ro

Start the NFS server:

# service nfsd start
# service mountd reload

Guest

Acquire the host’s IP address using ifconfig(8) on it first. We’ll assume it’s 192.168.1.5.

Mount the filesystems. /pool/dataset* corresponds to the actual mount point of the dataset in the host:

# mkdir -p /mnt/dataset_rw /mnt/dataset_ro
# mount -t nfs -o rw 192.168.1.5:/pool/dataset_rw /mnt/dataset_rw
# mount -t nfs -o ro 192.168.1.5:/pool/dataset_ro /mnt/dataset_ro

When done, unmount:

# umount /mnt/dataset_rw /mnt/dataset_ro

In case you want the filesystems to be mounted on boot, add the following lines to /etc/fstab:

192.168.1.5:/dataset_rw   /mnt/dataset_rw	nfs     rw      0       0
192.168.1.5:/dataset_ro   /mnt/dataset_ro	nfs     ro      0       0

Further reading