Run a virtual iPhone on an Apple Silicon Mac.
Swift
Utilize APFS CoW to make backups instant
vnescape74d0df7254f9
diff --git a/scripts/vm_backup.sh b/scripts/vm_backup.shindex c0f8c69b..03229074 100755--- a/scripts/vm_backup.sh+++ b/scripts/vm_backup.sh@@ -1,7 +1,7 @@#!/bin/zsh# vm_backup.sh — Save the current VM as a named backup.#-# Backups are stored under vm.backups/<name>/ using rsync --sparse.+# Backups are stored under vm.backups/<name>/.# The active VM remembers its name in vm/.vm_name for use by vm_switch.## Usage:@@ -9,6 +9,11 @@# make vm_backup NAME=ios18-jb BACKUP_INCLUDE_IPSW=1set -euo pipefail+# Try cp -c for APFS clone/COW first; fall back to cp -a where -c is unsupported.+_vphone_cp() {+ cp -a -c "$@" 2>/dev/null || cp -a "$@"+}+VM_DIR="${VM_DIR:-vm}"BACKUPS_DIR="${BACKUPS_DIR:-vm.backups}"NAME="${NAME:-}"@@ -68,19 +73,31 @@ echo "Dest : ${DEST}/"src_size="$(du -sh "${VM_DIR}" 2>/dev/null | cut -f1)"echo "Size : ${src_size} (on disk)"-RSYNC_EXCLUDES=()if [[ "${BACKUP_INCLUDE_IPSW}" != "1" ]]; then- RSYNC_EXCLUDES+=(--exclude '*_Restore*/')echo "IPSW : excluded (use BACKUP_INCLUDE_IPSW=1 to include)"+else+ echo "IPSW : included"fiecho ""# --- Sync ---mkdir -p "${DEST}"-rsync -aH --sparse --progress --delete \- "${RSYNC_EXCLUDES[@]}" \- "${VM_DIR}/" "${DEST}/"+# Use cp for speed.+# On APFS, cp -c can create an instant copy-on-write clone.+# If -c is unsupported, _vphone_cp falls back to cp -a.+while IFS= read -r -d '' item; do+ base="$(basename "${item}")"++ # Skip IPSW restore dirs if excluded.+ if [[ "${BACKUP_INCLUDE_IPSW}" != "1" && "${base}" == *_Restore* ]]; then+ continue+ fi++ if [[ -d "${item}" || -f "${item}" ]]; then+ _vphone_cp "${item}" "${DEST}/"+ fi+done < <(find "${VM_DIR}" -mindepth 1 -maxdepth 1 -print0)# Tag the active VM with this nameecho "${NAME}" > "${VM_DIR}/.vm_name"diff --git a/scripts/vm_restore.sh b/scripts/vm_restore.shindex 305a874b..cd970b4a 100755--- a/scripts/vm_restore.sh+++ b/scripts/vm_restore.sh@@ -4,8 +4,14 @@# Usage:# make vm_restore NAME=ios17# make vm_restore NAME=ios17 FORCE=1+set -euo pipefail+# Try cp -c for APFS clone/COW first; fall back to cp -a where -c is unsupported.+_vphone_cp() {+ cp -a -c "$@" 2>/dev/null || cp -a "$@"+}+VM_DIR="${VM_DIR:-vm}"BACKUPS_DIR="${BACKUPS_DIR:-vm.backups}"NAME="${NAME:-}"@@ -14,12 +20,50 @@ FORCE="${FORCE:-0}"validate_backup_name() {local name="$1"local label="${2:-NAME}"++ if [[ -z "${name}" ]]; then+ echo "ERROR: ${label} is required."+ exit 1+ fi+if [[ "$name" == */* || "$name" == .* ]]; thenecho "ERROR: ${label} must be a simple identifier (no slashes or leading dots)."exit 1fi}+list_backups() {+ if [[ ! -d "${BACKUPS_DIR}" ]]; then+ echo " (none)"+ return+ fi++ local found=0++ while IFS= read -r -d '' d; do+ if [[ -f "${d}/config.plist" ]]; then+ echo " - $(basename "${d}")"+ found=1+ fi+ done < <(find "${BACKUPS_DIR}" -mindepth 1 -maxdepth 1 -type d -print0)++ if [[ "${found}" != "1" ]]; then+ echo " (none)"+ fi+}++safe_clear_vm_dir() {+ if [[ -z "${VM_DIR}" || "${VM_DIR}" == "/" || "${VM_DIR}" == "." ]]; then+ echo "ERROR: Refusing to clear unsafe VM_DIR: '${VM_DIR}'"+ exit 1+ fi++ mkdir -p "${VM_DIR}"++ # Delete all direct contents, including hidden files.+ find "${VM_DIR}" -mindepth 1 -maxdepth 1 -exec rm -rf -- {} ++}+# --- Parse args ---while [[ $# -gt 0 ]]; docase "$1" in@@ -38,13 +82,7 @@ if [[ -z "${NAME}" ]]; thenecho " Usage: make vm_restore NAME=ios17"echo ""echo "Available backups:"- if [[ -d "${BACKUPS_DIR}" ]]; then- for d in "${BACKUPS_DIR}"/*/; do- [[ -f "${d}config.plist" ]] && echo " - $(basename "${d}")"- done- else- echo " (none)"- fi+ list_backupsexit 1fi@@ -57,13 +95,7 @@ if [[ ! -d "${SRC}" ]]; thenecho "ERROR: Backup '${NAME}' not found at ${SRC}/"echo ""echo "Available backups:"- if [[ -d "${BACKUPS_DIR}" ]]; then- for d in "${BACKUPS_DIR}"/*/; do- [[ -f "${d}config.plist" ]] && echo " - $(basename "${d}")"- done- else- echo " (none)"- fi+ list_backupsexit 1fi@@ -80,11 +112,12 @@ if pgrep -f "vphone-cli.*--config.*${VM_DIR}" >/dev/null 2>&1; thenfi# --- Confirm overwrite ----if [[ -d "${VM_DIR}" && -f "${VM_DIR}/Disk.img" && "${FORCE}" != "1" ]]; then+if [[ -d "${VM_DIR}" && "${FORCE}" != "1" ]]; thencurrent=""[[ -f "${VM_DIR}/.vm_name" ]] && current="$(< "${VM_DIR}/.vm_name")"echo "WARNING: ${VM_DIR}/ already exists${current:+ (current: '${current}')}."- echo " This will overwrite it with backup '${NAME}'."+ echo " This will replace it with backup '${NAME}'."+ echo " Existing files in ${VM_DIR}/ will be deleted first."echo " Back up first with: make vm_backup NAME=<name>"printf "Continue? [y/N] "read -r answer@@ -99,13 +132,16 @@ backup_size="$(du -sh "${SRC}" 2>/dev/null | cut -f1)"echo "Size : ${backup_size} (on disk)"echo ""-# --- Sync ----mkdir -p "${VM_DIR}"+# --- Restore ---+safe_clear_vm_dir-rsync -aH --sparse --progress --delete \- "${SRC}/" "${VM_DIR}/"+while IFS= read -r -d '' item; do+ if [[ -d "${item}" || -f "${item}" ]]; then+ _vphone_cp "${item}" "${VM_DIR}/"+ fi+done < <(find "${SRC}" -mindepth 1 -maxdepth 1 -print0)-# Tag the active VM+# Tag the active VM.echo "${NAME}" > "${VM_DIR}/.vm_name"echo ""diff --git a/scripts/vm_switch.sh b/scripts/vm_switch.shindex 37b5335e..4ab3204c 100755--- a/scripts/vm_switch.sh+++ b/scripts/vm_switch.sh@@ -6,8 +6,15 @@## Usage:# make vm_switch NAME=ios18+# make vm_switch NAME=ios18 BACKUP_INCLUDE_IPSW=1+set -euo pipefail+# Try cp -c for APFS clone/COW first; fall back to cp -a where -c is unsupported.+_vphone_cp() {+ cp -a -c "$@" 2>/dev/null || cp -a "$@"+}+VM_DIR="${VM_DIR:-vm}"BACKUPS_DIR="${BACKUPS_DIR:-vm.backups}"NAME="${NAME:-}"@@ -16,12 +23,88 @@ BACKUP_INCLUDE_IPSW="${BACKUP_INCLUDE_IPSW:-0}"validate_backup_name() {local name="$1"local label="${2:-NAME}"- if [[ "$name" == */* || "$name" == .* ]]; then++ if [[ -z "${name}" ]]; then+ echo "ERROR: ${label} is required."+ exit 1+ fi++ if [[ "${name}" == */* || "${name}" == .* ]]; thenecho "ERROR: ${label} must be a simple identifier (no slashes or leading dots)."exit 1fi}+validate_safe_path() {+ local path="$1"+ local label="$2"++ if [[ -z "${path}" || "${path}" == "/" || "${path}" == "." || "${path}" == ".." ]]; then+ echo "ERROR: Refusing unsafe ${label}: '${path}'"+ exit 1+ fi+}++list_backups() {+ if [[ ! -d "${BACKUPS_DIR}" ]]; then+ echo " (none)"+ return+ fi++ local found=0++ while IFS= read -r -d '' d; do+ if [[ -f "${d}/config.plist" ]]; then+ echo " - $(basename "${d}")"+ found=1+ fi+ done < <(find "${BACKUPS_DIR}" -mindepth 1 -maxdepth 1 -type d -print0)++ if [[ "${found}" != "1" ]]; then+ echo " (none)"+ fi+}++copy_children() {+ local src="$1"+ local dst="$2"+ local include_ipsw="${3:-1}"++ mkdir -p "${dst}"++ while IFS= read -r -d '' item; do+ base="$(basename "${item}")"++ if [[ "${include_ipsw}" != "1" && "${base}" == *_Restore* ]]; then+ continue+ fi++ _vphone_cp "${item}" "${dst}/"+ done < <(find "${src}" -mindepth 1 -maxdepth 1 \( -type f -o -type d -o -type l \) -print0)+}++replace_dir_with_tmp() {+ local tmp="$1"+ local dest="$2"+ local old="${dest}.old.$$"++ rm -rf -- "${old}"++ if [[ -e "${dest}" || -L "${dest}" ]]; then+ mv -- "${dest}" "${old}"+ fi++ if ! mv -- "${tmp}" "${dest}"; then+ if [[ -e "${old}" || -L "${old}" ]]; then+ mv -- "${old}" "${dest}"+ fi+ echo "ERROR: Failed to replace ${dest}"+ exit 1+ fi++ rm -rf -- "${old}"+}+# --- Parse args ---while [[ $# -gt 0 ]]; docase "$1" in@@ -40,17 +123,13 @@ if [[ -z "${NAME}" ]]; thenecho " Usage: make vm_switch NAME=ios18"echo ""echo "Available backups:"- if [[ -d "${BACKUPS_DIR}" ]]; then- for d in "${BACKUPS_DIR}"/*/; do- [[ -f "${d}config.plist" ]] && echo " - $(basename "${d}")"- done- else- echo " (none)"- fi+ list_backupsexit 1fivalidate_backup_name "${NAME}"+validate_safe_path "${VM_DIR}" "VM_DIR"+validate_safe_path "${BACKUPS_DIR}" "BACKUPS_DIR"TARGET="${BACKUPS_DIR}/${NAME}"@@ -58,13 +137,7 @@ if [[ ! -d "${TARGET}" || ! -f "${TARGET}/config.plist" ]]; thenecho "ERROR: Backup '${NAME}' not found."echo ""echo "Available backups:"- if [[ -d "${BACKUPS_DIR}" ]]; then- for d in "${BACKUPS_DIR}"/*/; do- [[ -f "${d}config.plist" ]] && echo " - $(basename "${d}")"- done- else- echo " (none)"- fi+ list_backupsexit 1fi@@ -75,6 +148,8 @@ if pgrep -f "vphone-cli.*--config.*${VM_DIR}" >/dev/null 2>&1; thenexit 1fi+mkdir -p "${BACKUPS_DIR}"+# --- Determine current VM name ---CURRENT=""if [[ -d "${VM_DIR}" && -f "${VM_DIR}/config.plist" ]]; then@@ -85,7 +160,8 @@ if [[ -d "${VM_DIR}" && -f "${VM_DIR}/config.plist" ]]; thenif [[ -z "${CURRENT}" ]]; thenecho "Current VM has no name. Give it one to save before switching."printf "Name for current VM: "- read -r CURRENT+ read -r CURRENT || CURRENT=""+if [[ -z "${CURRENT}" ]]; thenecho "ERROR: Cannot switch without saving the current VM."exit 1@@ -99,19 +175,18 @@ if [[ -d "${VM_DIR}" && -f "${VM_DIR}/config.plist" ]]; thenexit 0fi- # --- Save current ---echo "=== Saving current VM as '${CURRENT}' ==="+CURRENT_DEST="${BACKUPS_DIR}/${CURRENT}"- mkdir -p "${CURRENT_DEST}"+ TMP_CURRENT_DEST="${BACKUPS_DIR}/.${CURRENT}.tmp.$$"- RSYNC_EXCLUDES=()- if [[ "${BACKUP_INCLUDE_IPSW}" != "1" ]]; then- RSYNC_EXCLUDES+=(--exclude '*_Restore*/')- fi+ rm -rf -- "${TMP_CURRENT_DEST}"+ mkdir -p "${TMP_CURRENT_DEST}"++ copy_children "${VM_DIR}" "${TMP_CURRENT_DEST}" "${BACKUP_INCLUDE_IPSW}"+ echo "${CURRENT}" > "${TMP_CURRENT_DEST}/.vm_name"- rsync -aH --sparse --progress --delete \- "${RSYNC_EXCLUDES[@]}" \- "${VM_DIR}/" "${CURRENT_DEST}/"+ replace_dir_with_tmp "${TMP_CURRENT_DEST}" "${CURRENT_DEST}"echo ""fi@@ -119,12 +194,15 @@ fi# --- Restore target ---echo "=== Restoring '${NAME}' ==="-mkdir -p "${VM_DIR}"+TMP_VM_DIR="${VM_DIR}.restore.$$"++rm -rf -- "${TMP_VM_DIR}"+mkdir -p "${TMP_VM_DIR}"-rsync -aH --sparse --progress --delete \- "${TARGET}/" "${VM_DIR}/"+copy_children "${TARGET}" "${TMP_VM_DIR}" "1"+echo "${NAME}" > "${TMP_VM_DIR}/.vm_name"-echo "${NAME}" > "${VM_DIR}/.vm_name"+replace_dir_with_tmp "${TMP_VM_DIR}" "${VM_DIR}"echo ""echo "=== Switched: ${CURRENT:+${CURRENT} → }${NAME} ==="