diff options
| -rw-r--r-- | default.nix | 137 |
1 files changed, 70 insertions, 67 deletions
diff --git a/default.nix b/default.nix index 045d843..eeb4559 100644 --- a/default.nix +++ b/default.nix @@ -6,6 +6,26 @@ let inherit (pkgs) lib; + # 9P over vsock, for the store and /xchg: mount -t vsock9p PORT DIR + # mounts what the host serves on vsock port PORT (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. In the initrd, too, for the + # store. + mountVsock9p = pkgs.writeShellScript "mount.vsock9p" '' + # usage: mount.vsock9p PORT DIR [-o OPTIONS] + set -efu + opts= + if test "''${3-}" = -o; then + for o in ''${4//,/ }; do + case $o in x-*|nofail|auto|noauto|defaults) ;; *) opts="$opts -o $o" + esac + done + fi + 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 + ''; + nixos = import (pkgs.path + "/nixos") { inherit (pkgs.stdenv.hostPlatform) system; configuration = { config, modulesPath, ... }: { @@ -22,7 +42,7 @@ let boot.kernelPackages = kernelPackages; boot.initrd.allowMissingModules = true; # e.g. ahci, efivarfs with a cut-down kernel - boot.initrd.kernelModules = [ "virtio_mmio" "virtio_blk" "erofs" "overlay" ]; + boot.initrd.kernelModules = [ "virtio_mmio" "vmw_vsock_virtio_transport" "9p" "9pnet_fd" "overlay" ]; boot.kernelParams = [ "console=ttyS0" "reboot=k" "panic=1" "8250.nr_uarts=1" # only ttyS0 exists (stops 0x2f8/0x3e8/0x2e8 probing) @@ -35,12 +55,15 @@ let fsType = "tmpfs"; options = [ "mode=755" ]; }; - # The store: the image (read-only) with a tmpfs on top, so nix can add - # to it. What it adds lives in RAM, and is gone after a reboot. + # The store: the host's, read-only via 9P over vsock (see mount.vsock9p + # below and the run script), with a tmpfs on top, so nix can add to + # it. What it adds lives in RAM, and is gone after a reboot. The + # host's store doesn't change under a path, so 9P may cache freely. fileSystems."/nix/.ro-store" = { - device = "/dev/vda"; - fsType = "erofs"; - options = [ "ro" "nodev" "nosuid" ]; + device = "565"; + fsType = "vsock9p"; + options = [ "ro" "cache=loose" ]; + noCheck = true; neededForBoot = true; }; fileSystems."/nix/.rw-store" = { @@ -55,37 +78,36 @@ let }; # nix, with the host's nixpkgs for nix-shell -p and the like. Root - # goes through the daemon too, so the paths of the image get - # registered (see the run script) before anything uses the store, - # and only once something does, not at boot. + # goes through the daemon too, so the paths of the guest's closure get + # registered (fc.registration, see the run script) before anything + # uses the store, and only once something does, not at boot. The + # rest of the host's store is there, too, but not registered. nix.nixPath = [ "nixpkgs=${pkgs.path}" ]; environment.variables.NIX_REMOTE = "daemon"; systemd.services.nix-load-db = { requiredBy = [ "nix-daemon.service" ]; before = [ "nix-daemon.service" ]; - unitConfig.ConditionPathExists = "/nix/.ro-store/.registration"; serviceConfig.Type = "oneshot"; serviceConfig.RemainAfterExit = true; - script = "${config.nix.package.out}/bin/nix-store --load-db < /nix/.ro-store/.registration"; + script = '' + for arg in $(cat /proc/cmdline); do + case $arg in fc.registration=*) + ${config.nix.package.out}/bin/nix-store --load-db < "''${arg#*=}" + esac + done + ''; }; - # /xchg: the host's ./xchg via 9P over vsock, see the run script, - # mounted on first access. The kernel's 9P client can't do vsock - # itself, but it can use the fds of a connected socket (trans=fd), so - # the mount goes through the helper mount.xchg: socat connects and - # passes the socket to mount as stdin and stdout (nofork). Separate - # -o options, as socat would take commas as its own option separators. fileSystems."/xchg" = { - device = "xchg"; - fsType = "xchg"; + device = "564"; + fsType = "vsock9p"; noCheck = true; options = [ "x-systemd.automount" "nofail" ]; }; system.fsPackages = [ - (pkgs.writeShellScriptBin "mount.xchg" '' - # usage: mount.xchg SPEC DIR [-o OPTIONS] - exec ${pkgs.socat}/bin/socat VSOCK-CONNECT:2:564 EXEC:"${pkgs.util-linux}/bin/mount -t 9p -o trans=fd -o rfdno=0 -o wfdno=1 -o version=9p2000.L xchg $2",nofork - '') + (pkgs.runCommand "mount.vsock9p" { } "install -D ${mountVsock9p} $out/bin/mount.vsock9p") ]; + boot.initrd.systemd.extraBin."mount.vsock9p" = mountVsock9p; + boot.initrd.systemd.storePaths = [ pkgs.socat pkgs.util-linux ]; services.getty.autologinUser = "root"; systemd.targets.getty.wants = lib.mkForce [ ]; # no getty on tty1 @@ -173,19 +195,18 @@ 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@" ]); + boot_args = toString (nixos.config.boot.kernelParams ++ [ + "init=${toplevel}/init" + "fc.registration=${pkgs.closureInfo { rootPaths = [ toplevel ]; }}/registration" + "@TTY_ARGS@" + ]); }; network-interfaces = [{ iface_id = "eth0"; host_dev_name = "@TAP@"; # see host.nix guest_mac = "@MAC@"; }]; - drives = [{ - drive_id = "store"; - path_on_host = "@STORE_IMAGE@"; - is_root_device = false; - is_read_only = true; - }]; + drives = [ ]; vsock = { guest_cid = 3; uds_path = "@VSOCK@"; @@ -197,38 +218,16 @@ let }); in pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' - # usage: run-firecracker-vm [STORE-PATH...] - # Packs the closure of the guest system (plus any given store paths) into - # 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 vsock and mounted by the guest at /xchg. VMs started in the same - # directory share it. + # usage: run-firecracker-vm + # 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. 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 ]} + export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.findutils pkgs.gnused pkgs.util-linux pkgs.firecracker pkgs.unpfs ]} tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT - cache=''${XDG_CACHE_HOME:-$HOME/.cache}/run-firecracker-vm - mkdir -p "$cache" - - nix-store -qR ${toplevel} "$@" > "$tmp"/store-paths - img=$cache/$({ echo v2; cat "$tmp"/store-paths; } | sha256sum | cut -c-32).img - - # The image gets the paths' registration for the guest's nix database - # as .registration (nix ignores names starting with a dot). - if ! test -e "$img"; then - nix-store --dump-db $(cat "$tmp"/store-paths) > "$tmp"/.registration - { cat "$tmp"/store-paths; echo "$tmp"/.registration; } | - tar -c -P --verbatim-files-from \ - --transform 'flags=rSh;s|^/nix/store/||;s|^.*/\.registration$|.registration|' \ - --files-from - \ - | mkfs.erofs --quiet --tar=f --force-uid=0 --force-gid=0 --hard-dereference -T 0 "$img.$$" - mv "$img.$$" "$img" - find "$cache" -type f ! -path "$img" -delete - fi - # Tell systemd the terminal's type and size, so it doesn't query them via # ANSI sequences (and wait up to 333ms for each answer): PID 1 asks for # the type of /dev/console when starting (in the initrd and again after @@ -259,21 +258,25 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' n=''${tap#fctap} mac=$(printf '06:00:ac:10:%02x:%02x' $((n >> 8 & 255)) $((n & 255))) - # 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. + # The 9P servers, for the store and for /xchg, 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 host (CID 2), Firecracker connects to the socket + # uds_path_PORT. 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. The store has to be served before the + # guest's initrd wants it. vsock=$tmp/vsock: mkdir -p xchg + RUST_LOG=off unpfs "unix!$tmp/vsock!_565" /nix/store > /dev/null & + store=$! RUST_LOG=off unpfs "unix!$tmp/vsock!_564" xchg > /dev/null & - unpfs=$! - trap 'kill $unpfs; rm -rf "$tmp"' EXIT + xchg=$! + trap 'kill $store $xchg; rm -rf "$tmp"' EXIT + while ! test -S "$vsock"_565; do sleep 0.01; done sed \ - -e "s|@STORE_IMAGE@|$img|" \ -e "s|@TTY_ARGS@|$tty_args|" \ -e "s|@VSOCK@|$vsock|" \ -e "s|@TAP@|$tap|" \ |
