1{ system ? builtins.currentSystem, debug ? false }:
2
3with import ../lib/testing.nix { inherit system; };
4with pkgs.lib;
5
6let
7 testVMConfig = vmName: attrs: { config, pkgs, lib, ... }: let
8 guestAdditions = pkgs.linuxPackages.virtualboxGuestAdditions;
9
10 miniInit = ''
11 #!${pkgs.stdenv.shell} -xe
12 export PATH="${lib.makeBinPath [ pkgs.coreutils pkgs.utillinux ]}"
13
14 mkdir -p /run/dbus
15 cat > /etc/passwd <<EOF
16 root:x:0:0::/root:/bin/false
17 messagebus:x:1:1::/run/dbus:/bin/false
18 EOF
19 cat > /etc/group <<EOF
20 root:x:0:
21 messagebus:x:1:
22 EOF
23
24 "${pkgs.dbus.daemon}/bin/dbus-daemon" --fork \
25 --config-file="${pkgs.dbus.daemon}/share/dbus-1/system.conf"
26
27 ${guestAdditions}/bin/VBoxService
28 ${(attrs.vmScript or (const "")) pkgs}
29
30 i=0
31 while [ ! -e /mnt-root/shutdown ]; do
32 sleep 10
33 i=$(($i + 10))
34 [ $i -le 120 ] || fail
35 done
36
37 rm -f /mnt-root/boot-done /mnt-root/shutdown
38 '';
39 in {
40 boot.kernelParams = [
41 "console=tty0" "console=ttyS0" "ignore_loglevel"
42 "boot.trace" "panic=1" "boot.panic_on_fail"
43 "init=${pkgs.writeScript "mini-init.sh" miniInit}"
44 ];
45
46 fileSystems."/" = {
47 device = "vboxshare";
48 fsType = "vboxsf";
49 };
50
51 virtualisation.virtualbox.guest.enable = true;
52
53 boot.initrd.kernelModules = [
54 "af_packet" "vboxsf"
55 "virtio" "virtio_pci" "virtio_ring" "virtio_net" "vboxguest"
56 ];
57
58 boot.initrd.extraUtilsCommands = ''
59 copy_bin_and_libs "${guestAdditions}/bin/mount.vboxsf"
60 copy_bin_and_libs "${pkgs.utillinux}/bin/unshare"
61 ${(attrs.extraUtilsCommands or (const "")) pkgs}
62 '';
63
64 boot.initrd.postMountCommands = ''
65 touch /mnt-root/boot-done
66 hostname "${vmName}"
67 mkdir -p /nix/store
68 unshare -m ${escapeShellArg pkgs.stdenv.shell} -c '
69 mount -t vboxsf nixstore /nix/store
70 exec "$stage2Init"
71 '
72 poweroff -f
73 '';
74
75 system.requiredKernelConfig = with config.lib.kernelConfig; [
76 (isYes "SERIAL_8250_CONSOLE")
77 (isYes "SERIAL_8250")
78 ];
79 };
80
81 mkLog = logfile: tag: let
82 rotated = map (i: "${logfile}.${toString i}") (range 1 9);
83 all = concatMapStringsSep " " (f: "\"${f}\"") ([logfile] ++ rotated);
84 logcmd = "tail -F ${all} 2> /dev/null | logger -t \"${tag}\"";
85 in optionalString debug "$machine->execute(ru '${logcmd} & disown');";
86
87 testVM = vmName: vmScript: let
88 cfg = (import ../lib/eval-config.nix {
89 system = "i686-linux";
90 modules = [
91 ../modules/profiles/minimal.nix
92 (testVMConfig vmName vmScript)
93 ];
94 }).config;
95 in pkgs.vmTools.runInLinuxVM (pkgs.runCommand "virtualbox-image" {
96 preVM = ''
97 mkdir -p "$out"
98 diskImage="$(pwd)/qimage"
99 ${pkgs.vmTools.qemu}/bin/qemu-img create -f raw "$diskImage" 100M
100 '';
101
102 postVM = ''
103 echo "creating VirtualBox disk image..."
104 ${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -O vdi \
105 "$diskImage" "$out/disk.vdi"
106 '';
107
108 buildInputs = [ pkgs.utillinux pkgs.perl ];
109 } ''
110 ${pkgs.parted}/sbin/parted /dev/vda mklabel msdos
111 ${pkgs.parted}/sbin/parted /dev/vda -- mkpart primary ext2 1M -1s
112 . /sys/class/block/vda1/uevent
113 mknod /dev/vda1 b $MAJOR $MINOR
114
115 ${pkgs.e2fsprogs}/sbin/mkfs.ext4 /dev/vda1
116 ${pkgs.e2fsprogs}/sbin/tune2fs -c 0 -i 0 /dev/vda1
117 mkdir /mnt
118 mount /dev/vda1 /mnt
119 cp "${cfg.system.build.kernel}/bzImage" /mnt/linux
120 cp "${cfg.system.build.initialRamdisk}/initrd" /mnt/initrd
121
122 ${pkgs.grub2}/bin/grub-install --boot-directory=/mnt /dev/vda
123
124 cat > /mnt/grub/grub.cfg <<GRUB
125 set root=hd0,1
126 linux /linux ${concatStringsSep " " cfg.boot.kernelParams}
127 initrd /initrd
128 boot
129 GRUB
130 umount /mnt
131 '');
132
133 createVM = name: attrs: let
134 mkFlags = concatStringsSep " ";
135
136 sharePath = "/home/alice/vboxshare-${name}";
137
138 createFlags = mkFlags [
139 "--ostype Linux26"
140 "--register"
141 ];
142
143 vmFlags = mkFlags ([
144 "--uart1 0x3F8 4"
145 "--uartmode1 client /run/virtualbox-log-${name}.sock"
146 "--memory 768"
147 "--audio none"
148 ] ++ (attrs.vmFlags or []));
149
150 controllerFlags = mkFlags [
151 "--name SATA"
152 "--add sata"
153 "--bootable on"
154 "--hostiocache on"
155 ];
156
157 diskFlags = mkFlags [
158 "--storagectl SATA"
159 "--port 0"
160 "--device 0"
161 "--type hdd"
162 "--mtype immutable"
163 "--medium ${testVM name attrs}/disk.vdi"
164 ];
165
166 sharedFlags = mkFlags [
167 "--name vboxshare"
168 "--hostpath ${sharePath}"
169 ];
170
171 nixstoreFlags = mkFlags [
172 "--name nixstore"
173 "--hostpath /nix/store"
174 "--readonly"
175 ];
176 in {
177 machine = {
178 systemd.sockets."vboxtestlog-${name}" = {
179 description = "VirtualBox Test Machine Log Socket For ${name}";
180 wantedBy = [ "sockets.target" ];
181 before = [ "multi-user.target" ];
182 socketConfig.ListenStream = "/run/virtualbox-log-${name}.sock";
183 socketConfig.Accept = true;
184 };
185
186 systemd.services."vboxtestlog-${name}@" = {
187 description = "VirtualBox Test Machine Log For ${name}";
188 serviceConfig.StandardInput = "socket";
189 serviceConfig.StandardOutput = "syslog";
190 serviceConfig.SyslogIdentifier = "GUEST-${name}";
191 serviceConfig.ExecStart = "${pkgs.coreutils}/bin/cat";
192 };
193 };
194
195 testSubs = ''
196 my ${"$" + name}_sharepath = '${sharePath}';
197
198 sub checkRunning_${name} {
199 my $cmd = 'VBoxManage list runningvms | grep -q "^\"${name}\""';
200 my ($status, $out) = $machine->execute(ru $cmd);
201 return $status == 0;
202 }
203
204 sub cleanup_${name} {
205 $machine->execute(ru "VBoxManage controlvm ${name} poweroff")
206 if checkRunning_${name};
207 $machine->succeed("rm -rf ${sharePath}");
208 $machine->succeed("mkdir -p ${sharePath}");
209 $machine->succeed("chown alice.users ${sharePath}");
210 }
211
212 sub createVM_${name} {
213 vbm("createvm --name ${name} ${createFlags}");
214 vbm("modifyvm ${name} ${vmFlags}");
215 vbm("setextradata ${name} VBoxInternal/PDM/HaltOnReset 1");
216 vbm("storagectl ${name} ${controllerFlags}");
217 vbm("storageattach ${name} ${diskFlags}");
218 vbm("sharedfolder add ${name} ${sharedFlags}");
219 vbm("sharedfolder add ${name} ${nixstoreFlags}");
220 cleanup_${name};
221
222 ${mkLog "$HOME/VirtualBox VMs/${name}/Logs/VBox.log" "HOST-${name}"}
223 }
224
225 sub destroyVM_${name} {
226 cleanup_${name};
227 vbm("unregistervm ${name} --delete");
228 }
229
230 sub waitForVMBoot_${name} {
231 $machine->execute(ru(
232 'set -e; i=0; '.
233 'while ! test -e ${sharePath}/boot-done; do '.
234 'sleep 10; i=$(($i + 10)); [ $i -le 3600 ]; '.
235 'VBoxManage list runningvms | grep -q "^\"${name}\""; '.
236 'done'
237 ));
238 }
239
240 sub waitForIP_${name} ($) {
241 my $property = "/VirtualBox/GuestInfo/Net/$_[0]/V4/IP";
242 my $getip = "VBoxManage guestproperty get ${name} $property | ".
243 "sed -n -e 's/^Value: //p'";
244 my $ip = $machine->succeed(ru(
245 'for i in $(seq 1000); do '.
246 'if ipaddr="$('.$getip.')" && [ -n "$ipaddr" ]; then '.
247 'echo "$ipaddr"; exit 0; '.
248 'fi; '.
249 'sleep 1; '.
250 'done; '.
251 'echo "Could not get IPv4 address for ${name}!" >&2; '.
252 'exit 1'
253 ));
254 chomp $ip;
255 return $ip;
256 }
257
258 sub waitForStartup_${name} {
259 for (my $i = 0; $i <= 120; $i += 10) {
260 $machine->sleep(10);
261 return if checkRunning_${name};
262 eval { $_[0]->() } if defined $_[0];
263 }
264 die "VirtualBox VM didn't start up within 2 minutes";
265 }
266
267 sub waitForShutdown_${name} {
268 for (my $i = 0; $i <= 120; $i += 10) {
269 $machine->sleep(10);
270 return unless checkRunning_${name};
271 }
272 die "VirtualBox VM didn't shut down within 2 minutes";
273 }
274
275 sub shutdownVM_${name} {
276 $machine->succeed(ru "touch ${sharePath}/shutdown");
277 $machine->execute(
278 'set -e; i=0; '.
279 'while test -e ${sharePath}/shutdown '.
280 ' -o -e ${sharePath}/boot-done; do '.
281 'sleep 1; i=$(($i + 1)); [ $i -le 3600 ]; '.
282 'done'
283 );
284 waitForShutdown_${name};
285 }
286 '';
287 };
288
289 hostonlyVMFlags = [
290 "--nictype1 virtio"
291 "--nictype2 virtio"
292 "--nic2 hostonly"
293 "--hostonlyadapter2 vboxnet0"
294 ];
295
296 dhcpScript = pkgs: ''
297 ${pkgs.dhcp}/bin/dhclient \
298 -lf /run/dhcp.leases \
299 -pf /run/dhclient.pid \
300 -v eth0 eth1
301
302 otherIP="$(${pkgs.netcat}/bin/nc -l 1234 || :)"
303 ${pkgs.iputils}/bin/ping -I eth1 -c1 "$otherIP"
304 echo "$otherIP reachable" | ${pkgs.netcat}/bin/nc -l 5678 || :
305 '';
306
307 sysdDetectVirt = pkgs: ''
308 ${pkgs.systemd}/bin/systemd-detect-virt > /mnt-root/result
309 '';
310
311 vboxVMs = mapAttrs createVM {
312 simple = {};
313
314 detectvirt.vmScript = sysdDetectVirt;
315
316 test1.vmFlags = hostonlyVMFlags;
317 test1.vmScript = dhcpScript;
318
319 test2.vmFlags = hostonlyVMFlags;
320 test2.vmScript = dhcpScript;
321
322 headless.virtualisation.virtualbox.headless = true;
323 headless.services.xserver.enable = false;
324 };
325
326 mkVBoxTest = name: testScript: makeTest {
327 name = "virtualbox-${name}";
328
329 machine = { lib, config, ... }: {
330 imports = let
331 mkVMConf = name: val: val.machine // { key = "${name}-config"; };
332 vmConfigs = mapAttrsToList mkVMConf vboxVMs;
333 in [ ./common/user-account.nix ./common/x11.nix ] ++ vmConfigs;
334 virtualisation.memorySize = 2048;
335 virtualisation.virtualbox.host.enable = true;
336 services.xserver.displayManager.auto.user = "alice";
337 users.extraUsers.alice.extraGroups = let
338 inherit (config.virtualisation.virtualbox.host) enableHardening;
339 in lib.mkIf enableHardening (lib.singleton "vboxusers");
340 };
341
342 testScript = ''
343 sub ru ($) {
344 my $esc = $_[0] =~ s/'/'\\${"'"}'/gr;
345 return "su - alice -c '$esc'";
346 }
347
348 sub vbm {
349 $machine->succeed(ru("VBoxManage ".$_[0]));
350 };
351
352 sub removeUUIDs {
353 return join("\n", grep { $_ !~ /^UUID:/ } split(/\n/, $_[0]))."\n";
354 }
355
356 ${concatStrings (mapAttrsToList (_: getAttr "testSubs") vboxVMs)}
357
358 $machine->waitForX;
359
360 ${mkLog "$HOME/.config/VirtualBox/VBoxSVC.log" "HOST-SVC"}
361
362 ${testScript}
363 '';
364
365 meta = with pkgs.stdenv.lib.maintainers; {
366 maintainers = [ aszlig wkennington ];
367 };
368 };
369
370in mapAttrs mkVBoxTest {
371 simple-gui = ''
372 createVM_simple;
373 $machine->succeed(ru "VirtualBox &");
374 $machine->waitUntilSucceeds(
375 ru "xprop -name 'Oracle VM VirtualBox Manager'"
376 );
377 $machine->sleep(5);
378 $machine->screenshot("gui_manager_started");
379 $machine->sendKeys("ret");
380 $machine->screenshot("gui_manager_sent_startup");
381 waitForStartup_simple (sub {
382 $machine->sendKeys("ret");
383 });
384 $machine->screenshot("gui_started");
385 waitForVMBoot_simple;
386 $machine->screenshot("gui_booted");
387 shutdownVM_simple;
388 $machine->sleep(5);
389 $machine->screenshot("gui_stopped");
390 $machine->sendKeys("ctrl-q");
391 $machine->sleep(5);
392 $machine->screenshot("gui_manager_stopped");
393 destroyVM_simple;
394 '';
395
396 simple-cli = ''
397 createVM_simple;
398 vbm("startvm simple");
399 waitForStartup_simple;
400 $machine->screenshot("cli_started");
401 waitForVMBoot_simple;
402 $machine->screenshot("cli_booted");
403
404 $machine->nest("Checking for privilege escalation", sub {
405 $machine->fail("test -e '/root/VirtualBox VMs'");
406 $machine->fail("test -e '/root/.config/VirtualBox'");
407 $machine->succeed("test -e '/home/alice/VirtualBox VMs'");
408 });
409
410 shutdownVM_simple;
411 destroyVM_simple;
412 '';
413
414 headless = ''
415 createVM_headless;
416 $machine->succeed(ru("VBoxHeadless --startvm headless & disown %1"));
417 waitForStartup_headless;
418 waitForVMBoot_headless;
419 shutdownVM_headless;
420 destroyVM_headless;
421 '';
422
423 host-usb-permissions = ''
424 my $userUSB = removeUUIDs vbm("list usbhost");
425 print STDERR $userUSB;
426 my $rootUSB = removeUUIDs $machine->succeed("VBoxManage list usbhost");
427 print STDERR $rootUSB;
428
429 die "USB host devices differ for root and normal user"
430 if $userUSB ne $rootUSB;
431 die "No USB host devices found" if $userUSB =~ /<none>/;
432 '';
433
434 systemd-detect-virt = ''
435 createVM_detectvirt;
436 vbm("startvm detectvirt");
437 waitForStartup_detectvirt;
438 waitForVMBoot_detectvirt;
439 shutdownVM_detectvirt;
440 my $result = $machine->succeed("cat '$detectvirt_sharepath/result'");
441 chomp $result;
442 destroyVM_detectvirt;
443 die "systemd-detect-virt returned \"$result\" instead of \"oracle\""
444 if $result ne "oracle";
445 '';
446
447 net-hostonlyif = ''
448 createVM_test1;
449 createVM_test2;
450
451 vbm("startvm test1");
452 waitForStartup_test1;
453 waitForVMBoot_test1;
454
455 vbm("startvm test2");
456 waitForStartup_test2;
457 waitForVMBoot_test2;
458
459 $machine->screenshot("net_booted");
460
461 my $test1IP = waitForIP_test1 1;
462 my $test2IP = waitForIP_test2 1;
463
464 $machine->succeed("echo '$test2IP' | nc '$test1IP' 1234");
465 $machine->succeed("echo '$test1IP' | nc '$test2IP' 1234");
466
467 $machine->waitUntilSucceeds("nc '$test1IP' 5678 >&2");
468 $machine->waitUntilSucceeds("nc '$test2IP' 5678 >&2");
469
470 shutdownVM_test1;
471 shutdownVM_test2;
472
473 destroyVM_test1;
474 destroyVM_test2;
475 '';
476}