diff options
| author | tv <tv@krebsco.de> | 2026-11-20 16:52:15 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-20 16:52:15 +0000 |
| commit | 51c2e65e867c1c44418fead32e331c875f7c17fb (patch) | |
| tree | 8b2fc3a56a58761208aecf56977d1f31fe17d9df /pkgs/run-firecracker-vm/default.nix | |
| parent | a556852dd550fc0676708b89f9f0fcd6ff015250 (diff) | |
run-firecracker-vm: start fc-ssh-host-key at boot, before sshd
fc-ssh-host-key only ran on the first connection, as a requirement of
the socket-activated sshd@ instance, and without startWhenNeeded not at
all, so NixOS generated a random host key instead. Now it's required
by, and runs before, whichever of sshd.socket or sshd.service is in use,
startWhenNeeded is only a default (a layer can turn it off), and NixOS
doesn't generate host keys.
The run script now also deals with the key first thing, before starting
any servers: it may be a pipe (<(...)), so it's read once, for the copy;
a missing final newline gets added (sshd refuses the key without); and
ssh-keygen -y checks it's a key without passphrase, so a bad one stops
the run script right away rather than every connection.
Diffstat (limited to 'pkgs/run-firecracker-vm/default.nix')
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 48 |
1 files changed, 31 insertions, 17 deletions
diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index 18e767f..b96bb83 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -198,16 +198,22 @@ let services.getty.autologinUser = "root"; - # sshd, started on demand, with the host key the run script serves - # via 9P on vsock port 567 (--ssh-host-key; not in the store), copied - # to /run/ssh before sshd starts. Who may log in: + # sshd, started on demand (a layer may turn that off), with the host + # key the run script serves via 9P on vsock port 567 (--ssh-host-key; + # not in the store), copied to /run/ssh before sshd (or its socket) + # starts, and never one generated instead. Who may log in: # users.users.<name>.openssh.authorizedKeys, e.g. in a layer. services.openssh = { enable = true; - startWhenNeeded = true; + startWhenNeeded = lib.mkDefault true; + generateHostKeys = false; hostKeys = [{ path = "/run/ssh/ssh_host_ed25519_key"; type = "ed25519"; }]; }; - systemd.services.fc-ssh-host-key = { + systemd.services.fc-ssh-host-key = let + sshd = if config.services.openssh.startWhenNeeded then [ "sshd.socket" ] else [ "sshd.service" ]; + in { + requiredBy = sshd; + before = sshd; serviceConfig.Type = "oneshot"; serviceConfig.RemainAfterExit = true; script = '' @@ -218,10 +224,6 @@ let rmdir "$share" ''; }; - systemd.services."sshd@" = { - requires = [ "fc-ssh-host-key.service" ]; - after = [ "fc-ssh-host-key.service" ]; - }; systemd.targets.getty.wants = lib.mkForce [ ]; # no getty on tty1 # A fixed machine-id, so systemd doesn't consider every boot the first. @@ -506,6 +508,26 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT + # The SSH host key, a copy, in a directory only the user can read, for + # the guest to copy (see fc-ssh-host-key). + if ! test -e "$ssh_host_key"; then + mkdir -p "$(dirname "$ssh_host_key")" + ssh-keygen -q -t ed25519 -N "" -C run-firecracker-vm -f "$ssh_host_key" + fi + # (FILE may be a pipe, e.g. <(...), so it's only read once, for the copy.) + # sshd takes no key without its final newline, which, e.g., $(...) loses. + mkdir "$tmp"/ssh + key=$tmp/ssh/ssh_host_ed25519_key + cp "$ssh_host_key" "$key" + chmod 600 "$key" + if test -n "$(tail -c 1 "$key")"; then + echo >> "$key" + fi + if ! ssh-keygen -y -f "$key" > /dev/null; then + echo "$0: $ssh_host_key: not an SSH private key without passphrase" >&2 + exit 1 + fi + # disk VAR FILE SIZE NAME: sets VAR to the absolute path of FILE, an ext4 # image, created (sparse, of SIZE) if it doesn't exist, or without FILE, # of a scratch one, removed on exit; locked while the guest runs, as two @@ -620,14 +642,6 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' xchg=$! RUST_LOG=off unpfs "unix!$tmp/vsock!_566" "$nixpkgs" > /dev/null & nixpkgs_server=$! - # The SSH host key, a copy, in a directory only the user can read, for - # the guest to copy (see fc-ssh-host-key). - if ! test -e "$ssh_host_key"; then - mkdir -p "$(dirname "$ssh_host_key")" - ssh-keygen -q -t ed25519 -N "" -C run-firecracker-vm -f "$ssh_host_key" - fi - mkdir "$tmp"/ssh - cp "$ssh_host_key" "$tmp"/ssh/ssh_host_ed25519_key RUST_LOG=off unpfs "unix!$tmp/vsock!_567" "$tmp"/ssh > /dev/null & ssh_server=$! trap 'kill $store $xchg $nixpkgs_server $ssh_server; rm -rf "$tmp"; if test -n "$vm"; then : > "$vm"; if test -w "$vm.name"; then : > "$vm.name"; fi; fi' EXIT |
