Release NixOS 23.11
[NixPkgs.git] / nixos / modules / testing / test-instrumentation.nix
blob9ee77cd79a9b1cffbb26ca200786a32340d017cb
1 # This module allows the test driver to connect to the virtual machine
2 # via a root shell attached to port 514.
4 { options, config, lib, pkgs, ... }:
6 with lib;
8 let
9   cfg = config.testing;
11   qemu-common = import ../../lib/qemu-common.nix { inherit lib pkgs; };
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
22         if [[ -e /etc/profile ]]; then
23             source /etc/profile
24         fi
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=
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.
46         PS1= exec /usr/bin/env bash --norc /dev/hvc0
47       '';
48       serviceConfig.KillSignal = "SIGHUP";
49   };
55   options.testing = {
57     initrdBackdoor = lib.mkEnableOption (lib.mdDoc ''
58       enable backdoor.service in initrd. Requires
59       boot.initrd.systemd.enable to be enabled. Boot will pause in
60       stage 1 at initrd.target, and will listen for commands from the
61       Machine python interface, just like stage 2 normally does. This
62       enables commands to be sent to test and debug stage 1. Use
63       machine.switch_root() to leave stage 1 and proceed to stage 2.
64     '');
66   };
68   config = {
70     assertions = [
71       {
72         assertion = cfg.initrdBackdoor -> config.boot.initrd.systemd.enable;
73         message = ''
74           testing.initrdBackdoor requires boot.initrd.systemd.enable to be enabled.
75         '';
76       }
77     ];
79     systemd.services.backdoor = lib.mkMerge [
80       backdoorService
81       {
82         wantedBy = [ "multi-user.target" ];
83       }
84     ];
86     boot.initrd.systemd = lib.mkMerge [
87       {
88         contents."/etc/systemd/journald.conf".text = ''
89           [Journal]
90           ForwardToConsole=yes
91           MaxLevelConsole=debug
92         '';
94         extraConfig = config.systemd.extraConfig;
95       }
97       (lib.mkIf cfg.initrdBackdoor {
98         # Implemented in machine.switch_root(). Suppress the unit by
99         # making it a noop without removing it, which would break
100         # initrd-parse-etc.service
101         services.initrd-cleanup.serviceConfig.ExecStart = [
102           # Reset
103           ""
104           # noop
105           "/bin/true"
106         ];
108         services.backdoor = lib.mkMerge [
109           backdoorService
110           {
111             # TODO: Both stage 1 and stage 2 should use these same
112             # settings. But a lot of existing tests rely on
113             # backdoor.service having default orderings,
114             # e.g. systemd-boot.update relies on /boot being mounted
115             # as soon as backdoor starts. But it can be useful for
116             # backdoor to start even earlier.
117             wantedBy = [ "sysinit.target" ];
118             unitConfig.DefaultDependencies = false;
119             conflicts = [ "shutdown.target" "initrd-switch-root.target" ];
120             before = [ "shutdown.target" "initrd-switch-root.target" ];
121           }
122         ];
124         contents."/usr/bin/env".source = "${pkgs.coreutils}/bin/env";
125       })
126     ];
128     # Prevent agetty from being instantiated on the serial device, since it
129     # interferes with the backdoor (writes to it will randomly fail
130     # with EIO).  Likewise for hvc0.
131     systemd.services."serial-getty@${qemu-common.qemuSerialDevice}".enable = false;
132     systemd.services."serial-getty@hvc0".enable = false;
134     # Only set these settings when the options exist. Some tests (e.g. those
135     # that do not specify any nodes, or an empty attr set as nodes) will not
136     # have the QEMU module loaded and thuse these options can't and should not
137     # be set.
138     virtualisation = lib.optionalAttrs (options ? virtualisation.qemu) {
139       qemu = {
140         # Only use a serial console, no TTY.
141         # NOTE: optionalAttrs
142         #       test-instrumentation.nix appears to be used without qemu-vm.nix, so
143         #       we avoid defining consoles if not possible.
144         # TODO: refactor such that test-instrumentation can import qemu-vm
145         #       or declare virtualisation.qemu.console option in a module that's always imported
146         consoles = [ qemu-common.qemuSerialDevice ];
147         package  = lib.mkDefault pkgs.qemu_test;
148       };
149     };
151     boot.kernel.sysctl = {
152       "kernel.hung_task_timeout_secs" = 600;
153       # Panic on out-of-memory conditions rather than letting the
154       # OOM killer randomly get rid of processes, since this leads
155       # to failures that are hard to diagnose.
156       "vm.panic_on_oom" = lib.mkDefault 2;
157     };
159     boot.kernelParams = [
160       "console=${qemu-common.qemuSerialDevice}"
161       # Panic if an error occurs in stage 1 (rather than waiting for
162       # user intervention).
163       "panic=1" "boot.panic_on_fail"
164       # Using acpi_pm as a clock source causes the guest clock to
165       # slow down under high host load.  This is usually a bad
166       # thing, but for VM tests it should provide a bit more
167       # determinism (e.g. if the VM runs at lower speed, then
168       # timeouts in the VM should also be delayed).
169       "clock=acpi_pm"
170     ];
172     # `xwininfo' is used by the test driver to query open windows.
173     environment.systemPackages = [ pkgs.xorg.xwininfo ];
175     # Log everything to the serial console.
176     services.journald.extraConfig =
177       ''
178         ForwardToConsole=yes
179         MaxLevelConsole=debug
180       '';
182     systemd.extraConfig = ''
183       # Don't clobber the console with duplicate systemd messages.
184       ShowStatus=no
185       # Allow very slow start
186       DefaultTimeoutStartSec=300
187       DefaultDeviceTimeoutSec=300
188     '';
189     systemd.user.extraConfig = ''
190       # Allow very slow start
191       DefaultTimeoutStartSec=300
192       DefaultDeviceTimeoutSec=300
193     '';
195     boot.consoleLogLevel = 7;
197     # Prevent tests from accessing the Internet.
198     networking.defaultGateway = mkOverride 150 null;
199     networking.nameservers = mkOverride 150 [ ];
201     system.requiredKernelConfig = with config.lib.kernelConfig; [
202       (isYes "SERIAL_8250_CONSOLE")
203       (isYes "SERIAL_8250")
204       (isEnabled "VIRTIO_CONSOLE")
205     ];
207     networking.usePredictableInterfaceNames = false;
209     # Make it easy to log in as root when running the test interactively.
210     users.users.root.initialHashedPassword = mkOverride 150 "";
212     services.xserver.displayManager.job.logToJournal = true;
214     # Make sure we use the Guest Agent from the QEMU package for testing
215     # to reduce the closure size required for the tests.
216     services.qemuGuest.package = pkgs.qemu_test.ga;
218     # Squelch warning about unset system.stateVersion
219     system.stateVersion = lib.mkDefault lib.trivial.release;
220   };