1# This module allows the test driver to connect to the virtual machine
2# via a root shell attached to port 514.
3
4{ config, lib, pkgs, ... }:
5
6with lib;
7
8let kernel = config.boot.kernelPackages.kernel; in
9
10{
11
12 # This option is a dummy that if used in conjunction with
13 # modules/virtualisation/qemu-vm.nix gets merged with the same option defined
14 # there and only is declared here because some modules use
15 # test-instrumentation.nix but not qemu-vm.nix.
16 #
17 # One particular example are the boot tests where we want instrumentation
18 # within the images but not other stuff like setting up 9p filesystems.
19 options.virtualisation.qemu.program = mkOption { type = types.path; };
20
21 config = {
22
23 systemd.services.backdoor =
24 { wantedBy = [ "multi-user.target" ];
25 requires = [ "dev-hvc0.device" "dev-ttyS0.device" ];
26 after = [ "dev-hvc0.device" "dev-ttyS0.device" ];
27 script =
28 ''
29 export USER=root
30 export HOME=/root
31 export DISPLAY=:0.0
32
33 source /etc/profile
34
35 # Don't use a pager when executing backdoor
36 # actions. Because we use a tty, commands like systemctl
37 # or nix-store get confused into thinking they're running
38 # interactively.
39 export PAGER=
40
41 cd /tmp
42 exec < /dev/hvc0 > /dev/hvc0
43 while ! exec 2> /dev/ttyS0; do sleep 0.1; done
44 echo "connecting to host..." >&2
45 stty -F /dev/hvc0 raw -echo # prevent nl -> cr/nl conversion
46 echo
47 PS1= exec /bin/sh
48 '';
49 serviceConfig.KillSignal = "SIGHUP";
50 };
51
52 # Prevent agetty from being instantiated on ttyS0, since it
53 # interferes with the backdoor (writes to ttyS0 will randomly fail
54 # with EIO). Likewise for hvc0.
55 systemd.services."serial-getty@ttyS0".enable = false;
56 systemd.services."serial-getty@hvc0".enable = false;
57
58 boot.initrd.preDeviceCommands =
59 ''
60 echo 600 > /proc/sys/kernel/hung_task_timeout_secs
61 '';
62
63 boot.initrd.postDeviceCommands =
64 ''
65 # Using acpi_pm as a clock source causes the guest clock to
66 # slow down under high host load. This is usually a bad
67 # thing, but for VM tests it should provide a bit more
68 # determinism (e.g. if the VM runs at lower speed, then
69 # timeouts in the VM should also be delayed).
70 echo acpi_pm > /sys/devices/system/clocksource/clocksource0/current_clocksource
71 '';
72
73 boot.postBootCommands =
74 ''
75 # Panic on out-of-memory conditions rather than letting the
76 # OOM killer randomly get rid of processes, since this leads
77 # to failures that are hard to diagnose.
78 echo 2 > /proc/sys/vm/panic_on_oom
79
80 # Coverage data is written into /tmp/coverage-data.
81 mkdir -p /tmp/xchg/coverage-data
82 '';
83
84 # If the kernel has been built with coverage instrumentation, make
85 # it available under /proc/gcov.
86 boot.kernelModules = [ "gcov-proc" ];
87
88 # Panic if an error occurs in stage 1 (rather than waiting for
89 # user intervention).
90 boot.kernelParams =
91 [ "console=ttyS0" "panic=1" "boot.panic_on_fail" ];
92
93 # `xwininfo' is used by the test driver to query open windows.
94 environment.systemPackages = [ pkgs.xorg.xwininfo ];
95
96 # Log everything to the serial console.
97 services.journald.extraConfig =
98 ''
99 ForwardToConsole=yes
100 MaxLevelConsole=debug
101 '';
102
103 # Don't clobber the console with duplicate systemd messages.
104 systemd.extraConfig = "ShowStatus=no";
105
106 boot.consoleLogLevel = 7;
107
108 # Prevent tests from accessing the Internet.
109 networking.defaultGateway = mkOverride 150 "";
110 networking.nameservers = mkOverride 150 [ ];
111
112 systemd.globalEnvironment.GCOV_PREFIX = "/tmp/xchg/coverage-data";
113
114 system.requiredKernelConfig = with config.lib.kernelConfig; [
115 (isYes "SERIAL_8250_CONSOLE")
116 (isYes "SERIAL_8250")
117 (isEnabled "VIRTIO_CONSOLE")
118 ];
119
120 networking.usePredictableInterfaceNames = false;
121
122 # Make it easy to log in as root when running the test interactively.
123 users.extraUsers.root.initialHashedPassword = mkOverride 150 "";
124
125 services.xserver.displayManager.logToJournal = true;
126 };
127
128}