How to figure out whether a path is the root of a ZFS dataset mountpoint?

4

I'd like to figure out whether an arbitrary path is the root of a ZFS mountpoint for evaluation in a (Python) script (so return code of a command is preferred over parsing of output). The btrfs equivalent is btrfs subvolume show [path] which returns 0 or 1 is the path is the root of a btrfs subvolume or not.

Afaik I'd use zfs list and parse it, but maybe there's a more elegant way.

I'm using ZFS 0.6.5.8 on Ubuntu 16.10 with Linux 4.8.0.

Karl Richter

Posted 2017-04-01T12:41:11.247

Reputation: 1 641

Answers

3

Currently mounted file systems only

If you are willing to limit yourself to currently mounted ZFS file systems, you can parse /proc/mounts and don't need any particular ZFS knowledge (unless you specifically want to restrict yourself to ZFS file systems).

This may or may not work on non-Linux systems.

/proc/mounts is basically /etc/mtab, but is maintained by the kernel. It contains a list of file system backing devices, mount paths, file system types, and file system flags.

For example, to list the mount points of all mounted ZFS file systems, you could do something like

$ awk '$3 == "zfs" { print $2 }' < /proc/mounts

To check whether a given directory corresponds to a mount point for a mounted ZFS file system,

$ awk '$3 == "zfs" && $2 == "/some/particular/absolute/path" { print "yes" }' < /proc/mounts

To allow for all file systems (not just ZFS), simply remove the $3 == "zfs" check.

Mounted or unmounted ZFS file systems

If you need to include unmounted file systems on currently imported pools, then you need to use zfs get to get a list of all ZFS mountpoints within currently imported pools:

$ sudo zfs list -pH -o mountpoint | grep -q '^/some/particular/absolute/path$' && echo yes

will print yes if a file system with a mount point of /some/particular/absolute/path exists on a pool that is currently imported, whether or not that file system is currently mounted.

File systems on exported pools

I am not aware of any way of listing file systems on exported pools without out-of-band knowledge about the file systems on the pools in question. Hence, I do not believe this combination is possible.

Finishing notes

Always consider whether there exists a utility that does what you want. For example, df, as a side effect, prints the list of currently mounted file systems and their backing devices, and can be expected to not partake in any kernel magic (only using interfaces intended for public use). strace df 2>&1 | less is a good start in that case to see how one might go about finding the information you require.

a CVn

Posted 2017-04-01T12:41:11.247

Reputation: 26 553

2

You can use zfs get to display selected properties, like mountpoint.

# zfs get -pH -o value mountpoint rpool/swap

Explanation:

  • -p produces parsable output with tabs as separators
  • -H omits headers
  • -o name,property,value,source displays the four columns, or any variation of them

You will receive either the path that is set as mountpoint (in most cases this will be / plus dataset name (1), but it can be different(2)), - if no mountpoint is set (3), or legacy (4). You can then use grep to differentiate:

# zfs get -pH -o value mountpoint rpool
/rpool
# zfs get -pH -o value mountpoint rpool/export/home
/export/home
# zfs get -pH -o value mountpoint rpool/swap
-
# zfs get -pH -o value mountpoint rpool/ROOT
legacy

user121391

Posted 2017-04-01T12:41:11.247

Reputation: 1 228