diff options
| author | tv <tv@krebsco.de> | 2026-11-20 15:55:20 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-20 15:55:20 +0000 |
| commit | a556852dd550fc0676708b89f9f0fcd6ff015250 (patch) | |
| tree | 2b5391a2ebeedf8985a871a95b62c2397ea80d1e | |
| parent | 4f5ef7b716c6bf01380e44c6f270f7c02aa5b479 (diff) | |
run-firecracker-vm: sshd, with a host key from the host
The guests run sshd, started on demand, with an ed25519 host key that
isn't in the store: the run script takes it from --ssh-host-key FILE, by
default ~/.local/share/run-firecracker-vm/ssh_host_ed25519_key, created
on first use, so a user's guests share one key and one known_hosts line
covers them (see README.md), and serves a copy from its private
temporary directory via 9P on vsock port 567. In the guest,
fc-ssh-host-key copies it to /run/ssh (0600) before sshd starts, and
unmounts the share again. Who may log in is up to a layer
(users.users.<name>.openssh.authorizedKeys).
| -rw-r--r-- | README.md | 17 | ||||
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 47 |
2 files changed, 61 insertions, 3 deletions
@@ -87,6 +87,23 @@ not show up while the guest runs). Other modes: `mmap`, `readahead`, The guest gets 2 CPUs and 1024 MiB unless told otherwise (`--vcpus N`, 1 to 32; `--mem-size MIB`). +The guests run sshd. Its host key comes from the host at boot, so it's +not in the store: `--ssh-host-key FILE`, by default +`~/.local/share/run-firecracker-vm/ssh_host_ed25519_key`, created on first +use, so all your guests have the same one, and one known_hosts line does: + +```sh +echo "172.16.0.*,fdfc:fc::* $(cut -d' ' -f1,2 ~/.local/share/run-firecracker-vm/ssh_host_ed25519_key.pub)" >> ~/.ssh/known_hosts +``` + +Who may log in goes in a layer, e.g.: + +```nix +{ users.users.root.openssh.authorizedKeys.keys = [ "ssh-ed25519 AAAA..." ]; } +``` + +A guest on tap fctapN is 172.16.0.(N+2) (see `fc-egress vms`). + The guest's system can be built in layers, each a NixOS module on top of the ones before (the first on top of the base system): diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index 2f20528..18e767f 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -197,6 +197,31 @@ let boot.initrd.systemd.storePaths = [ pkgs.socat pkgs.util-linux ]; 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: + # users.users.<name>.openssh.authorizedKeys, e.g. in a layer. + services.openssh = { + enable = true; + startWhenNeeded = true; + hostKeys = [{ path = "/run/ssh/ssh_host_ed25519_key"; type = "ed25519"; }]; + }; + systemd.services.fc-ssh-host-key = { + serviceConfig.Type = "oneshot"; + serviceConfig.RemainAfterExit = true; + script = '' + share=$(mktemp -d) + ${mountVsock9p} 567 "$share" -o ro + install -D -m 600 "$share"/ssh_host_ed25519_key /run/ssh/ssh_host_ed25519_key + ${pkgs.util-linux}/bin/umount "$share" + 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. @@ -408,6 +433,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # [--mem-size MIB] # [--nixpkgs DIR] [--nixpkgs-cache MODE] # [--egress LIST] [--name NAME] [--vcpus N] + # [--ssh-host-key FILE] # The guest gets the host's store, read-only, via 9P over vsock, and the # directory xchg (in the current working directory) at /xchg, the same # way. VMs started in the same directory share it. Also the same way, @@ -429,11 +455,14 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # --name names the VM, for fc-egress and as the guest's host name; only # one running VM of the user's can have a given name. --vcpus: the # guest's number of CPUs (default 2; Firecracker takes 1 to 32). + # --ssh-host-key: the guest's sshd's (ed25519) host key, by default + # ~/.local/share/run-firecracker-vm/ssh_host_ed25519_key, created if it + # doesn't exist, so all of the user's guests have the same one. set -efu - export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.jq pkgs.util-linux pkgs.firecracker pkgs.unpfs ]} + export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.jq pkgs.openssh pkgs.util-linux pkgs.firecracker pkgs.unpfs ]} usage() { - echo "usage: $0 [--store FILE] [--store-size SIZE] [--home FILE] [--home-size SIZE] [--mem-size MIB] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME] [--vcpus N]" + echo "usage: $0 [--store FILE] [--store-size SIZE] [--home FILE] [--home-size SIZE] [--mem-size MIB] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME] [--vcpus N] [--ssh-host-key FILE]" } args= rw_store= @@ -444,6 +473,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' egress= name= vcpus=2 + ssh_host_key=''${XDG_DATA_HOME:-$HOME/.local/share}/run-firecracker-vm/ssh_host_ed25519_key nixpkgs=${lib.escapeShellArg (toString pkgs.path)} while test $# -gt 0; do case $1 in @@ -458,6 +488,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' --egress) egress=$2; shift 2 ;; --name) name=$2; shift 2 ;; --vcpus) vcpus=$2; shift 2 ;; + --ssh-host-key) ssh_host_key=$2; shift 2 ;; -h|--help) usage; exit ;; *) usage >&2; exit 1 esac @@ -589,7 +620,17 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' xchg=$! RUST_LOG=off unpfs "unix!$tmp/vsock!_566" "$nixpkgs" > /dev/null & nixpkgs_server=$! - trap 'kill $store $xchg $nixpkgs_server; rm -rf "$tmp"; if test -n "$vm"; then : > "$vm"; if test -w "$vm.name"; then : > "$vm.name"; fi; fi' 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 + 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 while ! test -S "$vsock"_565; do sleep 0.01; done sed \ |
