Dataset mount and encryption key status

I recently was struggling to configure encrypted /home on ZFS on Linux. A big pain point was determining the status of my datasets. I’d like to write a script zfs-list-long that in addition to the information provided by zfs list, would tell me for each dataset:

  1. Is it currently mounted?
  2. If the dataset has encryption, is the encryption key loaded?

How would I get that information from a script?

Both answers are generally right: zfsprops(7) will tell you about other things you can show.

Specifically what you asked for is something like:

$ zfs list -o name,used,avail,refer,mountpoint,mounted,keystatus

Examples:

On my local laptop (all encrypted datasets):

$ zfs list -o name,used,avail,refer,mountpoint,mounted,keystatus
NAME                  USED  AVAIL     REFER  MOUNTPOINT       MOUNTED  KEYSTATUS
lucy                  382G  1.37T       98K  none             no       available
lucy/debian          29.2G  1.37T     17.9G  /                yes      available
lucy/home             352G  1.37T      109K  /home            yes      available
...

On my home NAS (a mix of unencrypted filesystems and laptop backups with no keys locally):

$ zfs list -o name,used,avail,refer,mountpoint,mounted,keystatus
NAME                                            USED  AVAIL  REFER  MOUNTPOINT           MOUNTED  KEYSTATUS
garden                                         36.3G   322G    96K  /garden              yes      -
garden/ROOT                                    26.7G   322G    96K  none                 no       -
garden/ROOT/13.1-RELEASE-p6_2023-02-20_183106     8K   322G  5.52G  /                    no       -
garden/ROOT/13.1-RELEASE-p7_2023-04-11_175220     8K   322G  5.26G  /                    no       -
...
media/backup                                    471G   742G  36.5K  none                 no       -
media/backup/lucy                               471G   742G   118K  none                 no       unavailable
media/backup/lucy/debian                       47.3G   742G  18.1G  none                 no       unavailable
media/backup/lucy/home                          421G   742G   120K  none                 no       unavailable
...

You can get the mount status and keystatus with zfs get. zfs get all <filesystem> will list all of the properties as well.

Look at the man pages for zfs-list, zfs-get and zfsprops. They document scripting mode for each command along with which properties are available / what they are.

I’ve added this alias to my .bashrc:
alias zfslistfull=‘zfs list -o name,used,usedbydataset,usedbysnapshots,avail,refer,canmount,mounted,mountpoint’