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 = ''
96 extraConfig = config.systemd.extraConfig;
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 = [
110 services.backdoor = lib.mkMerge [
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" ];
127 "${pkgs.coreutils}/bin/env"
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;
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
142 virtualisation = lib.optionalAttrs (options ? virtualisation.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;
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;
163 boot.kernelParams = [
164 "console=${qemu-common.qemuSerialDevice}"
166 # Panic if an error occurs in stage 1 (rather than waiting for
167 # user intervention).
168 "panic=1" "boot.panic_on_fail"
169 # Using acpi_pm as a clock source causes the guest clock to
170 # slow down under high host load. This is usually a bad
171 # thing, but for VM tests it should provide a bit more
172 # determinism (e.g. if the VM runs at lower speed, then
173 # timeouts in the VM should also be delayed).
174 "clocksource=acpi_pm"
177 # `xwininfo' is used by the test driver to query open windows.
178 environment.systemPackages = [ pkgs.xorg.xwininfo ];
180 # Log everything to the serial console.
181 services.journald.extraConfig =
184 TTYPath=/dev/${qemu-common.qemuSerialDevice}
185 MaxLevelConsole=debug
188 systemd.extraConfig = ''
189 # Don't clobber the console with duplicate systemd messages.
191 # Allow very slow start
192 DefaultTimeoutStartSec=300
193 DefaultDeviceTimeoutSec=300
195 systemd.user.extraConfig = ''
196 # Allow very slow start
197 DefaultTimeoutStartSec=300
198 DefaultDeviceTimeoutSec=300
201 boot.consoleLogLevel = 7;
203 # Prevent tests from accessing the Internet.
204 networking.defaultGateway = mkOverride 150 null;
205 networking.nameservers = mkOverride 150 [ ];
207 system.requiredKernelConfig = with config.lib.kernelConfig; [
208 (isYes "SERIAL_8250_CONSOLE")
209 (isYes "SERIAL_8250")
210 (isEnabled "VIRTIO_CONSOLE")
213 networking.usePredictableInterfaceNames = false;
215 # Make it easy to log in as root when running the test interactively.
216 # This needs to be a file because of a quirk in systemd credentials,
217 # where you cannot specify an empty string as a value. systemd-sysusers
218 # uses credentials to set passwords on users.
219 users.users.root.hashedPasswordFile = mkOverride 150 "${pkgs.writeText "hashed-password.root" ""}";
221 services.displayManager.logToJournal = true;
223 services.logrotate.enable = mkOverride 150 false;
225 # Make sure we use the Guest Agent from the QEMU package for testing
226 # to reduce the closure size required for the tests.
227 services.qemuGuest.package = pkgs.qemu_test.ga;
229 # Squelch warning about unset system.stateVersion
230 system.stateVersion = lib.mkDefault lib.trivial.release;