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