at 21.11-pre 3.7 kB view raw
1# Test whether hibernation from partition works. 2 3{ system ? builtins.currentSystem 4, config ? {} 5, pkgs ? import ../.. { inherit system config; } 6}: 7 8with import ../lib/testing-python.nix { inherit system pkgs; }; 9 10let 11 # System configuration of the installed system, which is used for the actual 12 # hibernate testing. 13 installedConfig = with pkgs.lib; { 14 imports = [ 15 ../modules/testing/test-instrumentation.nix 16 ../modules/profiles/qemu-guest.nix 17 ../modules/profiles/minimal.nix 18 ]; 19 20 hardware.enableAllFirmware = mkForce false; 21 documentation.nixos.enable = false; 22 boot.loader.grub.device = "/dev/vda"; 23 24 systemd.services.backdoor.conflicts = [ "sleep.target" ]; 25 26 powerManagement.resumeCommands = "systemctl --no-block restart backdoor.service"; 27 28 fileSystems = { 29 "/".device = "/dev/vda2"; 30 }; 31 swapDevices = mkOverride 0 [ { device = "/dev/vda1"; } ]; 32 }; 33 installedSystem = (import ../lib/eval-config.nix { 34 inherit system; 35 modules = [ installedConfig ]; 36 }).config.system.build.toplevel; 37in makeTest { 38 name = "hibernate"; 39 40 nodes = { 41 # System configuration used for installing the installedConfig from above. 42 machine = { config, lib, pkgs, ... }: with lib; { 43 imports = [ 44 ../modules/profiles/installation-device.nix 45 ../modules/profiles/base.nix 46 ]; 47 48 nix.binaryCaches = mkForce [ ]; 49 nix.extraOptions = '' 50 hashed-mirrors = 51 connect-timeout = 1 52 ''; 53 54 virtualisation.diskSize = 8 * 1024; 55 virtualisation.emptyDiskImages = [ 56 # Small root disk for installer 57 512 58 ]; 59 virtualisation.bootDevice = "/dev/vdb"; 60 }; 61 }; 62 63 # 9P doesn't support reconnection to virtio transport after a hibernation. 64 # Therefore, machine just hangs on any Nix store access. 65 # To avoid this, we install NixOS onto a temporary disk with everything we need 66 # included into the store. 67 68 testScript = 69 '' 70 def create_named_machine(name): 71 return create_machine( 72 { 73 "qemuFlags": "-cpu max ${ 74 if system == "x86_64-linux" then "-m 1024" 75 else "-m 768 -enable-kvm -machine virt,gic-version=host"}", 76 "hdaInterface": "virtio", 77 "hda": "vm-state-machine/machine.qcow2", 78 "name": name, 79 } 80 ) 81 82 83 # Install NixOS 84 machine.start() 85 machine.succeed( 86 # Partition /dev/vda 87 "flock /dev/vda parted --script /dev/vda -- mklabel msdos" 88 + " mkpart primary linux-swap 1M 1024M" 89 + " mkpart primary ext2 1024M -1s", 90 "udevadm settle", 91 "mkfs.ext3 -L nixos /dev/vda2", 92 "mount LABEL=nixos /mnt", 93 "mkswap /dev/vda1 -L swap", 94 # Install onto /mnt 95 "nix-store --load-db < ${pkgs.closureInfo {rootPaths = [installedSystem];}}/registration", 96 "nixos-install --root /mnt --system ${installedSystem} --no-root-passwd", 97 ) 98 machine.shutdown() 99 100 # Start up 101 hibernate = create_named_machine("hibernate") 102 103 # Drop in file that checks if we un-hibernated properly (and not booted fresh) 104 hibernate.succeed( 105 "mkdir /run/test", 106 "mount -t ramfs -o size=1m ramfs /run/test", 107 "echo not persisted to disk > /run/test/suspended", 108 ) 109 110 # Hibernate machine 111 hibernate.succeed("systemctl hibernate &") 112 hibernate.wait_for_shutdown() 113 114 # Restore machine from hibernation, validate our ramfs file is there. 115 resume = create_named_machine("resume") 116 resume.start() 117 resume.succeed("grep 'not persisted to disk' /run/test/suspended") 118 ''; 119 120}