civodul pushed a commit to branch main
in repository shepherd.

commit 6a1ccc7168111033b10f3090d6f89e327ac4fc16
Author: Ludovic Courtès <l...@gnu.org>
AuthorDate: Sun Dec 22 20:05:54 2024 +0100
    service: Abort early on when kexec is unsupported or no image was loaded.
    
    * modules/shepherd/service.scm (root-service) <kexec>: Do nothing when
    ‘kexec-supported?’ or ‘kexec-loaded?’ returns false.
    * NEWS: Update.
---
 NEWS                         |  8 ++++++++
 modules/shepherd/service.scm | 33 ++++++++++++++++++++-------------
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/NEWS b/NEWS
index 6b6bb98..8e6da92 100644
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,14 @@ Please send Shepherd bug reports to bug-g...@gnu.org.
 
 * Changes in 1.0.1
 
+** ‘reboot --kexec’ aborts early on if no kexec image was loaded
+
+Previously, ‘reboot --kexec’ would proceed to reboot and just hang after
+stopping all the services if no kernel image had been loaded (with ‘kexec -l’
+or similar) or if kexec is unsupported (in particular on kernels other than
+Linux).  It now checks for system support and for a pre-loaded kernel image
+and does nothing if these two conditions are not meant.
+
 ** ‘log-rotation’ service explicitly skips non-regular files
 
 Previously, the log rotation service would attempt to rotate non-regular files
diff --git a/modules/shepherd/service.scm b/modules/shepherd/service.scm
index 8114e2a..b0be2c5 100644
--- a/modules/shepherd/service.scm
+++ b/modules/shepherd/service.scm
@@ -2774,19 +2774,26 @@ Clients such as 'herd' can read it and format it in a 
human-readable way."
      (kexec
       "Reboot the system and run kexec."
       (lambda (running)
-        (catch 'quit
-          (cut stop-service root-service)
-          (lambda (key)
-            (local-output (l10n "Rebooting with kexec..."))
-            (catch 'system-error
-              reboot-kexec
-              (lambda args
-                ;; It might be that kexec is unsupported or that no image had
-                ;; been loaded with 'kexec_file_load'.  At any rate, it's too
-                ;; late to roll back, so reboot normally.
-                (local-output (l10n "Failed to reboot with kexec: ~a")
-                              (strerror (system-error-errno args)))
-                (reboot)))))))
+        (cond ((not (kexec-supported?))
+               (local-output
+                (l10n "Rebooting with kexec is not supported; \
+doing nothing.")))
+              ((not (kexec-loaded?))
+               (local-output
+                (l10n "No kexec image loaded; doing nothing.")))
+              (else
+               (catch 'quit
+                 (cut stop-service root-service)
+                 (lambda (key)
+                   (local-output (l10n "Rebooting with kexec..."))
+                   (catch 'system-error
+                     reboot-kexec
+                     (lambda args
+                       ;; Something went wrong but it's too late to roll back
+                       ;; so reboot normally.
+                       (local-output (l10n "Failed to reboot with kexec: ~a")
+                                     (strerror (system-error-errno args)))
+                       (reboot)))))))))
 
      ;; Evaluate arbitrary code.
      (load

Reply via email to