Files
VirtualBuddy/VirtualCore/Source/Virtualization/VMController.swift
Guilherme Rambo a20d94dbc5 Implement support for managed preferences (#765)
* Implement support for managed preferences; allow disabling shared folders with managed preferences

* Implement managed preferences for USB passthrough, bridged networking, guest app, and microphone input

* Implement unified UI component for informing user about managed preference restrictions
2026-09-14 17:15:41 -03:00

659 lines
21 KiB
Swift

//
// VMController.swift
// VirtualCore
//
// Created by Guilherme Rambo on 07/04/22.
//
import Cocoa
import Foundation
import Virtualization
import Combine
import OSLog
public struct VMSessionOptions: Hashable, Codable {
@DecodableDefault.False
public var bootInRecoveryMode = false {
didSet {
guard bootInRecoveryMode != oldValue else { return }
resolveMutuallyExclusiveOptions()
}
}
@DecodableDefault.False
public var bootInDFUMode = false {
didSet {
guard bootInDFUMode != oldValue else { return }
resolveMutuallyExclusiveOptions()
}
}
@DecodableDefault.False
public var bootOnInstallDevice = false
@DecodableDefault.False
public var autoBoot = false
/// Used when restoring from a previously-saved state.
public var stateRestorationPackageURL: URL?
public static let `default` = VMSessionOptions()
public init(bootInRecoveryMode: Bool = false, bootInDFUMode: Bool = false, bootOnInstallDevice: Bool = false, autoBoot: Bool = false, stateRestorationPackageURL: URL? = nil) {
self.bootInRecoveryMode = bootInRecoveryMode
self.bootInDFUMode = bootInDFUMode
self.bootOnInstallDevice = bootOnInstallDevice
self.autoBoot = autoBoot
self.stateRestorationPackageURL = stateRestorationPackageURL
resolveMutuallyExclusiveOptions()
}
private mutating func resolveMutuallyExclusiveOptions() {
if bootInDFUMode {
bootInRecoveryMode = false
}
if bootInRecoveryMode {
bootInDFUMode = false
}
}
}
public enum VMState: Equatable {
case idle
case starting(_ message: String?)
case resizingDisk(_ message: String?)
case running(VZVirtualMachine)
case paused(VZVirtualMachine)
case savingState(VZVirtualMachine)
case stateSaveCompleted(VZVirtualMachine, VBSavedStatePackage)
case restoringState(VZVirtualMachine, VBSavedStatePackage)
case stopped(Error?)
}
public enum MACAddressConflictResolution {
case cancel
case continueAnyway
case randomize
}
@MainActor
public final class VMController: ObservableObject {
public let id: VBVirtualMachine.ID
private let name: String
private let library: VMLibraryController
private lazy var logger = Logger(for: Self.self)
@Published
public var options = VMSessionOptions.default {
didSet {
instance?.options = options
}
}
public typealias State = VMState
@Published
public private(set) var state = State.idle
/// Called from ``start()`` when the VM's MAC address collides with a currently-running VM.
public var macAddressConflictHandler: (@MainActor ([MACAddressConflict]) async -> MACAddressConflictResolution)?
private(set) var virtualMachine: VZVirtualMachine?
@Published
public var virtualMachineModel: VBVirtualMachine
public private(set) var savedStatesController: VMSavedStatesController
private var cancellables = Set<AnyCancellable>()
private let guestAppDiskImage: GuestAdditionsDiskImage
public init(with vm: VBVirtualMachine, library: VMLibraryController, options: VMSessionOptions? = nil) {
self.id = vm.id
self.name = vm.name
self.virtualMachineModel = vm
self.library = library
self.savedStatesController = VMSavedStatesController(library: library, virtualMachine: vm)
self.guestAppDiskImage = GuestAdditionsDiskImage(source: vm.configuration.guestAppDiskImageSource)
#if DEBUG
if ProcessInfo.isSwiftUIPreview { self.savedStatesController = .preview }
#endif
virtualMachineModel.reloadMetadata()
if virtualMachineModel.metadata.installImageURL != nil && !virtualMachineModel.metadata.installFinished {
self.options.bootOnInstallDevice = true
}
if let options {
self.options = options
}
/// Ensure configuration is persisted whenever it changes.
$virtualMachineModel
.dropFirst()
.debounce(for: .milliseconds(50), scheduler: DispatchQueue.main)
.sink { updatedModel in
do {
try updatedModel.saveMetadata()
} catch {
assertionFailure("Failed to save configuration: \(error)")
}
}
.store(in: &cancellables)
library.addController(self)
/// Make sure DFU mode flag is turned off if the app build doesn't allow DFU boot.
if !VBMacConfiguration.appBuildAllowsDFUMode {
self.options.bootInDFUMode = false
}
}
private var instance: VMInstance?
private func createInstance() throws -> VMInstance {
let newInstance = VMInstance(with: virtualMachineModel, library: library, onVMStop: { [weak self] error in
self?.state = .stopped(error)
})
newInstance.options = options
return newInstance
}
public func start() async throws {
// Check for MAC address collisions with running VMs before changing state.
guard await resolveMACAddressConflictsIfNeeded() else { return }
state = .starting(nil)
await waitForGuestDiskImageReadyIfNeeded()
if virtualMachineModel.hasPendingDiskImageResizes {
state = .resizingDisk("Checking disk images...")
do {
var updatedModel = virtualMachineModel
let didResize = try await updatedModel.checkAndResizeDiskImages { message in
self.state = .resizingDisk(message)
}
virtualMachineModel = updatedModel
if didResize {
presentDiskResizeCompletedAlert()
}
} catch {
logger.warning("Failed to resize disk images: \(error, privacy: .public)")
presentDiskResizeError(error)
state = .stopped(error)
throw error
}
}
state = .starting("Starting virtual machine...")
try await updatingState {
let newInstance = try createInstance()
self.instance = newInstance
if #available(macOS 14.0, *), let restorePackageURL = options.stateRestorationPackageURL {
do {
let package = try VBSavedStatePackage(url: restorePackageURL)
try await newInstance.restoreState(from: package) { vm, package in
try? await updatingState {
state = .restoringState(vm, package)
}
}
} catch {
guard !(error is CancellationError) else {
state = .idle
return
}
throw error
}
} else {
try await newInstance.startVM()
}
let vm = try newInstance.virtualMachine
state = .running(vm)
/// Update boot date VM metadata with the current date, only if not booting in recovery mode.
if !newInstance.isRecoveryBoot {
if virtualMachineModel.metadata.firstBootDate == nil {
logger.debug("Setting first boot date")
virtualMachineModel.metadata.firstBootDate = .now
}
virtualMachineModel.metadata.lastBootDate = .now
}
virtualMachineModel.metadata.installFinished = true
}
}
private func presentDiskResizeCompletedAlert() {
let alert = NSAlert()
alert.messageText = "Disk Image Expanded"
alert.informativeText = "The disk image now has more space, but the guest operating system still needs to claim it. In a macOS guest, run 'diskutil apfs resizeContainer disk0s2 0' in Terminal after starting up. In other guests, use the system's partitioning tools."
alert.alertStyle = .informational
alert.addButton(withTitle: "OK")
alert.runModal()
}
private func presentDiskResizeError(_ error: Error) {
let alert = NSAlert()
alert.messageText = "Disk Resize Failed"
alert.informativeText = "VirtualBuddy couldn't resize disk images before startup. The virtual machine was not started.\n\n\(error.localizedDescription)"
alert.alertStyle = .warning
alert.addButton(withTitle: "OK")
alert.runModal()
}
/// Checks whether this virtual machine's network devices share a MAC address with any running virtual machine,
/// asking the conflict handler how to proceed if so.
///
/// - Returns: `true` if startup should continue, or `false` if the user chose to cancel the launch.
private func resolveMACAddressConflictsIfNeeded() async -> Bool {
guard let handler = macAddressConflictHandler else { return true }
let conflicts = library.macAddressConflicts(for: virtualMachineModel)
guard !conflicts.isEmpty else { return true }
switch await handler(conflicts) {
case .cancel:
return false
case .continueAnyway:
return true
case .randomize:
for index in virtualMachineModel.configuration.hardware.networkDevices.indices {
virtualMachineModel.configuration.hardware.networkDevices[index].macAddress = VZMACAddress.randomLocallyAdministered().string.uppercased()
}
return true
}
}
/// If the virtual machine supports the guest app and has the toggle to auto-mount the guest image enabled,
/// this method waits until the guest disk image is ready before returning.
///
/// This is used to wait for the guest disk image to be ready before starting a virtual machine, which may occur
/// if the user launches VirtualBuddy then quickly attempts to start up a machine right after installing an app update.
///
/// It will also alert the user in case guest disk image generation has failed so that they know there's something wrong/
private func waitForGuestDiskImageReadyIfNeeded() async {
guard !VirtualBuddyManagedPreferences.guestAppDisabled,
virtualMachineModel.configuration.guestAdditionsEnabled,
virtualMachineModel.guestAppSupport != .unsupported
else { return }
/// Kick off legacy guest app download if needed.
if virtualMachineModel.configuration.guestAppVersion != nil {
Task { try? await guestAppDiskImage.installIfNeeded() }
}
let guestDiskState = guestAppDiskImage.state
logger.info("Guest disk image state is \(guestDiskState, privacy: .public)")
switch guestDiskState {
case .ready:
break
case .downloading, .installing:
await waitForGuestDiskImageReady()
case .installFailed(let error):
runGuestDiskImageErrorAlert(error: error)
}
}
private func waitForGuestDiskImageReady() async {
state = .starting("Preparing guest app disk image")
for await state in guestAppDiskImage.$state.values {
switch state {
case .ready:
logger.debug("Guest disk image is ready 🚀")
return
case .installFailed(let error):
logger.error("Guest disk image install failed - \(error, privacy: .public)")
return runGuestDiskImageErrorAlert(error: error)
case .downloading:
logger.debug("Guest disk image is downloading...")
case .installing:
logger.debug("Guest disk image is installing...")
}
}
}
private func runGuestDiskImageErrorAlert(error: Error) {
logger.debug(#function)
let alertSuppressionKey = "VBGuestDiskImageAlertSuppressed"
guard !UserDefaults.standard.bool(forKey: alertSuppressionKey) else {
logger.debug("Guest disk image error alert suppressed, ignoring error.")
return
}
let alert = NSAlert()
alert.messageText = "Guest App Image Error"
alert.informativeText =
"""
An error occurred when VirtualBuddy attempted to generate the disk image for the guest app. Restarting the app might fix it.
The virtual machine will boot normally, but the guest app disk image will not be mounted.
If the virtual machine already has the guest app installed, it will not be updated to the latest version.
\(error)
"""
alert.addButton(withTitle: "Continue")
alert.showsSuppressionButton = true
alert.runModal()
if let suppressionButton = alert.suppressionButton,
suppressionButton.state == .on
{
logger.info("Guest disk image error alert will be suppressed in the future.")
UserDefaults.standard.set(true, forKey: alertSuppressionKey)
}
}
public func pause() async throws {
try await updatingState {
let instance = try ensureInstance()
try await instance.pause()
let vm = try instance.virtualMachine
state = .paused(vm)
}
unhideCursor()
}
public func resume() async throws {
try await updatingState {
let instance = try ensureInstance()
try await instance.resume()
let vm = try instance.virtualMachine
state = .running(vm)
}
unhideCursor()
}
public func stop() async throws {
try await updatingState {
let instance = try ensureInstance()
try await instance.stop()
}
unhideCursor()
}
public func forceStop() async throws {
try await updatingState {
let instance = try ensureInstance()
try await instance.forceStop()
state = .stopped(nil)
}
unhideCursor()
}
/// Replaces the running virtual machine's network attachments and starts automatic retries for
/// any host interfaces that are not available yet.
public func reconnectNetwork() throws {
try ensureInstance().reconnectNetwork()
}
public var availableBridgeInterfaces: [VBNetworkDeviceInterface] {
VBNetworkDevice.bridgeInterfaces.sorted { lhs, rhs in
let nameOrder = lhs.name.localizedStandardCompare(rhs.name)
return nameOrder == .orderedSame ? lhs.id < rhs.id : nameOrder == .orderedAscending
}
}
public var activeBridgeInterfaceIdentifiers: Set<String> {
instance?.activeBridgeInterfaceIdentifiers ?? []
}
@available(macOS 27.0, *)
public var usbDeviceController: VMUSBDeviceController? {
instance?.usbDeviceController
}
public func changeBridgeInterface(to interfaceIdentifier: String) throws {
let instance = try ensureInstance()
objectWillChange.send()
try instance.changeBridgeInterface(to: interfaceIdentifier)
}
@available(macOS 14.0, *)
public func saveState(snapshotName name: String) async throws {
try await updatingState {
let instance = try ensureInstance()
let vm = try instance.virtualMachine
do {
let package = try await instance.saveState(snapshotName: name) {
state = .savingState(vm)
}
state = .stateSaveCompleted(vm, package)
} catch is CancellationError {
/// User cancellation is not an error, it may just be ignored here.
/// As of the current implementation of `VMInstance.saveState`, the VM won't be paused
/// because the only cancellation point is before that happens, but check for pause in here just in
/// case that behavior changes in the future.
try await resumeIfNeeded()
} catch {
throw error
}
try await resumeIfNeeded()
}
unhideCursor()
}
private func resumeIfNeeded() async throws {
guard !state.isRunning else { return }
try await Task.sleep(for: .seconds(1.5))
try await resume()
}
private func updatingState(perform block: () async throws -> Void) async throws {
do {
try await block()
} catch {
state = .stopped(error)
throw error
}
}
private func ensureInstance() throws -> VMInstance {
guard let instance = instance else {
throw CocoaError(.validationMissingMandatoryProperty)
}
instance.options = options
return instance
}
public func storeScreenshot(with data: Data) {
do {
try virtualMachineModel.write(data, forMetadataFileNamed: VBVirtualMachine.screenshotFileName)
try virtualMachineModel.invalidateThumbnail()
} catch {
logger.error("Error storing screenshot: \(error)")
}
}
public func invalidate() {
library.removeController(self)
}
deinit {
#if DEBUG
print("\(name) Bye bye 👋")
#endif
library.removeController(self)
VBMemoryLeakDebugAssertions.vb_objectIsBeingReleased(self)
}
}
public extension VMState {
static func ==(lhs: VMState, rhs: VMState) -> Bool {
switch lhs {
case .idle: return rhs.isIdle
case .starting: return rhs.isStarting
case .resizingDisk: return rhs.isResizingDisk
case .running: return rhs.isRunning
case .paused: return rhs.isPaused
case .stopped: return rhs.isStopped
case .savingState: return rhs.isSavingState
case .restoringState: return rhs.isRestoringState
case .stateSaveCompleted: return rhs.isStateSaveCompleted
}
}
var isIdle: Bool {
guard case .idle = self else { return false }
return true
}
var isStarting: Bool {
guard case .starting = self else { return false }
return true
}
var isResizingDisk: Bool {
guard case .resizingDisk = self else { return false }
return true
}
var isRunning: Bool {
guard case .running = self else { return false }
return true
}
var isPaused: Bool {
guard case .paused = self else { return false }
return true
}
var isStopped: Bool {
guard case .stopped = self else { return false }
return true
}
var isSavingState: Bool {
guard case .savingState = self else { return false }
return true
}
var isRestoringState: Bool {
guard case .restoringState = self else { return false }
return true
}
var isStateSaveCompleted: Bool {
guard case .stateSaveCompleted = self else { return false }
return true
}
var canStart: Bool {
switch self {
case .idle, .stopped:
return true
default:
return false
}
}
var canResume: Bool {
switch self {
case .paused:
return true
default:
return false
}
}
var canPause: Bool {
switch self {
case .running:
return true
default:
return false
}
}
}
public extension VMController {
var canStart: Bool { state.canStart }
var canResume: Bool { state.canResume }
var canPause: Bool { state.canPause }
var canReconnectNetwork: Bool { state.isRunning || state.isPaused }
var canChangeBridgeInterface: Bool {
canReconnectNetwork && instance?.hasBridgedNetworkAttachments == true
}
}
public extension VMController {
/// Workaround for cursor disappearing due to it being captured
/// by Virtualization during state transitions.
func unhideCursor() {
Task {
try? await Task.sleep(nanoseconds: 100_000_000)
NSCursor.unhide()
}
}
}
public extension VBMacConfiguration {
/// DFU mode option is currently shown in debug builds or when `VBShowDFUModeBootOption` is set in user defaults.
/// To enable in release builds: `defaults write codes.rambo.VirtualBuddy VBShowDFUModeBootOption -bool YES`
static var appBuildAllowsDFUMode: Bool {
#if DEBUG
return true
#else
return UserDefaults.standard.bool(forKey: "VBShowDFUModeBootOption")
#endif
}
}
extension VBMacConfiguration {
var guestAppDiskImageSource: GuestAdditionsDiskImage.Source {
if let guestAppVersion {
GuestAdditionsDiskImage.Source.catalog(guestAppVersion)
} else {
GuestAdditionsDiskImage.Source.embedded
}
}
}