From f6b2e0052d9c4a22c36e38a402c14f6f42603843 Mon Sep 17 00:00:00 2001 From: Felix Uhl Date: Thu, 22 Aug 2024 17:51:15 +0200 Subject: [PATCH] zfs_fs: Fix errors when not changing mountpoint Running `zfs set mountpoint=/mnt/my-ds tank/my-ds`, ZFS may try to unmount the dataset even if the mountpoint didn't change. To avoid the confusing error message, this command is now only run when the mountpoint actually changes. --- lib/types/zfs_fs.nix | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/types/zfs_fs.nix b/lib/types/zfs_fs.nix index 813d75a..7e6b7bb 100644 --- a/lib/types/zfs_fs.nix +++ b/lib/types/zfs_fs.nix @@ -73,7 +73,7 @@ "pbkdf2salt" "keyformat" ]; - updateOptions = builtins.removeAttrs config.options onetimeProperties; + updateOptions = builtins.removeAttrs config.options (onetimeProperties ++ [ "mountpoint" ]); mountpoint = config.options.mountpoint or config.mountpoint; in '' @@ -84,12 +84,15 @@ else zfs set ${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "${n}=${v}") updateOptions)} ${config._name} ${lib.optionalString (mountpoint != null) '' - # zfs will try unmount the dataset to change the mountpoint - # but this might fail if the dataset is in use - if ! zfs set mountpoint=${mountpoint} ${config._name}; then - echo "Failed to set mountpoint to '${mountpoint}' for ${config._name}." >&2 - echo "You may need to run when the pool is not mounted i.e. in a recovery system:" >&2 - echo " zfs set mountpoint=${mountpoint} ${config._name}" >&2 + if [[ "$(zfs get -H -o value mountpoint ${config._name})" != "${mountpoint}" ]]; then + echo "Changing mountpoint to '${mountpoint}' for ${config._name}..." + # zfs will try unmount the dataset to change the mountpoint + # but this might fail if the dataset is in use + if ! zfs set mountpoint=${mountpoint} ${config._name}; then + echo "Failed to set mountpoint to '${mountpoint}' for ${config._name}." >&2 + echo "You may need to run when the pool is not mounted i.e. in a recovery system:" >&2 + echo " zfs set mountpoint=${mountpoint} ${config._name}" >&2 + fi fi ''} ''}