Page MenuHomeFreeBSD

bsdinstall: restore the environment when restarting
Needs ReviewPublic

Authored by khorben_defora.org on Oct 18 2023, 8:22 PM.
Tags
None
Referenced Files
Unknown Object (File)
Thu, Jun 27, 3:58 AM
Unknown Object (File)
Tue, Jun 25, 8:49 AM
Unknown Object (File)
Fri, Jun 21, 12:02 AM
Unknown Object (File)
Fri, Jun 14, 1:40 PM
Unknown Object (File)
Fri, Jun 14, 12:42 PM
Unknown Object (File)
Fri, Jun 14, 10:11 AM
Unknown Object (File)
May 22 2024, 10:55 AM
Unknown Object (File)
May 9 2024, 4:30 AM

Details

Reviewers
emaste
bapt
jrtc27
Summary

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.

This is believed to solve PR #266987.

This is a broader and cleaner fix than D42183, which only solved this issue when choosing mirrors while setting up jails.
The patch is also available on GitHub at https://github.com/khorben/freebsd-src/tree/khorben/bsdinstall-restart-reset-env.

Test Plan

I have created a stand-alone copy of usr.sbin/bsdinstall, slightly modified in order to allow local testing without requiring a new installation image for every change.

$ git clone --branch khorben/bsdinstall-restart-reset-env https://github.com/khorben/bsdinstall.git
$ cd bsdinstall
$ less README.md
[...]
$ cat > test.sh << EOF
#!/bin/sh

BSDINSTALLDIR="\$PWD"
DESTDIR="\$BSDINSTALLDIR/destdir"
BSDINSTALL_DISTDIR="\$DESTDIR/usr/freebsd-dist"; export BSDINSTALL_DISTDIR
SRCDIR="/usr/src"

TMPDIR="\$(mktemp -d)"; export TMPDIR
BSDCFG_SHARE="\$SRCDIR/usr.sbin/bsdconfig/share" \
    BSDINSTALL_CHROOT="\$DESTDIR" \
    BSDINSTALL_CONFIGCURRENT="yes" \
    BSDINSTALL_SCRIPTS="\$BSDINSTALLDIR/scripts" \
    DISTRIBUTIONS="base.txz" \
    ./bsdinstall "\$@"
EOF
$ sh test.sh jail /tmp/jail
[select any item, confirm, deselect any item, confirm, repeat]

(Add, remove, or modify $DISTRIBUTIONS for exhaustive testing)

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped

Event Timeline

khorben_defora.org added a reviewer: bapt.
khorben_defora.org edited the summary of this revision. (Show Details)
usr.sbin/bsdinstall/scripts/auto
155

Should we not preserve these normally too? Probably it's fine not to, but better to have all variables be handled uniformly (perhaps even you could just have a list of variables that need saving/restoring and loop over them rather than copy/pasting the code for each?)

usr.sbin/bsdinstall/scripts/jail
71

This will restore an unset variable to an empty one, which isn't *quite* the same, and means you need to be careful about :- vs - (and for some variables maybe empty-but-set would even be valid)

khorben_defora.org added inline comments.
usr.sbin/bsdinstall/scripts/auto
155

You are right, the user may also have forced a workaround to be applied, even without bsdinstall discovering its need.
It makes sense to list the variables indeed.

usr.sbin/bsdinstall/scripts/jail
71

Are you speaking about code like export DISTRIBUTIONS="${DISTRIBUTIONS:-base.txz kernel.txz}"?

From what I can tell, the shell should behave the same way whether a variable is empty or unset:

$ D=1; echo ${D:-2}
1
$ D=; echo ${D:-2}
2
$ unset D; echo ${D:-2}
2

This is also confirmed in the manual page:

${parameter:-word}
        Use Default Values.  If parameter is unset or null, the expansion
        of word is substituted; otherwise, the value of parameter is
        substituted.

Let me know if there's something I am missing :)

usr.sbin/bsdinstall/scripts/jail
71
In the parameter expansions shown previously, use of the colon in the
format results in a test for a parameter that is unset or null; omis-
sion of the colon results in a test for a parameter that is only unset.

That is, with FOO=, ${FOO-bar} gives "", unlike ${FOO:-bar}, which gives "bar". This doesn't matter for DISTRIBUTIONS, but because this difference does matter, a true environment_restore would *unset* variables that were not set, rather than set them to an empty value.

This updated diff:

  • Uses a pre-defined list of variables to save or restore the environment
  • Lets the prefix be configured for the saved variables
  • Differentiates between empty and unset variables when saving and restoring them
  • Adds a few comments for clarity and consistency
khorben_defora.org retitled this revision from bsdinstall: reset the environment when restarting to bsdinstall: restore the environment when restarting.Oct 27 2023, 12:15 PM
khorben_defora.org edited the summary of this revision. (Show Details)