at master 8.5 kB view raw
1# This module allows the test driver to connect to the virtual machine 2# via a root shell attached to port 514. 3 4{ 5 options, 6 config, 7 lib, 8 pkgs, 9 ... 10}: 11 12with lib; 13 14let 15 cfg = config.testing; 16 17 qemu-common = import ../../lib/qemu-common.nix { inherit lib pkgs; }; 18 19 backdoorService = { 20 requires = [ 21 "dev-hvc0.device" 22 "dev-${qemu-common.qemuSerialDevice}.device" 23 ]; 24 after = [ 25 "dev-hvc0.device" 26 "dev-${qemu-common.qemuSerialDevice}.device" 27 ]; 28 script = '' 29 export USER=root 30 export HOME=/root 31 export DISPLAY=:0.0 32 33 # Determine if this script is ran with nounset 34 strict="false" 35 if set -o | grep --quiet --perl-regexp "nounset\s+on"; then 36 strict="true" 37 fi 38 39 if [[ -e /etc/profile ]]; then 40 # TODO: Currently shell profiles are not checked at build time, 41 # so we need to unset stricter options to source them 42 set +o nounset 43 # shellcheck disable=SC1091 44 source /etc/profile 45 [ "$strict" = "true" ] && set -o nounset 46 fi 47 48 # Don't use a pager when executing backdoor 49 # actions. Because we use a tty, commands like systemctl 50 # or nix-store get confused into thinking they're running 51 # interactively. 52 export PAGER= 53 54 cd /tmp 55 exec < /dev/hvc0 > /dev/hvc0 56 while ! exec 2> /dev/${qemu-common.qemuSerialDevice}; do sleep 0.1; done 57 echo "connecting to host..." >&2 58 stty -F /dev/hvc0 raw -echo # prevent nl -> cr/nl conversion 59 # The following line is essential since it signals to 60 # the test driver that the shell is ready. 61 # See: the connect method in the Machine class. 62 echo "Spawning backdoor root shell..." 63 # Passing the terminal device makes bash run non-interactively. 64 # Otherwise we get errors on the terminal because bash tries to 65 # setup things like job control. 66 # Note: calling bash explicitly here instead of sh makes sure that 67 # we can also run non-NixOS guests during tests. This, however, is 68 # mostly futureproofing as the test instrumentation is still very 69 # tightly coupled to NixOS. 70 PS1="" exec ${pkgs.bashNonInteractive}/bin/bash --norc /dev/hvc0 71 ''; 72 serviceConfig.KillSignal = "SIGHUP"; 73 }; 74 75 managerSettings = { 76 # Don't clobber the console with duplicate systemd messages. 77 ShowStatus = false; 78 # Allow very slow start 79 DefaultTimeoutStartSec = 300; 80 DefaultDeviceTimeoutSec = 300; 81 }; 82 83in 84 85{ 86 87 options.testing = { 88 89 initrdBackdoor = lib.mkEnableOption '' 90 backdoor.service in initrd. Requires 91 boot.initrd.systemd.enable to be enabled. Boot will pause in 92 stage 1 at initrd.target, and will listen for commands from the 93 Machine python interface, just like stage 2 normally does. This 94 enables commands to be sent to test and debug stage 1. Use 95 machine.switch_root() to leave stage 1 and proceed to stage 2 96 ''; 97 }; 98 99 config = { 100 101 assertions = [ 102 { 103 assertion = cfg.initrdBackdoor -> config.boot.initrd.systemd.enable; 104 message = '' 105 testing.initrdBackdoor requires boot.initrd.systemd.enable to be enabled. 106 ''; 107 } 108 ]; 109 110 systemd.services.backdoor = lib.mkMerge [ 111 backdoorService 112 { 113 wantedBy = [ "multi-user.target" ]; 114 } 115 ]; 116 117 boot.initrd.systemd = lib.mkMerge [ 118 { 119 contents."/etc/systemd/journald.conf".text = '' 120 [Journal] 121 ForwardToConsole=yes 122 TTYPath=/dev/${qemu-common.qemuSerialDevice} 123 MaxLevelConsole=debug 124 ''; 125 126 settings.Manager = managerSettings; 127 } 128 129 (lib.mkIf cfg.initrdBackdoor { 130 # Implemented in machine.switch_root(). Suppress the unit by 131 # making it a noop without removing it, which would break 132 # initrd-parse-etc.service 133 services.initrd-cleanup.serviceConfig.ExecStart = [ 134 # Reset 135 "" 136 # noop 137 "/bin/true" 138 ]; 139 140 services.backdoor = lib.mkMerge [ 141 backdoorService 142 { 143 # TODO: Both stage 1 and stage 2 should use these same 144 # settings. But a lot of existing tests rely on 145 # backdoor.service having default orderings, 146 # e.g. systemd-boot.update relies on /boot being mounted 147 # as soon as backdoor starts. But it can be useful for 148 # backdoor to start even earlier. 149 wantedBy = [ "sysinit.target" ]; 150 unitConfig.DefaultDependencies = false; 151 conflicts = [ 152 "shutdown.target" 153 "initrd-switch-root.target" 154 ]; 155 before = [ 156 "shutdown.target" 157 "initrd-switch-root.target" 158 ]; 159 } 160 ]; 161 162 storePaths = [ 163 "${pkgs.coreutils}/bin/env" 164 ]; 165 }) 166 ]; 167 168 # Prevent agetty from being instantiated on the serial device, since it 169 # interferes with the backdoor (writes to it will randomly fail 170 # with EIO). Likewise for hvc0. 171 systemd.services."serial-getty@${qemu-common.qemuSerialDevice}".enable = false; 172 systemd.services."serial-getty@hvc0".enable = false; 173 174 # Only set these settings when the options exist. Some tests (e.g. those 175 # that do not specify any nodes, or an empty attr set as nodes) will not 176 # have the QEMU module loaded and thuse these options can't and should not 177 # be set. 178 virtualisation = lib.optionalAttrs (options ? virtualisation.qemu) { 179 qemu = { 180 # NOTE: optionalAttrs 181 # test-instrumentation.nix appears to be used without qemu-vm.nix, so 182 # we avoid defining attributes if not possible. 183 # TODO: refactor such that test-instrumentation can import qemu-vm 184 package = lib.mkDefault pkgs.qemu_test; 185 }; 186 }; 187 188 boot.kernel.sysctl = { 189 "kernel.hung_task_timeout_secs" = 600; 190 # Panic on out-of-memory conditions rather than letting the 191 # OOM killer randomly get rid of processes, since this leads 192 # to failures that are hard to diagnose. 193 "vm.panic_on_oom" = lib.mkDefault 2; 194 }; 195 196 boot.kernelParams = [ 197 "console=${qemu-common.qemuSerialDevice}" 198 "console=tty0" 199 # Panic if an error occurs in stage 1 (rather than waiting for 200 # user intervention). 201 "panic=1" 202 "boot.panic_on_fail" 203 # Using acpi_pm as a clock source causes the guest clock to 204 # slow down under high host load. This is usually a bad 205 # thing, but for VM tests it should provide a bit more 206 # determinism (e.g. if the VM runs at lower speed, then 207 # timeouts in the VM should also be delayed). 208 "clocksource=acpi_pm" 209 ]; 210 211 # `xwininfo' is used by the test driver to query open windows. 212 environment.systemPackages = [ pkgs.xorg.xwininfo ]; 213 214 # Log everything to the serial console. 215 services.journald.extraConfig = '' 216 ForwardToConsole=yes 217 TTYPath=/dev/${qemu-common.qemuSerialDevice} 218 MaxLevelConsole=debug 219 ''; 220 221 systemd.settings.Manager = managerSettings; 222 systemd.user.extraConfig = '' 223 # Allow very slow start 224 DefaultTimeoutStartSec=300 225 DefaultDeviceTimeoutSec=300 226 ''; 227 228 boot.consoleLogLevel = 7; 229 230 # Prevent tests from accessing the Internet. 231 networking.defaultGateway = mkOverride 150 null; 232 networking.nameservers = mkOverride 150 [ ]; 233 234 system.requiredKernelConfig = with config.lib.kernelConfig; [ 235 (isYes "SERIAL_8250_CONSOLE") 236 (isYes "SERIAL_8250") 237 (isEnabled "VIRTIO_CONSOLE") 238 ]; 239 240 networking.usePredictableInterfaceNames = false; 241 242 # Make it easy to log in as root when running the test interactively. 243 # This needs to be a file because of a quirk in systemd credentials, 244 # where you cannot specify an empty string as a value. systemd-sysusers 245 # uses credentials to set passwords on users. 246 users.users.root.hashedPasswordFile = mkOverride 150 "${pkgs.writeText "hashed-password.root" ""}"; 247 248 services.displayManager.logToJournal = true; 249 250 services.logrotate.enable = mkOverride 150 false; 251 252 # Make sure we use the Guest Agent from the QEMU package for testing 253 # to reduce the closure size required for the tests. 254 services.qemuGuest.package = pkgs.qemu_test.ga; 255 256 # Squelch warning about unset system.stateVersion 257 system.stateVersion = (lib.mkOverride 1200) lib.trivial.release; 258 }; 259 260}