Skip to content

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

C

ops: scripts/install-krun-agent.sh keeps the krunkit VM running on the Mac (launchd: started again after a guest reboot, and at login)

Heyang Zhou08799fd1ca65
diff --git a/scripts/install-krun-agent.sh b/scripts/install-krun-agent.sh
new file mode 100755
index 0000000..f1ced9e
--- /dev/null
+++ b/scripts/install-krun-agent.sh
@@ -0,0 +1,46 @@
+#!/bin/bash
+# On the Mac host: keep the Linux VM running under krunkit as a launchd agent of this user. krunkit exits when the guest
+# reboots or powers off; launchd starts it again (KeepAlive), and at login (RunAtLoad). With the hosted macOS as a service
+# in the VM (scripts/install-service.sh) a reboot of either comes back by itself.
+# scripts/install-krun-agent.sh install and start
+# scripts/install-krun-agent.sh remove stop the VM (the guest shut down first) and remove the agent
+# Settings: the environment of scripts/krun-vm.sh (CPUS, MEM, DISK, SSH_PORT), written into the agent at install.
+set -euo pipefail
+P=$(cd "$(dirname "$0")/.." && pwd)
+L=org.xnulinux.krun-vm
+domain() { launchctl print "gui/$(id -u)" >/dev/null 2>&1 && echo "gui/$(id -u)" || echo "user/$(id -u)"; } # no login session: the user domain
+PL=$HOME/Library/LaunchAgents/$L.plist
+S=${S:-$HOME/.local/state/xnupoc-krun}
+if [ "${1:-}" = remove ]; then
+ # a clean guest shutdown first (the hosted macOS's disk synced by xnulinux.service), then the agent
+ ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=10 -p "${SSH_PORT:-2223}" \
+ ${VMKEY:+-i "$VMKEY"} [email protected] 'sudo systemctl poweroff' 2>/dev/null || true
+ for _ in $(seq 1 120); do pgrep -f "$HOME/.local/krunkit/bin/krunkit" >/dev/null || break; sleep 1; done
+ launchctl bootout "$(domain)/$L" 2>/dev/null || true
+ rm -f "$PL"; echo "removed"; exit 0
+fi
+mkdir -p "$(dirname "$PL")" "$S"
+env_xml() { for v in CPUS MEM DISK SSH_PORT KRUN; do [ -n "${!v:-}" ] && printf ' <key>%s</key><string>%s</string>\n' "$v" "${!v}"; done; true; }
+cat > "$PL" <<PLIST
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>Label</key><string>$L</string>
+ <key>ProgramArguments</key><array><string>/bin/bash</string><string>$P/scripts/krun-vm.sh</string></array>
+ <key>EnvironmentVariables</key>
+ <dict>
+ <key>PATH</key><string>/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin</string>
+$(env_xml)
+ </dict>
+ <key>RunAtLoad</key><true/>
+ <key>KeepAlive</key><true/>
+ <key>ThrottleInterval</key><integer>10</integer>
+ <key>StandardOutPath</key><string>$S/agent.log</string>
+ <key>StandardErrorPath</key><string>$S/agent.log</string>
+</dict>
+</plist>
+PLIST
+launchctl bootout "$(domain)/$L" 2>/dev/null || true
+launchctl bootstrap "$(domain)" "$PL"
+echo "installed: $L (log $S/agent.log); scripts/install-krun-agent.sh remove to stop it"