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

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