aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs')
-rw-r--r--pkgs/run-firecracker-vm/default.nix48
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