aboutsummaryrefslogtreecommitdiffstats
path: root/default.nix
diff options
context:
space:
mode:
Diffstat (limited to 'default.nix')
-rw-r--r--default.nix43
1 files changed, 32 insertions, 11 deletions
diff --git a/default.nix b/default.nix
index 0c7e52c..20a26d2 100644
--- a/default.nix
+++ b/default.nix
@@ -38,7 +38,22 @@ let
fsType = "erofs";
options = [ "ro" "nodev" "nosuid" ];
};
- # /xchg gets mounted via 9P, see systemd.mount-extra in the run script.
+ # /xchg: the host's ./xchg via 9P over vsock, see the run script.
+ # The kernel's 9P client can't do vsock itself, but it can use the fds
+ # of a connected socket (trans=fd), which socat passes to mount as
+ # stdin and stdout (nofork). Separate -o options, as socat would take
+ # commas as its own option separators.
+ boot.kernelModules = [ "vmw_vsock_virtio_transport" ];
+ systemd.services.xchg = {
+ wantedBy = [ "multi-user.target" ];
+ serviceConfig.Type = "oneshot";
+ serviceConfig.RemainAfterExit = true;
+ path = [ pkgs.socat pkgs.util-linux ];
+ script = ''
+ mkdir -p /xchg
+ socat VSOCK-CONNECT:2:564 EXEC:"mount -t 9p -o trans=fd -o rfdno=0 -o wfdno=1 -o version=9p2000.L xchg /xchg",nofork
+ '';
+ };
services.getty.autologinUser = "root";
systemd.targets.getty.wants = lib.mkForce [ ]; # no getty on tty1
@@ -124,7 +139,7 @@ let
boot-source = {
kernel_image_path = "${kernel.dev}/vmlinux";
initrd_path = "${initialRamdisk}/${nixos.config.system.boot.loader.initrdFile}";
- boot_args = toString (nixos.config.boot.kernelParams ++ [ "init=${toplevel}/init" "@TTY_ARGS@" "@XCHG_ARGS@" ]);
+ boot_args = toString (nixos.config.boot.kernelParams ++ [ "init=${toplevel}/init" "@TTY_ARGS@" ]);
};
network-interfaces = [{
iface_id = "eth0";
@@ -137,6 +152,10 @@ let
is_root_device = false;
is_read_only = true;
}];
+ vsock = {
+ guest_cid = 3;
+ uds_path = "@VSOCK@";
+ };
machine-config = {
vcpu_count = 2;
mem_size_mib = 1024;
@@ -149,7 +168,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
# an erofs image, which the guest mounts read-only at /nix/store.
# The image of the last closure is kept in the cache directory.
# The directory xchg (in the current working directory) is served via 9P
- # over TCP and mounted by the guest at /xchg. VMs started in the same
+ # over vsock and mounted by the guest at /xchg. VMs started in the same
# directory share it.
set -efu
export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.gnutar pkgs.util-linux pkgs.erofs-utils pkgs.nix pkgs.firecracker pkgs.unpfs ]}
@@ -202,21 +221,23 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
n=''${tap#fctap}
mac=$(printf '06:00:ac:10:%02x:%02x' $((n >> 8 & 255)) $((n & 255)))
- # One 9P server per VM, on a port derived from the tap's number. Its
- # files end up owned by the user running this script. RUST_LOG=off
- # silences its logging of failed requests (like walking to a file that
- # doesn't exist), fatal errors still go to stderr.
- port=$((5640 + n))
+ # The 9P server for /xchg, one per VM. Its files end up owned by the
+ # user running this script. When the guest connects to vsock port 564 on
+ # the host (CID 2), Firecracker connects to the socket uds_path_564.
+ # unpfs listens on ADDR:PORT (from unix!ADDR!PORT), hence the colon at the
+ # end of uds_path. RUST_LOG=off silences its logging of failed requests
+ # (like walking to a file that doesn't exist), fatal errors still go to
+ # stderr.
+ vsock=$tmp/vsock:
mkdir -p xchg
- RUST_LOG=off unpfs "tcp!172.16.0.1!$port" xchg > /dev/null &
+ RUST_LOG=off unpfs "unix!$tmp/vsock!_564" xchg > /dev/null &
unpfs=$!
trap 'kill $unpfs; rm -rf "$tmp"' EXIT
- xchg_args="systemd.mount-extra=172.16.0.1:/xchg:9p:trans=tcp,port=$port,version=9p2000.L,x-systemd.automount,nofail"
sed \
-e "s|@STORE_IMAGE@|$img|" \
-e "s|@TTY_ARGS@|$tty_args|" \
- -e "s|@XCHG_ARGS@|$xchg_args|" \
+ -e "s|@VSOCK@|$vsock|" \
-e "s|@TAP@|$tap|" \
-e "s|@MAC@|$mac|" \
${config} > "$tmp"/config.json