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.switch.enable = true;
29
30 system.build.diskImage = import ../lib/make-disk-image.nix {
31 inherit config lib pkgs;
32 label = rootFslabel;
33 partitionTableType = "efi";
34 format = "qcow2";
35 bootSize = "32M";
36 additionalSpace = "0M";
37 copyChannel = false;
38 };
39 };
40in
41{
42 name = "qemu-vm-external-disk-image";
43
44 meta.maintainers = with lib.maintainers; [ nikstur ];
45
46 nodes.machine =
47 {
48 config,
49 lib,
50 pkgs,
51 ...
52 }:
53 {
54 virtualisation.directBoot.enable = false;
55 virtualisation.mountHostNixStore = false;
56 virtualisation.useEFIBoot = true;
57
58 # This stops the qemu-vm module from overriding the fileSystems option
59 # with virtualisation.fileSystems.
60 virtualisation.fileSystems = lib.mkForce { };
61
62 imports = [ externalModule ];
63 };
64
65 testScript =
66 { nodes, ... }:
67 ''
68 import os
69 import subprocess
70 import tempfile
71
72 tmp_disk_image = tempfile.NamedTemporaryFile()
73
74 subprocess.run([
75 "${nodes.machine.virtualisation.qemu.package}/bin/qemu-img",
76 "create",
77 "-f",
78 "qcow2",
79 "-b",
80 "${nodes.machine.system.build.diskImage}/nixos.qcow2",
81 "-F",
82 "qcow2",
83 tmp_disk_image.name,
84 ])
85
86 # Set NIX_DISK_IMAGE so that the qemu script finds the right disk image.
87 os.environ['NIX_DISK_IMAGE'] = tmp_disk_image.name
88
89 machine.succeed("findmnt --kernel --source ${rootFsDevice} --target /")
90
91 # Make sure systemd boot didn't clobber this
92 machine.succeed("[ ! -e /homeless-shelter ]")
93 '';
94}