1# Tests that you can boot from an external disk image with the qemu-vm module.
2# "External" here means that the image was not produced within the qemu-vm
3# module and relies on the fileSystems option also set outside the qemu-vm
4# module. Most notably, this tests that you can stop the qemu-vm module from
5# overriding fileSystems with virtualisation.fileSystems so you don't have to
6# replicate the previously set fileSystems in virtualisation.fileSystems.
7
8{ lib, ... }:
9
10let
11 rootFslabel = "external";
12 rootFsDevice = "/dev/disk/by-label/${rootFslabel}";
13
14 externalModule =
15 {
16 config,
17 lib,
18 pkgs,
19 ...
20 }:
21 {
22 boot.loader.systemd-boot.enable = true;
23
24 fileSystems = {
25 "/".device = rootFsDevice;
26 };
27
28 system.build.diskImage = import ../lib/make-disk-image.nix {
29 inherit config lib pkgs;
30 label = rootFslabel;
31 partitionTableType = "efi";
32 format = "qcow2";
33 bootSize = "32M";
34 additionalSpace = "0M";
35 copyChannel = false;
36 };
37 };
38in
39{
40 name = "qemu-vm-external-disk-image";
41
42 meta.maintainers = with lib.maintainers; [ nikstur ];
43
44 nodes.machine =
45 {
46 config,
47 lib,
48 pkgs,
49 ...
50 }:
51 {
52 virtualisation.directBoot.enable = false;
53 virtualisation.mountHostNixStore = false;
54 virtualisation.useEFIBoot = true;
55
56 # This stops the qemu-vm module from overriding the fileSystems option
57 # with virtualisation.fileSystems.
58 virtualisation.fileSystems = lib.mkForce { };
59
60 imports = [ externalModule ];
61 };
62
63 testScript =
64 { nodes, ... }:
65 ''
66 import os
67 import subprocess
68 import tempfile
69
70 tmp_disk_image = tempfile.NamedTemporaryFile()
71
72 subprocess.run([
73 "${nodes.machine.virtualisation.qemu.package}/bin/qemu-img",
74 "create",
75 "-f",
76 "qcow2",
77 "-b",
78 "${nodes.machine.system.build.diskImage}/nixos.qcow2",
79 "-F",
80 "qcow2",
81 tmp_disk_image.name,
82 ])
83
84 # Set NIX_DISK_IMAGE so that the qemu script finds the right disk image.
85 os.environ['NIX_DISK_IMAGE'] = tmp_disk_image.name
86
87 machine.succeed("findmnt --kernel --source ${rootFsDevice} --target /")
88
89 # Make sure systemd boot didn't clobber this
90 machine.succeed("[ ! -e /homeless-shelter ]")
91 '';
92}