Add support for "systemctl reboot --boot-loader-menu=xx"

Related: rhbz#1857389

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
This commit is contained in:
Javier Martinez Canillas 2020-08-12 11:39:25 +02:00
parent 47cf63735c
commit db0149e860
No known key found for this signature in database
GPG key ID: C751E590D63F3D69
4 changed files with 236 additions and 1 deletions

View file

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

View file

@ -0,0 +1,32 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Hans de Goede <hdegoede@redhat.com>
Date: Thu, 23 Jul 2020 09:27:36 +0200
Subject: [PATCH] systemd-integration.sh: Also set old menu_show_once grubenv
var
Downstream RH / Fedora patch for compatibility with old, not (yet)
regenerated grub.cfg files which miss the menu_show_once_timeout check.
This older grubenv variable leads to a fixed timeout of 60 seconds.
Note that the new menu_show_once_timeout will overrule these 60 seconds
if both are set and the grub.cfg does have the menu_show_once_timeout
check.
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
util/systemd/systemd-integration.sh.in | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/util/systemd/systemd-integration.sh.in b/util/systemd/systemd-integration.sh.in
index dc1218597bc..a4c071c5b0c 100644
--- a/util/systemd/systemd-integration.sh.in
+++ b/util/systemd/systemd-integration.sh.in
@@ -4,3 +4,8 @@ TIMEOUT_USEC=$(cat /run/systemd/reboot-to-boot-loader-menu)
TIMEOUT=$(((TIMEOUT_USEC + 500000) / 1000000))
@grub_editenv@ - set menu_show_once_timeout=$TIMEOUT
+
+# Downstream RH / Fedora patch for compatibility with old, not (yet)
+# regenerated grub.cfg files which miss the menu_show_once_timeout check
+# this older grubenv variable leads to a fixed timeout of 60 seconds
+@grub_editenv@ - set menu_show_once=1

View file

@ -263,3 +263,5 @@ Patch0262: 0262-Fix-const-char-pointers-in-grub-core-net-efi-ip6_con.patch
Patch0263: 0263-Fix-const-char-pointers-in-grub-core-net-efi-net.c.patch
Patch0264: 0264-Fix-const-char-pointers-in-grub-core-net-efi-pxe.c.patch
Patch0265: 0265-Fix-const-char-pointers-in-grub-core-net-url.c.patch
Patch0266: 0266-Add-systemd-integration-scripts-to-make-systemctl-re.patch
Patch0267: 0267-systemd-integration.sh-Also-set-old-menu_show_once-g.patch

View file

@ -14,7 +14,7 @@
Name: grub2
Epoch: 1
Version: 2.04
Release: 27%{?dist}
Release: 28%{?dist}
Summary: Bootloader with support for Linux, Multiboot and more
License: GPLv3+
URL: http://www.gnu.org/software/grub/
@ -263,8 +263,11 @@ ln -s ../grub-boot-success.timer \
# Install systemd system-update unit to set boot_indeterminate for offline-upd
install -D -m 0755 -t %{buildroot}%{_unitdir} docs/grub-boot-indeterminate.service
install -d -m 0755 %{buildroot}%{_unitdir}/system-update.target.wants
install -d -m 0755 %{buildroot}%{_unitdir}/reboot.target.wants
ln -s ../grub-boot-indeterminate.service \
%{buildroot}%{_unitdir}/system-update.target.wants
ln -s ../grub2-systemd-integration.service \
%{buildroot}%{_unitdir}/reboot.target.wants
# Don't run debuginfo on all the grub modules and whatnot; it just
# rejects them, complains, and slows down extraction.
@ -398,6 +401,9 @@ rm -r /boot/grub2.tmp/ || :
%{_userunitdir}/timers.target.wants
%{_unitdir}/grub-boot-indeterminate.service
%{_unitdir}/system-update.target.wants
%{_unitdir}/%{name}-systemd-integration.service
%{_unitdir}/reboot.target.wants
%{_unitdir}/systemd-logind.service.d
%{_infodir}/%{name}*
%{_datarootdir}/grub/*
%{_sbindir}/%{name}-install
@ -413,6 +419,7 @@ rm -r /boot/grub2.tmp/ || :
%{_bindir}/%{name}-mkimage
%{_bindir}/%{name}-mkrelpath
%{_bindir}/%{name}-script-check
%{_libexecdir}/%{name}
%{_datadir}/man/man?/*
# exclude man pages from tools-extra
@ -509,6 +516,10 @@ rm -r /boot/grub2.tmp/ || :
%endif
%changelog
* Wed Aug 12 2020 Javier Martinez Canillas <javierm@redhat.com> - 2.04-28
- Add support for "systemctl reboot --boot-loader-menu=xx" (hdegoede)
Related: rhbz#1857389
* Mon Aug 10 2020 Peter Jones <pjones@redhat.com> - 2.04-27
- Attempt to enable dual-signing in f33
- "Minor" bug fixes. For f33: