Skip to content

Commit 402474b

Browse files
committed
Make alt-w a toggle between R/O and R/W imports
1 parent 064519b commit 402474b

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

90zfsbootmenu/zfsbootmenu-lib.sh

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ kexec_kernel() {
237237

238238
# Export if read-write, to ensure a clean pool
239239
pool="${selected%%/*}"
240-
if [ "$( zpool get -H -o value readonly "${pool}" )" = "off" ]; then
240+
if is_writable "${pool}"; then
241241
export_pool "${pool}"
242242
fi
243243

@@ -853,6 +853,27 @@ resume_prompt() {
853853
return 0
854854
}
855855

856+
# arg1: pool name
857+
# prints nothing
858+
# returns: 0 when pool is writable, 1 otherwise
859+
860+
is_writable() {
861+
local pool roflag
862+
863+
pool="${1}"
864+
[ -n "${pool}" ] || return 1
865+
866+
# Pool is not writable if the property can't be read
867+
roflag="$( zpool get -H -o value readonly "${pool}" 2>/dev/null )" || return 1
868+
869+
if [ "x${roflag}" = "xoff" ]; then
870+
return 0
871+
fi
872+
873+
# Otherwise, pool is not writable
874+
return 1
875+
}
876+
856877
# arg1: pool name
857878
# prints: nothing
858879
# returns: 0 on success, 1 on failure
@@ -863,15 +884,15 @@ set_rw_pool() {
863884
pool="${1}"
864885
[ -n "${pool}" ] || return 1
865886

887+
# If force_export is set, skip evaluating if the pool is already read-write
888+
# shellcheck disable=SC2154
889+
[ -n "${force_export}" ] || ! is_writable "${pool}" || return 0
890+
866891
if grep -q "${pool}" "${BASE}/degraded" >/dev/null 2>&1; then
867892
color=red delay=10 timed_prompt "Operation prohibited" "Pool '${pool}' cannot be imported read-write"
868893
return 1
869894
fi
870895

871-
# If force_export is set, skip evaluating if the pool is already read-write
872-
# shellcheck disable=SC2154
873-
[ -n "${force_export}" ] || [ "x$( zpool get -H -o value readonly "${pool}" )" = "xon" ] || return 0
874-
875896
resume_prompt "${pool}" || return 1
876897

877898
if export_pool "${pool}" ; then

90zfsbootmenu/zfsbootmenu.sh

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ if [[ -n "${BOOTFS}" ]]; then
126126
tput clear
127127
if ! key_wrapper "${BOOTFS}" ; then
128128
emergency_shell "unable to load required key for ${BOOTFS}"
129-
elif find_be_kernels "${BOOTFS}" ; then
130-
# Automatically select a kernel and boot it
129+
elif find_be_kernels "${BOOTFS}" ; then
130+
# Automatically select a kernel and boot it
131131
kexec_kernel "$( select_kernel "${BOOTFS}" )"
132132
fi
133133
fi
@@ -256,14 +256,14 @@ while true; do
256256
;;
257257
# Check available space early in the process
258258
"enter")
259-
avail_space_exact="$( zfs list -p -H -o available "${selected_snap%/*}" )"
259+
avail_space_exact="$( zfs list -p -H -o available "${parent_ds}" )"
260260
be_size_exact="$( zfs list -p -H -o refer "${selected_snap}" )"
261261
leftover_space=$(( avail_space_exact - be_size_exact ))
262262
if [ "${leftover_space}" -le 0 ]; then
263-
avail_space="$( zfs list -H -o available "${selected_snap%/*}" )"
263+
avail_space="$( zfs list -H -o available "${parent_ds}" )"
264264
be_size="$( zfs list -H -o refer "${selected_snap}" )"
265265
color=red delay=10 timed_prompt "Insufficient space for duplication" \
266-
"'${selected_snap%/*}' has ${avail_space} free but needs ${be_size}"
266+
"'${parent_ds}' has ${avail_space} free but needs ${be_size}"
267267
continue
268268
fi
269269
;;
@@ -322,7 +322,17 @@ while true; do
322322
;;
323323
"alt-w")
324324
pool="${selected_be%%/*}"
325-
if set_rw_pool "${pool}"; then
325+
need_key=''
326+
327+
if is_writable "${pool}"; then
328+
if export_pool "${pool}" && read_write='' import_pool "${pool}"; then
329+
need_key=1
330+
fi
331+
elif set_rw_pool "${pool}"; then
332+
need_key=1
333+
fi
334+
335+
if [ -n "${need_key}" ]; then
326336
CLEAR_SCREEN=1 key_wrapper "${pool}"
327337
fi
328338
;;

0 commit comments

Comments
 (0)