Skip to content

Commit 5559dd5

Browse files
authored
VZVirtualMachineInstance: Rework installRosetta flow (#169)
Because we were installing rosetta directly in the constructor for VZVirtualMachineInstance, and we'd prefer to not have the constructor async as it pollutes so much more, we had devised this gnarly callback approach for the install that is a bit of an eyesore. This changes focus is on moving the install flow to a method that is already async so we can piggyback off of it, and removing the install logic from the config -> VZConfig conversion. Now the install will occur during start() if rosetta is not installed.
1 parent 2dfabeb commit 5559dd5

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

Sources/Containerization/VZVirtualMachineInstance.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ extension VZVirtualMachineInstance {
131131
)
132132
}
133133

134+
// Do any necessary setup needed prior to starting the guest.
135+
try await self.prestart()
136+
134137
try await self.vm.start(queue: self.queue)
135138

136139
let agent = Vminitd(
@@ -203,32 +206,28 @@ extension VZVirtualMachineInstance {
203206
port: port
204207
)
205208
}
209+
210+
func prestart() async throws {
211+
if self.config.rosetta && VZLinuxRosettaDirectoryShare.availability == .notInstalled {
212+
self.logger?.info("installing rosetta")
213+
try await VZVirtualMachineInstance.Configuration.installRosetta()
214+
}
215+
}
206216
}
207217

208218
extension VZVirtualMachineInstance.Configuration {
209-
public static func installRosetta() throws {
210-
#if arch(arm64)
219+
public static func installRosetta() async throws {
211220
do {
212-
let _err: Mutex<Swift.Error?> = .init(nil)
213-
VZLinuxRosettaDirectoryShare.installRosetta(completionHandler: { error in
214-
_err.withLock {
215-
$0 = error
216-
}
217-
})
218-
let err = _err.withLock { $0 }
219-
guard let err else {
220-
return
221-
}
222-
throw err
221+
try await VZLinuxRosettaDirectoryShare.installRosetta()
223222
} catch {
224223
throw ContainerizationError(
225224
.internalError,
226225
message: "failed to install rosetta",
227226
cause: error
228227
)
229228
}
230-
#endif
231229
}
230+
232231
private func serialPort(path: URL) throws -> [VZVirtioConsoleDeviceSerialPortConfiguration] {
233232
let c = VZVirtioConsoleDeviceSerialPortConfiguration()
234233
c.attachment = try VZFileSerialPortAttachment(url: path, append: true)
@@ -261,7 +260,8 @@ extension VZVirtualMachineInstance.Configuration {
261260
message: "rosetta was requested but is not supported on this machine"
262261
)
263262
case .notInstalled:
264-
try Self.installRosetta()
263+
// NOTE: If rosetta isn't installed, we'll error with a nice error message
264+
// during .start() of the virtual machine instance.
265265
fallthrough
266266
case .installed:
267267
let share = try VZLinuxRosettaDirectoryShare()

0 commit comments

Comments
 (0)