Skip to content

Commit

Permalink
vm(apple): support cancelling installation
Browse files Browse the repository at this point in the history
  • Loading branch information
osy committed May 29, 2023
1 parent 672ee38 commit e9b309a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
13 changes: 9 additions & 4 deletions Managers/UTMAppleVirtualMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import Virtualization

private(set) var apple: VZVirtualMachine!

private var installProgress: Progress?

private var progressObserver: NSKeyValueObservation?

private var sharedDirectoriesChanged: AnyCancellable?
Expand Down Expand Up @@ -161,12 +163,13 @@ import Virtualization
}

override func vmStop(force: Bool) async throws {
guard state == .vmStarted || state == .vmPaused else {
return
}
changeState(.vmStopping)
do {
try await _vmStop(force: force)
if let installProgress = installProgress {
installProgress.cancel()
} else {
try await _vmStop(force: force)
}
changeState(.vmStopped)
} catch {
changeState(.vmStopped)
Expand Down Expand Up @@ -329,13 +332,15 @@ import Virtualization
self.progressObserver = installer.progress.observe(\.fractionCompleted, options: [.initial, .new]) { progress, change in
self.delegate?.virtualMachine?(self, didUpdateInstallationProgress: progress.fractionCompleted)
}
self.installProgress = installer.progress
installer.install { result in
continuation.resume(with: result)
}
}
}
changeState(.vmStarted)
progressObserver = nil
installProgress = nil
delegate?.virtualMachine?(self, didCompleteInstallation: true)
#else
throw UTMAppleVirtualMachineError.operatingSystemInstallNotSupported
Expand Down
11 changes: 5 additions & 6 deletions Platform/macOS/Display/VMDisplayAppleWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Foundation
class VMDisplayAppleWindowController: VMDisplayWindowController {
var mainView: NSView?

var isInstalling: Bool = false
var isInstallSuccessful: Bool = false

var appleVM: UTMAppleVirtualMachine! {
vm as? UTMAppleVirtualMachine
Expand Down Expand Up @@ -55,7 +55,7 @@ class VMDisplayAppleWindowController: VMDisplayWindowController {
super.windowDidLoad()
if #available(macOS 12, *), let ipswUrl = appleConfig.system.boot.macRecoveryIpswURL {
showConfirmAlert(NSLocalizedString("Would you like to install macOS? If an existing operating system is already installed on the primary drive of this VM, then it will be erased.", comment: "VMDisplayAppleWindowController")) {
self.isInstalling = true
self.isInstallSuccessful = false
self.appleVM.requestInstallVM(with: ipswUrl)
}
}
Expand Down Expand Up @@ -102,8 +102,8 @@ class VMDisplayAppleWindowController: VMDisplayWindowController {

override func virtualMachine(_ vm: UTMVirtualMachine, didTransitionTo state: UTMVMState) {
super.virtualMachine(vm, didTransitionTo: state)
if state == .vmStopped && isInstalling {
isInstalling = false
if state == .vmStopped && isInstallSuccessful {
isInstallSuccessful = false
vm.requestVmStart()
}
}
Expand Down Expand Up @@ -255,8 +255,7 @@ extension VMDisplayAppleWindowController {
self.enterSuspended(isBusy: true)
self.appleConfig.system.boot.macRecoveryIpswURL = nil
self.appleVM.registryEntry.macRecoveryIpsw = nil
} else {
self.isInstalling = false
self.isInstallSuccessful = true
}
}
}
Expand Down

0 comments on commit e9b309a

Please sign in to comment.