summaryrefslogtreecommitdiffstats
path: root/nixos/tests/hibernate.nix
blob: 7004516d03a854877daa34ce3c73c6e9a8d6167b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Test whether hibernation from partition works.

{
  system ? builtins.currentSystem,
  config ? { },
  pkgs ? import ../.. { inherit system config; },
  systemdStage1,
}:

with import ../lib/testing-python.nix { inherit system pkgs; };

makeTest {
  name = "hibernate" + pkgs.lib.optionalString systemdStage1 "-systemd-stage-1";

  nodes = {
    machine =
      {
        config,
        lib,
        pkgs,
        ...
      }:
      {
        imports = [
          ./common/auto-format-root-device.nix
        ];

        powerManagement.powerDownCommands = "systemctl --no-block stop backdoor.service";
        powerManagement.resumeCommands = "systemctl --no-block restart backdoor.service";

        virtualisation.emptyDiskImages = [ (2 * config.virtualisation.memorySize) ];
        # virtiofs doesn't support hibernation
        virtualisation.useNixStoreImage = true;
        virtualisation.sharedDirectories = lib.mkForce { };

        swapDevices = lib.mkOverride 0 [
          {
            device = "/dev/vdc";
            options = [ "x-systemd.makefs" ];
          }
        ];
        boot.initrd.systemd.enable = systemdStage1;
        virtualisation.useEFIBoot = true;
      };
  };

  testScript = ''
    # Drop in file that checks if we un-hibernated properly (and not booted fresh)
    machine.wait_for_unit("default.target")
    machine.succeed(
        "mkdir /run/test",
        "mount -t ramfs -o size=1m ramfs /run/test",
        "echo not persisted to disk > /run/test/suspended",
    )

    # Hibernate machine
    machine.execute("systemctl hibernate >&2 &", check_return=False)
    machine.wait_for_shutdown()

    # Restore machine from hibernation, validate our ramfs file is there.
    machine.start()
    machine.succeed("grep 'not persisted to disk' /run/test/suspended")

    # Ensure we don't restore from hibernation when booting again
    machine.crash()
    machine.wait_for_unit("default.target")
    machine.fail("grep 'not persisted to disk' /run/test/suspended")
  '';

}