File 5001-sleep-don-t-init-sys-power-resume-if-resume-option-i.patch of Package systemd

From 9cbb486f8bbef5c7a51762af841e593cdf0cdc8c Mon Sep 17 00:00:00 2001
From: Franck Bui <fbui@suse.com>
Date: Wed, 26 Jul 2023 17:04:10 +0200
Subject: [PATCH 5001/5001] sleep: don't init /sys/power/resume if 'resume='
 option is missing and EFI is disabled

Otherwise in such case a first `systemctl hibernate` would fail but would still
initialize /sys/power/resume fooling a second `systemctl hibernate` into
believing that 'resume=' is correctly set and can be used by the resume process
to find the swap device to resume from.

Follow-up for #27330.

(cherry picked from commit f1f331a252d22c15f37d03524cce967664358c5c)

[fbui: fixes bsc#1186606]
[fbui: this version is pretty different from the original commit as the support
       for specifying the hibernation location via an EFI variable has not been
       backported.]
---
 src/sleep/sleep.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c
index a3aeb24633..134d315658 100644
--- a/src/sleep/sleep.c
+++ b/src/sleep/sleep.c
@@ -37,6 +37,7 @@
 
 static SleepOperation arg_operation = _SLEEP_OPERATION_INVALID;
 
+#if 0
 static int write_hibernate_location_info(const HibernateLocation *hibernate_location) {
         char offset_str[DECIMAL_STR_MAX(uint64_t)];
         char resume_str[DECIMAL_STR_MAX(unsigned) * 2 + STRLEN(":")];
@@ -82,6 +83,7 @@ static int write_hibernate_location_info(const HibernateLocation *hibernate_loca
 
         return 0;
 }
+#endif
 
 static int write_mode(char **modes) {
         int r = 0;
@@ -185,7 +187,6 @@ static int execute(
                 NULL
         };
 
-        _cleanup_(hibernate_location_freep) HibernateLocation *hibernate_location = NULL;
         _cleanup_fclose_ FILE *f = NULL;
         char **modes, **states;
         int r;
@@ -213,16 +214,21 @@ static int execute(
 
         /* Configure hibernation settings if we are supposed to hibernate */
         if (!strv_isempty(modes)) {
+                _cleanup_(hibernate_location_freep) HibernateLocation *hibernate_location = NULL;
+
                 r = find_hibernate_location(&hibernate_location);
                 if (r < 0)
                         return log_error_errno(r, "Failed to find location to hibernate to: %m");
-                if (r == 0) { /* 0 means: no hibernation location was configured in the kernel so far, let's
-                               * do it ourselves then. > 0 means: kernel already had a configured hibernation
-                               * location which we shouldn't touch. */
-                        r = write_hibernate_location_info(hibernate_location);
-                        if (r < 0)
-                                return log_error_errno(r, "Failed to prepare for hibernation: %m");
-                }
+                if (r == 0)
+                        /* r == 0 means: no hibernation location was configured in the kernel, IOW "resume="
+                         * is missing or systemd-hibernate-resume-generator is not included in initrd. Either
+                         * case refuse to proceed as the resume process wouldn't find the swap device to work
+                         * with. */
+                        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
+                                               "resume= is wrong or missing (the resume generator might be missing in initrd), refusing.");
+
+                /* r > 0 means: kernel already had a configured hibernation location and it matches one of
+                 * the swap device we found. All is good. */
 
                 r = write_mode(modes);
                 if (r < 0)
-- 
2.35.3

openSUSE Build Service is sponsored by