diff mbox

PRELIMINARY: Add support for hibernation.

Message ID 87bn0u1msx.fsf@netris.org
State New
Headers show

Commit Message

Mark H Weaver Aug. 15, 2016, 8:03 a.m. UTC
Hello Guix,

Here's a preliminary patch to add support for hibernation.  To enable
it, you'll also need to add a line like this to your 'operating-system'
definition.

  (kernel-arguments '("resume=/dev/sda2"))

Where the device named is a swap partition.

WARNING: Since this is preliminary work, I recommend that the first time
you test this, be prepared for the possibility that resume will fail.
So far I've only tested it with simple partitions, without encryption or
RAID.

It may be that we should add a dedicated 'resume-device' field to the
'operating-system'.  Thoughts?

     Mark

Comments

Tomáš Čech Aug. 15, 2016, 7:57 p.m. UTC | #1
Hi Mark,

On Mon, Aug 15, 2016 at 04:03:26AM -0400, Mark H Weaver wrote:
>Hello Guix,
>
>Here's a preliminary patch to add support for hibernation.  To enable
>it, you'll also need to add a line like this to your 'operating-system'
>definition.
>
>  (kernel-arguments '("resume=/dev/sda2"))
>
>Where the device named is a swap partition.
>
>WARNING: Since this is preliminary work, I recommend that the first time
>you test this, be prepared for the possibility that resume will fail.
>So far I've only tested it with simple partitions, without encryption or
>RAID.
>
>It may be that we should add a dedicated 'resume-device' field to the
>'operating-system'.  Thoughts?

That sounds useful. Small note, in all cases I have seen it was using
swap partition - it could be good default value when finished.

S_W
Ludovic Courtès Sept. 2, 2016, 1:03 p.m. UTC | #2
Hello!

Mark H Weaver <mhw@netris.org> skribis:

> Here's a preliminary patch to add support for hibernation.  To enable
> it, you'll also need to add a line like this to your 'operating-system'
> definition.
>
>   (kernel-arguments '("resume=/dev/sda2"))
>
> Where the device named is a swap partition.

Awesome!  I missed this.

> It may be that we should add a dedicated 'resume-device' field to the
> 'operating-system'.  Thoughts?

I think it’s OK this way (though we should give an example in the
manual), but not strong opinion.

> From ad1d583eb6f009a2dfbc284cb8368608caf27a05 Mon Sep 17 00:00:00 2001
> From: Mark H Weaver <mhw@netris.org>
> Date: Sun, 14 Aug 2016 05:33:12 -0400
> Subject: [PATCH] PRELIMINARY: Add support for hibernation.
>
> * gnu/build/linux-boot.scm (boot-system): Look for a resume=<device-name>
> argument on the linux command line, and if present, attempt to resume from
> hibernation.
> * gnu/services/desktop.scm (<elogind-configuration>): Change the default
> value of the 'handle-hibernate-key' key to 'hibernate'.

[...]

> +            (resume-device (find-long-option "resume" args)))

Could you mention this argument under “Initial RAM Disk”?

> +       ;;
> +       ;; Attempt to resume from hibernation.
> +       ;;
> +       ;; IMPORTANT: This *must* happen before we mount any filesystems on
> +       ;; disk.  Quoting linux-libre/Documentation/swsusp.txt:
> +       ;; 
> +       ;; * BIG FAT WARNING **************************************************
> +       ;; *
> +       ;; * If you touch anything on disk between suspend and resume...
> +       ;; *				...kiss your data goodbye.
> +       ;; *
> +       ;; * If you do resume from initrd after your filesystems are mounted...
> +       ;; *				...bye bye root partition.
> +       ;; *			[this is actually same case as above]
> +       ;; *
> +       (when (and resume-device
> +                  (file-exists? resume-device)
> +                  (file-exists? "/sys/power/resume"))
> +         (false-if-exception
> +          (let* ((device-base-name
> +                  ;; The base name of the device file, after resolving
> +                  ;; symlinks.
> +                  (let loop ((file resume-device))
> +                    (match (stat:type (lstat file))
> +                      ('symlink
> +                       (let ((target (readlink file)))
> +                         (if (string-prefix? "/" target)
> +                             (loop target)
> +                             (loop (string-append (dirname file) "/" target)))))
> +                      (_ (basename file)))))
> +                 (major+minor
> +                  ;; The major:minor string (e.g. "8:2") corresponding
> +                  ;; to the resume device.
> +                  (call-with-input-file (string-append "/sys/class/block/"
> +                                                       device-base-name
> +                                                       "/dev")
> +                    read-line)))
> +            ;; Write the major:minor string to /sys/power/resume
> +            ;; to attempt resume from hibernation.
> +            (when major+minor
> +              (call-with-output-file "/sys/power/resume"
> +                (cut display major+minor <>))))))

Could you turn the (false-if-exception …) expression into a separate
procedure?

I would perhaps remove ‘false-if-exception’: that way, if something goes
wrong, we’ll be able to see what the error is and get a REPL.  It might
also be useful to display a message like “resuming from device /dev/xyz
(8:2)…”

Apart from that it seems alright!

It’d be interesting to see if we can write a test.  The existing tests
do not use a swap partition, but adding one shouldn’t be too hard.

Thank you,
Ludo’.
diff mbox

Patch

From ad1d583eb6f009a2dfbc284cb8368608caf27a05 Mon Sep 17 00:00:00 2001
From: Mark H Weaver <mhw@netris.org>
Date: Sun, 14 Aug 2016 05:33:12 -0400
Subject: [PATCH] PRELIMINARY: Add support for hibernation.

* gnu/build/linux-boot.scm (boot-system): Look for a resume=<device-name>
argument on the linux command line, and if present, attempt to resume from
hibernation.
* gnu/services/desktop.scm (<elogind-configuration>): Change the default
value of the 'handle-hibernate-key' key to 'hibernate'.
---
 gnu/build/linux-boot.scm | 51 +++++++++++++++++++++++++++++++++++++++++++++---
 gnu/services/desktop.scm |  6 +-----
 2 files changed, 49 insertions(+), 8 deletions(-)

diff --git a/gnu/build/linux-boot.scm b/gnu/build/linux-boot.scm
index c3febba..9732175 100644
--- a/gnu/build/linux-boot.scm
+++ b/gnu/build/linux-boot.scm
@@ -24,6 +24,7 @@ 
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-26)
   #:use-module (ice-9 match)
+  #:use-module (ice-9 rdelim)
   #:use-module (ice-9 ftw)
   #:use-module (guix build utils)
   #:use-module (gnu build linux-modules)
@@ -376,9 +377,10 @@  to it are lost."
   (call-with-error-handling
    (lambda ()
      (mount-essential-file-systems)
-     (let* ((args    (linux-command-line))
-            (to-load (find-long-option "--load" args))
-            (root    (find-long-option "--root" args)))
+     (let* ((args          (linux-command-line))
+            (to-load       (find-long-option "--load" args))
+            (root          (find-long-option "--root" args))
+            (resume-device (find-long-option "resume" args)))
 
        (when (member "--repl" args)
          (start-repl))
@@ -403,6 +405,49 @@  to it are lost."
          (unless (pre-mount)
            (error "pre-mount actions failed")))
 
+       ;;
+       ;; Attempt to resume from hibernation.
+       ;;
+       ;; IMPORTANT: This *must* happen before we mount any filesystems on
+       ;; disk.  Quoting linux-libre/Documentation/swsusp.txt:
+       ;; 
+       ;; * BIG FAT WARNING **************************************************
+       ;; *
+       ;; * If you touch anything on disk between suspend and resume...
+       ;; *				...kiss your data goodbye.
+       ;; *
+       ;; * If you do resume from initrd after your filesystems are mounted...
+       ;; *				...bye bye root partition.
+       ;; *			[this is actually same case as above]
+       ;; *
+       (when (and resume-device
+                  (file-exists? resume-device)
+                  (file-exists? "/sys/power/resume"))
+         (false-if-exception
+          (let* ((device-base-name
+                  ;; The base name of the device file, after resolving
+                  ;; symlinks.
+                  (let loop ((file resume-device))
+                    (match (stat:type (lstat file))
+                      ('symlink
+                       (let ((target (readlink file)))
+                         (if (string-prefix? "/" target)
+                             (loop target)
+                             (loop (string-append (dirname file) "/" target)))))
+                      (_ (basename file)))))
+                 (major+minor
+                  ;; The major:minor string (e.g. "8:2") corresponding
+                  ;; to the resume device.
+                  (call-with-input-file (string-append "/sys/class/block/"
+                                                       device-base-name
+                                                       "/dev")
+                    read-line)))
+            ;; Write the major:minor string to /sys/power/resume
+            ;; to attempt resume from hibernation.
+            (when major+minor
+              (call-with-output-file "/sys/power/resume"
+                (cut display major+minor <>))))))
+
        (if root
            (mount-root-file-system (canonicalize-device-spec root)
                                    root-fs-type
diff --git a/gnu/services/desktop.scm b/gnu/services/desktop.scm
index bf21707..84321b8 100644
--- a/gnu/services/desktop.scm
+++ b/gnu/services/desktop.scm
@@ -606,11 +606,7 @@  include the @command{udisksctl} command, part of UDisks, and GNOME Disks."
   (handle-suspend-key              elogind-handle-suspend-key
                                    (default 'suspend))
   (handle-hibernate-key            elogind-handle-hibernate-key
-                                   ;; (default 'hibernate)
-                                   ;; XXX Ignore it for now, since we don't
-                                   ;; yet handle resume-from-hibernation in
-                                   ;; our initrd.
-                                   (default 'ignore))
+                                   (default 'hibernate))
   (handle-lid-switch               elogind-handle-lid-switch
                                    (default 'suspend))
   (handle-lid-switch-docked        elogind-handle-lid-switch-docked
-- 
2.9.2