Fixes for Ventura (#129)

* Fixes for Ventura

Still a noob in SwiftUI and Swift concurrency, but it seems on Ventura a task group is not actually running on main or something. Either way I think this change simplifies things but launching a VM in a task and then just continuing with either VNC or built-in graphics.

* Check VM's state
This commit is contained in:
Fedor Korotkov
2022-06-17 10:04:57 -04:00
committed by GitHub
parent 0d633de04a
commit 89217c23a2
2 changed files with 40 additions and 34 deletions
+31 -34
View File
@@ -31,48 +31,45 @@ struct Run: AsyncParsableCommand {
func run() async throws {
if recovery && vnc {
print("You can't run in recovery and use VNC!")
Foundation.exit(1)
Foundation.exit(1)
}
let vmDir = try VMStorageLocal().open(name)
vm = try VM(vmDir: vmDir)
try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
do {
try await vm!.run(recovery)
Task {
do {
try await vm!.run(recovery)
Foundation.exit(0)
} catch {
if error.localizedDescription.contains("Failed to lock auxiliary storage.") {
print("Virtual machine \"\(name)\" is already running!")
} else {
print(error)
}
Foundation.exit(1)
Foundation.exit(0)
} catch {
if error.localizedDescription.contains("Failed to lock auxiliary storage.") {
print("Virtual machine \"\(name)\" is already running!")
} else {
print(error)
}
}
if vnc {
group.addTask(operation: {
do {
print("Waiting for the VM to boot...")
let resolvedIP = try await IP.resolveIP(vm!.config, secondsToWait: 60)
guard let ip = resolvedIP else {
throw IPNotFound()
}
let url = URL(string: "vnc://\(ip)")!
print("Opening \(url)")
NSWorkspace.shared.open(url)
} catch {
print("Failed to get an IP for screen sharing: \(error)")
}
})
} else if !noGraphics {
runUI()
}
try await group.waitForAll()
Foundation.exit(1)
}
}
if vnc {
do {
print("Waiting for the VM to boot...")
let resolvedIP = try await IP.resolveIP(vm!.config, secondsToWait: 60)
guard let ip = resolvedIP else {
throw IPNotFound()
}
let url = URL(string: "vnc://\(ip)")!
print("Opening \(url)")
NSWorkspace.shared.open(url)
} catch {
print("Failed to get an IP for screen sharing: \(error)")
}
while !(vm?.inFinalState ?? false) {
try await Task.sleep(nanoseconds: 1_000_000)
}
} else if !noGraphics {
runUI()
}
}
+9
View File
@@ -77,6 +77,15 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject {
try data.write(to: expectedIPSWLocation, options: [.atomic])
return expectedIPSWLocation
}
var inFinalState: Bool {
get {
virtualMachine.state == VZVirtualMachine.State.stopped ||
virtualMachine.state == VZVirtualMachine.State.paused ||
virtualMachine.state == VZVirtualMachine.State.error
}
}
init(vmDir: VMDirectory, ipswURL: URL?, diskSizeGB: UInt16) async throws {
let ipswURL = ipswURL != nil ? ipswURL! : try await VM.retrieveLatestIPSW();