Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Key templates of unshipped presets by every patch; read a preset file once

Lakr233c503e1c51be0
diff --git a/Documents/Guides/create-and-run.md b/Documents/Guides/create-and-run.md
index d337ba24..4b0a2ac6 100644
--- a/Documents/Guides/create-and-run.md
+++ b/Documents/Guides/create-and-run.md
@@ -51,7 +51,7 @@ The guest follows the Mac's time zone and clock. `vphone-vm` sends the Mac's zon
## Templates
-A template is a complete machine kept in `<library>/.templates/<id>/`. It boots once while it is built, its setup boot, and never again once it is frozen. `vm create` without `--no-template` works out the template key from its options and the two IPSWs before anything is restored: the guest device, the iOS and cloudOS versions and builds, the patch preset and the boot-chain patches it resolves to, the bundle series (`2.9` for 2.9.x), the disk size, what is trimmed from the guest's System volume (`--trim`), and the slimming switches below. Then:
+A template is a complete machine kept in `<library>/.templates/<id>/`. It boots once while it is built, its setup boot, and never again once it is frozen. `vm create` without `--no-template` works out the template key from its options and the two IPSWs before anything is restored: the guest device, the iOS and cloudOS versions and builds, the patch preset and the boot-chain patches it resolves to (every patch it resolves to, guest ones included, for a preset the bundle does not ship: a preset file or one in `~/.vphone/patches_presets` can change under the same identifier, and a clone keeps its template's guest patches), the bundle series (`2.9` for 2.9.x), the disk size, what is trimmed from the guest's System volume (`--trim`), and the slimming switches below. Then:
- If a template with that key exists, the VM is cloned from it with a new identity, given the requested CPU, memory and network, and booted once to check vphoned. This takes seconds and needs neither root nor the IPSWs; nothing is downloaded. The builds come from the IPSWs when they are local files or already in the IPSW cache, and otherwise from a template recorded with the same two sources, as [`vm template find`](#finding-a-template) works them out; a local IPSW that has been deleted counts as not there.
- Otherwise the IPSWs are downloaded if they are not there, and the full pipeline runs into a staging folder, `.templates/.building-<id>-<uuid>/`: `cfw install`, the offline trim, then the setup boot; the result is frozen and renamed to `.templates/<id>/` in one step, and the VM is cloned from it. The restore tree is removed before the freeze whatever `--keep-artifacts` says: a template never keeps it, because its ~11 GB would stay pinned for as long as the template lives and no clone can use it. `--keep-artifacts` keeps it only with `--no-template`, and `vm template adopt` removes it too. A second `vm create` with the same key waits for the first instead of restoring a second copy. A failed build stays in its staging folder for inspection.
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareCommand.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareCommand.swift
index 13baed94..14b7c6ee 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareCommand.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhoneFirmwareCommand.swift
@@ -739,7 +739,7 @@ struct VPhoneFirmwareExportPresetCommand: ParsableCommand {
guard url.pathExtension == "plist", !identifier.isEmpty, !VPhonePatchPresetStore.isPresetFile(identifier) else {
throw ValidationError("The output is a .plist file whose name, without .plist, is the preset's identifier.")
}
- guard !VPhonePatchPresetStore.shippedPresets().contains(where: { $0.identifier == identifier }) else {
+ guard !VPhonePatchPresetStore.isShipped(identifier) else {
throw ValidationError("'\(identifier)' is a shipped preset's identifier; name the file something else.")
}
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhonePatchPresetStore.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhonePatchPresetStore.swift
index a29fcb19..ab3b9047 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhonePatchPresetStore.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/Firmware/VPhonePatchPresetStore.swift
@@ -294,6 +294,13 @@ public enum VPhonePatchPresetStore {
return FirmwarePatchSetCatalog.builtInPresets
}
+ /// Whether the bundle ships a preset with this identifier. Only then does
+ /// the identifier alone say what the preset turns on: a user's preset or
+ /// a preset file can change under the same one.
+ static func isShipped(_ identifier: String) -> Bool {
+ shippedPresets().contains { $0.identifier == identifier }
+ }
+
/// The preset with this identifier, or nil.
public static func preset(named identifier: String) -> VPhonePatchPreset? {
availablePresets().first { $0.identifier == identifier }
@@ -318,7 +325,7 @@ public enum VPhonePatchPresetStore {
/// The VM's copy, when it is the preset named. Never a shipped identifier.
private static func vmCopy(named identifier: String, data: Data?) -> VPhonePatchPreset? {
guard let data, let copy = try? VPhonePatchPreset.decode(data), copy.identifier == identifier,
- !shippedPresets().contains(where: { $0.identifier == identifier })
+ !isShipped(identifier)
else { return nil }
return copy
}
@@ -391,7 +398,7 @@ public enum VPhonePatchPresetStore {
throw ValidationError("Preset file \(url.path) has the identifier '\(preset.identifier)'; an identifier has no '/' and no .plist suffix.")
}
// As for ~/.vphone/patches_presets: nothing can redefine a shipped preset.
- if shippedPresets().contains(where: { $0.identifier == preset.identifier }) {
+ if isShipped(preset.identifier) {
throw ValidationError("Preset file \(url.path) uses the identifier '\(preset.identifier)' of a shipped preset; give it its own.")
}
return preset
diff --git a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneMachineTemplateKeys.swift b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneMachineTemplateKeys.swift
index 5d285c98..5b70aa37 100644
--- a/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneMachineTemplateKeys.swift
+++ b/VPhoneExecutable/VPhoneCommand/VPhoneCommand/VirtualMachine/VPhoneMachineTemplateKeys.swift
@@ -40,6 +40,15 @@ enum VPhoneMachineTemplateKeys {
}.sorted()
}
+ /// The patches a key digests. A shipped preset's identifier fixes its
+ /// guest patches within a series, so the boot chain is enough. Any other
+ /// preset, a user's or a preset file's, can change them under the same
+ /// identifier, and a clone keeps its template's guest and preset copy, so
+ /// every patch counts.
+ static func keyedPatches(_ enabled: some Sequence<String>, preset identifier: String) -> [String] {
+ VPhonePatchPresetStore.isShipped(identifier) ? bootChainPatches(enabled) : Set(enabled).sorted()
+ }
+
/// Resolves the preset against the two versions the way `fw patch` will.
static func plan(
preset: VPhonePatchPreset,
@@ -91,7 +100,7 @@ enum VPhoneMachineTemplateKeys {
cloudOSBuild: cloudOS.build,
patchPreset: plan.presetIdentifier,
bootChainPlanDigest: VPhoneMachineTemplateKey.planDigest(
- bootChainPatches: bootChainPatches(plan.enabled),
+ bootChainPatches: keyedPatches(plan.enabled, preset: plan.presetIdentifier),
parameters: plan.parameters,
),
bundleSeries: series,
@@ -199,7 +208,7 @@ enum VPhoneMachineTemplateKeys {
cloudOSBuild: info.cloudOS.build,
patchPreset: plan.presetIdentifier,
bootChainPlanDigest: VPhoneMachineTemplateKey.planDigest(
- bootChainPatches: bootChainPatches(plan.selectedPatches),
+ bootChainPatches: keyedPatches(plan.selectedPatches, preset: plan.presetIdentifier),
parameters: plan.parameters,
),
bundleSeries: series,
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings b/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings
index 0c655637..fe75b727 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings
+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Localizable.xcstrings
@@ -9117,33 +9117,6 @@
"zh-Hans" : { "stringUnit" : { "state" : "translated", "value" : "更新 %@ 的内核" } }
}
},
- "Guest environment updated." : {
- "localizations" : {
- "en" : { "stringUnit" : { "state" : "translated", "value" : "Guest environment updated." } },
- "ja" : { "stringUnit" : { "state" : "translated", "value" : "ゲスト環境を更新しました。" } },
- "ko" : { "stringUnit" : { "state" : "translated", "value" : "게스트 환경을 업데이트했습니다." } },
- "vi" : { "stringUnit" : { "state" : "translated", "value" : "Đã cập nhật môi trường máy khách." } },
- "zh-Hans" : { "stringUnit" : { "state" : "translated", "value" : "客户机环境已更新。" } }
- }
- },
- "Custom firmware installed." : {
- "localizations" : {
- "en" : { "stringUnit" : { "state" : "translated", "value" : "Custom firmware installed." } },
- "ja" : { "stringUnit" : { "state" : "translated", "value" : "カスタムファームウェアをインストールしました。" } },
- "ko" : { "stringUnit" : { "state" : "translated", "value" : "커스텀 펌웨어를 설치했습니다." } },
- "vi" : { "stringUnit" : { "state" : "translated", "value" : "Đã cài firmware tùy chỉnh." } },
- "zh-Hans" : { "stringUnit" : { "state" : "translated", "value" : "自定义固件已安装。" } }
- }
- },
- "Kernel updated." : {
- "localizations" : {
- "en" : { "stringUnit" : { "state" : "translated", "value" : "Kernel updated." } },
- "ja" : { "stringUnit" : { "state" : "translated", "value" : "カーネルを更新しました。" } },
- "ko" : { "stringUnit" : { "state" : "translated", "value" : "커널을 업데이트했습니다." } },
- "vi" : { "stringUnit" : { "state" : "translated", "value" : "Đã cập nhật kernel." } },
- "zh-Hans" : { "stringUnit" : { "state" : "translated", "value" : "内核已更新。" } }
- }
- },
"It continues if you close this window." : {
"localizations" : {
"en" : { "stringUnit" : { "state" : "translated", "value" : "It continues if you close this window." } },
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadCreationPipeline.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadCreationPipeline.swift
index 0edcb0dc..aee49e26 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadCreationPipeline.swift
+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadCreationPipeline.swift
@@ -111,9 +111,11 @@ final class VPhoneLaunchpadCreationPipeline {
VPhoneLaunchpadMachinePath(libraryRoot: libraryRoot, name: name)
}
+ /// For `fw set-patches` on a machine that already keeps the preset
+ /// (``VPhoneLaunchpadPatchSelection/machinePresetArguments``).
var patchOverrides: VPhoneLaunchpadPatchOverrides {
VPhoneLaunchpadPatchOverrides(
- preset: patches.presetArgument,
+ preset: patches.preset,
blocked: patches.blocked,
allowed: patches.allowed,
guestPatches: guestPatches,
@@ -228,7 +230,7 @@ final class VPhoneLaunchpadCreationPipeline {
/// `fw patch`, as both the step's log line and the run build it. One array so
/// the command the sheet shows cannot drift from the command that runs.
private func patchArguments(_ name: String) -> [String] {
- ["fw", "patch", name] + options.patches.presetArguments
+ ["fw", "patch", name] + options.patches.machinePresetArguments
}
private var deviceArguments: [String] {
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMaintenanceView.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMaintenanceView.swift
index ec4024ad..62c11461 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMaintenanceView.swift
+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMaintenanceView.swift
@@ -33,14 +33,6 @@ final class VPhoneLaunchpadMaintenanceRun {
}
}
- var finished: String {
- switch self {
- case .installCustomFirmware: String(localized: "Custom firmware installed.")
- case .updateGuestEnvironment: String(localized: "Guest environment updated.")
- case .updateKernel: String(localized: "Kernel updated.")
- }
- }
-
var failure: String {
switch self {
case .installCustomFirmware: String(localized: "Unable to install custom firmware. Check the log for details.")
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadPatchCatalog.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadPatchCatalog.swift
index 19398124..ff953352 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadPatchCatalog.swift
+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadPatchCatalog.swift
@@ -362,6 +362,15 @@ nonisolated struct VPhoneLaunchpadPatchSelection: Hashable, Sendable {
presetFile == nil && preset == Self.defaultPreset ? [] : ["--preset", presetArgument]
}
+ /// The `--preset` flag for a step on a machine that already keeps the
+ /// preset: `fw set-patches` before `fw prepare` copies a file into it
+ /// with ``presetArguments``, and a clone has its template's copy. Once the
+ /// bundle has read the file, `preset` is its identifier, so a later step
+ /// or a retry does not need the file again.
+ var machinePresetArguments: [String] {
+ preset == Self.defaultPreset ? [] : ["--preset", preset]
+ }
+
/// Takes what the bundle read from ``presetFile``: the identifier it
/// declares, and whether it customises the system before the restore.
mutating func adoptFile(from catalog: VPhoneLaunchpadPatchCatalog) {