git: 477fbe6732fe - stable/13 - bsdinstall: bootconfig: Try to clean old efi boot entries

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Thu, 30 Dec 2021 09:10:14 UTC
The branch stable/13 has been updated by manu:

URL: https://cgit.FreeBSD.org/src/commit/?id=477fbe6732feb9f37e2790f153c78dba3b7df239

commit 477fbe6732feb9f37e2790f153c78dba3b7df239
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2021-12-08 09:37:54 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2021-12-30 09:09:57 +0000

    bsdinstall: bootconfig: Try to clean old efi boot entries
    
    If one install FreeBSD on the same machine multiple times in a row or
    on different harddrive they have a lot of 'FreeBSD' efi boot entries added.
    With this patch we now do :
    - If there is no 'FreeBSD' entry we add one like before
    - If there is one or more entries we ask the user if they want to delete
      them all and add a new one
      - If they say yes we do that
      - If they say no we prompt them an inputbox so they can enter a different
        entry name if they want, it defaults to 'FreeBSD'
    
    Reviewed by:    bapt, imp
    MFC after:      2 weeks
    Sponsored by:   Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D33330
    
    (cherry picked from commit 40c928e7b80a7e2ba3652f7c3dc1d30fe92e5b48)
---
 usr.sbin/bsdinstall/scripts/bootconfig | 54 ++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)

diff --git a/usr.sbin/bsdinstall/scripts/bootconfig b/usr.sbin/bsdinstall/scripts/bootconfig
index c5218b10a271..8c0e87991248 100755
--- a/usr.sbin/bsdinstall/scripts/bootconfig
+++ b/usr.sbin/bsdinstall/scripts/bootconfig
@@ -32,6 +32,8 @@ FREEBSD_BOOTLABEL="FreeBSD"
 
 BSDCFG_SHARE="/usr/share/bsdconfig"
 . $BSDCFG_SHARE/common.subr || exit 1
+f_dprintf "%s: loading_includes..." "$0"
+f_include $BSDCFG_SHARE/dialog.subr
 
 : ${TMPDIR:="/tmp"}
 
@@ -40,6 +42,55 @@ die() {
 	exit 1
 }
 
+dialog_uefi_entryname()
+{
+	local prompt="Please enter a name for the new entry"
+	local hline=
+	local value="$*"
+	local height width
+
+	f_dialog_inputbox_size height width \
+		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$prompt" "$value" "$hline"
+
+	$DIALOG \
+		--title "$DIALOG_TITLE"         \
+		--backtitle "$DIALOG_BACKTITLE" \
+		--hline "$hline"                \
+		--ok-label "Ok"                 \
+		--no-cancel                     \
+		--inputbox "$prompt"            \
+		$height $width "$value"         \
+		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
+}
+
+update_uefi_bootentry()
+{
+	nentries=$(efibootmgr | grep -c 'FreeBSD$')
+	# No entries so directly create one and return
+	if [ ${nentries} -eq 0 ]; then
+		f_dprintf "Creating UEFI boot entry"
+		efibootmgr --create --activate --label "$FREEBSD_BOOTLABEL" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null
+		return
+	fi
+
+	$DIALOG --backtitle 'FreeBSD Installer' --title 'Boot configuration' \
+	    --yesno 'There is multiple "FreeBSD" efi boot entries, would you like to remove them all and add a new one?' 0 0
+	if [ $? -eq $DIALOG_OK ]; then
+		for entry in $(efibootmgr | awk '$NF == "FreeBSD" { sub(/.*Boot/,"", $1); sub(/\*/,"", $1); print $1 }'); do
+			efibootmgr -B -b ${entry}
+		done
+		efibootmgr --create --activate --label "$FREEBSD_BOOTLABEL" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null
+		return
+	fi
+
+	FREEBSD_BOOTLABEL=$(dialog_uefi_entryname "${FREEBSD_BOOTLABEL}")
+	[ $? -eq $DIALOG_CANCEL ] && exit 1
+	efibootmgr --create --activate --label "$FREEBSD_BOOTLABEL" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null
+}
+
+f_dialog_title "Boot configuration"
+f_dialog_backtitle "FreeBSD Installer"
+
 if [ `uname -m` == powerpc ]; then
 	platform=`sysctl -n hw.platform`
 	if [ "$platform" == ps3 -o "$platform" == powernv ]; then
@@ -87,8 +138,7 @@ if [ -n "$(awk '{if ($2=="/boot/efi") printf("%s\n",$1);}' $PATH_FSTAB)" ]; then
 	fi
 
 	if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
-		f_dprintf "Creating UEFI boot entry"
-		efibootmgr --create --activate --label "$FREEBSD_BOOTLABEL" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null
+		update_uefi_bootentry
 	fi
 
 	f_dprintf "Finished configuring ESP"