1# This module allows the test driver to connect to the virtual machine
2# via a root shell attached to port 514.
3
4{ options, config, lib, pkgs, ... }:
5
6with lib;
7with import ../../lib/qemu-flags.nix { inherit pkgs; };
8
9{
10
11 config = {
12
13 systemd.services.backdoor =
14 { wantedBy = [ "multi-user.target" ];
15 requires = [ "dev-hvc0.device" "dev-${qemuSerialDevice}.device" ];
16 after = [ "dev-hvc0.device" "dev-${qemuSerialDevice}.device" ];
17 script =
18 ''
19 export USER=root
20 export HOME=/root
21 export DISPLAY=:0.0
22
23 source /etc/profile
24
25 # Don't use a pager when executing backdoor
26 # actions. Because we use a tty, commands like systemctl
27 # or nix-store get confused into thinking they're running
28 # interactively.
29 export PAGER=
30
31 cd /tmp
32 exec < /dev/hvc0 > /dev/hvc0
33 while ! exec 2> /dev/${qemuSerialDevice}; do sleep 0.1; done
34 echo "connecting to host..." >&2
35 stty -F /dev/hvc0 raw -echo # prevent nl -> cr/nl conversion
36 echo
37 PS1= exec /bin/sh
38 '';
39 serviceConfig.KillSignal = "SIGHUP";
40 };
41
42 # Prevent agetty from being instantiated on the serial device, since it
43 # interferes with the backdoor (writes to it will randomly fail
44 # with EIO). Likewise for hvc0.
45 systemd.services."serial-getty@${qemuSerialDevice}".enable = false;
46 systemd.services."serial-getty@hvc0".enable = false;
47
48 # Only set these settings when the options exist. Some tests (e.g. those
49 # that do not specify any nodes, or an empty attr set as nodes) will not
50 # have the QEMU module loaded and thuse these options can't and should not
51 # be set.
52 virtualisation = lib.optionalAttrs (options ? virtualisation.qemu) {
53 qemu = {
54 # Only use a serial console, no TTY.
55 # NOTE: optionalAttrs
56 # test-instrumentation.nix appears to be used without qemu-vm.nix, so
57 # we avoid defining consoles if not possible.
58 # TODO: refactor such that test-instrumentation can import qemu-vm
59 # or declare virtualisation.qemu.console option in a module that's always imported
60 consoles = [ qemuSerialDevice ];
61 package = lib.mkDefault pkgs.qemu_test;
62 };
63 };
64
65 boot.initrd.preDeviceCommands =
66 ''
67 echo 600 > /proc/sys/kernel/hung_task_timeout_secs
68 '';
69
70 boot.initrd.postDeviceCommands =
71 ''
72 # Using acpi_pm as a clock source causes the guest clock to
73 # slow down under high host load. This is usually a bad
74 # thing, but for VM tests it should provide a bit more
75 # determinism (e.g. if the VM runs at lower speed, then
76 # timeouts in the VM should also be delayed).
77 echo acpi_pm > /sys/devices/system/clocksource/clocksource0/current_clocksource
78 '';
79
80 boot.postBootCommands =
81 ''
82 # Panic on out-of-memory conditions rather than letting the
83 # OOM killer randomly get rid of processes, since this leads
84 # to failures that are hard to diagnose.
85 echo 2 > /proc/sys/vm/panic_on_oom
86 '';
87
88 # Panic if an error occurs in stage 1 (rather than waiting for
89 # user intervention).
90 boot.kernelParams =
91 [ "console=${qemuSerialDevice}" "panic=1" "boot.panic_on_fail" ];
92
93 # `xwininfo' is used by the test driver to query open windows.
94 environment.systemPackages = [ pkgs.xorg.xwininfo ];
95
96 # Log everything to the serial console.
97 services.journald.extraConfig =
98 ''
99 ForwardToConsole=yes
100 MaxLevelConsole=debug
101 '';
102
103 systemd.extraConfig = ''
104 # Don't clobber the console with duplicate systemd messages.
105 ShowStatus=no
106 # Allow very slow start
107 DefaultTimeoutStartSec=300
108 '';
109
110 boot.consoleLogLevel = 7;
111
112 # Prevent tests from accessing the Internet.
113 networking.defaultGateway = mkOverride 150 "";
114 networking.nameservers = mkOverride 150 [ ];
115
116 system.requiredKernelConfig = with config.lib.kernelConfig; [
117 (isYes "SERIAL_8250_CONSOLE")
118 (isYes "SERIAL_8250")
119 (isEnabled "VIRTIO_CONSOLE")
120 ];
121
122 networking.usePredictableInterfaceNames = false;
123
124 # Make it easy to log in as root when running the test interactively.
125 users.users.root.initialHashedPassword = mkOverride 150 "";
126
127 services.xserver.displayManager.job.logToJournal = true;
128
129 # Make sure we use the Guest Agent from the QEMU package for testing
130 # to reduce the closure size required for the tests.
131 services.qemuGuest.package = pkgs.qemu_test.ga;
132 };
133
134}