summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Geerinckx-Rice <me@tobias.gr>2016-02-26 17:57:07 +0100
committerGuix Patches Tester <>2020-05-03 12:07:15 +0100
commit2cb0b79895ce4bef53869961f5370c9378057d15 (patch)
tree1238bb6dbe62cf18965ccb0201353de8e7f53b38
parenta414d6ac24653e59ddb9ae4352ec1a252c6af55b (diff)
downloadpatches-series-3829.tar
patches-series-3829.tar.gz
linux-boot: Resume from hibernation.series-3829
* gnu/build/linux-boot.scm (resume-if-hibernated): New procedure. (boot-system): Call it unless ‘noresume’ was specified.
-rw-r--r--gnu/build/linux-boot.scm52
1 files changed, 52 insertions, 0 deletions
diff --git a/gnu/build/linux-boot.scm b/gnu/build/linux-boot.scm
index 05e833c0c6..74e76b6a31 100644
--- a/gnu/build/linux-boot.scm
+++ b/gnu/build/linux-boot.scm
@@ -1,5 +1,6 @@
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2016, 2017, 2019 Tobias Geerinckx-Rice <me@tobias.gr>
;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
;;; Copyright © 2019 Guillaume Le Vaillant <glv@posteo.net>
;;;
@@ -110,6 +111,51 @@ OPTION doesn't appear in ARGUMENTS."
(substring arg (+ 1 (string-index arg #\=)))))
arguments)))
+(define (resume-if-hibernated device)
+ "Resume from hibernation if possible. This is safe ONLY if no on-disk file
+systems have been mounted; calling it later risks severe file system corruption!
+See <Documentation/swsusp.txt> in the kernel source directory. This is the
+caller's responsibility, as is catching exceptions if resumption was supposed to
+happen but didn't.
+
+Resume only from DEVICE if it's a string. If it's #f, use the kernel's default
+hibernation device (CONFIG_PM_STD_PARTITION). Never return if resumption
+succeeds. Return nothing otherwise. The kernel logs any details to dmesg."
+
+ (define (string->major:minor string)
+ "Return a string with MAJOR:MINOR numbers of the device specified by STRING"
+
+ ;; The "resume=" kernel command-line option always provides a string, which
+ ;; can represent a device, a UUID, or a label. Check for all three.
+ (let* ((spec (cond ((string-prefix? "/" string) string)
+ ((uuid string) => identity)
+ (else (file-system-label string))))
+ ;; XXX kernel's swsusp_resume_can_resume() waits if ‘resumewait’ is
+ ;; found on the command line; our canonicalize-device-spec gives up
+ ;; after 20 seconds. We could loop (ew!) if someone relies on it…
+ (device (canonicalize-device-spec spec))
+ (rdev (stat:rdev (stat device)))
+ (minor (modulo rdev 256))
+ (major (/ (- rdev minor) 256)))
+ (format #f "~a:~a" major minor)))
+
+ ;; Write the MAJOR:MINOR numbers of the specified or default resume DEVICE to
+ ;; this magic file. The kernel will immediately try to resume from it.
+ (let ((resume "/sys/power/resume"))
+ (when (file-exists? resume) ; this kernel supports hibernation
+ ;; Honour the kernel's default device (only) if none other was given.
+ (let ((major:minor (if device
+ (string->major:minor device)
+ (let ((default (call-with-input-file resume
+ read-line)))
+ ;; Don't waste time echoing ‘nothing’ to /sys.
+ (if (string=? "0:0" default)
+ #f
+ default)))))
+ (when major:minor
+ (call-with-output-file resume ; may throw an ‘Invalid argument’
+ (cut display major:minor <>))))))) ; may never return
+
(define* (make-disk-device-nodes base major #:optional (minor 0))
"Make the block device nodes around BASE (something like \"/root/dev/sda\")
with the given MAJOR number, starting with MINOR."
@@ -504,6 +550,12 @@ upon error."
(load-linux-modules-from-directory linux-modules
linux-module-directory)
+ (unless (member "noresume" args)
+ ;; Try to resume immediately after loading (storage) modules
+ ;; but before any on-disk file systems have been mounted.
+ (false-if-exception ; failure is not fatal
+ (resume-if-hibernated (find-long-option "resume" args))))
+
(when keymap-file
(let ((status (system* "loadkeys" keymap-file)))
(unless (zero? status)