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 cfg = config.testing;
10
11 qemu-common = import ../../lib/qemu-common.nix { inherit lib pkgs; };
12
13 backdoorService = {
14 requires = [ "dev-hvc0.device" "dev-${qemu-common.qemuSerialDevice}.device" ];
15 after = [ "dev-hvc0.device" "dev-${qemu-common.qemuSerialDevice}.device" ];
16 script =
17 ''
18 export USER=root
19 export HOME=/root
20 export DISPLAY=:0.0
21
22 if [[ -e /etc/profile ]]; then
23 source /etc/profile
24 fi
25
26 # Don't use a pager when executing backdoor
27 # actions. Because we use a tty, commands like systemctl
28 # or nix-store get confused into thinking they're running
29 # interactively.
30 export PAGER=
31
32 cd /tmp
33 exec < /dev/hvc0 > /dev/hvc0
34 while ! exec 2> /dev/${qemu-common.qemuSerialDevice}; do sleep 0.1; done
35 echo "connecting to host..." >&2
36 stty -F /dev/hvc0 raw -echo # prevent nl -> cr/nl conversion
37 # The following line is essential since it signals to
38 # the test driver that the shell is ready.
39 # See: the connect method in the Machine class.
40 echo "Spawning backdoor root shell..."
41 # Passing the terminal device makes bash run non-interactively.
42 # Otherwise we get errors on the terminal because bash tries to
43 # setup things like job control.
44 # Note: calling bash explicitly here instead of sh makes sure that
45 # we can also run non-NixOS guests during tests. This, however, is
46 # mostly futureproofing as the test instrumentation is still very
47 # tightly coupled to NixOS.
48 PS1= exec ${pkgs.coreutils}/bin/env bash --norc /dev/hvc0
49 '';
50 serviceConfig.KillSignal = "SIGHUP";
51 };
52
53in
54
55{
56
57 options.testing = {
58
59 initrdBackdoor = lib.mkEnableOption ''
60 enable backdoor.service in initrd. Requires
61 boot.initrd.systemd.enable to be enabled. Boot will pause in
62 stage 1 at initrd.target, and will listen for commands from the
63 Machine python interface, just like stage 2 normally does. This
64 enables commands to be sent to test and debug stage 1. Use
65 machine.switch_root() to leave stage 1 and proceed to stage 2.
66 '';
67
68 };
69
70 config = {
71
72 assertions = [
73 {
74 assertion = cfg.initrdBackdoor -> config.boot.initrd.systemd.enable;
75 message = ''
76 testing.initrdBackdoor requires boot.initrd.systemd.enable to be enabled.
77 '';
78 }
79 ];
80
81 systemd.services.backdoor = lib.mkMerge [
82 backdoorService
83 {
84 wantedBy = [ "multi-user.target" ];
85 }
86 ];
87
88 boot.initrd.systemd = lib.mkMerge [
89 {
90 contents."/etc/systemd/journald.conf".text = ''
91 [Journal]
92 ForwardToConsole=yes
93 MaxLevelConsole=debug
94 '';
95
96 extraConfig = config.systemd.extraConfig;
97 }
98
99 (lib.mkIf cfg.initrdBackdoor {
100 # Implemented in machine.switch_root(). Suppress the unit by
101 # making it a noop without removing it, which would break
102 # initrd-parse-etc.service
103 services.initrd-cleanup.serviceConfig.ExecStart = [
104 # Reset
105 ""
106 # noop
107 "/bin/true"
108 ];
109
110 services.backdoor = lib.mkMerge [
111 backdoorService
112 {
113 # TODO: Both stage 1 and stage 2 should use these same
114 # settings. But a lot of existing tests rely on
115 # backdoor.service having default orderings,
116 # e.g. systemd-boot.update relies on /boot being mounted
117 # as soon as backdoor starts. But it can be useful for
118 # backdoor to start even earlier.
119 wantedBy = [ "sysinit.target" ];
120 unitConfig.DefaultDependencies = false;
121 conflicts = [ "shutdown.target" "initrd-switch-root.target" ];
122 before = [ "shutdown.target" "initrd-switch-root.target" ];
123 }
124 ];
125
126 storePaths = [
127 "${pkgs.coreutils}/bin/env"
128 ];
129 })
130 ];
131
132 # Prevent agetty from being instantiated on the serial device, since it
133 # interferes with the backdoor (writes to it will randomly fail
134 # with EIO). Likewise for hvc0.
135 systemd.services."serial-getty@${qemu-common.qemuSerialDevice}".enable = false;
136 systemd.services."serial-getty@hvc0".enable = false;
137
138 # Only set these settings when the options exist. Some tests (e.g. those
139 # that do not specify any nodes, or an empty attr set as nodes) will not
140 # have the QEMU module loaded and thuse these options can't and should not
141 # be set.
142 virtualisation = lib.optionalAttrs (options ? virtualisation.qemu) {
143 qemu = {
144 # Only use a serial console, no TTY.
145 # NOTE: optionalAttrs
146 # test-instrumentation.nix appears to be used without qemu-vm.nix, so
147 # we avoid defining consoles if not possible.
148 # TODO: refactor such that test-instrumentation can import qemu-vm
149 # or declare virtualisation.qemu.console option in a module that's always imported
150 consoles = [ qemu-common.qemuSerialDevice ];
151 package = lib.mkDefault pkgs.qemu_test;
152 };
153 };
154
155 boot.kernel.sysctl = {
156 "kernel.hung_task_timeout_secs" = 600;
157 # Panic on out-of-memory conditions rather than letting the
158 # OOM killer randomly get rid of processes, since this leads
159 # to failures that are hard to diagnose.
160 "vm.panic_on_oom" = lib.mkDefault 2;
161 };
162
163 boot.kernelParams = [
164 "console=${qemu-common.qemuSerialDevice}"
165 # Panic if an error occurs in stage 1 (rather than waiting for
166 # user intervention).
167 "panic=1" "boot.panic_on_fail"
168 # Using acpi_pm as a clock source causes the guest clock to
169 # slow down under high host load. This is usually a bad
170 # thing, but for VM tests it should provide a bit more
171 # determinism (e.g. if the VM runs at lower speed, then
172 # timeouts in the VM should also be delayed).
173 "clocksource=acpi_pm"
174 ];
175
176 # `xwininfo' is used by the test driver to query open windows.
177 environment.systemPackages = [ pkgs.xorg.xwininfo ];
178
179 # Log everything to the serial console.
180 services.journald.extraConfig =
181 ''
182 ForwardToConsole=yes
183 MaxLevelConsole=debug
184 '';
185
186 systemd.extraConfig = ''
187 # Don't clobber the console with duplicate systemd messages.
188 ShowStatus=no
189 # Allow very slow start
190 DefaultTimeoutStartSec=300
191 DefaultDeviceTimeoutSec=300
192 '';
193 systemd.user.extraConfig = ''
194 # Allow very slow start
195 DefaultTimeoutStartSec=300
196 DefaultDeviceTimeoutSec=300
197 '';
198
199 boot.consoleLogLevel = 7;
200
201 # Prevent tests from accessing the Internet.
202 networking.defaultGateway = mkOverride 150 null;
203 networking.nameservers = mkOverride 150 [ ];
204
205 system.requiredKernelConfig = with config.lib.kernelConfig; [
206 (isYes "SERIAL_8250_CONSOLE")
207 (isYes "SERIAL_8250")
208 (isEnabled "VIRTIO_CONSOLE")
209 ];
210
211 networking.usePredictableInterfaceNames = false;
212
213 # Make it easy to log in as root when running the test interactively.
214 # This needs to be a file because of a quirk in systemd credentials,
215 # where you cannot specify an empty string as a value. systemd-sysusers
216 # uses credentials to set passwords on users.
217 users.users.root.hashedPasswordFile = mkOverride 150 "${pkgs.writeText "hashed-password.root" ""}";
218
219 services.displayManager.logToJournal = true;
220
221 services.logrotate.enable = mkOverride 150 false;
222
223 # Make sure we use the Guest Agent from the QEMU package for testing
224 # to reduce the closure size required for the tests.
225 services.qemuGuest.package = pkgs.qemu_test.ga;
226
227 # Squelch warning about unset system.stateVersion
228 system.stateVersion = lib.mkDefault lib.trivial.release;
229 };
230
231}