Files
VirtualBuddy/VirtualCore/Source/Models/VBStorageDeviceContainer.swift
Guilherme Rambo 2a7cf4151b New virtual machine state preservation system (#780)
* 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
2026-10-05 12:51:43 -03:00

57 lines
1.8 KiB
Swift

import Foundation
/// Adopted by ``VBVirtualMachine`` in order to help resolve storage devices when bootstrapping a VM.
public protocol VBStorageDeviceContainer {
var bundleURL: URL { get }
var storageDevices: [VBStorageDevice] { get }
var bootDevice: VBStorageDevice { get throws }
var bootDiskImage: VBManagedDiskImage { get throws }
var allowDiskImageCreation: Bool { get }
func diskImageURL(for image: VBManagedDiskImage) -> URL
func diskImageURL(for device: VBStorageDevice) -> URL
}
// MARK: - Default Implementations
/// These default implementations take care of resolving disk images.
public extension VBStorageDeviceContainer {
var allowDiskImageCreation: Bool { false }
var bootDevice: VBStorageDevice {
get throws {
guard let device = storageDevices.first(where: { $0.isBootVolume }) else {
throw Failure("The virtual machine lacks a bootable storage device.")
}
return device
}
}
var bootDiskImage: VBManagedDiskImage {
get throws {
let device = try bootDevice
guard case .managedImage(let image) = device.backing else {
throw Failure("The boot device must use a disk image managed by VirtualBuddy")
}
return image
}
}
func diskImageURL(for image: VBManagedDiskImage) -> URL {
bundleURL
.appendingPathComponent(image.filename)
.appendingPathExtension(image.format.fileExtension)
}
func diskImageURL(for device: VBStorageDevice) -> URL {
switch device.backing {
case .managedImage(let image):
return diskImageURL(for: image)
case .customImage(let customURL):
return customURL
}
}
}