Control: retitle -1 GRUB not installed or installed to the wrong device

I found that grub-installer may generate a wrong bootdev value for grub-pc when state=2 and previous_state!=1. This may happen at least in the following three cases if the user selects the device in the proposed list:

- if the first device listed by grub-mkdevicemap is the installation media (/cdrom or /hd-media) or has no known partition table nor filesystem, GRUB may be installed to the disk containing /boot instead of the device selected by the user;

- if unsupported OS are detected by os-prober, bootdev may be empty and GRUB is not installed;

- if the user answers "no" to "Install the GRUB boot loader to your primary drive?", bootdev may be empty and GRUB is not installed.

It does not happen if the user chose "Enter the device manually".

The common root cause is grub-installer/bootdev being used without being manually set by the user even after bootdev was set by select_bootdev.

Attached is a minimal patch which uses grub-installer/bootdev only when if was manually set by the user, and not when the bootdev was set by select_bootdev. I tested it with the last case. Test and feedback appreciated.
From 78376e99d67b713d79553f9cdfb924c261f633d6 Mon Sep 17 00:00:00 2001
From: Pascal Hambourg <pas...@plouf.fr.eu.org>
Date: Sun, 7 May 2023 20:19:07 +0200
Subject: [PATCH] Fix wrong or empty bootdev value for grub-pc

grub-installer may generate a wrong bootdev value for grub-pc when
state=2 and previous_state!=1. This may happen at least in the
following three cases if the user selects the device in the proposed
list:
- if default_bootdev is the installation media (/cdrom or /hd-media)
or has no known partition table nor filesystem, bootdev will be the
disk containing /boot instead of the device selected by the user;
- if unsupported OS are detected by os-prober, bootdev will be empty;
- if the user answers "no" to "Install the GRUB boot loader to your
primary drive?", bootdev will be empty.

It does not happen if the user chose "Enter the device manually".

The common root cause is grub-installer/bootdev being used without
being set by the user even after bootdev was set by select_bootdev.
This minimal patch uses grub-installer/bootdev only after being set
by the user.
---
 grub-installer | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/grub-installer b/grub-installer
index 2f0de04d..5656af84 100755
--- a/grub-installer
+++ b/grub-installer
@@ -912,25 +912,27 @@ while : ; do
 		fi
 
 		if [ ! -e "$bootdev" ]; then
-		    db_input critical grub-installer/bootdev || true
-		fi
-		if ! db_go; then
-			if [ "$q" ]; then
-				state=1
+			db_input critical grub-installer/bootdev || true
+			if ! db_go; then
+				if [ "$q" ]; then
+					state=1
+				else
+					# back up to menu
+					db_progress STOP
+					exit 10
+				fi
 			else
-				# back up to menu
-				db_progress STOP
-				exit 10
-			fi
-		else
-			db_get grub-installer/bootdev
-			bootdev=$RET
-			if echo "$bootdev" | grep -qv '('; then
-				mappedbootdev=$(mapdevfs "$bootdev") || true
-				if [ -n "$mappedbootdev" ]; then
-					bootdev="$mappedbootdev"
+				db_get grub-installer/bootdev
+				bootdev=$RET
+				if echo "$bootdev" | grep -qv '('; then
+					mappedbootdev=$(mapdevfs "$bootdev") || true
+					if [ -n "$mappedbootdev" ]; then
+						bootdev="$mappedbootdev"
+					fi
 				fi
+				break
 			fi
+		else
 			break
 		fi
 	else
-- 
2.30.2

Reply via email to