Run a virtual iPhone on an Apple Silicon Mac.
Swift
Preserve VM boot state when cloning and launching
Lakr6e9732de680e
diff --git a/Documents/Guides/create-and-run.md b/Documents/Guides/create-and-run.mdindex 250acbfb..545a730f 100644--- a/Documents/Guides/create-and-run.md+++ b/Documents/Guides/create-and-run.md@@ -68,4 +68,6 @@ vphone-cli vm export myphone --out myphone.tzstvphone-cli vm import myphone.tzst --name restored```+`vm clone` copies the complete machine state, including its device identity and boot files. It uses APFS copy-on-write when available. Edit the identity yourself if you need a different device.+Run resource-heavy creations **one at a time**. Both the IPSWs and temporary restore tree consume substantial disk space, and patching large caches can be memory intensive. Check free space before starting a second VM.diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineTransferCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineTransferCommand.swiftindex 45f62301..d0be3565 100644--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineTransferCommand.swift+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneVirtualMachineTransferCommand.swift@@ -6,8 +6,8 @@ import VPhoneCoreKitstruct VPhoneVirtualMachineCloneCommand: ParsableCommand {static let configuration = CommandConfiguration(commandName: "clone",- abstract: "Clone a VM bundle (fast APFS clone; resets device identity)",- discussion: "The clone starts as a new device, with NVRAM, machine identifier, and SHSH blobs cleared. SEPStorage is copied unchanged, so a clone of a restored VM may need to be restored again.",+ abstract: "Copy a VM bundle (using APFS copy-on-write when available)",+ discussion: "The copy keeps the machine identifier, NVRAM, SEP storage, and SHSH blobs unchanged. Edit the device identity yourself if you need a different one.",)@OptionGroup var lib: VPhoneLibraryOptiondiff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachine.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachine.swiftindex dfc058ba..92c842b5 100644--- a/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachine.swift+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachine.swift@@ -101,14 +101,16 @@ class VPhoneVirtualMachine: NSObject, VZVirtualMachineDelegate {print("[vphone] Warning: failed to resolve ECID from machineIdentifier")}- // .allowOverwrite truncates whatever is at nvramURL, so a symbolic link- // there is refused rather than followed.- try VPhoneVirtualMachineManifest.requireRegularFileIfPresent(at: options.nvramURL)- let auxStorage = try VZMacAuxiliaryStorage(- creatingStorageAt: options.nvramURL,- hardwareModel: hwModel,- options: .allowOverwrite,- )+ let auxStorage: VZMacAuxiliaryStorage+ if try VPhoneVirtualMachineManifest.requireRegularFileIfPresent(at: options.nvramURL) {+ auxStorage = VZMacAuxiliaryStorage(url: options.nvramURL)+ } else {+ auxStorage = try VZMacAuxiliaryStorage(+ creatingStorageAt: options.nvramURL,+ hardwareModel: hwModel,+ options: [],+ )+ }try VPhoneHostFilePermissions.makeAccessible(at: options.nvramURL)platform.auxiliaryStorage = auxStorageplatform.hardwareModel = hwModeldiff --git a/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleOperations.swift b/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleOperations.swiftindex eedfdf4f..6d14d6b7 100644--- a/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleOperations.swift+++ b/VPhoneKit/VPhoneCoreKit/Bundle/VPhoneBundleOperations.swift@@ -149,10 +149,8 @@ public enum VPhoneBundleOperations {// MARK: - Clone- /// Clone a bundle with a fast APFS copy-on-write clone (fallback: recursive- /// copy), then reset the boot-identity artifacts so the clone comes up as a- /// fresh device on next boot. NOTE: SEPStorage is copied as-is — cloning an- /// already-restored VM may need a re-restore for a fully clean identity.+ /// Copy the whole bundle, using APFS copy-on-write when available. The+ /// machine identifier and boot state remain unchanged in the copy.public static func clone(bundleNamed name: String,to newName: String,@@ -171,28 +169,10 @@ public enum VPhoneBundleOperations {try? fm.removeItem(at: dst) // clear any partial clonefile output firsttry fm.copyItem(at: src, to: dst)}- try resetIdentity(inBundleAt: dst)try VPhoneHostFilePermissions.makeAccessible(at: dst)return try VPhoneBundle.load(at: dst)}- private static func resetIdentity(inBundleAt dir: URL) throws {- let fm = FileManager.default- for name in ["nvram.bin", "udid-prediction.txt"] {- let u = dir.appendingPathComponent(name)- if fm.fileExists(atPath: u.path) {- try fm.removeItem(at: u)- }- }- let entries = try fm.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil)- for u in entries where u.pathExtension == "shsh" {- try fm.removeItem(at: u)- }- let configURL = dir.appendingPathComponent("config.plist")- let manifest = try VPhoneVirtualMachineManifest.load(from: configURL)- try manifest.updating(machineIdentifier: Data()).write(to: configURL)- }-// MARK: - Export/// `.vphoned.signed` is re-staged on the next launch and need not be exported.diff --git a/VPhoneKit/VPhoneCoreKitTests/Bundle/BundleOperationsTests.swift b/VPhoneKit/VPhoneCoreKitTests/Bundle/BundleOperationsTests.swiftindex f41773b1..980343f4 100644--- a/VPhoneKit/VPhoneCoreKitTests/Bundle/BundleOperationsTests.swift+++ b/VPhoneKit/VPhoneCoreKitTests/Bundle/BundleOperationsTests.swift@@ -252,7 +252,7 @@ struct BundleOperationsTests {}}- @Test func `clone copies bundle and resets identity`() throws {+ @Test func `clone preserves bundle contents and keeps copy independent`() throws {let root = try makeRoot()defer { try? FileManager.default.removeItem(at: root) }let rom = try fakeROM(); let seprom = try fakeROM()@@ -269,7 +269,7 @@ struct BundleOperationsTests {),in: lib,)- // Simulate a booted/restored VM: identity artifacts present + non-empty machineIdentifier.+ // Simulate a booted/restored VM with identity artifacts.let fm = FileManager.defaulttry Data([1, 2, 3]).write(to: src.url.appendingPathComponent("nvram.bin"))try Data([4]).write(to: src.url.appendingPathComponent("udid-prediction.txt"))@@ -282,13 +282,15 @@ struct BundleOperationsTests {// Copy happened (disk + ROMs present in the clone).#expect(fm.fileExists(atPath: clone.url.appendingPathComponent("Disk.img").path))#expect(fm.fileExists(atPath: clone.url.appendingPathComponent("AVPBooter.vresearch1.bin").path))- // Identity artifacts cleared in the clone.- #expect(!fm.fileExists(atPath: clone.url.appendingPathComponent("nvram.bin").path))- #expect(!fm.fileExists(atPath: clone.url.appendingPathComponent("udid-prediction.txt").path))- #expect(!fm.fileExists(atPath: clone.url.appendingPathComponent("ABC123.shsh").path))- #expect(clone.manifest.machineIdentifier.isEmpty)- // Original untouched.- #expect(fm.fileExists(atPath: src.url.appendingPathComponent("nvram.bin").path))+ // A clone has the same boot identity and state as the source.+ #expect(try Data(contentsOf: clone.url.appendingPathComponent("nvram.bin")) == Data([1, 2, 3]))+ #expect(try Data(contentsOf: clone.url.appendingPathComponent("udid-prediction.txt")) == Data([4]))+ #expect(try Data(contentsOf: clone.url.appendingPathComponent("ABC123.shsh")) == Data([5]))+ #expect(try Data(contentsOf: clone.url.appendingPathComponent("SEPStorage")) == Data(contentsOf: src.url.appendingPathComponent("SEPStorage")))+ #expect(clone.manifest.machineIdentifier == Data([9, 9]))+ // Writing to the copy does not change the source, including with CoW.+ try Data([8]).write(to: clone.url.appendingPathComponent("nvram.bin"))+ #expect(try Data(contentsOf: src.url.appendingPathComponent("nvram.bin")) == Data([1, 2, 3]))#expect(try lib.bundle(named: "src").manifest.machineIdentifier == Data([9, 9]))}diff --git a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/ManifestTests.swift b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/ManifestTests.swiftindex 1af47057..a0c6aa2e 100644--- a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/ManifestTests.swift+++ b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/ManifestTests.swift@@ -88,7 +88,7 @@ struct ManifestTests {return url}- /// An imported config.plist is untrusted: vphone-vm overwrites the NVRAM+ /// An imported config.plist is untrusted: vphone-vm opens the NVRAM/// file and attaches the disk read-write, so every file name must stay/// directly inside the VM folder.@Test(arguments: ["diskImage", "nvramStorage", "sepStorage", "romImages.avpBooter", "romImages.avpSEPBooter"])diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachineSheets.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachineSheets.swiftindex 334ec201..96ace8c5 100644--- a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachineSheets.swift+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachineSheets.swift@@ -76,13 +76,13 @@ struct VPhoneLaunchpadNameSheet: View {let title: LocalizedStringKeylet action: LocalizedStringKeylet initial: String- var note: LocalizedStringKey?+ let existingName: Stringlet onConfirm: (String) -> Void@Environment(\.dismiss) private var dismiss@State private var name = ""private var isValid: Bool {- VPhoneLaunchpadNames.isValidMachineName(name) && name != initial+ VPhoneLaunchpadNames.isValidMachineName(name) && name != existingName}var body: some View {@@ -90,7 +90,7 @@ struct VPhoneLaunchpadNameSheet: View {Section {TextField("Name", text: $name)} footer: {- Text(note ?? "Use letters, numbers, periods, hyphens, and underscores.")+ Text("Use letters, numbers, periods, hyphens, and underscores.").foregroundStyle(.secondary)}}diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachinesView.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachinesView.swiftindex d8199e0a..da5023e0 100644--- a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachinesView.swift+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachinesView.swift@@ -244,7 +244,7 @@ struct VPhoneLaunchpadMachinesView: View {case let .settings(machine):VPhoneLaunchpadMachineSettingsView(machine: machine)case let .rename(name):- VPhoneLaunchpadNameSheet(title: "Rename \(name)", action: "Rename", initial: name) { newName in+ VPhoneLaunchpadNameSheet(title: "Rename \(name)", action: "Rename", initial: name, existingName: name) { newName inTask { await library.rename(name, to: newName) }}case let .clone(name):@@ -252,7 +252,7 @@ struct VPhoneLaunchpadMachinesView: View {title: "Clone \(name)",action: "Clone",initial: "\(name)-clone",- note: "The clone starts as a new device. Its Secure Enclave data is copied unchanged, so you may need to restore the clone again.",+ existingName: name,) { newName inTask { await library.clone(name, as: newName) }}