release.sh: add chroot cleanup routine

The chroot_cleanup routine handles any cleanup needed post-chroot_setup,
etc. This consists of purely tearing down `${CHROOTDIR}/dev` today, but
might involve additional steps, as needed for custom functions. This
allows end-users to override the various chroot functions without having
to modify code in main() or replicate the unmount procedure in an
equivalent routine setup via the trap builtin.

This change modifies the /dev unmount process to use `umount -f` instead
of `umount`. The latter can result in failures if resources are still
mounted or are running post-build, whereas the former will clean up any
resources still in use by processes running in the chroot at time of
build. Moreover, the `chroot_cleanup` routine is now called when the
script is killed with `SIGINT` and `SIGTERM`, as well as at `EXIT`,
better ensuring that the script's resources are cleaned up in relatively common
scenarios that can be detected/handled.

MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D55450 (as part of a larger change)
This commit is contained in:
Enji Cooper committed 2026-03-21 20:02:09 -07:00
1 parent b24fc79095
commit 60baee1fa4
1 file changed
+21 -1
+21 -1
View File
@@ -53,6 +53,14 @@ load_target_env() { }
# buildenv_setup(): set up the build environment needed for post-chroot_setup()
buildenv_setup() { }
# chroot_cleanup(): Clean up resources setup in chroot_setup() at exit.
#
# This function can be built upon. `_chroot_cleanup` must be added to the end of
# the override function, if overridden.
chroot_cleanup() {
_chroot_cleanup
} # chroot_cleanup()
usage() {
echo "Usage: $0 [-c release.conf]"
exit 1
@@ -436,6 +444,18 @@ chroot_arm_build_release() {
return 0
} # chroot_arm_build_release()
# chroot_cleanup(): Clean up resources setup in chroot_setup() at exit.
#
# This contains steps which must be executed at exit.
#
# Do not override this function: override `chroot_cleanup instead.
_chroot_cleanup() {
if [ -c "${CHROOTDIR}/dev/null" ]; then
echo "Unmounting /dev in ${CHROOTDIR}"
umount -f "${CHROOTDIR}/dev"
fi
}
# main(): Start here.
main() {
set -e # Everything must succeed
@@ -460,7 +480,7 @@ main() {
fi
fi
env_check
trap "umount ${CHROOTDIR}/dev" EXIT # Clean up devfs mount on exit
trap chroot_cleanup INT EXIT TERM
chroot_setup
extra_chroot_setup
chroot_build_target