Skip to content

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

C

krun-vm.sh: hold tart's lock on the VM while krunkit runs it (#18)

Heyang Zhou9e770f8aa597
diff --git a/scripts/krun-vm.sh b/scripts/krun-vm.sh
index b2434a8..c909796 100644
--- a/scripts/krun-vm.sh
+++ b/scripts/krun-vm.sh
@@ -2,7 +2,9 @@
# SPDX-License-Identifier: GPL-2.0-only
# 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
+# Metal on the host GPU). The VM's disk is tart's own, and two VMs on one disk destroy its file system: krunkit runs holding
+# tart's own lock on the VM (an fcntl write lock on its config.json), so `tart list` shows it running, `tart run` refuses
+# to start it (and `tart stop` ends krunkit), and this script refuses while tart, or another krunkit, has it. 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).
#
@@ -13,7 +15,22 @@ 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:-6}; MEM=${MEM:-16384}; 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
+CONFIG=$(dirname "$DISK")/config.json
+# locked COMMAND...: takes tart's lock on the VM, then becomes COMMAND, which keeps it for its lifetime (an fcntl lock is the
+# process's own, so exec keeps it; the descriptor must stay open)
+locked() {
+ exec python3 - "$CONFIG" "$@" <<'PY'
+import fcntl, os, sys
+fd = os.open(sys.argv[1], os.O_RDWR)
+try:
+ fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
+except OSError:
+ sys.exit(f'{os.path.dirname(sys.argv[1])}: the VM is running (tart or another krunkit has its lock): stop it first')
+os.set_inheritable(fd, True)
+os.execv(sys.argv[2], sys.argv[2:])
+PY
+}
+(locked /usr/bin/true) || exit 1 # before anything of a running VM's is touched
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" &
@@ -21,7 +38,7 @@ 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" \
+locked "$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" \