-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Linux support * Support macOS 12 * Improve readability a bit * Remove useless variable
- Loading branch information
Showing
10 changed files
with
237 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Foundation | ||
|
||
enum Architecture: String, Codable { | ||
case arm64 | ||
case amd64 | ||
} | ||
|
||
func CurrentArchitecture() -> Architecture { | ||
#if arch(arm64) | ||
return .arm64 | ||
#elseif arch(x86_64) | ||
return .amd64 | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import Virtualization | ||
|
||
struct Darwin: Platform { | ||
var ecid: VZMacMachineIdentifier | ||
var hardwareModel: VZMacHardwareModel | ||
|
||
init(ecid: VZMacMachineIdentifier, hardwareModel: VZMacHardwareModel) { | ||
self.ecid = ecid | ||
self.hardwareModel = hardwareModel | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
|
||
let encodedECID = try container.decode(String.self, forKey: .ecid) | ||
guard let data = Data.init(base64Encoded: encodedECID) else { | ||
throw DecodingError.dataCorruptedError(forKey: .ecid, | ||
in: container, | ||
debugDescription: "failed to initialize Data using the provided value") | ||
} | ||
guard let ecid = VZMacMachineIdentifier.init(dataRepresentation: data) else { | ||
throw DecodingError.dataCorruptedError(forKey: .ecid, | ||
in: container, | ||
debugDescription: "failed to initialize VZMacMachineIdentifier using the provided value") | ||
} | ||
self.ecid = ecid | ||
|
||
let encodedHardwareModel = try container.decode(String.self, forKey: .hardwareModel) | ||
guard let data = Data.init(base64Encoded: encodedHardwareModel) else { | ||
throw DecodingError.dataCorruptedError(forKey: .hardwareModel, in: container, debugDescription: "") | ||
} | ||
guard let hardwareModel = VZMacHardwareModel.init(dataRepresentation: data) else { | ||
throw DecodingError.dataCorruptedError(forKey: .hardwareModel, in: container, debugDescription: "") | ||
} | ||
self.hardwareModel = hardwareModel | ||
} | ||
|
||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
|
||
try container.encode(ecid.dataRepresentation.base64EncodedString(), forKey: .ecid) | ||
try container.encode(hardwareModel.dataRepresentation.base64EncodedString(), forKey: .hardwareModel) | ||
} | ||
|
||
func os() -> OS { | ||
.darwin | ||
} | ||
|
||
func bootLoader(nvramURL: URL) throws -> VZBootLoader { | ||
VZMacOSBootLoader() | ||
} | ||
|
||
func platform(nvramURL: URL) -> VZPlatformConfiguration { | ||
let result = VZMacPlatformConfiguration() | ||
|
||
result.machineIdentifier = ecid | ||
result.auxiliaryStorage = VZMacAuxiliaryStorage(contentsOf: nvramURL) | ||
result.hardwareModel = hardwareModel | ||
|
||
return result | ||
} | ||
|
||
func graphicsDevice(vmConfig: VMConfig) -> VZGraphicsDeviceConfiguration { | ||
let result = VZMacGraphicsDeviceConfiguration() | ||
|
||
if let hostMainScreen = NSScreen.main { | ||
let vmScreenSize = NSSize(width: vmConfig.display.width, height: vmConfig.display.height) | ||
result.displays = [ | ||
VZMacGraphicsDisplayConfiguration(for: hostMainScreen, sizeInPoints: vmScreenSize) | ||
] | ||
|
||
return result | ||
} | ||
|
||
result.displays = [ | ||
VZMacGraphicsDisplayConfiguration( | ||
widthInPixels: vmConfig.display.width, | ||
heightInPixels: vmConfig.display.height, | ||
// A reasonable guess according to Apple's documentation[1] | ||
// [1]: https://developer.apple.com/documentation/coregraphics/1456599-cgdisplayscreensize | ||
pixelsPerInch: 72 | ||
) | ||
] | ||
|
||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Virtualization | ||
|
||
@available(macOS 13, *) | ||
struct Linux: Platform { | ||
func os() -> OS { | ||
.linux | ||
} | ||
|
||
func bootLoader(nvramURL: URL) throws -> VZBootLoader { | ||
let result = VZEFIBootLoader() | ||
|
||
result.variableStore = VZEFIVariableStore(url: nvramURL) | ||
|
||
return result | ||
} | ||
|
||
func platform(nvramURL: URL) -> VZPlatformConfiguration { | ||
VZGenericPlatformConfiguration() | ||
} | ||
|
||
func graphicsDevice(vmConfig: VMConfig) -> VZGraphicsDeviceConfiguration { | ||
let result = VZVirtioGraphicsDeviceConfiguration() | ||
|
||
result.scanouts = [ | ||
VZVirtioGraphicsScanoutConfiguration( | ||
widthInPixels: vmConfig.display.width, | ||
heightInPixels: vmConfig.display.height | ||
) | ||
] | ||
|
||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Virtualization | ||
|
||
enum OS: String, Codable { | ||
case darwin | ||
case linux | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Virtualization | ||
|
||
protocol Platform: Codable { | ||
func os() -> OS | ||
func bootLoader(nvramURL: URL) throws -> VZBootLoader | ||
func platform(nvramURL: URL) -> VZPlatformConfiguration | ||
func graphicsDevice(vmConfig: VMConfig) -> VZGraphicsDeviceConfiguration | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.