mirror of
https://github.com/insidegui/VirtualBuddy.git
synced 2026-12-02 00:59:45 +08:00
* Fix HostingWindowController allowing repeated requests to close while waiting for confirmation * New virtual machine state preservation system with automatic save on close/quit and safe restoration * Use visible directories for saved state storage for easier debugging in Finder * Hide state restoration controls for unsupported Linux VMs * Add app-level preference for what happens by default when attempting to close a running VM * Use a custom sheet for VM close confirmation * Improve VM close choice sheet * Harden session storage against path traversal, fix race conditions in state coordination
75 lines
2.5 KiB
Swift
75 lines
2.5 KiB
Swift
/*
|
||
See LICENSE folder for this sample’s licensing information.
|
||
|
||
Abstract:
|
||
Helper that creates various configuration objects exposed in the `VZVirtualMachineConfiguration`.
|
||
*/
|
||
|
||
import Foundation
|
||
import Virtualization
|
||
|
||
@available(macOS 13.0, *)
|
||
struct LinuxVirtualMachineConfigurationHelper: VirtualMachineConfigurationHelper {
|
||
let vm: VBVirtualMachine
|
||
let restoration: SavedSessionRestoration? = nil
|
||
|
||
init(vm: VBVirtualMachine) {
|
||
self.vm = vm
|
||
}
|
||
|
||
func createInstallDevice(installImageURL: URL) throws -> VZStorageDeviceConfiguration {
|
||
let attachment = try VZDiskImageStorageDeviceAttachment(url: installImageURL, readOnly: true, cachingMode: .cached, synchronizationMode: .fsync)
|
||
let usbDeviceConfiguration = VZUSBMassStorageDeviceConfiguration(attachment: attachment)
|
||
return usbDeviceConfiguration
|
||
}
|
||
|
||
func createBootLoader() throws -> VZBootLoader {
|
||
let efi = VZEFIBootLoader()
|
||
let storeURL = vm.metadataDirectoryURL.appendingPathComponent("nvram")
|
||
if FileManager.default.fileExists(atPath: storeURL.path) {
|
||
efi.variableStore = VZEFIVariableStore(url: storeURL)
|
||
} else {
|
||
efi.variableStore = try VZEFIVariableStore(creatingVariableStoreAt: storeURL, options: [])
|
||
}
|
||
return efi
|
||
}
|
||
|
||
func createGraphicsDevices() -> [VZGraphicsDeviceConfiguration] {
|
||
let graphicsConfiguration = VZVirtioGraphicsDeviceConfiguration()
|
||
|
||
graphicsConfiguration.scanouts = vm.configuration.hardware.displayDevices.map(\.vzScanout)
|
||
|
||
return [graphicsConfiguration]
|
||
}
|
||
|
||
@available(macOS 13.0, *)
|
||
func createSpiceAgentConsoleDeviceConfiguration() -> VZVirtioConsoleDeviceConfiguration? {
|
||
guard vm.configuration.clipboardSharingEnabled else { return nil }
|
||
|
||
let consoleDevice = VZVirtioConsoleDeviceConfiguration()
|
||
|
||
let spiceAgentPort = VZVirtioConsolePortConfiguration()
|
||
spiceAgentPort.name = VZSpiceAgentPortAttachment.spiceAgentPortName
|
||
spiceAgentPort.attachment = VZSpiceAgentPortAttachment()
|
||
consoleDevice.ports[0] = spiceAgentPort
|
||
|
||
return consoleDevice
|
||
}
|
||
|
||
@available(macOS 15.0, *)
|
||
func createUSBControllers() -> [VZUSBControllerConfiguration] {
|
||
[VZXHCIControllerConfiguration()]
|
||
}
|
||
}
|
||
|
||
// MARK: - Configuration Models -> Virtualization
|
||
|
||
@available(macOS 13.0, *)
|
||
extension VBDisplayDevice {
|
||
|
||
var vzScanout: VZVirtioGraphicsScanoutConfiguration {
|
||
VZVirtioGraphicsScanoutConfiguration(widthInPixels: width, heightInPixels: height)
|
||
}
|
||
|
||
}
|