diff options
Diffstat (limited to 'pkgs')
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index c8a77e5..fa4abc2 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -42,6 +42,60 @@ let exec ${pkgs.socat}/bin/socat VSOCK-CONNECT:2:"$1" EXEC:"${pkgs.util-linux}/bin/mount -t 9p -o trans=fd -o rfdno=0 -o wfdno=1 -o version=9p2000.L -o msize=1048576$opts vsock9p $2",nofork ''; + # The guest's side of fc-egress (see modules/host): asks the host + # whether the guest's egress allows URIs, over bash's /dev/tcp, as the + # guest may have no HTTP client. + fc-egress = pkgs.writeShellScriptBin "fc-egress" '' + # usage: fc-egress check URI... + # Whether this guest's egress allows each URI (https://HOST[/PATH], + # tcp://HOST:PORT, udp://HOST:PORT): true or false, and the URI, a line + # each. Exit status 0 if all of them are allowed, 1 if not, 2 on + # errors. The whitelist itself is the host's, see fc-egress there. + set -eu + usage() { + echo "usage: fc-egress check URI..." >&2 + exit 2 + } + test "''${1-}" = check || usage + shift + test $# -gt 0 || usage + uris=("$@") + body= + for uri; do + case $uri in *$'\n'* | *$'\r'*) + echo "fc-egress: bad URI" >&2 + exit 2 + esac + body+=$uri$'\n' + done + export LC_ALL=C # for the length in bytes + if ! exec 3<>/dev/tcp/172.16.0.1/80; then + echo "fc-egress: can't reach the host" >&2 + exit 2 + fi + printf 'POST /check HTTP/1.0\r\nHost: 172.16.0.1\r\nContent-Type: text/plain\r\nContent-Length: %d\r\n\r\n%s' "''${#body}" "$body" >&3 + read -r _ status _ <&3 || status= + while IFS= read -r line <&3 && test -n "''${line%$'\r'}"; do :; done + if test "$status" != 200; then + echo "fc-egress: the host answered ''${status:-nothing}" >&2 + cat <&3 >&2 + exit 2 + fi + all=0 + i=0 + while read -r verdict <&3; do + test $i -lt $# || break + printf '%s %s\n' "$verdict" "''${uris[i]}" + test "$verdict" = true || all=1 + i=$((i + 1)) + done + if test $i != $#; then + echo "fc-egress: $i answers for $# URIs" >&2 + exit 2 + fi + exit $all + ''; + # The system of the first n layers (the base system being layer 0), # with n + 1 images. evalSystem = n: import (pkgs.path + "/nixos") { @@ -134,6 +188,9 @@ let (pkgs.runCommand "mount.vsock9p" { } "install -D ${mountVsock9p} $out/bin/mount.vsock9p") ]; + # fc-egress check URI..., see fc-egress above. + 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) |
