aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/run-firecracker-vm/default.nix
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/run-firecracker-vm/default.nix')
-rw-r--r--pkgs/run-firecracker-vm/default.nix47
1 files changed, 44 insertions, 3 deletions
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 \