Prefix all variables that are local with underscore.

This just to make them easier to see.

Reviewed by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Rob Norris <robn@despairlabs.com>
Signed-off-by: Turbo Fredriksson <turbo@bayour.com>
Closes #18000
This commit is contained in:
Turbo Fredriksson 2025-12-03 10:39:50 +00:00 committed by Brian Behlendorf
parent d3b447de4e
commit 33dd57e1b4

View File

@ -60,10 +60,10 @@ disable_plymouth()
# Get a ZFS filesystem property value. # Get a ZFS filesystem property value.
get_fs_value() get_fs_value()
{ {
get_fs="${1}" _get_fs="${1}"
get_value="${2}" _get_value="${2}"
"${ZFS}" get -H -ovalue "${get_value}" "${get_fs}" 2> /dev/null "${ZFS}" get -H -ovalue "${_get_value}" "${_get_fs}" 2> /dev/null
} }
# Find the 'bootfs' property on pool $1. # Find the 'bootfs' property on pool $1.
@ -71,7 +71,7 @@ get_fs_value()
# pool by exporting it again. # pool by exporting it again.
find_rootfs() find_rootfs()
{ {
find_rootfs_pool="${1}" _find_rootfs_pool="${1}"
# If 'POOL_IMPORTED' isn't set, no pool imported and therefore # If 'POOL_IMPORTED' isn't set, no pool imported and therefore
# we won't be able to find a root fs. # we won't be able to find a root fs.
@ -85,7 +85,7 @@ find_rootfs()
# Not set, try to find it in the 'bootfs' property of the pool. # Not set, try to find it in the 'bootfs' property of the pool.
# NOTE: zpool does not support 'get -H -ovalue bootfs'... # NOTE: zpool does not support 'get -H -ovalue bootfs'...
ZFS_BOOTFS=$("${ZPOOL}" list -H -obootfs "${find_rootfs_pool}") ZFS_BOOTFS=$("${ZPOOL}" list -H -obootfs "${_find_rootfs_pool}")
# Make sure it's not '-' and that it starts with /. # Make sure it's not '-' and that it starts with /.
if [ "${ZFS_BOOTFS}" != "-" ] && \ if [ "${ZFS_BOOTFS}" != "-" ] && \
@ -97,7 +97,7 @@ find_rootfs()
fi fi
# Not boot fs here, export it and later try again.. # Not boot fs here, export it and later try again..
"${ZPOOL}" export "${find_rootfs_pool}" "${ZPOOL}" export "${_find_rootfs_pool}"
POOL_IMPORTED= POOL_IMPORTED=
ZFS_BOOTFS= ZFS_BOOTFS=
return 1 return 1
@ -106,11 +106,11 @@ find_rootfs()
# Support function to get a list of all pools, separated with ';' # Support function to get a list of all pools, separated with ';'
find_pools() find_pools()
{ {
find_pools=$("${@}" 2> /dev/null | \ _find_pools=$("${@}" 2> /dev/null | \
sed -Ee '/pool:|^[a-zA-Z0-9]/!d' -e 's@.*: @@' | \ sed -Ee '/pool:|^[a-zA-Z0-9]/!d' -e 's@.*: @@' | \
tr '\n' ';') tr '\n' ';')
echo "${find_pools%%;}" # Return without the last ';'. echo "${_find_pools%%;}" # Return without the last ';'.
} }
# Get a list of all available pools # Get a list of all available pools
@ -122,33 +122,33 @@ get_pools()
fi fi
# Get the base list of available pools. # Get the base list of available pools.
available_pools="$(find_pools "${ZPOOL}" import)" _available_pools="$(find_pools "${ZPOOL}" import)"
# Just in case - seen it happen (that a pool isn't visible/found # Just in case - seen it happen (that a pool isn't visible/found
# with a simple "zpool import" but only when using the "-d" # with a simple "zpool import" but only when using the "-d"
# option or setting ZPOOL_IMPORT_PATH). # option or setting ZPOOL_IMPORT_PATH).
if [ -d "/dev/disk/by-id" ] if [ -d "/dev/disk/by-id" ]
then then
npools="$(find_pools "${ZPOOL}" import -d /dev/disk/by-id)" _npools="$(find_pools "${ZPOOL}" import -d /dev/disk/by-id)"
if [ -n "${npools}" ] if [ -n "${_npools}" ]
then then
# Because we have found extra pool(s) here, which wasn't # Because we have found extra pool(s) here, which wasn't
# found 'normally', we need to force USE_DISK_BY_ID to # found 'normally', we need to force USE_DISK_BY_ID to
# make sure we're able to actually import it/them later. # make sure we're able to actually import it/them later.
USE_DISK_BY_ID='yes' USE_DISK_BY_ID='yes'
if [ -n "${available_pools}" ] if [ -n "${_available_pools}" ]
then then
# Filter out duplicates (pools found with the simple # Filter out duplicates (pools found with the simple
# "zpool import" but which is also found with the # "zpool import" but which is also found with the
# "zpool import -d ..."). # "zpool import -d ...").
npools="$(echo "${npools}" | sed "s,${available_pools},,")" _npools="$(echo "${_npools}" | sed "s,${_available_pools},,")"
# Add the list to the existing list of # Add the list to the existing list of
# available pools # available pools
available_pools="${available_pools};${npools}" _available_pools="${_available_pools};${_npools}"
else else
available_pools="${npools}" _available_pools="${_npools}"
fi fi
fi fi
fi fi
@ -156,45 +156,45 @@ get_pools()
# Filter out any exceptions... # Filter out any exceptions...
if [ -n "${ZFS_POOL_EXCEPTIONS}" ] if [ -n "${ZFS_POOL_EXCEPTIONS}" ]
then then
found="" _found=""
apools="" _apools=""
OLD_IFS="${IFS}" ; IFS=";" OLD_IFS="${IFS}" ; IFS=";"
for pool in ${available_pools} for pool in ${_available_pools}
do do
for exception in ${ZFS_POOL_EXCEPTIONS} for exception in ${ZFS_POOL_EXCEPTIONS}
do do
[ "${pool}" = "${exception}" ] && continue 2 [ "${pool}" = "${exception}" ] && continue 2
found="${pool}" _found="${pool}"
done done
if [ -n "${found}" ] if [ -n "${_found}" ]
then then
if [ -n "${apools}" ] if [ -n "${_apools}" ]
then then
apools="${apools};${pool}" _apools="${_apools};${pool}"
else else
apools="${pool}" _apools="${pool}"
fi fi
fi fi
done done
IFS="${OLD_IFS}" IFS="${OLD_IFS}"
available_pools="${apools}" _available_pools="${_apools}"
fi fi
# Return list of available pools. # Return list of available pools.
echo "${available_pools}" echo "${_available_pools}"
} }
# Import given pool $1 # Import given pool $1
import_pool() import_pool()
{ {
import_pool="${1}" _import_pool="${1}"
# Verify that the pool isn't already imported # Verify that the pool isn't already imported
# Make as sure as we can to not require '-f' to import. # Make as sure as we can to not require '-f' to import.
"${ZPOOL}" get -H -o value name,guid 2>/dev/null | grep -Fxq "${import_pool}" && return 0 "${ZPOOL}" get -H -o value name,guid 2>/dev/null | grep -Fxq "${_import_pool}" && return 0
# For backwards compatibility, make sure that ZPOOL_IMPORT_PATH is set # For backwards compatibility, make sure that ZPOOL_IMPORT_PATH is set
# to something we can use later with the real import(s). We want to # to something we can use later with the real import(s). We want to
@ -226,10 +226,10 @@ import_pool()
[ "${quiet}" != "y" ] && zfs_log_begin_msg \ [ "${quiet}" != "y" ] && zfs_log_begin_msg \
"Importing pool '${import_pool}' using defaults" "Importing pool '${_import_pool}' using defaults"
ZFS_CMD="${ZPOOL} import -N ${ZPOOL_FORCE} ${ZPOOL_IMPORT_OPTS}" ZFS_CMD="${ZPOOL} import -N ${ZPOOL_FORCE} ${ZPOOL_IMPORT_OPTS}"
ZFS_STDERR="$(${ZFS_CMD} "${import_pool}" 2>&1)" ZFS_STDERR="$(${ZFS_CMD} "${_import_pool}" 2>&1)"
ZFS_ERROR="${?}" ZFS_ERROR="${?}"
if [ "${ZFS_ERROR}" != 0 ] if [ "${ZFS_ERROR}" != 0 ]
then then
@ -238,10 +238,10 @@ import_pool()
if [ -f "${ZPOOL_CACHE}" ] if [ -f "${ZPOOL_CACHE}" ]
then then
[ "${quiet}" != "y" ] && zfs_log_begin_msg \ [ "${quiet}" != "y" ] && zfs_log_begin_msg \
"Importing pool '${import_pool}' using cachefile." "Importing pool '${_import_pool}' using cachefile."
ZFS_CMD="${ZPOOL} import -c ${ZPOOL_CACHE} -N ${ZPOOL_FORCE} ${ZPOOL_IMPORT_OPTS}" ZFS_CMD="${ZPOOL} import -c ${ZPOOL_CACHE} -N ${ZPOOL_FORCE} ${ZPOOL_IMPORT_OPTS}"
ZFS_STDERR="$(${ZFS_CMD} "${import_pool}" 2>&1)" ZFS_STDERR="$(${ZFS_CMD} "${_import_pool}" 2>&1)"
ZFS_ERROR="${?}" ZFS_ERROR="${?}"
fi fi
@ -251,11 +251,11 @@ import_pool()
disable_plymouth disable_plymouth
echo "" echo ""
echo "Command: ${ZFS_CMD} '${import_pool}'" echo "Command: ${ZFS_CMD} '${_import_pool}'"
echo "Message: ${ZFS_STDERR}" echo "Message: ${ZFS_STDERR}"
echo "Error: ${ZFS_ERROR}" echo "Error: ${ZFS_ERROR}"
echo "" echo ""
echo "Failed to import pool '${import_pool}'." echo "Failed to import pool '${_import_pool}'."
echo "Manually import the pool and exit." echo "Manually import the pool and exit."
shell shell
fi fi
@ -272,6 +272,8 @@ import_pool()
# with more logging etc. # with more logging etc.
load_module_initrd() load_module_initrd()
{ {
_retval=0
ZFS_INITRD_PRE_MOUNTROOT_SLEEP=${ROOTDELAY:-0} ZFS_INITRD_PRE_MOUNTROOT_SLEEP=${ROOTDELAY:-0}
if [ "${ZFS_INITRD_PRE_MOUNTROOT_SLEEP}" -gt 0 ]; then if [ "${ZFS_INITRD_PRE_MOUNTROOT_SLEEP}" -gt 0 ]; then
@ -296,10 +298,10 @@ load_module_initrd()
# Load the module # Load the module
if load_module "zfs"; then if load_module "zfs"; then
ret=0 _retval=0
break break
else else
ret=1 _retval=1
fi fi
[ "$(/bin/date -u +%s)" -gt "${END}" ] && break [ "$(/bin/date -u +%s)" -gt "${END}" ] && break
@ -310,7 +312,7 @@ load_module_initrd()
[ "${quiet}" != "y" ] && zfs_log_end_msg [ "${quiet}" != "y" ] && zfs_log_end_msg
fi fi
[ "${ret}" -ne 0 ] && return 1 [ "${_retval}" -ne 0 ] && return "${_retval}"
if [ "${ZFS_INITRD_POST_MODPROBE_SLEEP}" -gt 0 ] 2>/dev/null if [ "${ZFS_INITRD_POST_MODPROBE_SLEEP}" -gt 0 ] 2>/dev/null
then then
@ -322,61 +324,61 @@ load_module_initrd()
[ "${quiet}" != "y" ] && zfs_log_end_msg [ "${quiet}" != "y" ] && zfs_log_end_msg
fi fi
return 0 return "${_retval}"
} }
# Mount a given filesystem # Mount a given filesystem
mount_fs() mount_fs()
{ {
mount_fs="${1}" _mount_fs="${1}"
# Check that the filesystem exists # Check that the filesystem exists
"${ZFS}" list -oname -tfilesystem -H "${mount_fs}" > /dev/null 2>&1 || return 1 "${ZFS}" list -oname -tfilesystem -H "${_mount_fs}" > /dev/null 2>&1 || return 1
# Skip filesystems with canmount=off. The root fs should not have # Skip filesystems with canmount=off. The root fs should not have
# canmount=off, but ignore it for backwards compatibility just in case. # canmount=off, but ignore it for backwards compatibility just in case.
if [ "${mount_fs}" != "${ZFS_BOOTFS}" ] if [ "${_mount_fs}" != "${ZFS_BOOTFS}" ]
then then
canmount="$(get_fs_value "${mount_fs}" canmount)" _canmount="$(get_fs_value "${_mount_fs}" canmount)"
[ "${canmount}" = "off" ] && return 0 [ "${_canmount}" = "off" ] && return 0
fi fi
# Need the _original_ datasets mountpoint! # Need the _original_ datasets mountpoint!
mountpoint="$(get_fs_value "${mount_fs}" mountpoint)" _mountpoint="$(get_fs_value "${_mount_fs}" mountpoint)"
ZFS_CMD="mount.zfs -o zfsutil" ZFS_CMD="mount.zfs -o zfsutil"
if [ "${mountpoint}" = "legacy" ] || [ "${mountpoint}" = "none" ]; then if [ "${_mountpoint}" = "legacy" ] || [ "${_mountpoint}" = "none" ]; then
# Can't use the mountpoint property. Might be one of our # Can't use the mountpoint property. Might be one of our
# clones. Check the 'org.zol:mountpoint' property set in # clones. Check the 'org.zol:mountpoint' property set in
# clone_snap() if that's usable. # clone_snap() if that's usable.
mountpoint1="$(get_fs_value "${mount_fs}" org.zol:mountpoint)" _mountpoint1="$(get_fs_value "${_mount_fs}" org.zol:mountpoint)"
if [ "${mountpoint1}" = "legacy" ] || if [ "${_mountpoint1}" = "legacy" ] ||
[ "${mountpoint1}" = "none" ] || [ "${_mountpoint1}" = "none" ] ||
[ "${mountpoint1}" = "-" ] [ "${_mountpoint1}" = "-" ]
then then
if [ "${mount_fs}" != "${ZFS_BOOTFS}" ]; then if [ "${_mount_fs}" != "${ZFS_BOOTFS}" ]; then
# We don't have a proper mountpoint and this # We don't have a proper mountpoint and this
# isn't the root fs. # isn't the root fs.
return 0 return 0
fi fi
if [ "${mountpoint}" = "legacy" ]; then if [ "${_mountpoint}" = "legacy" ]; then
ZFS_CMD="mount.zfs" ZFS_CMD="mount.zfs"
fi fi
# Last hail-mary: Hope 'rootmnt' is set! # Last hail-mary: Hope 'rootmnt' is set!
mountpoint="" _mountpoint=""
else else
mountpoint="${mountpoint1}" _mountpoint="${_mountpoint1}"
fi fi
fi fi
# Possibly decrypt a filesystem using native encryption. # Possibly decrypt a filesystem using native encryption.
decrypt_fs "${mount_fs}" decrypt_fs "${_mount_fs}"
[ "${quiet}" != "y" ] && \ [ "${quiet}" != "y" ] && \
zfs_log_begin_msg "Mounting '${mount_fs}' on '${rootmnt}/${mountpoint}'" zfs_log_begin_msg "Mounting '${_mount_fs}' on '${rootmnt}/${_mountpoint}'"
[ -n "${ZFS_DEBUG}" ] && \ [ -n "${ZFS_DEBUG}" ] && \
zfs_log_begin_msg "CMD: '${ZFS_CMD} ${mount_fs} ${rootmnt}/${mountpoint}'" zfs_log_begin_msg "CMD: '${ZFS_CMD} ${_mount_fs} ${rootmnt}/${_mountpoint}'"
ZFS_STDERR="$(${ZFS_CMD} "${mount_fs}" "${rootmnt}/${mountpoint}" 2>&1)" ZFS_STDERR="$(${ZFS_CMD} "${_mount_fs}" "${rootmnt}/${_mountpoint}" 2>&1)"
ZFS_ERROR="${?}" ZFS_ERROR="${?}"
if [ "${ZFS_ERROR}" != 0 ] if [ "${ZFS_ERROR}" != 0 ]
then then
@ -384,11 +386,11 @@ mount_fs()
disable_plymouth disable_plymouth
echo "" echo ""
echo "Command: ${ZFS_CMD} ${mount_fs} ${rootmnt}/${mountpoint}" echo "Command: ${ZFS_CMD} ${_mount_fs} ${rootmnt}/${_mountpoint}"
echo "Message: ${ZFS_STDERR}" echo "Message: ${ZFS_STDERR}"
echo "Error: ${ZFS_ERROR}" echo "Error: ${ZFS_ERROR}"
echo "" echo ""
echo "Failed to mount ${mount_fs} on ${rootmnt}/${mountpoint}." echo "Failed to mount ${_mount_fs} on ${rootmnt}/${_mountpoint}."
echo "Manually mount the filesystem and exit." echo "Manually mount the filesystem and exit."
shell shell
else else
@ -401,13 +403,13 @@ mount_fs()
# Unlock a ZFS native encrypted filesystem. # Unlock a ZFS native encrypted filesystem.
decrypt_fs() decrypt_fs()
{ {
decrypt_fs="${1}" _decrypt_fs="${1}"
# If pool encryption is active and the zfs command understands '-o encryption' # If pool encryption is active and the zfs command understands '-o encryption'
if [ "$("${ZPOOL}" list -H -o feature@encryption "${decrypt_fs%%/*}")" = 'active' ]; then if [ "$("${ZPOOL}" list -H -o feature@encryption "${_decrypt_fs%%/*}")" = 'active' ]; then
# Determine dataset that holds key for root dataset # Determine dataset that holds key for root dataset
ENCRYPTIONROOT="$(get_fs_value "${decrypt_fs}" encryptionroot)" ENCRYPTIONROOT="$(get_fs_value "${_decrypt_fs}" encryptionroot)"
KEYLOCATION="$(get_fs_value "${ENCRYPTIONROOT}" keylocation)" KEYLOCATION="$(get_fs_value "${ENCRYPTIONROOT}" keylocation)"
echo "${ENCRYPTIONROOT}" > /run/zfs_fs_name echo "${ENCRYPTIONROOT}" > /run/zfs_fs_name
@ -466,7 +468,7 @@ decrypt_fs()
# Destroy a given filesystem. # Destroy a given filesystem.
destroy_fs() destroy_fs()
{ {
destroy_fs="${1}" _destroy_fs="${1}"
[ "${quiet}" != "y" ] && \ [ "${quiet}" != "y" ] && \
zfs_log_begin_msg "Destroying '${destroy_fs}'" zfs_log_begin_msg "Destroying '${destroy_fs}'"
@ -484,8 +486,8 @@ destroy_fs()
echo "Message: ${ZFS_STDERR}" echo "Message: ${ZFS_STDERR}"
echo "Error: ${ZFS_ERROR}" echo "Error: ${ZFS_ERROR}"
echo "" echo ""
echo "Failed to destroy '${destroy_fs}'. Please make sure that it is not available." echo "Failed to destroy '${_destroy_fs}'. Please make sure that it is not available."
echo "Hint: Try: zfs destroy -Rfn ${destroy_fs}" echo "Hint: Try: zfs destroy -Rfn ${_destroy_fs}"
echo "If this dryrun looks good, then remove the 'n' from '-Rfn' and try again." echo "If this dryrun looks good, then remove the 'n' from '-Rfn' and try again."
shell shell
else else
@ -501,11 +503,11 @@ destroy_fs()
# mounted with a 'zfs mount -a' in the init/systemd scripts). # mounted with a 'zfs mount -a' in the init/systemd scripts).
clone_snap() clone_snap()
{ {
clone_snap="${1}" _clone_snap="${1}"
clone_destfs="${2}" _clone_destfs="${2}"
clone_mountpoint="${3}" _clone_mountpoint="${3}"
[ "${quiet}" != "y" ] && zfs_log_begin_msg "Cloning '${clone_snap}' to '${clone_destfs}'" [ "${quiet}" != "y" ] && zfs_log_begin_msg "Cloning '${_clone_snap}' to '${_clone_destfs}'"
# Clone the snapshot into a dataset we can boot from # Clone the snapshot into a dataset we can boot from
# + We don't want this filesystem to be automatically mounted, we # + We don't want this filesystem to be automatically mounted, we
@ -513,8 +515,8 @@ clone_snap()
# + We don't need any mountpoint set for the same reason. # + We don't need any mountpoint set for the same reason.
# We use the 'org.zol:mountpoint' property to remember the mountpoint. # We use the 'org.zol:mountpoint' property to remember the mountpoint.
ZFS_CMD="${ZFS} clone -o canmount=noauto -o mountpoint=none" ZFS_CMD="${ZFS} clone -o canmount=noauto -o mountpoint=none"
ZFS_CMD="${ZFS_CMD} -o org.zol:mountpoint=${clone_mountpoint}" ZFS_CMD="${ZFS_CMD} -o org.zol:mountpoint=${_clone_mountpoint}"
ZFS_CMD="${ZFS_CMD} ${clone_snap} ${clone_destfs}" ZFS_CMD="${ZFS_CMD} ${_clone_snap} ${_clone_destfs}"
ZFS_STDERR="$(${ZFS_CMD} 2>&1)" ZFS_STDERR="$(${ZFS_CMD} 2>&1)"
ZFS_ERROR="${?}" ZFS_ERROR="${?}"
if [ "${ZFS_ERROR}" != 0 ] if [ "${ZFS_ERROR}" != 0 ]
@ -529,7 +531,7 @@ clone_snap()
echo "" echo ""
echo "Failed to clone snapshot." echo "Failed to clone snapshot."
echo "Make sure that any problems are corrected and then make sure" echo "Make sure that any problems are corrected and then make sure"
echo "that the dataset '${clone_destfs}' exists and is bootable." echo "that the dataset '${_clone_destfs}' exists and is bootable."
shell shell
else else
[ "${quiet}" != "y" ] && zfs_log_end_msg [ "${quiet}" != "y" ] && zfs_log_end_msg
@ -541,11 +543,11 @@ clone_snap()
# Rollback a given snapshot. # Rollback a given snapshot.
rollback_snap() rollback_snap()
{ {
rollback_snap="${1}" _rollback_snap="${1}"
[ "${quiet}" != "y" ] && zfs_log_begin_msg "Rollback ${rollback_snap}" [ "${quiet}" != "y" ] && zfs_log_begin_msg "Rollback ${_rollback_snap}"
ZFS_CMD="${ZFS} rollback -Rf ${rollback_snap}" ZFS_CMD="${ZFS} rollback -Rf ${_rollback_snap}"
ZFS_STDERR="$(${ZFS_CMD} 2>&1)" ZFS_STDERR="$(${ZFS_CMD} 2>&1)"
ZFS_ERROR="${?}" ZFS_ERROR="${?}"
if [ "${ZFS_ERROR}" != 0 ] if [ "${ZFS_ERROR}" != 0 ]
@ -571,14 +573,14 @@ rollback_snap()
# to the user to choose from. # to the user to choose from.
ask_user_snap() ask_user_snap()
{ {
ask_snap="${1}" _ask_snap="${1}"
# We need to temporarily disable debugging. Set 'debug' so we # We need to temporarily disable debugging. Set 'debug' so we
# remember to enabled it again. # remember to enabled it again.
if [ -n "${ZFS_DEBUG}" ]; then if [ -n "${ZFS_DEBUG}" ]; then
unset ZFS_DEBUG unset ZFS_DEBUG
set +x set +x
debug=1 _debug=1
fi fi
# Because we need the resulting snapshot, which is sent on # Because we need the resulting snapshot, which is sent on
@ -586,7 +588,7 @@ ask_user_snap()
echo "What snapshot do you want to boot from?" > /dev/stderr echo "What snapshot do you want to boot from?" > /dev/stderr
# shellcheck disable=SC2046 # shellcheck disable=SC2046
IFS=" IFS="
" set -- $("${ZFS}" list -H -oname -tsnapshot -r "${ask_snap}") " set -- $("${ZFS}" list -H -oname -tsnapshot -r "${_ask_snap}")
i=1 i=1
for snap in "${@}"; do for snap in "${@}"; do
@ -596,66 +598,66 @@ ask_user_snap()
# expr instead of test here because [ a -lt 0 ] errors out, # expr instead of test here because [ a -lt 0 ] errors out,
# but expr falls back to lexicographical, which works out right # but expr falls back to lexicographical, which works out right
snapnr=0 _snapnr=0
while expr "${snapnr}" "<" 1 > /dev/null || while expr "${_snapnr}" "<" 1 > /dev/null ||
expr "${snapnr}" ">" "${#}" > /dev/null expr "${_snapnr}" ">" "${#}" > /dev/null
do do
printf "%s" "Snap nr [1-${#}]? " > /dev/stderr printf "%s" "Snap nr [1-${#}]? " > /dev/stderr
read -r snapnr read -r _snapnr
done done
# Re-enable debugging. # Re-enable debugging.
if [ -n "${debug}" ]; then if [ -n "${_debug}" ]; then
ZFS_DEBUG=1 ZFS_DEBUG=1
set -x set -x
fi fi
eval echo '$'"${snapnr}" eval echo '$'"${_snapnr}"
} }
setup_snapshot_booting() setup_snapshot_booting()
{ {
boot_snap="${1}" _boot_snap="${1}"
retval=0 _retval=0
# Make sure that the snapshot specified actually exists. # Make sure that the snapshot specified actually exists.
if [ -z "$(get_fs_value "${boot_snap}" type)" ] if [ -z "$(get_fs_value "${_boot_snap}" type)" ]
then then
# Snapshot does not exist (...@<null> ?) # Snapshot does not exist (...@<null> ?)
# ask the user for a snapshot to use. # ask the user for a snapshot to use.
snap="$(ask_user_snap "${boot_snap%%@*}")" snap="$(ask_user_snap "${_boot_snap%%@*}")"
fi fi
# Separate the full snapshot ('${snap}') into it's filesystem and # Separate the full snapshot ('${snap}') into it's filesystem and
# snapshot names. Would have been nice with a split() function.. # snapshot names. Would have been nice with a split() function..
rootfs="${boot_snap%%@*}" _rootfs="${_boot_snap%%@*}"
snapname="${boot_snap##*@}" _snapname="${_boot_snap##*@}"
ZFS_BOOTFS="${rootfs}_${snapname}" ZFS_BOOTFS="${_rootfs}_${_snapname}"
if ! grep -qiE '(^|[^\\](\\\\)* )(rollback)=(on|yes|1)( |$)' /proc/cmdline if ! grep -qiE '(^|[^\\](\\\\)* )(rollback)=(on|yes|1)( |$)' /proc/cmdline
then then
# If the destination dataset for the clone # If the destination dataset for the clone
# already exists, destroy it. Recursively # already exists, destroy it. Recursively
if [ -n "$(get_fs_value "${rootfs}_${snapname}" type)" ] if [ -n "$(get_fs_value "${_rootfs}_${_snapname}" type)" ]
then then
filesystems="$("${ZFS}" list -oname -tfilesystem -H \ _filesystems="$("${ZFS}" list -oname -tfilesystem -H \
-r -Sname "${ZFS_BOOTFS}")" -r -Sname "${ZFS_BOOTFS}")"
for fs in ${filesystems}; do for fs in ${_filesystems}; do
destroy_fs "${boot_snap}" destroy_fs "${_boot_snap}"
done done
fi fi
fi fi
# Get all snapshots, recursively (might need to clone /usr, /var etc # Get all snapshots, recursively (might need to clone /usr, /var etc
# as well). # as well).
for s in $("${ZFS}" list -H -oname -tsnapshot -r "${rootfs}" | \ for s in $("${ZFS}" list -H -oname -tsnapshot -r "${_rootfs}" | \
grep "${snapname}") grep "${_snapname}")
do do
if grep -qiE '(^|[^\\](\\\\)* )(rollback)=(on|yes|1)( |$)' /proc/cmdline if grep -qiE '(^|[^\\](\\\\)* )(rollback)=(on|yes|1)( |$)' /proc/cmdline
then then
# Rollback snapshot # Rollback snapshot
rollback_snap "${s}" || retval="$((retval + 1))" rollback_snap "${s}" || _retval="$((_retval + 1))"
ZFS_BOOTFS="${rootfs}" ZFS_BOOTFS="${_rootfs}"
else else
# Setup a destination filesystem name. # Setup a destination filesystem name.
# Ex: Called with 'rpool/ROOT/debian@snap2' # Ex: Called with 'rpool/ROOT/debian@snap2'
@ -663,35 +665,35 @@ setup_snapshot_booting()
# rpool/ROOT/debian/boot@snap2 => rpool/ROOT/debian_snap2/boot # rpool/ROOT/debian/boot@snap2 => rpool/ROOT/debian_snap2/boot
# rpool/ROOT/debian/usr@snap2 => rpool/ROOT/debian_snap2/usr # rpool/ROOT/debian/usr@snap2 => rpool/ROOT/debian_snap2/usr
# rpool/ROOT/debian/var@snap2 => rpool/ROOT/debian_snap2/var # rpool/ROOT/debian/var@snap2 => rpool/ROOT/debian_snap2/var
subfs="${s##"${rootfs}"}" _subfs="${s##"${_rootfs}"}"
subfs="${subfs%%@"${snapname}"}" _subfs="${_subfs%%@"${_snapname}"}"
destfs="${rootfs}_${snapname}" # base fs. _destfs="${_rootfs}_${_snapname}" # base fs.
[ -n "${subfs}" ] && destfs="${destfs}${subfs}" # + sub fs. [ -n "${_subfs}" ] && _destfs="${_destfs}${_subfs}" # + sub fs.
# Get the mountpoint of the filesystem, to be used # Get the mountpoint of the filesystem, to be used
# with clone_snap(). If legacy or none, then use # with clone_snap(). If legacy or none, then use
# the sub fs value. # the sub fs value.
mountpoint="$(get_fs_value "${s%%@*}" mountpoint)" _mountpoint="$(get_fs_value "${s%%@*}" mountpoint)"
if [ "${mountpoint}" = "legacy" ] || \ if [ "${_mountpoint}" = "legacy" ] || \
[ "${mountpoint}" = "none" ] [ "${_mountpoint}" = "none" ]
then then
if [ -n "${subfs}" ]; then if [ -n "${_subfs}" ]; then
mountpoint="${subfs}" _mountpoint="${_subfs}"
else else
mountpoint="/" _mountpoint="/"
fi fi
fi fi
# Clone the snapshot into its own # Clone the snapshot into its own
# filesystem # filesystem
clone_snap "${s}" "${destfs}" "${mountpoint}" || \ clone_snap "${s}" "${_destfs}" "${_mountpoint}" || \
retval="$((retval + 1))" _retval="$((_retval + 1))"
fi fi
done done
# If we haven't return yet, we have a problem... # If we haven't return yet, we have a problem...
return "${retval}" return "${_retval}"
} }
# ================================================================ # ================================================================