1 # Test whether hibernation from partition works.
3 { system ? builtins.currentSystem
5 , pkgs ? import ../.. { inherit system config; }
6 , systemdStage1 ? false
9 with import ../lib/testing-python.nix { inherit system pkgs; };
12 # System configuration of the installed system, which is used for the actual
14 installedConfig = with pkgs.lib; {
16 ../modules/testing/test-instrumentation.nix
17 ../modules/profiles/qemu-guest.nix
18 ../modules/profiles/minimal.nix
21 hardware.enableAllFirmware = mkForce false;
22 documentation.nixos.enable = false;
23 boot.loader.grub.device = "/dev/vda";
25 systemd.services.backdoor.conflicts = [ "sleep.target" ];
27 powerManagement.resumeCommands = "systemctl --no-block restart backdoor.service";
30 "/".device = "/dev/vda2";
32 swapDevices = mkOverride 0 [ { device = "/dev/vda1"; } ];
33 boot.resumeDevice = mkIf systemdStage1 "/dev/vda1";
34 boot.initrd.systemd = mkIf systemdStage1 {
36 emergencyAccess = true;
39 installedSystem = (import ../lib/eval-config.nix {
41 modules = [ installedConfig ];
42 }).config.system.build.toplevel;
47 # System configuration used for installing the installedConfig from above.
48 machine = { config, lib, pkgs, ... }: with lib; {
50 ../modules/profiles/installation-device.nix
51 ../modules/profiles/base.nix
55 substituters = mkForce [];
56 hashed-mirrors = null;
60 virtualisation.diskSize = 8 * 1024;
61 virtualisation.emptyDiskImages = [
62 # Small root disk for installer
65 virtualisation.bootDevice = "/dev/vdb";
69 # 9P doesn't support reconnection to virtio transport after a hibernation.
70 # Therefore, machine just hangs on any Nix store access.
71 # To avoid this, we install NixOS onto a temporary disk with everything we need
72 # included into the store.
76 def create_named_machine(name):
77 machine = create_machine(
79 "qemuFlags": "-cpu max ${
80 if system == "x86_64-linux" then "-m 1024"
81 else "-m 768 -enable-kvm -machine virt,gic-version=host"}",
82 "hdaInterface": "virtio",
83 "hda": "vm-state-machine/machine.qcow2",
87 driver.machines.append(machine)
95 "flock /dev/vda parted --script /dev/vda -- mklabel msdos"
96 + " mkpart primary linux-swap 1M 1024M"
97 + " mkpart primary ext2 1024M -1s",
99 "mkfs.ext3 -L nixos /dev/vda2",
100 "mount LABEL=nixos /mnt",
101 "mkswap /dev/vda1 -L swap",
103 "nix-store --load-db < ${pkgs.closureInfo {rootPaths = [installedSystem];}}/registration",
104 "nixos-install --root /mnt --system ${installedSystem} --no-root-passwd --no-channel-copy >&2",
109 hibernate = create_named_machine("hibernate")
111 # Drop in file that checks if we un-hibernated properly (and not booted fresh)
114 "mount -t ramfs -o size=1m ramfs /run/test",
115 "echo not persisted to disk > /run/test/suspended",
119 hibernate.execute("systemctl hibernate >&2 &", check_return=False)
120 hibernate.wait_for_shutdown()
122 # Restore machine from hibernation, validate our ramfs file is there.
123 resume = create_named_machine("resume")
125 resume.succeed("grep 'not persisted to disk' /run/test/suspended")
127 # Ensure we don't restore from hibernation when booting again
129 resume.wait_for_unit("default.target")
130 resume.fail("grep 'not persisted to disk' /run/test/suspended")