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