The following question relates to ZFS on Linux (ZoL) / OpenZFS, as it is provided e.g. in Debian Buster and Debian Bullseye.
As the title says, I'd like to know whether ZFS snapshot names theoretically can contain newlines (as normal filenames can do), and if yes, how I can work safely through a list of snapshot names in a script.
I have tried to create snapshots with such names, but zfs (fortunately) refused to do that. However, I'd like to be sure, and therefore, I'm asking here.
A bit of background: On this site and elsewhere, there often is the question how we could parse the output of
ls to work through the list of file names name by name. The answer mostly is: Don't do this, because there can be unexpected side effects, for example if the file names contain newlines; instead, use bash's globbing. I have understood that (in fact, I always did it like that).
However, when it comes to ZFS snapshot names, there is no globbing. For example, on my box, when I issue something like
zfs list -H -r -o name -t snapshot rpool/vm-garak, I get a list of snapshot names having the entries separated by a newline:
root@cerberus ~/scripts # zfs list -H -r -o name -t snapshot rpool/vm-garak rpool/vm-garak@Q-2021-10-03-12-09-01 rpool/vm-garak@T-2021-10-03-12-14-01 rpool/vm-garak@T-2021-10-03-12-19-01 rpool/vm-garak@Q-2021-10-03-12-24-01 rpool/vm-garak@T-2021-10-03-12-29-01 rpool/vm-garak@T-2021-10-03-12-34-01 rpool/vm-garak@Q-2021-10-03-12-39-01 rpool/vm-garak@T-2021-10-03-12-44-01 rpool/vm-garak@T-2021-10-03-12-49-01 rpool/vm-garak@H-2021-10-03-12-54-01
I have some scripts which work through this list name by name; that is, line by line, relying on the fact the the newline character reliably indicates a new snapshot name.
As long as I have the snapshot creation under my control, this is safe, because I can avoid unreasonable snapshot names. But the snapshots are created by somebody else, so what if there is a newline in the name? As mentioned above, I had no success with creating such snapshot names, but I am surely not aware of all weird methods which could produce them.
A final note: I am aware that I eventually could get away with globbing as long as it concerns normal dataset (file system) snapshots, because ZFS puts them into a hidden directory and makes them accessible as normal directories / files. However, in my case, the snapshots are snapshots from ZVOLs, which ZFS does not make accessible that way.