1{
2 system ? builtins.currentSystem,
3 config ? { },
4 pkgs ? import ../.. { inherit system config; },
5}:
6
7with import ../lib/testing-python.nix { inherit system pkgs; };
8
9let
10 lib = pkgs.lib;
11 qemu-common = import ../lib/qemu-common.nix { inherit lib pkgs; };
12
13 mkStartCommand =
14 {
15 memory ? 2048,
16 cdrom ? null,
17 usb ? null,
18 pxe ? null,
19 uboot ? false,
20 uefi ? false,
21 extraFlags ? [ ],
22 }:
23 let
24 qemu = qemu-common.qemuBinary pkgs.qemu_test;
25
26 flags = [
27 "-m"
28 (toString memory)
29 "-netdev"
30 ("user,id=net0" + (lib.optionalString (pxe != null) ",tftp=${pxe},bootfile=netboot.ipxe"))
31 "-device"
32 (
33 "virtio-net-pci,netdev=net0"
34 + (lib.optionalString (pxe != null && uefi) ",romfile=${pkgs.ipxe}/ipxe.efirom")
35 )
36 ]
37 ++ lib.optionals (cdrom != null) [
38 "-cdrom"
39 cdrom
40 ]
41 ++ lib.optionals (usb != null) [
42 "-device"
43 "usb-ehci"
44 "-drive"
45 "id=usbdisk,file=${usb},if=none,readonly"
46 "-device"
47 "usb-storage,drive=usbdisk"
48 ]
49 ++ lib.optionals (pxe != null) [
50 "-boot"
51 "order=n"
52 ]
53 ++ lib.optionals uefi [
54 "-drive"
55 "if=pflash,format=raw,unit=0,readonly=on,file=${pkgs.OVMF.firmware}"
56 "-drive"
57 "if=pflash,format=raw,unit=1,readonly=on,file=${pkgs.OVMF.variables}"
58 ]
59 ++ extraFlags;
60
61 flagsStr = lib.concatStringsSep " " flags;
62 in
63 "${qemu} ${flagsStr}";
64
65 iso =
66 (import ../lib/eval-config.nix {
67 system = null;
68 modules = [
69 ../modules/installer/cd-dvd/installation-cd-minimal.nix
70 ../modules/testing/test-instrumentation.nix
71 { nixpkgs.pkgs = pkgs; }
72 ];
73 }).config.system.build.isoImage;
74
75 sd =
76 (import ../lib/eval-config.nix {
77 system = null;
78 modules = [
79 ../modules/installer/sd-card/sd-image-x86_64.nix
80 ../modules/testing/test-instrumentation.nix
81 {
82 sdImage.compressImage = false;
83 nixpkgs.pkgs = pkgs;
84 }
85 ];
86 }).config.system.build.sdImage;
87
88 makeBootTest =
89 name: config:
90 let
91 startCommand = mkStartCommand config;
92 in
93 makeTest {
94 name = "boot-" + name;
95 nodes = { };
96 testScript = ''
97 machine = create_machine("${startCommand}")
98 machine.start()
99 machine.wait_for_unit("multi-user.target")
100 machine.succeed("nix store verify --no-trust -r --option experimental-features nix-command /run/current-system")
101
102 with subtest("Check whether the channel got installed correctly"):
103 machine.succeed("nix-instantiate --dry-run '<nixpkgs>' -A hello")
104 machine.succeed("nix-env --dry-run -iA nixos.procps")
105
106 machine.shutdown()
107 '';
108 };
109
110 makeNetbootTest =
111 name: extraConfig:
112 let
113 config =
114 (import ../lib/eval-config.nix {
115 system = null;
116 modules = [
117 ../modules/installer/netboot/netboot.nix
118 ../modules/testing/test-instrumentation.nix
119 {
120 boot.kernelParams = [
121 "serial"
122 "live.nixos.passwordHash=$6$jnwR50SkbLYEq/Vp$wmggwioAkfmwuYqd5hIfatZWS/bO6hewzNIwIrWcgdh7k/fhUzZT29Vil3ioMo94sdji/nipbzwEpxecLZw0d0" # "password"
123 ];
124
125 nixpkgs.pkgs = pkgs;
126 }
127 {
128 key = "serial";
129 }
130 ];
131 }).config;
132 ipxeBootDir = pkgs.symlinkJoin {
133 name = "ipxeBootDir";
134 paths = [
135 config.system.build.netbootRamdisk
136 config.system.build.kernel
137 config.system.build.netbootIpxeScript
138 ];
139 };
140 startCommand = mkStartCommand (
141 {
142 pxe = ipxeBootDir;
143 }
144 // extraConfig
145 );
146 in
147 makeTest {
148 name = "boot-netboot-" + name;
149 nodes = { };
150 testScript = ''
151 machine = create_machine("${startCommand}")
152 machine.start()
153 machine.wait_for_unit("multi-user.target")
154 machine.succeed("grep 'serial' /proc/cmdline")
155 machine.succeed("grep 'live.nixos.passwordHash' /proc/cmdline")
156 machine.succeed("grep '$6$jnwR50SkbLYEq/Vp$wmggwioAkfmwuYqd5hIfatZWS/bO6hewzNIwIrWcgdh7k/fhUzZT29Vil3ioMo94sdji/nipbzwEpxecLZw0d0' /etc/shadow")
157 machine.shutdown()
158 '';
159 };
160in
161{
162 uefiCdrom = makeBootTest "uefi-cdrom" {
163 uefi = true;
164 cdrom = "${iso}/iso/${iso.isoName}";
165 };
166
167 uefiUsb = makeBootTest "uefi-usb" {
168 uefi = true;
169 usb = "${iso}/iso/${iso.isoName}";
170 };
171
172 uefiNetboot = makeNetbootTest "uefi" {
173 uefi = true;
174 };
175}
176// lib.optionalAttrs (pkgs.stdenv.hostPlatform.system == "x86_64-linux") {
177 biosCdrom = makeBootTest "bios-cdrom" {
178 cdrom = "${iso}/iso/${iso.isoName}";
179 };
180
181 biosUsb = makeBootTest "bios-usb" {
182 usb = "${iso}/iso/${iso.isoName}";
183 };
184
185 biosNetboot = makeNetbootTest "bios" { };
186
187 ubootExtlinux =
188 let
189 sdImage = "${sd}/sd-image/${sd.imageName}";
190 mutableImage = "/tmp/linked-image.qcow2";
191
192 startCommand = mkStartCommand {
193 extraFlags = [
194 "-bios"
195 "${pkgs.ubootQemuX86}/u-boot.rom"
196 "-machine"
197 "type=pc,accel=tcg"
198 "-drive"
199 "file=${mutableImage},if=virtio"
200 ];
201 };
202 in
203 makeTest {
204 name = "boot-uboot-extlinux";
205 nodes = { };
206 testScript = ''
207 import os
208
209 # Create a mutable linked image backed by the read-only SD image
210 if os.system("qemu-img create -f qcow2 -F raw -b ${sdImage} ${mutableImage}") != 0:
211 raise RuntimeError("Could not create mutable linked image")
212
213 machine = create_machine("${startCommand}")
214 machine.start()
215 machine.wait_for_unit("multi-user.target")
216 machine.succeed("nix store verify -r --no-trust --option experimental-features nix-command /run/current-system")
217 machine.shutdown()
218 '';
219
220 # kernel can't find rootfs after boot - investigate?
221 meta.broken = true;
222 };
223}