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, ... }:
11 qemu-common = import ../../lib/qemu-common.nix { inherit lib pkgs; };
14 requires = [ "dev-hvc0.device" "dev-${qemu-common.qemuSerialDevice}.device" ];
15 after = [ "dev-hvc0.device" "dev-${qemu-common.qemuSerialDevice}.device" ];
22 if [[ -e /etc/profile ]]; then
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
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
48 serviceConfig.KillSignal = "SIGHUP";
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.
72 assertion = cfg.initrdBackdoor -> config.boot.initrd.systemd.enable;
74 testing.initrdBackdoor requires boot.initrd.systemd.enable to be enabled.
79 systemd.services.backdoor = lib.mkMerge [
82 wantedBy = [ "multi-user.target" ];
86 boot.initrd.systemd = lib.mkMerge [
88 contents."/etc/systemd/journald.conf".text = ''
94 extraConfig = config.systemd.extraConfig;
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 = [
108 services.backdoor = lib.mkMerge [
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" ];
124 contents."/usr/bin/env".source = "${pkgs.coreutils}/bin/env";
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
138 virtualisation = lib.optionalAttrs (options ? virtualisation.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;
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;
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).
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 =
179 MaxLevelConsole=debug
182 systemd.extraConfig = ''
183 # Don't clobber the console with duplicate systemd messages.
185 # Allow very slow start
186 DefaultTimeoutStartSec=300
187 DefaultDeviceTimeoutSec=300
189 systemd.user.extraConfig = ''
190 # Allow very slow start
191 DefaultTimeoutStartSec=300
192 DefaultDeviceTimeoutSec=300
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")
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;