XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
ops: the hosted macOS as a systemd service (scripts/install-service.sh, /etc/xnulinux.conf, xnulinux-boot.sh --wait with run logs in /var/log/xnulinux/)
Heyang Zhou6f8e96a7630b
diff --git a/scripts/install-service.sh b/scripts/install-service.shnew file mode 100755index 0000000..9452547--- /dev/null+++ b/scripts/install-service.sh@@ -0,0 +1,45 @@+#!/bin/bash+# In the VM (once, after scripts/vm-install-fki-service.sh and with the disk from scripts/mk-macvm-disk.sh): run the hosted+# macOS as a system service. It starts at boot, is stopped cleanly (its disk synced) when the VM shuts down, and is+# started again after a panic or a run that ended by itself; each run's kernel log is in /var/log/xnulinux/.+# sudo systemctl status xnulinux journalctl -u xnulinux settings: /etc/xnulinux.conf (then: systemctl restart xnulinux)+set -euo pipefail+P=$(cd "$(dirname "$0")/.." && pwd)+U=$(id -un)+[ -f /var/tmp/mac.conf ] || { echo "no /var/tmp/mac.conf: make the disk first (scripts/mk-macvm-disk.sh)" >&2; exit 1; }+[ -f /etc/xnulinux.conf ] || sudo tee /etc/xnulinux.conf >/dev/null <<'CONF'+# The hosted macOS (scripts/xnulinux-boot.sh reads this, then /var/tmp/mac.conf for the disk's UUIDs); the environment+# overrides what is written as ${NAME:-default}+CPUS=${CPUS:-2} # its CPUs (the Linux CPUs from 0 up; Linux keeps the rest and its device interrupts)+RAM_MB=${RAM_MB:-7900} # its memory, within fki's carve-out+#DISK=/var/tmp/mac.img+#GPU=auto # off: CPU rendering only+#XNU_ARGS="model=VirtualMac2,1"+#BOOTARGS="amfi_get_out_of_my_way=1 lp-sip0=2050"+CONF+sudo tee /etc/systemd/system/xnulinux.service >/dev/null <<UNIT+[Unit]+Description=xnulinux: the hosted macOS+After=xnulinux-fki.service network-online.target+Wants=network-online.target+Requires=xnulinux-fki.service++[Service]+Type=simple+User=$U+WorkingDirectory=$P+ExecStart=$P/scripts/xnulinux-boot.sh --wait+ExecStop=$P/scripts/xnulinux-boot.sh stop+TimeoutStartSec=infinity+TimeoutStopSec=60+Restart=on-failure+RestartSec=10+StartLimitIntervalSec=600+StartLimitBurst=5++[Install]+WantedBy=multi-user.target+UNIT+sudo systemctl daemon-reload+sudo systemctl enable xnulinux.service+echo "installed: sudo systemctl start xnulinux (and at every boot); settings in /etc/xnulinux.conf"diff --git a/scripts/xnulinux-boot.sh b/scripts/xnulinux-boot.shindex d546c8e..e43cc2d 100755--- a/scripts/xnulinux-boot.sh+++ b/scripts/xnulinux-boot.sh@@ -4,7 +4,13 @@# and loads xnu.ko on the disk mk-macvm-disk.sh made. Returns once XNU is running; xnu.ko stays loaded until the hosted# system halts or `xnulinux-boot.sh stop` ends it.#-# Configuration: /var/tmp/mac.conf (written by mk-macvm-disk.sh: ROOT_UUID, BOOT_UUID), then the environment:+# Usage: xnulinux-boot.sh boot it and return once XNU runs+# xnulinux-boot.sh --wait boot it and stay until the run ends (systemd: scripts/install-service.sh); the run's+# kernel log goes to /var/log/xnulinux/, the exit status says how it ended+# xnulinux-boot.sh stop end it (the disk synced first)+#+# Configuration: /etc/xnulinux.conf, then /var/tmp/mac.conf (written by mk-macvm-disk.sh: ROOT_UUID, BOOT_UUID), then the+# environment:# DISK (/var/tmp/mac.img) RAM_MB (7900) BOOTARGS CONSOLE_IN (/var/tmp/console-in) INPUT_IN (/var/tmp/xnu-input)# FB_DUMP (/var/tmp/xnu-fb.raw) KEYSTORE (/var/tmp/xnu-keystore.bin) TRUSTCACHE (xnu/trustcache-mac.bin)# IMAGE (xnu/kc.bin: the kernel collection under /lib/firmware) GPU (auto|off) CPUS (1: the hosted system's CPUs) XNU_ARGS (more xnu.ko parameters)@@ -18,6 +24,8 @@ if [ "${1:-}" = stop ]; then # end the running hosted system (its disk synced fisudo rmmod xnu && echo "hosted system stopped"exitfi+WAIT=0; [ "${1:-}" = --wait ] && WAIT=1+[ -f /etc/xnulinux.conf ] && . /etc/xnulinux.conf[ -f /var/tmp/mac.conf ] && . /var/tmp/mac.confDISK=${DISK:-/var/tmp/mac.img}; RAM_MB=${RAM_MB:-7900}; GPU=${GPU:-auto}; CPUS=${CPUS:-1}CONSOLE_IN=${CONSOLE_IN:-/var/tmp/console-in}; INPUT_IN=${INPUT_IN:-/var/tmp/xnu-input}; FB_DUMP=${FB_DUMP:-/var/tmp/xnu-fb.raw}@@ -83,3 +91,17 @@ if [ "$GPU" != off ] && [ -e /dev/dri/renderD128 ]; thenecho "gpu: xnudrmd on /dev/dri/renderD128 (journalctl -u xnudrmd)"fiecho "booting: console scripts/xnu-console.sh, screen tools/hosted/fb2png.py $FB_DUMP"+[ $WAIT = 1 ] || exit 0++# --wait: until the run ends (insmod returns), then keep its kernel log and say how it ended: 0 a halt or a stop asked for,+# 2 an XNU panic, 3 the run ended otherwise+START=$(date +%s)+while pgrep -f "insmod xnu-ko/xnu.ko" >/dev/null; do sleep 5; done+LOGDIR=/var/log/xnulinux; sudo mkdir -p "$LOGDIR"+LOG=$LOGDIR/run-$(date -d @"$START" +%Y%m%d-%H%M%S).log+{ cat /var/tmp/xnu-insmod.log; sudo journalctl -k -q -o short-monotonic --no-pager --since @"$START" | grep -a "fki:"; } |+ sudo tee "$LOG" >/dev/null+ls -1t "$LOGDIR"/run-*.log | tail -n +21 | xargs -r sudo rm -f # the last 20 runs+if grep -aq "XNU panic" "$LOG"; then echo "the hosted system panicked: $LOG" >&2; exit 2; fi+if grep -aq "run stopped by the host\|halt\|shutdown" "$LOG"; then echo "the hosted system ended: $LOG"; exit 0; fi+echo "the run ended: $LOG" >&2; exit 3