Skip to content

Commit

Permalink
zsnapshots: allow filesystem selection when no argument is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
ahesford committed Jan 30, 2024
1 parent cd7d55d commit 13f12f3
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions zfsbootmenu/bin/zsnapshots
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
# vim: softtabstop=2 shiftwidth=2 expandtab

sources=(
/lib/profiling-lib.sh
Expand All @@ -19,18 +20,61 @@ done

unset src sources

list_root_filesystems() {
local fs mountpoint active ret

ret=1
while IFS=$'\t' read -r fs mountpoint active; do
if [ "${mountpoint}" = "/" ]; then
[ "${active}" = "off" ] && continue;
elif [ "${mountpoint}" = "legacy" ]; then
[ "${active}" = "on" ] || continue;
else
continue;
fi

echo "${fs}"
ret=0
done <<< "$( zfs list -H -t filesystem -o name,mountpoint,org.zfsbootmenu:active )"
return "${ret}"
}

# Replace the global_header function with a stub
global_header() {
echo -n -e "\\033[1;33m[ Recover from snapshot ]"
}

if [ $# -ne 1 ] ; then
echo "Usage: $0 filesystem"
exit
fi
fs_header() {
echo -n -e "\\033[1;33m[ Select a filesystem ]"
}

fs="${1}"

if [ -z "${fs}" ]; then
if ! candidates="$(list_root_filesystems 2>/dev/null)"; then
zerror "no root candidates found; specify a filesystem manually"
exit 1
fi

header="$( column_wrap "^[RETURN] select:[ESCAPE] cancel" )"

if ! fs="$(
fzf --header="${header}" --prompt "Filesystem > " \
${HAS_BORDER:+--border-label="$( fs_header )"} \
${HAS_BORDER:+--preview-label-pos=2:bottom} \
--preview-window="up:${PREVIEW_HEIGHT}${HAS_BORDER:+,border-sharp}" \
--preview="/libexec/zfsbootmenu-preview {} '${BOOTFS}'" <<< "${candidates}" \
)"; then
tput clear
exit 0;
fi
fi

if [ -z "${fs}" ]; then
zerror "a filesystem must be selected to browse snapshots"
exit 1
fi

if ! is_zfs_filesystem "${fs}" ; then
zerror "'${fs}' is not a ZFS filesystem"
exit 1
Expand Down

0 comments on commit 13f12f3

Please sign in to comment.