bash: add zfs-fix-encryptionroot
authorJulien Moutinho <julm+julm-nix@sourcephile.fr>
Tue, 28 Feb 2023 17:04:05 +0000 (18:04 +0100)
committerJulien Moutinho <julm+julm-nix@sourcephile.fr>
Tue, 28 Feb 2023 17:04:05 +0000 (18:04 +0100)
home-manager/profiles/bash.nix

index 7c314a67b9372759ca189d4176c6bfc30ecb911c..62f697dacb741956878a45b2840c7a1e5c45c731 100644 (file)
@@ -126,8 +126,27 @@ with lib;
       }
       edit-in-commit () { $EDITOR $(git diff-tree --no-commit-id --name-only -r "$@"); }
       edit-in-git-grep () { $EDITOR $(git grep --name-only --recursive "$@"); }
-      zfs-mount () { for d in $(zfs list -rH -o name "$@"); do sudo zfs mount -l "$d"; done; }
+      zfs-mount () {
+        for d in $(zfs list -rH -o name "$@"); do
+          mountpoint /mnt/"$d" ||
+          sudo zfs mount -l "$d"
+        done
+      }
       zfs-unmount () { sudo zfs unmount -u "$@"; }
+
+      # Raw replicating an encrypted ZFS dataset
+      # changes the encryptionroot to itself.
+      # Use zfs-fix-encryptionroot $new_encryptionroot
+      # to restore the inheritance.
+      # Note that it needs to decrypt all those datasets.
+      zfs-fix-encryptionroot () {
+        zfs load-key "$1"
+        for i in $(zfs list -rHo name "$1" | tail -n +2); do
+          echo >&2 "$i"
+          test "$(zfs get -Ho value encryptionroot $i)" = "$1" ||
+          zfs change-key -li "$i"
+      done
+      }
     '';
     profileExtra = ''
     '';