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.nix83
1 files changed, 27 insertions, 56 deletions
diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix
index f78aaa5..106ec05 100644
--- a/pkgs/run-firecracker-vm/default.nix
+++ b/pkgs/run-firecracker-vm/default.nix
@@ -215,9 +215,9 @@ let
environment.systemPackages = [ fc-egress ];
# 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:
+ # key a secret (--secret ssh_host_ed25519_key=FILE; see fc-secrets),
+ # in place 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;
@@ -225,29 +225,13 @@ let
generateHostKeys = false;
hostKeys = [{ path = "/run/ssh/ssh_host_ed25519_key"; type = "ed25519"; }];
};
- systemd.services.fc-ssh-host-key = let
- sshd = if config.services.openssh.startWhenNeeded then [ "sshd.socket" ] else [ "sshd.service" ];
- in {
- requiredBy = sshd;
- # Early, without the default dependencies: sshd.socket is before
- # sockets.target, which is before basic.target, which a service is
- # after by default, so it can't be before sshd.socket.
- before = sshd ++ [ "shutdown.target" ];
- conflicts = [ "shutdown.target" ];
- unitConfig.DefaultDependencies = false;
- serviceConfig.Type = "oneshot";
- serviceConfig.RemainAfterExit = true;
- script = ''
- share=$(mktemp -d -p /run)
- ${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"
- '';
+ fc.secrets.ssh_host_ed25519_key = lib.mkIf config.services.openssh.enable {
+ path = "/run/ssh/ssh_host_ed25519_key";
+ mode = "0600";
};
# The secrets (fc.secrets), which the run script serves via 9P on
- # vsock port 568 (--secret NAME=FILE; never in the store), copied
+ # vsock port 567 (--secret NAME=FILE; never in the store), copied
# into place, each with its owner and mode, before sysinit.target
# (so before any service with the default dependencies), but after
# the users get created, for the owners; any other secret the run
@@ -263,7 +247,7 @@ let
serviceConfig.RemainAfterExit = true;
script = ''
share=$(mktemp -d -p /run)
- ${mountVsock9p} 568 "$share" -o ro
+ ${mountVsock9p} 567 "$share" -o ro
trap '${pkgs.util-linux}/bin/umount "$share"; rmdir "$share"' EXIT
install -d -m 0755 /run/secrets
${lib.concatStrings (lib.mapAttrsToList (name: s: ''
@@ -514,7 +498,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] [--secret NAME=FILE]...
+ # [--secret NAME=FILE]...
# The guest gets the directory xchg (in the current working directory)
# at /xchg, via 9P over vsock. VMs started in the same directory share
# it. Its store is read-only, unless it has nix (layers/nix.nix).
@@ -539,9 +523,6 @@ 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.
# --secret NAME=FILE: a secret for the guest, NAME letters, digits, '.',
# '_' and '-', FILE read once (it may be a pipe, like <(...)), and handed
# to the guest only, never in the store (see fc-secrets); a guest
@@ -550,7 +531,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
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] [--ssh-host-key FILE] [--secret NAME=FILE]..."
+ 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] [--secret NAME=FILE]..."
}
args=
rw_store=
@@ -561,7 +542,6 @@ 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)}
nix_opt= # the last option that's only for a guest with nix
secrets=() # NAME=FILE, as given
@@ -578,7 +558,6 @@ 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 ;;
--secret) secrets+=("$2"); shift 2 ;;
-h|--help) usage; exit ;;
*) usage >&2; exit 1
@@ -614,28 +593,9 @@ 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
-
- # The secrets, copies, likewise, for the guest to copy (see fc-secrets),
- # each FILE read once. The guest's own (fc.secrets) are required.
+ # The secrets, copies, in a directory only the user can read, for the
+ # guest to copy (see fc-secrets), each FILE read once. The guest's own
+ # (fc.secrets) are required.
mkdir -m 700 "$tmp"/secrets
for secret in ''${secrets[@]+"''${secrets[@]}"}; do
s=''${secret%%=*}
@@ -654,6 +614,18 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
fi
chmod 600 "$tmp/secrets/$s"
done
+ # sshd takes no host key without its final newline, which, e.g., $(...)
+ # loses, and none with a passphrase.
+ key=$tmp/secrets/ssh_host_ed25519_key
+ if test -e "$key"; then
+ if test -n "$(tail -c 1 "$key")"; then
+ echo >> "$key"
+ fi
+ if ! ssh-keygen -y -f "$key" > /dev/null; then
+ echo "$0: --secret ssh_host_ed25519_key: not an SSH private key without passphrase" >&2
+ exit 1
+ fi
+ fi
for s in ${lib.escapeShellArgs (lib.attrNames nixos.config.fc.secrets)}; do
if ! test -e "$tmp/secrets/$s"; then
echo "$0: the guest needs the secret $s: --secret $s=FILE is required" >&2
@@ -795,7 +767,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
vm=
fi
- # The 9P servers, for /xchg, the SSH host key, the secrets, and with nix, the store
+ # The 9P servers, for /xchg, the secrets, and with nix, the store
# and nixpkgs, one each per VM. They run as the user running this
# script: xchg's files end up owned by the user, and the store is
# read-only to them. When the guest connects to vsock port PORT on the
@@ -815,8 +787,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
}
mkdir -p xchg
serve 564 xchg
- serve 567 "$tmp"/ssh
- serve 568 "$tmp"/secrets
+ serve 567 "$tmp"/secrets
${lib.optionalString nix ''
serve 565 /nix/store
serve 566 "$nixpkgs"