Skip to content

Commit

Permalink
registry: save bookmark to IPSW installer
Browse files Browse the repository at this point in the history
This should allow moved VMs to retain the installer IPSW as well as when UTM
is quit and re-opened before an installation is completed.

Resolves #4938
  • Loading branch information
osy committed Mar 6, 2023
1 parent 9e17023 commit bbfacf8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
16 changes: 13 additions & 3 deletions Managers/UTMAppleVirtualMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ import Virtualization
let oldConfig = appleConfig
config = UTMConfigurationWrapper(wrapping: newConfig)
updateConfigFromRegistry()
if #available(macOS 12, *) {
newConfig.system.boot.macRecoveryIpswURL = oldConfig.system.boot.macRecoveryIpswURL
}
}

override func accessShortcut() async throws {
Expand Down Expand Up @@ -320,6 +317,10 @@ import Virtualization
}
changeState(.vmStarting)
do {
_ = ipswUrl.startAccessingSecurityScopedResource()
defer {
ipswUrl.stopAccessingSecurityScopedResource()
}
try await createAppleVM()
#if os(macOS) && arch(arm64)
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
Expand Down Expand Up @@ -502,6 +503,12 @@ extension UTMAppleVirtualMachine {
registryEntry.externalDrives = registryEntry.externalDrives.filter({ element in
configDrives.contains(where: { $0.id == element.key && $0.isExternal })
})
// save IPSW reference
if let url = appleConfig.system.boot.macRecoveryIpswURL {
_ = url.startAccessingSecurityScopedResource()
registryEntry.macRecoveryIpsw = try UTMRegistryEntry.File(url: url, isReadOnly: true)
url.stopAccessingSecurityScopedResource()
}
}

@MainActor override func updateConfigFromRegistry() {
Expand All @@ -513,5 +520,8 @@ extension UTMAppleVirtualMachine {
appleConfig.drives[i].imageURL = registryEntry.externalDrives[id]?.url
}
}
if let file = registryEntry.macRecoveryIpsw {
appleConfig.system.boot.macRecoveryIpswURL = file.url
}
}
}
15 changes: 15 additions & 0 deletions Managers/UTMRegistryEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import Foundation

@Published private var _hasMigratedConfig: Bool

@Published private var _macRecoveryIpsw: File?

private enum CodingKeys: String, CodingKey {
case name = "Name"
case package = "Package"
Expand All @@ -45,6 +47,7 @@ import Foundation
case windowSettings = "WindowSettings"
case terminalSettings = "TerminalSettings"
case hasMigratedConfig = "MigratedConfig"
case macRecoveryIpsw = "MacRecoveryIpsw"
}

init(newFrom vm: UTMVirtualMachine) {
Expand Down Expand Up @@ -77,6 +80,7 @@ import Foundation
_windowSettings = try container.decode([Int: Window].self, forKey: .windowSettings)
_terminalSettings = try container.decodeIfPresent([Int: Terminal].self, forKey: .terminalSettings) ?? [:]
_hasMigratedConfig = try container.decodeIfPresent(Bool.self, forKey: .hasMigratedConfig) ?? false
_macRecoveryIpsw = try container.decodeIfPresent(File.self, forKey: .macRecoveryIpsw)
}

func encode(to encoder: Encoder) throws {
Expand All @@ -92,6 +96,7 @@ import Foundation
if _hasMigratedConfig {
try container.encode(_hasMigratedConfig, forKey: .hasMigratedConfig)
}
try container.encodeIfPresent(_macRecoveryIpsw, forKey: .macRecoveryIpsw)
}

func asDictionary() throws -> [String: Any] {
Expand Down Expand Up @@ -195,6 +200,16 @@ extension UTMRegistryEntryDecodable {
}
}

var macRecoveryIpsw: File? {
get {
_macRecoveryIpsw
}

set {
_macRecoveryIpsw = newValue
}
}

func setExternalDrive(_ file: File, forId id: String) {
externalDrives[id] = file
}
Expand Down

0 comments on commit bbfacf8

Please sign in to comment.