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 # The following line is essential since it signals to
40 # the test driver that the shell is ready.
41 # See: the connect method in the Machine class.
42 echo "Spawning backdoor root shell..."
43 # Passing the terminal device makes bash run non-interactively.
44 # Otherwise we get errors on the terminal because bash tries to
45 # setup things like job control.
46 # Note: calling bash explicitly here instead of sh makes sure that
47 # we can also run non-NixOS guests during tests.
48 PS1= exec /usr/bin/env bash --norc /dev/hvc0
49 '';
50 serviceConfig.KillSignal = "SIGHUP";
51 };
52
53 # Prevent agetty from being instantiated on the serial device, since it
54 # interferes with the backdoor (writes to it will randomly fail
55 # with EIO). Likewise for hvc0.
56 systemd.services."serial-getty@${qemu-common.qemuSerialDevice}".enable = false;
57 systemd.services."serial-getty@hvc0".enable = false;
58
59 # Only set these settings when the options exist. Some tests (e.g. those
60 # that do not specify any nodes, or an empty attr set as nodes) will not
61 # have the QEMU module loaded and thuse these options can't and should not
62 # be set.
63 virtualisation = lib.optionalAttrs (options ? virtualisation.qemu) {
64 qemu = {
65 # Only use a serial console, no TTY.
66 # NOTE: optionalAttrs
67 # test-instrumentation.nix appears to be used without qemu-vm.nix, so
68 # we avoid defining consoles if not possible.
69 # TODO: refactor such that test-instrumentation can import qemu-vm
70 # or declare virtualisation.qemu.console option in a module that's always imported
71 consoles = [ qemu-common.qemuSerialDevice ];
72 package = lib.mkDefault pkgs.qemu_test;
73 };
74 };
75
76 boot.kernel.sysctl = {
77 "kernel.hung_task_timeout_secs" = 600;
78 # Panic on out-of-memory conditions rather than letting the
79 # OOM killer randomly get rid of processes, since this leads
80 # to failures that are hard to diagnose.
81 "vm.panic_on_oom" = lib.mkDefault 2;
82 };
83
84 boot.kernelParams = [
85 "console=${qemu-common.qemuSerialDevice}"
86 # Panic if an error occurs in stage 1 (rather than waiting for
87 # user intervention).
88 "panic=1" "boot.panic_on_fail"
89 # Using acpi_pm as a clock source causes the guest clock to
90 # slow down under high host load. This is usually a bad
91 # thing, but for VM tests it should provide a bit more
92 # determinism (e.g. if the VM runs at lower speed, then
93 # timeouts in the VM should also be delayed).
94 "clock=acpi_pm"
95 ];
96
97 # `xwininfo' is used by the test driver to query open windows.
98 environment.systemPackages = [ pkgs.xorg.xwininfo ];
99
100 # Log everything to the serial console.
101 services.journald.extraConfig =
102 ''
103 ForwardToConsole=yes
104 MaxLevelConsole=debug
105 '';
106
107 boot.initrd.systemd.contents."/etc/systemd/journald.conf".text = ''
108 [Journal]
109 ForwardToConsole=yes
110 MaxLevelConsole=debug
111 '';
112
113 systemd.extraConfig = ''
114 # Don't clobber the console with duplicate systemd messages.
115 ShowStatus=no
116 # Allow very slow start
117 DefaultTimeoutStartSec=300
118 '';
119 systemd.user.extraConfig = ''
120 # Allow very slow start
121 DefaultTimeoutStartSec=300
122 '';
123
124 boot.initrd.systemd.extraConfig = config.systemd.extraConfig;
125
126 boot.consoleLogLevel = 7;
127
128 # Prevent tests from accessing the Internet.
129 networking.defaultGateway = mkOverride 150 "";
130 networking.nameservers = mkOverride 150 [ ];
131
132 system.requiredKernelConfig = with config.lib.kernelConfig; [
133 (isYes "SERIAL_8250_CONSOLE")
134 (isYes "SERIAL_8250")
135 (isEnabled "VIRTIO_CONSOLE")
136 ];
137
138 networking.usePredictableInterfaceNames = false;
139
140 # Make it easy to log in as root when running the test interactively.
141 users.users.root.initialHashedPassword = mkOverride 150 "";
142
143 services.xserver.displayManager.job.logToJournal = true;
144
145 # Make sure we use the Guest Agent from the QEMU package for testing
146 # to reduce the closure size required for the tests.
147 services.qemuGuest.package = pkgs.qemu_test.ga;
148
149 # Squelch warning about unset system.stateVersion
150 system.stateVersion = lib.mkDefault lib.trivial.release;
151 };
152
153}