Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Hand the guest the VM's name through vphoned instead of NVRAM

Jacky2fc8850cffc0
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swift b/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swift
index 55ea13cb..fd3fbc8f 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/GuestCommunication/VPhoneGuestControl.swift
@@ -55,6 +55,9 @@ final class VPhoneGuestControl {
/// The guest's mDNS name from the manifest, applied on every connect; nil
/// puts back a name vphoned replaced, and leaves any other alone.
@ObservationIgnored var guestLocalHostName: String?
+ /// The guest's device name, the VM's name, applied on every connect; nil
+ /// gives the guest its own back.
+ @ObservationIgnored var guestDeviceName: String?
/// Names the guest resolves locally, worked out on each connect (the Mac's
/// name or a bridged address can change while the VM runs). Nil sends
/// nothing.
@@ -229,6 +232,9 @@ final class VPhoneGuestControl {
if capabilities.contains("network_hostname") {
Task { await applyGuestLocalHostName(guestLocalHostName) }
}
+ if capabilities.contains("device_name") {
+ Task { await applyGuestDeviceName(guestDeviceName) }
+ }
if capabilities.contains("network_static_names"), let entries = guestStaticNames?() {
Task { await applyGuestStaticNames(entries) }
}
@@ -935,6 +941,22 @@ extension VPhoneGuestControl {
}
}
+extension VPhoneGuestControl {
+ /// Pin the guest's device name to `name`, what Xcode, devicectl and Finder
+ /// show, or with nil give the guest its own back. vphoned stores it where
+ /// the guest's hooks read it at every boot, and applies it at once.
+ func applyGuestDeviceName(_ name: String?) async {
+ do {
+ let result = try await call("device.name.set", params: ["name": name ?? NSNull()])
+ if result["changed"] as? Bool == true {
+ print("[device] guest device name \(name.map { "set to \($0)" } ?? "restored to its own")")
+ }
+ } catch {
+ print("[device] could not set the guest's device name: \(error)")
+ }
+ }
+}
+
extension VPhoneGuestControl {
/// Replace the names vphoned has the guest resolve locally; an empty list
/// withdraws them.
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneBootCommand+VirtualMachine.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneBootCommand+VirtualMachine.swift
index ebab5d2a..7d887e02 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneBootCommand+VirtualMachine.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneBootCommand+VirtualMachine.swift
@@ -32,7 +32,6 @@ extension VPhoneBootCommand {
screenScale: manifest.screenConfig.scale,
kernelDebugPort: kernelDebugPort,
isPadGuest: manifest.guestDevice.isPad,
- deviceName: vmDir.lastPathComponent,
)
}
}
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
index 7b33a2de..46f3b20e 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
@@ -113,6 +113,7 @@ class VPhoneVirtualMachineAppDelegate: NSObject, NSApplicationDelegate {
self.control = control
if !command.dfu {
control.unlocksAtStartup = vm.unlocksAtStartup
+ control.guestDeviceName = Self.guestDeviceName(forConfig: options.configURL)
startNetworkServices(vm: vm, control: control)
let vphonedURL = URL(fileURLWithPath: command.vphonedBin)
if FileManager.default.fileExists(atPath: vphonedURL.path) {
@@ -336,6 +337,20 @@ class VPhoneVirtualMachineAppDelegate: NSObject, NSApplicationDelegate {
hostAutomationServer = server
}
+ /// The guest shows the VM's name, the folder holding config.plist, as its
+ /// device name. A VM name that cannot be one is not used, and the guest
+ /// keeps its own.
+ private static func guestDeviceName(forConfig config: URL) -> String? {
+ let name = VPhoneDockName.name(forConfig: config)
+ do {
+ try VPhoneGuestDeviceName.validate(name)
+ return name
+ } catch {
+ print("[device] the VM name is not used as the device name: \(error)")
+ return nil
+ }
+ }
+
/// Hold the guest to its configured address and open its forwarded ports.
@MainActor
private func startNetworkServices(vm: VPhoneVirtualMachine, control: VPhoneGuestControl) {
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachine.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachine.swift
index 4cfe3b4d..d3881051 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachine.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VirtualMachine/VPhoneVirtualMachine.swift
@@ -44,9 +44,6 @@ class VPhoneVirtualMachine: NSObject, VZVirtualMachineDelegate {
var kernelDebugPort: Int?
/// The guest runs iPadOS: Esc is a key there, not the back gesture.
var isPadGuest = false
- /// The VM's name, written to NVRAM before boot as the guest's device
- /// name; nil removes it.
- var deviceName: String?
}
private struct DeviceIdentity {
@@ -143,7 +140,6 @@ class VPhoneVirtualMachine: NSObject, VZVirtualMachineDelegate {
print("[vphone] NVRAM boot-args: \(bootArgs)")
}
}
- Self.applyGuestDeviceName(options.deviceName, to: auxStorage)
// --- Boot loader with custom ROM ---
let bootloader = VZMacOSBootLoader()
@@ -379,60 +375,6 @@ class VPhoneVirtualMachine: NSObject, VZVirtualMachineDelegate {
try content.write(to: outputURL, atomically: true, encoding: .utf8)
}
- // MARK: - Guest Device Name
-
- /// Hand the guest the VM's name as its device name in NVRAM before boot.
- /// Written on every launch, so a rename applies from the next boot. A name
- /// that cannot be a device name is removed instead, and the guest goes back
- /// to its own. Never fails the boot: a guest without the name only shows
- /// its own.
- private static func applyGuestDeviceName(_ name: String?, to auxStorage: VZMacAuxiliaryStorage) {
- let variable = VPhoneGuestDeviceName.nvramVariable
- guard let name else {
- removeNVRAMVariable(variable, from: auxStorage)
- return
- }
- do {
- try VPhoneGuestDeviceName.validate(name)
- } catch {
- print("[vphone] Warning: the VM name is not used as the device name: \(error)")
- removeNVRAMVariable(variable, from: auxStorage)
- return
- }
- // The bytes alone: no terminating NUL.
- let ok = Dynamic(auxStorage)
- ._setDataValue(Data(name.utf8), forNVRAMVariableNamed: variable, error: nil)
- .asBool ?? false
- if ok {
- print("[vphone] NVRAM \(variable): \(name)")
- } else {
- print("[vphone] Warning: could not set NVRAM \(variable); the guest keeps its own name")
- }
- }
-
- /// Remove an NVRAM variable if it is there. `_removeNVRAMVariableNamed:error:`
- /// is private and returns a BOOL, so its presence is checked first; a
- /// missing variable is left alone rather than reported as a failure.
- private static func removeNVRAMVariable(_ variable: String, from auxStorage: VZMacAuxiliaryStorage) {
- let remove = NSSelectorFromString("_removeNVRAMVariableNamed:error:")
- guard auxStorage.responds(to: remove) else {
- print("[vphone] Warning: this macOS has no VZMacAuxiliaryStorage._removeNVRAMVariableNamed:error:; NVRAM \(variable) left as it is")
- return
- }
- let read = NSSelectorFromString("_dataValueForNVRAMVariableNamed:error:")
- if auxStorage.responds(to: read),
- Dynamic(auxStorage)._dataValueForNVRAMVariableNamed(variable, error: nil).asObject == nil
- {
- return
- }
- let ok = Dynamic(auxStorage)._removeNVRAMVariableNamed(variable, error: nil).asBool ?? false
- if ok {
- print("[vphone] NVRAM \(variable): removed")
- } else {
- print("[vphone] Warning: could not remove NVRAM \(variable)")
- }
- }
-
// MARK: - Battery
/// Update the synthetic battery charge and connectivity at runtime.
diff --git a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestDeviceName.swift b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestDeviceName.swift
index a1cb83ed..0244ffa9 100644
--- a/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestDeviceName.swift
+++ b/VPhoneKit/VPhoneCoreKit/VirtualMachine/VPhoneGuestDeviceName.swift
@@ -28,16 +28,12 @@ extension VPhoneGuestDeviceNameError: CustomStringConvertible, LocalizedError {
// MARK: - Device Name
/// The guest shows the VM's name as its device name: what Xcode's device list
-/// and `xcrun devicectl list devices` report. vphone-vm hands it to the guest
-/// before every boot in the NVRAM variable `nvramVariable`, as the name's UTF-8
-/// bytes with no terminating NUL; guest hooks have configd publish it and
-/// lockdownd refuse to change it. A VM name that cannot be a device name is
-/// not handed over, and the guest keeps its own. A rename applies from the
-/// next boot.
+/// and `xcrun devicectl list devices` report. vphone-vm hands it to vphoned
+/// (`device.name.set`) after every connect; vphoned stores it where guest hooks
+/// read it, so configd publishes it and lockdownd refuses to change it, from
+/// that moment and from the start of every later boot. A VM name that cannot
+/// be a device name is not handed over, and the guest keeps its own.
public enum VPhoneGuestDeviceName {
- /// The NVRAM variable vphone-vm writes and the guest reads.
- public static let nvramVariable = "vphone-device-name"
-
/// The longest name accepted, in UTF-8 bytes. The guest applies the same
/// limit (VPhoneGuestComponents/DeviceName/DeviceNamePolicy.c).
public static let maximumByteCount = 255
diff --git a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/GuestDeviceNameTests.swift b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/GuestDeviceNameTests.swift
index b5ccaaa0..e59e168a 100644
--- a/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/GuestDeviceNameTests.swift
+++ b/VPhoneKit/VPhoneCoreKitTests/VirtualMachine/GuestDeviceNameTests.swift
@@ -3,10 +3,6 @@ import Testing
@testable import VPhoneCoreKit
struct GuestDeviceNameTests {
- @Test func `the NVRAM variable name is the one the guest reads`() {
- #expect(VPhoneGuestDeviceName.nvramVariable == "vphone-device-name")
- }
-
@Test(arguments: [
"lab-a",
"Jacky's iPhone",