summaryrefslogtreecommitdiffstats
path: root/nixos/tests/qemu-vm-external-disk-image.nix
blob: 160ec1945140cb20ce3705f398c725c7ba4c1e0a (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Tests that you can boot from an external disk image with the qemu-vm module.
# "External" here means that the image was not produced within the qemu-vm
# module and relies on the fileSystems option also set outside the qemu-vm
# module. Most notably, this tests that you can stop the qemu-vm module from
# overriding fileSystems with virtualisation.fileSystems so you don't have to
# replicate the previously set fileSystems in virtualisation.fileSystems.

{ lib, ... }:

let
  rootFslabel = "external";
  rootFsDevice = "/dev/disk/by-label/${rootFslabel}";

  externalModule =
    {
      config,
      lib,
      pkgs,
      ...
    }:
    {
      boot.loader.systemd-boot.enable = true;

      fileSystems = {
        "/".device = rootFsDevice;
        "/".fsType = "ext4";
      };

      system.switch.enable = true;

      system.build.diskImage = import ../lib/make-disk-image.nix {
        inherit config lib pkgs;
        label = rootFslabel;
        partitionTableType = "efi";
        format = "qcow2";
        bootSize = "128M";
        additionalSpace = "0M";
        copyChannel = false;
      };
    };
in
{
  name = "qemu-vm-external-disk-image";

  meta.maintainers = with lib.maintainers; [ nikstur ];

  nodes.machine =
    {
      config,
      lib,
      pkgs,
      ...
    }:
    {
      virtualisation.directBoot.enable = false;
      virtualisation.mountHostNixStore = false;
      virtualisation.useEFIBoot = true;

      # This stops the qemu-vm module from overriding the fileSystems option
      # with virtualisation.fileSystems.
      virtualisation.fileSystems = lib.mkForce { };

      imports = [ externalModule ];
    };

  testScript =
    { nodes, ... }:
    ''
      import os
      import subprocess
      import tempfile

      tmp_disk_image = tempfile.NamedTemporaryFile()

      subprocess.run([
        "${nodes.machine.virtualisation.qemu.package}/bin/qemu-img",
        "create",
        "-f",
        "qcow2",
        "-b",
        "${nodes.machine.system.build.diskImage}/nixos.qcow2",
        "-F",
        "qcow2",
        tmp_disk_image.name,
      ])

      # Set NIX_DISK_IMAGE so that the qemu script finds the right disk image.
      os.environ['NIX_DISK_IMAGE'] = tmp_disk_image.name

      machine.succeed("findmnt --kernel --source ${rootFsDevice} --target /")

      # Make sure systemd boot didn't clobber this
      machine.succeed("[ ! -e /homeless-shelter ]")
    '';
}