diff --git a/config/ext-rachel.json b/config/ext-rachel.json new file mode 100644 index 0000000000000000000000000000000000000000..cd69ab5c584a162a8203ed21b1b12f7f2bdd289d --- /dev/null +++ b/config/ext-rachel.json @@ -0,0 +1,20 @@ +{ + "dataset": "work/ext-rachel", + "root": "/srv/ext-rachel", + "bridge": "virbr-ext", + "mac": "52:54:00:e7:00:02", + "gatewayAddress": "10.101.0.1", + "guestAddress": "10.101.0.2", + "sshPort": 2223, + "cpus": "3-7,19-23", + "gpu": [ + "10de:2204", + "10de:1aef" + ], + "authorizedKeys": [ + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBFQ8bkawCPFLOdVbUs/tf19bFflcal4Ss3BwjGa6mun", + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJVKqjtc1WZyfyQZIZzbNvZ7L2yEYh6rn7k7uGpiuNwP", + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOihVPOrOuT79omYc+AJYkmZceaAphi+7vOnhaYi87Hx", + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMpxNpkRLTUijhd3HSaOvKYn2MWXEY+YEFdsPNZhBROn" + ] +} diff --git a/nixos/ext-rachel.nix b/nixos/ext-rachel.nix new file mode 100644 index 0000000000000000000000000000000000000000..1326dd8ea98c8f8e213e859ae620a6aa4af76169 --- /dev/null +++ b/nixos/ext-rachel.nix @@ -0,0 +1,150 @@ +{ lib, pkgs, ... }: +let + box = builtins.fromJSON (builtins.readFile ../config/ext-rachel.json); + privateRanges = [ "10.0.0.0/8" "172.16.0.0/12" "192.168.0.0/16" "100.64.0.0/10" "169.254.0.0/16" ]; + # Open forwarding: libvirt adds no firewall rules, so the rules below are the whole policy. + network = pkgs.writeText "ext-rachel-network.xml" '' + + ext-rachel + + + + + + + + ''; +in +{ + # The 3090 belongs to the guest; vfio-pci claims it before nvidia can. + boot.initrd.kernelModules = [ "vfio_pci" "vfio" "vfio_iommu_type1" ]; + boot.kernelParams = [ "vfio-pci.ids=${lib.concatStringsSep "," box.gpu}" ]; + hardware.nvidia.nvidiaPersistenced = lib.mkForce false; + + systemd.services.ext-rachel-storage = { + wantedBy = [ "multi-user.target" ]; + before = [ "libvirtd.service" ]; + requires = [ "zfs-import-work.service" ]; + after = [ "zfs-import-work.service" "zfs-mount.service" ]; + serviceConfig = { Type = "oneshot"; RemainAfterExit = true; }; + script = '' + if ! ${pkgs.zfs}/bin/zfs list -H -o name ${box.dataset} >/dev/null 2>&1; then + ${pkgs.zfs}/bin/zfs create -o mountpoint=${box.root} -o compression=lz4 -o atime=off -o recordsize=64K ${box.dataset} + fi + ${pkgs.zfs}/bin/zfs set quota=560G ${box.dataset} + test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint ${box.root})" = ${box.dataset} + chmod 0700 ${box.root} + ''; + }; + + systemd.services.ext-rachel-network = { + wantedBy = [ "multi-user.target" ]; + requires = [ "libvirtd.service" ]; + after = [ "libvirtd.service" ]; + before = [ "libvirt-guests.service" ]; + serviceConfig = { Type = "oneshot"; RemainAfterExit = true; }; + path = [ pkgs.libvirt ]; + script = '' + virsh net-define ${network} + virsh net-autostart ext-rachel + virsh net-info ext-rachel | grep -q '^Active:.*yes' || virsh net-start ext-rachel + ''; + }; + + # Pinned vCPUs alone would still share those cores with host processes. + virtualisation.libvirtd.hooks.qemu.ext-rachel-cpus = pkgs.writeShellScript "ext-rachel-cpus" '' + test "$1" = ext-rachel -a "$2" = started || exit 0 + # The guest may start before knife-linux reserves its own CPUs here. + reserved=$(cat /sys/fs/cgroup/machine.slice/cpuset.cpus.exclusive) + echo "''${reserved:+$reserved,}${box.cpus}" > /sys/fs/cgroup/machine.slice/cpuset.cpus.exclusive + for scope in /sys/fs/cgroup/machine.slice/machine-qemu*ext\\x2drachel.scope; do + echo ${box.cpus} > $scope/cpuset.cpus.exclusive + echo ${box.cpus} > $scope/cpuset.cpus + echo root > $scope/cpuset.cpus.partition + done + ''; + + # libvirt-guests suspends guests at host shutdown, which fails with a passed-through GPU. + systemd.services.ext-rachel-shutdown = { + wantedBy = [ "multi-user.target" ]; + after = [ "libvirt-guests.service" ]; + # Stopping this unit shuts the guest down; a changed unit must not do that on switch. + restartIfChanged = false; + serviceConfig = { Type = "oneshot"; RemainAfterExit = true; TimeoutStopSec = 300; }; + path = [ pkgs.libvirt ]; + script = "true"; + preStop = '' + virsh shutdown ext-rachel 2>/dev/null || exit 0 + for _ in $(seq 1 120); do + virsh domstate ext-rachel | grep -q 'shut off' && exit 0 + sleep 2 + done + ''; + }; + + # Port 22 is the host's sshd, so the host account `rachel` only relays into the guest with its own key. + users.users.rachel = { + isSystemUser = true; + group = "rachel"; + home = "/var/lib/ext-rachel-relay"; + createHome = true; + shell = pkgs.bash; + openssh.authorizedKeys.keys = box.authorizedKeys; + }; + users.groups.rachel = { }; + systemd.services.ext-rachel-relay-key = { + wantedBy = [ "multi-user.target" ]; + serviceConfig = { Type = "oneshot"; RemainAfterExit = true; User = "rachel"; }; + script = '' + test -f ~/id_ed25519 || ${pkgs.openssh}/bin/ssh-keygen -q -t ed25519 -N "" -C ext-rachel-relay -f ~/id_ed25519 + ''; + }; + services.openssh.extraConfig = let + relay = pkgs.writeShellScript "ext-rachel-relay" '' + ssh() { exec ${pkgs.openssh}/bin/ssh -F /dev/null -e none -o BatchMode=yes -i ~/id_ed25519 \ + -o UserKnownHostsFile=~/known_hosts -o StrictHostKeyChecking=accept-new "$@" rachel@${box.guestAddress} "''${cmd[@]}"; } + case "$SSH_ORIGINAL_COMMAND" in + "") cmd=(); [ -t 0 ] && ssh -t || ssh ;; + *sftp-server) cmd=(sftp); ssh -s ;; + *) cmd=("$SSH_ORIGINAL_COMMAND"); [ -t 0 ] && ssh -t || ssh ;; + esac + ''; + in '' + Match User rachel + ForceCommand ${relay} + PasswordAuthentication no + KbdInteractiveAuthentication no + AllowTcpForwarding no + AllowAgentForwarding no + AllowStreamLocalForwarding no + PermitTunnel no + X11Forwarding no + ''; + + networking.nat = { + enable = true; + externalInterface = "enp4s0"; + internalInterfaces = [ box.bridge ]; + forwardPorts = [ + { sourcePort = box.sshPort; destination = "${box.guestAddress}:22"; proto = "tcp"; } + ]; + }; + + networking.firewall.extraCommands = '' + iptables -N ext-rachel 2>/dev/null || iptables -F ext-rachel + iptables -A ext-rachel -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT + ${lib.concatMapStringsSep "\n" (range: "iptables -A ext-rachel -d ${range} -j DROP") privateRanges} + iptables -A ext-rachel -p tcp --dport 25 -j DROP + iptables -A ext-rachel -j ACCEPT + iptables -I FORWARD -i ${box.bridge} -j ext-rachel + iptables -I nixos-fw 1 -i ${box.bridge} -j DROP + iptables -I nixos-fw 1 -i ${box.bridge} -p udp -m multiport --dports 53,67 -j ACCEPT + iptables -I nixos-fw 1 -i ${box.bridge} -p tcp --dport 53 -j ACCEPT + iptables -I nixos-fw 1 -i ${box.bridge} -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT + ''; + networking.firewall.extraStopCommands = '' + iptables -D FORWARD -i ${box.bridge} -j ext-rachel 2>/dev/null || true + iptables -F ext-rachel 2>/dev/null || true + iptables -X ext-rachel 2>/dev/null || true + ''; +} diff --git a/nixos/knife-linux.nix b/nixos/knife-linux.nix index d8736d1abf8010a9cc8e55730764c823dd7d987d..c2ffaa86f0daf5bc11d73389e2d842bb0e5336ed 100644 --- a/nixos/knife-linux.nix +++ b/nixos/knife-linux.nix @@ -14,7 +14,8 @@ let exec ${pkgs.podman}/bin/podman --root=${root}/containers/storage --runroot=/run/knife-linux/containers "$@" ''; run = "${podman}/bin/knife-linux-podman"; - cpus = "3-15,19-31"; + cpus = "8-15,24-31"; + extRachel = builtins.fromJSON (builtins.readFile ../config/ext-rachel.json); checkWork = pkgs.writeShellScript "check-knife-work" '' test "$(${pkgs.util-linux}/bin/findmnt -n -o SOURCE --mountpoint /srv/work)" = work ''; @@ -66,19 +67,19 @@ in run "run --rm --replace --name=knife-linux --pull=never" "--sdnotify=conmon --cgroups=no-conmon --log-driver=journald --init --hostname=knife-linux" "--user=${uid}:${gid} --umask=0077 --cap-drop=ALL --security-opt=no-new-privileges" - "--memory=96g --pids-limit=32768 --shm-size=2g --network=podman" + "--memory=72g --pids-limit=32768 --shm-size=2g --network=podman" "--volume=${root}/home:/home/clo" "--volume=/srv/work:/work" "--volume=/opt/studio-project-store/current/bin/jj-real:/usr/local/bin/jj:ro" "--device=/dev/kvm" "localhost/knife-linux" ]; - # Agents get 13 cores to themselves and the host keeps three. A partition + # Agents get 8 cores to themselves, ext-rachel 5 and the host keeps three. A partition # rather than a CPU quota: an exhausted quota freezes the Knife server with the builds. ExecStartPost = "-" + pkgs.writeShellScript "knife-linux-cpus" '' set -e scope=/sys/fs/cgroup$(${run} inspect --format '{{.State.CgroupPath}}' knife-linux) - echo ${cpus} > /sys/fs/cgroup/machine.slice/cpuset.cpus.exclusive + echo ${cpus},${extRachel.cpus} > /sys/fs/cgroup/machine.slice/cpuset.cpus.exclusive echo ${cpus} > $scope/cpuset.cpus.exclusive echo ${cpus} > $scope/cpuset.cpus echo root > $scope/cpuset.cpus.partition diff --git a/nixos/snowglobe.nix b/nixos/snowglobe.nix index 81392202430f74dfbf021926a5ca10c75ae670e4..85dbe021f9796b3fb32a13438841edcfb0c6c73c 100644 --- a/nixos/snowglobe.nix +++ b/nixos/snowglobe.nix @@ -8,6 +8,7 @@ let irohNetwork = builtins.fromJSON (builtins.readFile ../service/iroh-relay/network.json); spacebarVoice = builtins.fromJSON (builtins.readFile ../service/spacebar/voice-network.json); extMantikafasi = builtins.fromJSON (builtins.readFile ../config/ext-mantikafasi.json); + extRachel = builtins.fromJSON (builtins.readFile ../config/ext-rachel.json); udpPorts = xmppUDPPorts ++ [ irohNetwork.quic spacebarVoice.media spacebarVoice.nativeMedia ]; encryptionRoots = { clover = "clover"; @@ -18,7 +19,7 @@ let }; in { - imports = [ ./snowglobe-network.nix ./mac-studio-sandbox.nix ./knife-linux.nix ./mac-vm.nix ./ext-mantikafasi.nix ./vm-disks.nix ./zfs-read-cache.nix ./gpu-unlock.nix ]; + imports = [ ./snowglobe-network.nix ./mac-studio-sandbox.nix ./knife-linux.nix ./mac-vm.nix ./ext-mantikafasi.nix ./ext-rachel.nix ./vm-disks.nix ./zfs-read-cache.nix ./gpu-unlock.nix ]; networking.hostName = "snowglobe"; environment.etc."machine-info".text = '' @@ -87,7 +88,7 @@ in script = '' description="$(${pkgs.python3}/bin/python3 ${./upnp-discover.py} ${lib.escapeShellArg config.networking.defaultGateway.address})" exec ${pkgs.miniupnpc}/bin/upnpc -u "$description" -e "Snow Globe services" -r \ - ${lib.concatMapStringsSep " " (port: "${toString port} TCP") (xmppTCPPorts ++ [ extMantikafasi.sshPort extMantikafasi.vncPort ])} \ + ${lib.concatMapStringsSep " " (port: "${toString port} TCP") (xmppTCPPorts ++ [ extMantikafasi.sshPort extMantikafasi.vncPort extRachel.sshPort ])} \ ${lib.concatMapStringsSep " " (port: "${toString port} UDP") (udpPorts ++ lib.range xmppNetwork.relay_first xmppNetwork.relay_last)} ''; }; @@ -148,7 +149,7 @@ in }; environment.variables.SNOWGLOBE_POOL = pool; environment.variables.SNOWGLOBE_DOMAIN = "paperclover.net"; - environment.variables.SNOWGLOBE_LOCAL_AI = "true"; + environment.variables.SNOWGLOBE_LOCAL_AI = "false"; system.stateVersion = "26.05"; } diff --git a/nixos/zfs-read-cache.nix b/nixos/zfs-read-cache.nix index b3e97092a97b2b4d3b466b5d732313dc7b914484..250beabbb47fc6601421a447d4a225f49f9f77ae 100644 --- a/nixos/zfs-read-cache.nix +++ b/nixos/zfs-read-cache.nix @@ -7,7 +7,7 @@ let in { # Reclaim pressure otherwise shrinks the ARC to a few GiB with most memory free. - boot.kernelParams = [ "zfs.zfs_arc_min=${toString (32 * 1024 * 1024 * 1024)}" ]; + boot.kernelParams = [ "zfs.zfs_arc_min=${toString (16 * 1024 * 1024 * 1024)}" ]; systemd.services.snowglobe-zfs-read-cache = { wantedBy = [ "multi-user.target" ]; diff --git a/tools/ext-rachel.md b/tools/ext-rachel.md new file mode 100644 index 0000000000000000000000000000000000000000..c3bc971b9388eef8fc5648b3773727f8103626f1 --- /dev/null +++ b/tools/ext-rachel.md @@ -0,0 +1,33 @@ +# ext-rachel + +`ext-rachel` is a Windows 11 Pro libvirt guest (`windows-11-pro-v2` preset, 10 vCPU, 40 GiB) owned by the +dashboard user `rachel`, with the RTX 3090 passed through. Its 500 GB disk is a standalone qcow2 on the `work` +NVMe pool (`work/ext-rachel` at `/srv/ext-rachel`); the creation artifacts, seed and `access.json` stay in +`/srv/vm/ext-rachel`. `config/ext-rachel.json` and `nixos/ext-rachel.nix` own the host side. + +``` +internet ──UPnP──> Snow Globe :2223 ─DNAT─> 10.101.0.2:22 (Windows OpenSSH, PowerShell) +internet ──> Snow Globe :22 as rachel ─ForceCommand ssh─> 10.101.0.2:22 +dashboard console / Computer MCP ─── libvirt VNC + guest agent (owner rachel) +guest ── virbr-ext (forward mode='open') ── MASQUERADE ── internet only +``` + +- **GPU:** `vfio-pci.ids` claims both 3090 functions at boot, so local AI is off (`SNOWGLOBE_LOCAL_AI=false`) + and `nvidia-persistenced` is disabled. The guest uses the GeForce driver; the emulated VGA stays the primary + display so the dashboard console and Computer MCP keep working. Games pick the 3090 in Windows' Graphics + settings ("High performance"). Restoring local AI means removing the import, a reboot, and redeploying. +- **Isolation:** libvirt adds no rules for an open network; `ext-rachel` drops new connections to private, + CGNAT and link-local ranges and port 25. From the guest, Snow Globe answers only DHCP and DNS. + Snow Globe itself can still connect into the guest. +- **CPUs and memory:** vCPUs are pinned 1:1 to cores 3-7 and their siblings 19-23, which a libvirt hook makes an + exclusive cpuset partition beside `knife-linux` (8 cores); the host keeps 0-2 and 16-18. VFIO pins all 40 GiB, so + the ZFS ARC floor is 16 GiB and `knife-linux` is capped at 72 GiB to leave headroom. +- **OpenSSH:** the guest runs the Win32-OpenSSH MSI. The Windows `OpenSSH.Server` capability was removed because its + servicing deleted the MSI's `sshd` service; `msiexec /fa ` restores it. +- **Shutdown:** `ext-rachel-shutdown` shuts the guest down before `libvirt-guests`, whose managed save cannot + capture a VFIO device. +- **Access:** account `rachel`, password in `/srv/vm/ext-rachel/access.json`. + `ssh rachel@snowglobe.paperclover.net` takes only the keys in `config/ext-rachel.json`; the host account + relays shells, commands and sftp/scp into the guest with `/var/lib/ext-rachel-relay/id_ed25519`, whose public + half must be in the guest's `administrators_authorized_keys`. Port 2223 also takes the password. MCP: `https://snowglobe.paperclover.net/mcp/computer`, + signed in as `rachel`; MCP grants only reach VMs the connection's own account owns. diff --git a/tools/knife-linux.md b/tools/knife-linux.md index 3fc588603b8c8d744df4cf5b02bc115d75f4911f..c43d5cd15d70d8b89f5eddeb307908575a927545 100644 --- a/tools/knife-linux.md +++ b/tools/knife-linux.md @@ -4,7 +4,7 @@ agents directly beside ZFS. [knife-linux.nix](../nixos/knife-linux.nix) builds it from [knife-linux.Containerfile](../nixos/knife-linux.Containerfile) on each start and runs it as the `knife-linux` system service: uid 3000 (`clo`), no -capabilities, 13 cores (26 threads) to itself, a 96 GiB memory limit, `/dev/kvm`, and Podman's NAT network. +capabilities, 8 cores (16 threads) to itself, a 72 GiB memory limit, `/dev/kvm`, and Podman's NAT network. Knife listens on container loopback port 3773; devices reach it through the mesh as `snowglobe` (device `4b85ee48…`). User namespaces work inside, but Podman's seccomp filter blocks the `chroot` Chromium-based sandboxes need, so Chromium,