46968b6
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
46968b6
From: Hans de Goede <hdegoede@redhat.com>
46968b6
Date: Wed, 22 Jul 2020 14:03:42 +0200
46968b6
Subject: [PATCH] Add systemd integration scripts to make "systemctl reboot
46968b6
 --boot-loader-menu=xxx" work with grub
46968b6
46968b6
This commit adds a number of scripts / config files to make
46968b6
"systemctl reboot --boot-loader-menu=xxx" work with grub:
46968b6
46968b6
1. /lib/systemd/system/systemd-logind.service.d/10-grub.conf
46968b6
This sets SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU in the env. for logind,
46968b6
indicating that the boot-loader which is used supports this feature, see:
46968b6
https://github.com/systemd/systemd/blob/master/docs/ENVIRONMENT.md
46968b6
46968b6
2. /lib/systemd/system/grub-systemd-integration.service
46968b6
   /lib/systemd/system/reboot.target.wants/grub-systemd-integration.service ->
46968b6
     ../grub-systemd-integration.service
46968b6
   /usr/libexec/grub/grub-systemd-integration.sh
46968b6
46968b6
The symlink in the .wants dir causes the added service file to be started
46968b6
by systemd just before rebooting the system.
46968b6
If /run/systemd/reboot-to-boot-loader-menu exist then the service will run
46968b6
the grub-systemd-integration.sh script.
46968b6
This script sets the new menu_show_once_timeout grubenv variable to the
46968b6
requested timeout in seconds.
46968b6
46968b6
3. /etc/grub.d/14_menu_show_once
46968b6
46968b6
This new grub-mkconfig snippet adds the necessary code to the generated
46968b6
grub.conf to honor the new menu_show_once_timeout variable, and to
46968b6
automatically clear it after consuming it.
46968b6
46968b6
Note the service and libexec script use grub-systemd-integration as name
46968b6
because in the future they may be used to add further integration with
46968b6
systemctl reboot --foo options, e.g. support for --boot-loader-entry=NAME.
46968b6
46968b6
A few notes about upstreaming this patch from the rhboot grub2 fork:
46968b6
1. I have deliberately put the grub.conf bits for this in a new / separate
46968b6
   grub-mkconfig snippet generator for easy upstreaming
46968b6
2. Even though the commit message mentions the .wants symlink for the .service
46968b6
   I have been unable to come up with a clean way to do this at "make install"
46968b6
   time, this should be fixed before upstreaming.
46968b6
46968b6
Downstream notes:
46968b6
1. Since make install does not add the .wants symlink, this needs to be done
46968b6
   in grub2.spec %install
46968b6
2. This is keeping support for the "old" Fedora specific menu_show_once env
46968b6
   variable, which has a hardcoded timeout of 60 sec in 12_menu_auto_hide in
46968b6
   place for now. This can be dropped (eventually) in a follow-up patch once
46968b6
   GNOME has been converted to use the systemd dbus API equivalent of
46968b6
   "systemctl reboot --boot-loader-menu=xxx".
46968b6
46968b6
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
46968b6
---
46968b6
 Makefile.util.def                                | 27 ++++++++++++++++++++++++
46968b6
 conf/Makefile.common                             |  6 ++++++
46968b6
 util/grub.d/14_menu_show_once.in                 | 13 ++++++++++++
46968b6
 util/systemd/10-grub-logind-service.conf.in      |  2 ++
46968b6
 util/systemd/grub-systemd-integration.service.in |  8 +++++++
46968b6
 util/systemd/systemd-integration.sh.in           |  6 ++++++
46968b6
 6 files changed, 62 insertions(+)
46968b6
 create mode 100755 util/grub.d/14_menu_show_once.in
46968b6
 create mode 100644 util/systemd/10-grub-logind-service.conf.in
46968b6
 create mode 100644 util/systemd/grub-systemd-integration.service.in
46968b6
 create mode 100644 util/systemd/systemd-integration.sh.in
46968b6
46968b6
diff --git a/Makefile.util.def b/Makefile.util.def
e622855
index 2e6ad979c3..9927c2cfd6 100644
46968b6
--- a/Makefile.util.def
46968b6
+++ b/Makefile.util.def
46968b6
@@ -470,6 +470,12 @@ script = {
46968b6
   installdir = grubconf;
46968b6
 };
46968b6
 
46968b6
+script = {
46968b6
+  name = '14_menu_show_once';
46968b6
+  common = util/grub.d/14_menu_show_once.in;
46968b6
+  installdir = grubconf;
46968b6
+};
46968b6
+
46968b6
 script = {
46968b6
   name = '01_users';
46968b6
   common = util/grub.d/01_users.in;
46968b6
@@ -569,6 +575,27 @@ script = {
46968b6
   installdir = grubconf;
46968b6
 };
46968b6
 
46968b6
+script = {
46968b6
+  name = 'grub-systemd-integration.service';
46968b6
+  common = util/systemd/grub-systemd-integration.service.in;
46968b6
+  installdir = systemdunit;
46968b6
+  condition = COND_HOST_LINUX;
46968b6
+};
46968b6
+
46968b6
+script = {
46968b6
+  name = 'systemd-integration.sh';
46968b6
+  common = util/systemd/systemd-integration.sh.in;
46968b6
+  installdir = grublibexec;
46968b6
+  condition = COND_HOST_LINUX;
46968b6
+};
46968b6
+
46968b6
+script = {
46968b6
+  name = '10-grub-logind-service.conf';
46968b6
+  common = util/systemd/10-grub-logind-service.conf.in;
46968b6
+  installdir = systemd_logind_service_d;
46968b6
+  condition = COND_HOST_LINUX;
46968b6
+};
46968b6
+
46968b6
 program = {
46968b6
   mansection = 1;
46968b6
   name = grub-mkrescue;
46968b6
diff --git a/conf/Makefile.common b/conf/Makefile.common
e622855
index 0647c53b91..9fe5863b2d 100644
46968b6
--- a/conf/Makefile.common
46968b6
+++ b/conf/Makefile.common
46968b6
@@ -63,8 +63,11 @@ CCASFLAGS_LIBRARY = $(UTILS_CCASFLAGS)
46968b6
 # Other variables
46968b6
 
46968b6
 grubconfdir = $(sysconfdir)/grub.d
46968b6
+grublibexecdir = $(libexecdir)/$(grubdirname)
46968b6
 platformdir = $(pkglibdir)/$(target_cpu)-$(platform)
46968b6
 starfielddir = $(pkgdatadir)/themes/starfield
46968b6
+systemdunitdir = ${prefix}/lib/systemd/system
46968b6
+systemd_logind_service_ddir = $(systemdunitdir)/systemd-logind.service.d
46968b6
 
46968b6
 CFLAGS_GNULIB = -Wno-undef -Wno-unused -Wno-unused-parameter -Wno-redundant-decls -Wno-unreachable-code -Werror=trampolines -fno-trampolines
46968b6
 CPPFLAGS_GNULIB = -I$(top_builddir)/grub-core/lib/gnulib -I$(top_srcdir)/grub-core/lib/gnulib
46968b6
@@ -121,6 +124,9 @@ noinst_LIBRARIES =
46968b6
 dist_noinst_DATA =
46968b6
 platform_SCRIPTS =
46968b6
 platform_PROGRAMS =
46968b6
+grublibexec_SCRIPTS =
46968b6
+systemdunit_SCRIPTS =
46968b6
+systemd_logind_service_d_SCRIPTS =
46968b6
 
46968b6
 TESTS =
46968b6
 EXTRA_DIST =
46968b6
diff --git a/util/grub.d/14_menu_show_once.in b/util/grub.d/14_menu_show_once.in
46968b6
new file mode 100755
e622855
index 0000000000..1cd7f36142
46968b6
--- /dev/null
46968b6
+++ b/util/grub.d/14_menu_show_once.in
46968b6
@@ -0,0 +1,13 @@
46968b6
+#! /bin/sh
46968b6
+# Force the menu to be shown once, with a timeout of ${menu_show_once_timeout}
46968b6
+# if requested by ${menu_show_once_timeout} being set in the env.
46968b6
+cat << EOF
46968b6
+if [ x\$feature_timeout_style = xy ]; then
46968b6
+  if [ "\${menu_show_once_timeout}" ]; then
46968b6
+    set timeout_style=menu
46968b6
+    set timeout="\${menu_show_once_timeout}"
46968b6
+    unset menu_show_once_timeout
46968b6
+    save_env menu_show_once_timeout
46968b6
+  fi
46968b6
+fi
46968b6
+EOF
46968b6
diff --git a/util/systemd/10-grub-logind-service.conf.in b/util/systemd/10-grub-logind-service.conf.in
46968b6
new file mode 100644
e622855
index 0000000000..f2d4ac0073
46968b6
--- /dev/null
46968b6
+++ b/util/systemd/10-grub-logind-service.conf.in
46968b6
@@ -0,0 +1,2 @@
46968b6
+[Service]
46968b6
+Environment=SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU=true
46968b6
diff --git a/util/systemd/grub-systemd-integration.service.in b/util/systemd/grub-systemd-integration.service.in
46968b6
new file mode 100644
e622855
index 0000000000..c81fb594ce
46968b6
--- /dev/null
46968b6
+++ b/util/systemd/grub-systemd-integration.service.in
46968b6
@@ -0,0 +1,8 @@
46968b6
+[Unit]
46968b6
+Description=Grub2 systemctl reboot --boot-loader-menu=... support
46968b6
+Before=umount.target systemd-reboot.service
46968b6
+DefaultDependencies=no
46968b6
+ConditionPathExists=/run/systemd/reboot-to-boot-loader-menu
46968b6
+
46968b6
+[Service]
46968b6
+ExecStart=@libexecdir@/@grubdirname@/systemd-integration.sh
46968b6
diff --git a/util/systemd/systemd-integration.sh.in b/util/systemd/systemd-integration.sh.in
46968b6
new file mode 100644
e622855
index 0000000000..dc1218597b
46968b6
--- /dev/null
46968b6
+++ b/util/systemd/systemd-integration.sh.in
46968b6
@@ -0,0 +1,6 @@
46968b6
+#!/bin/sh
46968b6
+
46968b6
+TIMEOUT_USEC=$(cat /run/systemd/reboot-to-boot-loader-menu)
46968b6
+TIMEOUT=$(((TIMEOUT_USEC + 500000) / 1000000))
46968b6
+
46968b6
+@grub_editenv@ - set menu_show_once_timeout=$TIMEOUT