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 = { config, lib, pkgs, ... }: {
15 boot.loader.systemd-boot.enable = true;
16
17 fileSystems = {
18 "/".device = rootFsDevice;
19 };
20
21 system.build.diskImage = import ../lib/make-disk-image.nix {
22 inherit config lib pkgs;
23 label = rootFslabel;
24 partitionTableType = "efi";
25 format = "qcow2";
26 bootSize = "32M";
27 additionalSpace = "0M";
28 copyChannel = false;
29 };
30 };
31in
32{
33 name = "qemu-vm-external-disk-image";
34
35 meta.maintainers = with lib.maintainers; [ nikstur ];
36
37 nodes.machine = { config, lib, pkgs, ... }: {
38 virtualisation.directBoot.enable = false;
39 virtualisation.mountHostNixStore = false;
40 virtualisation.useEFIBoot = true;
41
42 # This stops the qemu-vm module from overriding the fileSystems option
43 # with virtualisation.fileSystems.
44 virtualisation.fileSystems = lib.mkForce { };
45
46 imports = [ externalModule ];
47 };
48
49 testScript = { nodes, ... }: ''
50 import os
51 import subprocess
52 import tempfile
53
54 tmp_disk_image = tempfile.NamedTemporaryFile()
55
56 subprocess.run([
57 "${nodes.machine.virtualisation.qemu.package}/bin/qemu-img",
58 "create",
59 "-f",
60 "qcow2",
61 "-b",
62 "${nodes.machine.system.build.diskImage}/nixos.qcow2",
63 "-F",
64 "qcow2",
65 tmp_disk_image.name,
66 ])
67
68 # Set NIX_DISK_IMAGE so that the qemu script finds the right disk image.
69 os.environ['NIX_DISK_IMAGE'] = tmp_disk_image.name
70
71 machine.succeed("findmnt --kernel --source ${rootFsDevice} --target /")
72 '';
73}