Skip to content

Commit

Permalink
Use virtio drive, cached mode and full synchronization mode on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
amalchuk committed Nov 30, 2023
1 parent 5bcbc77 commit ddb97cf
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions Sources/tart/VM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,14 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject {
}

// Storage
var devices: [VZStorageDeviceConfiguration] = [
VZVirtioBlockDeviceConfiguration(attachment: try VZDiskImageStorageDeviceAttachment(url: diskURL, readOnly: false))
]
let attachment: VZDiskImageStorageDeviceAttachment = try getStorageDevice(diskURL: diskURL, useFsWorkAround: vmConfig.os == .linux)
var device: VZStorageDeviceConfiguration
if #available(macOS 14, *), vmConfig.os == .linux {
device = VZNVMExpressControllerDeviceConfiguration(attachment: attachment)
} else {
device = VZVirtioBlockDeviceConfiguration(attachment: attachment)
}
var devices: [VZStorageDeviceConfiguration] = [device]
devices.append(contentsOf: additionalStorageDevices)
configuration.storageDevices = devices

Expand Down Expand Up @@ -362,6 +367,15 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject {
return configuration
}

private static func getStorageDevice(diskURL: URL, useFsWorkAround: Bool = false) throws -> VZDiskImageStorageDeviceAttachment {
if #available(macOS 12, *), useFsWorkAround {
// Use cached caching mode for virtio drive to prevent fs corruption on linux when possible
return try VZDiskImageStorageDeviceAttachment(url: diskURL, readOnly: false, cachingMode: .cached, synchronizationMode: .full)
} else {
return try VZDiskImageStorageDeviceAttachment(url: diskURL, readOnly: false)
}
}

func guestDidStop(_ virtualMachine: VZVirtualMachine) {
print("guest has stopped the virtual machine")
sema.signal()
Expand Down

0 comments on commit ddb97cf

Please sign in to comment.