8631a2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8631a2
From: Hans de Goede <hdegoede@redhat.com>
8631a2
Date: Wed, 6 Jun 2018 08:44:11 +0200
8631a2
Subject: [PATCH] Add auto-hide menu support
8631a2
8631a2
On single-os systems we do not want to show the menu, unless something
8631a2
went wrong with the previous boot, in which case the user may need the
8631a2
menu to debug/fix the problem.
8631a2
8631a2
This commit adds a new grub.d/00_menu_auto_hide file which emits a
8631a2
config snippet implementing this. I've chosen to do this in a separate
8631a2
grub.d file because chances of this going upstream are small and this way
8631a2
it will be easier to rebase.
8631a2
8631a2
Since auto-hiding the menu requires detecting the previous boot was ok,
8631a2
we get fastboot support (where we don't check for a key at all) for free
8631a2
so this commit also adds support for this.
8631a2
8631a2
The new config-file code uses the following variables:
8631a2
8631a2
menu_auto_hide     Set this to "1" to activate the new auto-hide feature
8631a2
                   Set this to "2" to auto-hide the menu even when multiple
8631a2
                   operating systems are installed. Note the menu will still
8631a2
                   auto show after booting an other os as that won't set
8631a2
                   boot_success.
8631a2
menu_show_once     Set this to "1" to force showing the menu once.
8631a2
boot_success       The OS sets this to "1" to indicate a successful boot.
8631a2
boot_indeterminate The OS increments this integer when rebooting after e.g.
8631a2
                   installing updates or a selinux relabel.
8631a2
fastboot           If set to "1" and the conditions for auto-hiding the menu
8631a2
                   are met, the menu is not shown and all checks for keypresses
8631a2
                   are skipped, booting the default immediately.
8631a2
8631a2
30_os-prober.in changes somewhat inspired by:
8631a2
https://git.launchpad.net/~ubuntu-core-dev/grub/+git/ubuntu/tree/debian/patches/quick_boot.patch
8631a2
8631a2
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
8631a2
---
8631a2
Changes in v2:
8631a2
-Drop shutdown_success tests, there is no meaningful way for systemd to set
8631a2
 this flag (by the time it knows all filesystems are unmounted or read-only
8631a2
-Drop fwsetup_once support, systemd already supports booting directly into
8631a2
 the fwsetup by doing "systemctl reboot --firmware"
8631a2
---
8631a2
 Makefile.util.def                |  6 +++++
8631a2
 util/grub.d/00_menu_auto_hide.in | 50 ++++++++++++++++++++++++++++++++++++++++
8631a2
 util/grub.d/30_os-prober.in      | 18 +++++++++++++++
8631a2
 3 files changed, 74 insertions(+)
8631a2
 create mode 100644 util/grub.d/00_menu_auto_hide.in
8631a2
8631a2
diff --git a/Makefile.util.def b/Makefile.util.def
b32e65
index cbd661d63..0fdfdd91f 100644
8631a2
--- a/Makefile.util.def
8631a2
+++ b/Makefile.util.def
8631a2
@@ -448,6 +448,12 @@ script = {
8631a2
   installdir = grubconf;
8631a2
 };
8631a2
 
8631a2
+script = {
8631a2
+  name = '00_menu_auto_hide';
8631a2
+  common = util/grub.d/00_menu_auto_hide.in;
8631a2
+  installdir = grubconf;
8631a2
+};
8631a2
+
8631a2
 script = {
8631a2
   name = '01_users';
8631a2
   common = util/grub.d/01_users.in;
8631a2
diff --git a/util/grub.d/00_menu_auto_hide.in b/util/grub.d/00_menu_auto_hide.in
8631a2
new file mode 100644
b32e65
index 000000000..a10fe45bb
8631a2
--- /dev/null
8631a2
+++ b/util/grub.d/00_menu_auto_hide.in
8631a2
@@ -0,0 +1,50 @@
8631a2
+#! /bin/sh
8631a2
+
8631a2
+# Disable / skip generating menu-auto-hide config parts on serial terminals
8631a2
+for x in ${GRUB_TERMINAL_INPUT} ${GRUB_TERMINAL_OUTPUT}; do
8631a2
+  case "$x" in
8631a2
+    serial*)
8631a2
+      exit 0
8631a2
+      ;;
8631a2
+  esac
8631a2
+done
8631a2
+
8631a2
+cat << EOF
8631a2
+if [ "\${boot_success}" = "1" -o "\${boot_indeterminate}" = "1" ]; then
8631a2
+  set last_boot_ok=1
8631a2
+else
8631a2
+  set last_boot_ok=0
8631a2
+fi
8631a2
+
8631a2
+# Reset boot_indeterminate after a successful boot
8631a2
+if [ "\${boot_success}" = "1" ] ; then
8631a2
+  set boot_indeterminate=0
8631a2
+  save_env boot_indeterminate
8631a2
+# Avoid boot_indeterminate causing the menu to be hidden more then once
8631a2
+elif [ "\${boot_indeterminate}" = "1" ]; then
8631a2
+  set boot_indeterminate=2
8631a2
+  save_env boot_indeterminate
8631a2
+fi
8631a2
+set boot_success=0
8631a2
+save_env boot_success
8631a2
+
8631a2
+if [ x\$feature_timeout_style = xy ] ; then
8631a2
+  if [ "\${menu_show_once}" ]; then
8631a2
+    unset menu_show_once
8631a2
+    save_env menu_show_once
8631a2
+    set timeout_style=menu
8631a2
+    unset timeout
8631a2
+  elif [ "\${menu_auto_hide}" -a "\${last_boot_ok}" = "1" ]; then
8631a2
+    set orig_timeout_style=\${timeout_style}
8631a2
+    set orig_timeout=\${timeout}
8631a2
+    if [ "\${fastboot}" = "1" ]; then
8631a2
+      # timeout_style=menu + timeout=0 avoids the countdown code keypress check
8631a2
+      set timeout_style=menu
8631a2
+      set timeout=0
8631a2
+    else
8631a2
+      set timeout_style=hidden
8631a2
+      set timeout=1
8631a2
+    fi
8631a2
+  fi
8631a2
+fi
8631a2
+EOF
8631a2
diff --git a/util/grub.d/30_os-prober.in b/util/grub.d/30_os-prober.in
b32e65
index 13a3a6bc7..ab634393a 100644
8631a2
--- a/util/grub.d/30_os-prober.in
8631a2
+++ b/util/grub.d/30_os-prober.in
8631a2
@@ -42,6 +42,7 @@ if [ -z "${OSPROBED}" ] ; then
8631a2
 fi
8631a2
 
8631a2
 osx_entry() {
8631a2
+    found_other_os=1
8631a2
     # TRANSLATORS: it refers on the OS residing on device %s
8631a2
     onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
8631a2
     hints=""
8631a2
@@ -102,6 +103,7 @@ for OS in ${OSPROBED} ; do
8631a2
 
8631a2
   case ${BOOT} in
8631a2
     chain)
8631a2
+      found_other_os=1
8631a2
 
8631a2
 	  onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
8631a2
       cat << EOF
8631a2
@@ -132,6 +134,7 @@ EOF
8631a2
 EOF
8631a2
     ;;
8631a2
     efi)
8631a2
+      found_other_os=1
8631a2
 
8631a2
 	EFIPATH=${DEVICE#*@}
8631a2
 	DEVICE=${DEVICE%@*}
8631a2
@@ -176,6 +179,7 @@ EOF
8631a2
 	  LINITRD="${LINITRD#/boot}"
8631a2
 	fi
8631a2
 
8631a2
+        found_other_os=1
8631a2
 	onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
8631a2
 	recovery_params="$(echo "${LPARAMS}" | grep single)" || true
8631a2
 	counter=1
8631a2
@@ -249,6 +253,7 @@ EOF
8631a2
       done
8631a2
     ;;
8631a2
     hurd)
8631a2
+      found_other_os=1
8631a2
       onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
8631a2
       cat << EOF
8631a2
 menuentry '$(echo "${LONGNAME} $onstr" | grub_quote)' --class hurd --class gnu --class os \$menuentry_id_option 'osprober-gnuhurd-/boot/gnumach.gz-false-$(grub_get_device_id "${DEVICE}")' {
8631a2
@@ -275,6 +280,7 @@ EOF
8631a2
 EOF
8631a2
     ;;
8631a2
     minix)
8631a2
+      found_other_os=1
8631a2
 	  cat << EOF
8631a2
 menuentry "${LONGNAME} (on ${DEVICE}, Multiboot)" {
8631a2
 EOF
8631a2
@@ -306,3 +312,15 @@ EOF
8631a2
       esac
8631a2
   esac
8631a2
 done
8631a2
+
8631a2
+# We override the results of the menu_auto_hide code here, this is a bit ugly,
8631a2
+# but grub-mkconfig writes out the file linearly, so this is the only way
8631a2
+if [ "${found_other_os}" = "1" ]; then
8631a2
+  cat << EOF
8631a2
+# Other OS found, undo autohiding of menu unless menu_auto_hide=2
8631a2
+if [ "\${orig_timeout_style}" -a "\${menu_auto_hide}" != "2" ]; then
8631a2
+  set timeout_style=\${orig_timeout_style}
8631a2
+  set timeout=\${orig_timeout}
8631a2
+fi
8631a2
+EOF
8631a2
+fi