Skip to content

Commit 1bd8073

Browse files
committed
nixos/test-driver: add backdoor based on systemd-ssh-proxy & AF_VSOCK
With this it's possible to trivially SSH into running machines from the test-driver. This is especially useful when running VM tests interactively on a remote system. This is based on `systemd-ssh-proxy(1)`, so there's no need to configure any additional networking on the host-side. Suggested-by: Ryan Lahfa <[email protected]>
1 parent cb74a2a commit 1bd8073

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

nixos/doc/manual/development/running-nixos-tests-interactively.section.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,35 @@ using:
6363
Once the connection is established, you can enter commands in the socat terminal
6464
where socat is running.
6565

66+
## SSH Access for test machines {#sec-nixos-test-ssh-access}
67+
68+
An SSH-based backdoor to log into machines can be enabled with
69+
70+
```nix
71+
{
72+
name = "…";
73+
nodes.machines = { /* … */ };
74+
sshBackdoor.enable = true;
75+
}
76+
```
77+
78+
This creates a [vsock socket](https://man7.org/linux/man-pages/man7/vsock.7.html)
79+
for each VM to log in with SSH. This configures root login with an empty password.
80+
81+
When the VMs get started interactively with the test-driver, it's possible to
82+
connect to `machine` with
83+
84+
```
85+
$ ssh vsock/3 -o User=root
86+
```
87+
88+
The socket numbers correspond to the node number of the test VM, but start
89+
at three instead of one because that's the lowest possible
90+
vsock number.
91+
92+
On non-NixOS systems you'll probably need to enable
93+
the SSH config from {manpage}`systemd-ssh-proxy(1)` yourself.
94+
6695
## Port forwarding to NixOS test VMs {#sec-nixos-test-port-forwarding}
6796

6897
If your test has only a single VM, you may use e.g.

nixos/doc/manual/redirects.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,6 +1817,9 @@
18171817
"sec-test-options-reference": [
18181818
"index.html#sec-test-options-reference"
18191819
],
1820+
"test-opt-sshBackdoor.enable": [
1821+
"index.html#test-opt-sshBackdoor.enable"
1822+
],
18201823
"test-opt-defaults": [
18211824
"index.html#test-opt-defaults"
18221825
],
@@ -1904,6 +1907,9 @@
19041907
"sec-nixos-test-shell-access": [
19051908
"index.html#sec-nixos-test-shell-access"
19061909
],
1910+
"sec-nixos-test-ssh-access": [
1911+
"index.html#sec-nixos-test-ssh-access"
1912+
],
19071913
"sec-nixos-test-port-forwarding": [
19081914
"index.html#sec-nixos-test-port-forwarding"
19091915
],

nixos/lib/testing/nodes.nix

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let
1313
mapAttrs
1414
mkDefault
1515
mkIf
16+
mkMerge
1617
mkOption
1718
mkForce
1819
optional
@@ -77,6 +78,14 @@ in
7778
{
7879

7980
options = {
81+
sshBackdoor = {
82+
enable = mkOption {
83+
default = false;
84+
type = types.bool;
85+
description = "Whether to turn on the vsock-based SSH backdoor for all VMs.";
86+
};
87+
};
88+
8089
node.type = mkOption {
8190
type = types.raw;
8291
default = baseOS.type;
@@ -172,10 +181,15 @@ in
172181

173182
passthru.nodes = config.nodesCompat;
174183

175-
defaults = mkIf config.node.pkgsReadOnly {
176-
nixpkgs.pkgs = config.node.pkgs;
177-
imports = [ ../../modules/misc/nixpkgs/read-only.nix ];
178-
};
184+
defaults = mkMerge [
185+
(mkIf config.node.pkgsReadOnly {
186+
nixpkgs.pkgs = config.node.pkgs;
187+
imports = [ ../../modules/misc/nixpkgs/read-only.nix ];
188+
})
189+
(mkIf config.sshBackdoor.enable {
190+
testing.sshBackdoor.enable = true;
191+
})
192+
];
179193

180194
};
181195
}

nixos/modules/testing/test-instrumentation.nix

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ in
8787
machine.switch_root() to leave stage 1 and proceed to stage 2
8888
'';
8989

90+
sshBackdoor = {
91+
enable = mkEnableOption "vsock-based ssh backdoor for the VM";
92+
};
93+
9094
};
9195

9296
config = {
@@ -100,6 +104,18 @@ in
100104
}
101105
];
102106

107+
services.openssh = mkIf config.testing.sshBackdoor.enable {
108+
enable = true;
109+
settings = {
110+
PermitRootLogin = "yes";
111+
PermitEmptyPasswords = "yes";
112+
};
113+
};
114+
115+
security.pam.services.sshd = mkIf config.testing.sshBackdoor.enable {
116+
allowNullPassword = true;
117+
};
118+
103119
systemd.services.backdoor = lib.mkMerge [
104120
backdoorService
105121
{
@@ -175,6 +191,10 @@ in
175191
# we avoid defining attributes if not possible.
176192
# TODO: refactor such that test-instrumentation can import qemu-vm
177193
package = lib.mkDefault pkgs.qemu_test;
194+
195+
options = mkIf config.testing.sshBackdoor.enable [
196+
"-device vhost-vsock-pci,guest-cid=${toString (config.virtualisation.test.nodeNumber + 2)}"
197+
];
178198
};
179199
};
180200

0 commit comments

Comments
 (0)