Skip to content

XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)

C

scripts: run the xnupoc VM under krunkit (virtio-gpu Venus on the host GPU)

Heyang Zhou1cc031aa621b
diff --git a/scripts/krun-vm.sh b/scripts/krun-vm.sh
new file mode 100644
index 0000000..421eeb4
--- /dev/null
+++ b/scripts/krun-vm.sh
@@ -0,0 +1,42 @@
+#!/bin/bash
+# On the Mac host: run the xnupoc Linux VM under krunkit (libkrun on Hypervisor.framework) instead of tart, for a GPU the
+# guest can use: krunkit gives Linux a virtio-gpu with Venus (Vulkan commands forwarded to virglrenderer, then MoltenVK and
+# Metal on the host GPU). The VM's disk is tart's own (stop tart first; both never run at once). Networking is gvproxy
+# (user-mode, NAT to the host's network) with the guest's SSH on 127.0.0.1:$SSH_PORT, forwarded to whatever address the
+# guest got from gvproxy's DHCP (it is not always .2: a lease from an earlier run can move it to .3).
+#
+# Needs krunkit, libkrun-efi, virglrenderer, MoltenVK and gvproxy under $KRUN (scripts/krun-install.sh).
+set -euo pipefail
+KRUN=${KRUN:-$HOME/.local/krunkit}
+DISK=${DISK:-$HOME/.tart/vms/xnupoc/disk.img}
+MAC=${MAC:-$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["macAddress"])' "$(dirname "$DISK")/config.json")}
+CPUS=${CPUS:-12}; MEM=${MEM:-24576}; SSH_PORT=${SSH_PORT:-2223}
+S=${S:-$HOME/.local/state/xnupoc-krun}; mkdir -p "$S"
+if pgrep -f "tart run xnupoc" >/dev/null; then echo "tart is running xnupoc: stop it first" >&2; exit 1; fi
+rm -f "$S"/gv.sock "$S"/gvapi.sock
+"$KRUN"/bin/gvproxy -listen-vfkit "unixgram://$S/gv.sock" -listen "unix://$S/gvapi.sock" -ssh-port -1 \
+ -log-file "$S/gvproxy.log" -pid-file "$S/gvproxy.pid" &
+GV=$!
+trap 'kill $GV 2>/dev/null' EXIT
+for _ in $(seq 1 50); do [ -S "$S/gv.sock" ] && break; sleep 0.1; done
+: > "$S/console.log"
+"$KRUN"/bin/krunkit --cpus "$CPUS" --memory "$MEM" \
+ --bootloader "efi,variable-store=$S/efi-vars,create" \
+ --device "virtio-blk,path=$DISK,format=raw" \
+ --device "virtio-net,type=unixgram,path=$S/gv.sock,mac=$MAC,offloading=on,vfkitMagic=on" \
+ --device "virtio-serial,logFilePath=$S/console.log" \
+ --device "virtio-gpu,width=1280,height=800" \
+ --restful-uri "unix://$S/rest.sock" --pidfile "$S/krunkit.pid" --log-file "$S/krunkit.log" &
+KR=$!
+trap 'kill $KR $GV 2>/dev/null' EXIT
+api() { curl -sf --unix-socket "$S/gvapi.sock" "http://gv$1" "${@:2}"; }
+for _ in $(seq 1 120); do
+ ip=$(api /services/dhcp/leases | python3 -c 'import json,sys; m=sys.argv[1].lower(); print(next((k for k,v in json.load(sys.stdin).items() if v.lower()==m), ""))' "$MAC" 2>/dev/null || true)
+ [ -n "$ip" ] && break; sleep 1
+done
+if [ -n "$ip" ]; then
+ api /services/forwarder/expose -X POST -d "{\"local\":\"127.0.0.1:$SSH_PORT\",\"remote\":\"$ip:22\"}" && echo "ssh: 127.0.0.1:$SSH_PORT -> $ip:22"
+else
+ echo "the guest never took a DHCP lease" >&2
+fi
+wait $KR
diff --git a/scripts/vm.sh b/scripts/vm.sh
index dcbd89e..9e5a33e 100755
--- a/scripts/vm.sh
+++ b/scripts/vm.sh
@@ -1,7 +1,8 @@
#!/bin/bash
# On tokyo: run a command (or a script with -s FILE) in the xnupoc VM.
W="$(cd "$(dirname "$0")/../.." && pwd)" # the session work dir (vmkey, vm.ip)
-ip=$(cat $W/vm.ip); O="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=10 -o ServerAliveInterval=30 -o LogLevel=ERROR -i $W/vmkey"
+ip=$(cat $W/vm.ip); port=$(cat $W/vm.port 2>/dev/null || echo 22) # vm.port: 2223 when the VM runs under krunkit (gvproxy)
+O="-o Port=$port -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=10 -o ServerAliveInterval=30 -o LogLevel=ERROR -i $W/vmkey"
if [ "$1" = -s ]; then exec ssh $O admin@$ip bash -s < "$2"; fi
if [ "$1" = -put ]; then exec scp -O $O -r "$2" admin@$ip:"$3"; fi
if [ "$1" = -get ]; then exec scp -O $O -r admin@$ip:"$2" "$3"; fi