The branch stable/14 has been updated by khorben: URL: https://cgit.FreeBSD.org/src/commit/?id=d0d92b35d5404243dc25dee8cf12b75288e0eacb
commit d0d92b35d5404243dc25dee8cf12b75288e0eacb Author: Pierre Pronchery <khor...@freebsd.org> AuthorDate: 2025-05-23 15:50:42 +0000 Commit: Pierre Pronchery <khor...@freebsd.org> CommitDate: 2025-07-24 23:37:49 +0000 bsdinstall: restore the environment when restarting It is possible to restart the installation process upon errors, when installing normally through the `auto` script, or when installing a jail with the `jail` script. However, some values obtained interactively from the user or guessed by some scripts were kept in the environment when restarting the process; this made it impossible to re-run some steps as expected after the restart. For instance, if a bad choice of mirror was made in the `mirrorselect` phase, restarting the installer remembered the choice made, and would never prompt for a different one again. Rebooting was then the only easy way out of this situation. This change restores a pre-defined list of environment variables when restarting the installation process. PR: 266987 Reviewed by: emaste Approved by: philip (mentor) Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D42281 (cherry picked from commit 9de72af2cceb6fc4aead0990cccdf565531bc248) --- usr.sbin/bsdinstall/scripts/auto | 33 ++++++++++++++++++++++++++ usr.sbin/bsdinstall/scripts/jail | 51 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/usr.sbin/bsdinstall/scripts/auto b/usr.sbin/bsdinstall/scripts/auto index abd445282316..ab0191feea17 100755 --- a/usr.sbin/bsdinstall/scripts/auto +++ b/usr.sbin/bsdinstall/scripts/auto @@ -34,6 +34,13 @@ f_include $BSDCFG_SHARE/dialog.subr ############################################################ GLOBALS +# +# List of environment variables that may be defined by the user, but modified +# during the installation process. They are then restored when restarting this +# script. +# +user_env_vars="BSDINSTALL_DISTSITE DISTRIBUTIONS WORKAROUND_GPTACTIVE WORKAROUND_LENOVO ZFSBOOT_PARTITION_SCHEME" + # # Strings that should be moved to an i18n file and loaded with f_include_lang() # @@ -90,6 +97,7 @@ error() --yes-label "$msg_restart" \ --yesno "$prompt" $height $width then + environment_restore exec $0 # NOTREACHED fi @@ -138,10 +146,35 @@ dialog_workaround() --yesno "$prompt" $height $width } +# environment_restore +# +# Restore a list of environment variables when this script is restarted. +# +environment_restore() +{ + for var in $user_env_vars; do + eval "if [ -n \"\${ORIG_$var}\" -o -z \"\${ORIG_$var-z}\" ]; then $var=\${ORIG_$var}; else unset $var; fi" + done +} + +# environment_save +# +# Save any user-defined environment variable that may be modified during the +# installation process. They are then restored when restarting this script. +# +environment_save() +{ + for var in $user_env_vars; do + eval "if [ -n \"\${$var}\" -o -z \"\${$var-z}\" ]; then ORIG_$var=\${$var}; else unset ORIG_$var; fi" + done +} + ############################################################ MAIN f_dprintf "Began Installation at %s" "$( date )" +environment_save + rm -rf $BSDINSTALL_TMPETC mkdir $BSDINSTALL_TMPETC diff --git a/usr.sbin/bsdinstall/scripts/jail b/usr.sbin/bsdinstall/scripts/jail index e5822087e97b..191cc5e89563 100755 --- a/usr.sbin/bsdinstall/scripts/jail +++ b/usr.sbin/bsdinstall/scripts/jail @@ -31,13 +31,23 @@ BSDCFG_SHARE="/usr/share/bsdconfig" . $BSDCFG_SHARE/common.subr || exit 1 -############################################################ MAIN +############################################################ GLOBALS -: ${BSDDIALOG_OK=0} +# +# List of environment variables that may be defined by the user, but modified +# during the installation process. They are then restored when restarting this +# script. +# +user_env_vars="BSDINSTALL_DISTSITE DISTRIBUTIONS" -f_dprintf "Began Installation at %s" "$( date )" -export BSDINSTALL_CHROOT=$1 +############################################################ FUNCTIONS +# error [$msg] +# +# Display generic error message when a script fails. An optional message +# argument can preceed the generic message. User is given the choice of +# restarting the installer or exiting. +# error() { local msg if [ -n "$1" ]; then @@ -49,10 +59,43 @@ error() { if [ $? -ne $BSDDIALOG_OK ]; then exit else + environment_restore exec $0 $BSDINSTALL_CHROOT fi } +# environment_restore +# +# Restore a list of environment variables when this script is restarted. +# +environment_restore() +{ + for var in $user_env_vars; do + eval "if [ -n \"\${ORIG_$var}\" -o -z \"\${ORIG_$var-z}\" ]; then $var=\${ORIG_$var}; else unset $var; fi" + done +} + +# environment_save +# +# Save any user-defined environment variable that may be modified during the +# installation process. They are then restored when restarting this script. +# +environment_save() +{ + for var in $user_env_vars; do + eval "if [ -n \"\${$var}\" -o -z \"\${$var-z}\" ]; then ORIG_$var=\${$var}; else unset ORIG_$var; fi" + done +} + +############################################################ MAIN + +: ${BSDDIALOG_OK=0} + +f_dprintf "Began Installation at %s" "$( date )" +export BSDINSTALL_CHROOT=$1 + +environment_save + rm -rf $BSDINSTALL_TMPETC mkdir $BSDINSTALL_TMPETC mkdir -p $1 || error "mkdir failed for $1"