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. 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
50 serviceConfig.KillSignal = "SIGHUP";
59 initrdBackdoor = lib.mkEnableOption ''
60 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
74 assertion = cfg.initrdBackdoor -> config.boot.initrd.systemd.enable;
76 testing.initrdBackdoor requires boot.initrd.systemd.enable to be enabled.
81 systemd.services.backdoor = lib.mkMerge [
84 wantedBy = [ "multi-user.target" ];
88 boot.initrd.systemd = lib.mkMerge [
90 contents."/etc/systemd/journald.conf".text = ''
93 TTYPath=/dev/${qemu-common.qemuSerialDevice}
97 extraConfig = config.systemd.extraConfig;
100 (lib.mkIf cfg.initrdBackdoor {
101 # Implemented in machine.switch_root(). Suppress the unit by
102 # making it a noop without removing it, which would break
103 # initrd-parse-etc.service
104 services.initrd-cleanup.serviceConfig.ExecStart = [
111 services.backdoor = lib.mkMerge [
114 # TODO: Both stage 1 and stage 2 should use these same
115 # settings. But a lot of existing tests rely on
116 # backdoor.service having default orderings,
117 # e.g. systemd-boot.update relies on /boot being mounted
118 # as soon as backdoor starts. But it can be useful for
119 # backdoor to start even earlier.
120 wantedBy = [ "sysinit.target" ];
121 unitConfig.DefaultDependencies = false;
122 conflicts = [ "shutdown.target" "initrd-switch-root.target" ];
123 before = [ "shutdown.target" "initrd-switch-root.target" ];
128 "${pkgs.coreutils}/bin/env"
133 # Prevent agetty from being instantiated on the serial device, since it
134 # interferes with the backdoor (writes to it will randomly fail
135 # with EIO). Likewise for hvc0.
136 systemd.services."serial-getty@${qemu-common.qemuSerialDevice}".enable = false;
137 systemd.services."serial-getty@hvc0".enable = false;
139 # Only set these settings when the options exist. Some tests (e.g. those
140 # that do not specify any nodes, or an empty attr set as nodes) will not
141 # have the QEMU module loaded and thuse these options can't and should not
143 virtualisation = lib.optionalAttrs (options ? virtualisation.qemu) {
145 # Only use a serial console, no TTY.
146 # NOTE: optionalAttrs
147 # test-instrumentation.nix appears to be used without qemu-vm.nix, so
148 # we avoid defining consoles if not possible.
149 # TODO: refactor such that test-instrumentation can import qemu-vm
150 # or declare virtualisation.qemu.console option in a module that's always imported
151 consoles = [ qemu-common.qemuSerialDevice ];
152 package = lib.mkDefault pkgs.qemu_test;
156 boot.kernel.sysctl = {
157 "kernel.hung_task_timeout_secs" = 600;
158 # Panic on out-of-memory conditions rather than letting the
159 # OOM killer randomly get rid of processes, since this leads
160 # to failures that are hard to diagnose.
161 "vm.panic_on_oom" = lib.mkDefault 2;
164 boot.kernelParams = [
165 "console=${qemu-common.qemuSerialDevice}"
167 # Panic if an error occurs in stage 1 (rather than waiting for
168 # user intervention).
169 "panic=1" "boot.panic_on_fail"
170 # Using acpi_pm as a clock source causes the guest clock to
171 # slow down under high host load. This is usually a bad
172 # thing, but for VM tests it should provide a bit more
173 # determinism (e.g. if the VM runs at lower speed, then
174 # timeouts in the VM should also be delayed).
175 "clocksource=acpi_pm"
178 # `xwininfo' is used by the test driver to query open windows.
179 environment.systemPackages = [ pkgs.xorg.xwininfo ];
181 # Log everything to the serial console.
182 services.journald.extraConfig =
185 TTYPath=/dev/${qemu-common.qemuSerialDevice}
186 MaxLevelConsole=debug
189 systemd.extraConfig = ''
190 # Don't clobber the console with duplicate systemd messages.
192 # Allow very slow start
193 DefaultTimeoutStartSec=300
194 DefaultDeviceTimeoutSec=300
196 systemd.user.extraConfig = ''
197 # Allow very slow start
198 DefaultTimeoutStartSec=300
199 DefaultDeviceTimeoutSec=300
202 boot.consoleLogLevel = 7;
204 # Prevent tests from accessing the Internet.
205 networking.defaultGateway = mkOverride 150 null;
206 networking.nameservers = mkOverride 150 [ ];
208 system.requiredKernelConfig = with config.lib.kernelConfig; [
209 (isYes "SERIAL_8250_CONSOLE")
210 (isYes "SERIAL_8250")
211 (isEnabled "VIRTIO_CONSOLE")
214 networking.usePredictableInterfaceNames = false;
216 # Make it easy to log in as root when running the test interactively.
217 # This needs to be a file because of a quirk in systemd credentials,
218 # where you cannot specify an empty string as a value. systemd-sysusers
219 # uses credentials to set passwords on users.
220 users.users.root.hashedPasswordFile = mkOverride 150 "${pkgs.writeText "hashed-password.root" ""}";
222 services.displayManager.logToJournal = true;
224 services.logrotate.enable = mkOverride 150 false;
226 # Make sure we use the Guest Agent from the QEMU package for testing
227 # to reduce the closure size required for the tests.
228 services.qemuGuest.package = pkgs.qemu_test.ga;
230 # Squelch warning about unset system.stateVersion
231 system.stateVersion = lib.mkDefault lib.trivial.release;