Blame SOURCES/0166-Add-systemd-integration-scripts-to-make-systemctl-re.patch

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