civodul pushed a commit to branch main
in repository shepherd.

commit 2fedd9ad0d88d116ded82b524d04366df47f2b12
Author: Ludovic Courtès <l...@gnu.org>
AuthorDate: Sun Dec 22 20:05:04 2024 +0100

    system: Add ‘kexec-supported?’ and ‘kexec-loaded?’.
    
    * modules/shepherd/system.scm.in (kexec-supported?, kexec-loaded?): New
    procedures.
---
 modules/shepherd/system.scm.in | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/modules/shepherd/system.scm.in b/modules/shepherd/system.scm.in
index 46f1848..fa3cd21 100644
--- a/modules/shepherd/system.scm.in
+++ b/modules/shepherd/system.scm.in
@@ -22,12 +22,15 @@
 (define-module (shepherd system)
   #:use-module (system foreign)
   #:use-module (ice-9 binary-ports)
+  #:autoload   (ice-9 textual-ports) (get-string-all)
   #:use-module (rnrs bytevectors)
   #:use-module (srfi srfi-71)
   #:use-module (srfi srfi-26)
   #:export (disable-reboot-on-ctrl-alt-del
             reboot
             reboot-kexec
+            kexec-supported?
+            kexec-loaded?
             halt
             power-off
             max-file-descriptors
@@ -107,6 +110,22 @@ the BIOS phase.  Return #f on failure or throw an 
exception on non-Linux systems
       (throw 'system-error 'kexec  "~A" (list (strerror ENOSYS))
              (list ENOSYS))))
 
+(define (kexec-supported?)
+  "Return true if the system supports rebooting via kexec (this is normally
+the case on Linux)."
+  (->bool RB_KEXEC))
+
+(define (kexec-loaded?)
+  "Return true if a kexec image has been previously loaded (with the
+@command{kexec -l} command or similar)."
+  (catch 'system-error
+    (lambda ()
+      (string=? (string-trim-right
+                 (call-with-input-file "/sys/kernel/kexec_loaded"
+                   get-string-all))
+                "1"))
+    (const #f)))
+
 (define (halt)
   "Halt the system.  Return #f on failure."
   (%libc-reboot RB_HALT_SYSTEM))

Reply via email to