@inkeep/impulse-compute 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,60 @@
1
+ import Foundation
2
+ import Virtualization
3
+
4
+ let dir = URL(fileURLWithPath: CommandLine.arguments[1])
5
+ guard let raw = try? Data(contentsOf: dir.appendingPathComponent("config.json")),
6
+ let cfg = try? JSONSerialization.jsonObject(with: raw) as? [String: Any],
7
+ let hmText = cfg["hardwareModel"] as? String, let hmData = Data(base64Encoded: hmText),
8
+ let midText = cfg["machineIdentifier"] as? String, let midData = Data(base64Encoded: midText),
9
+ let hm = VZMacHardwareModel(dataRepresentation: hmData),
10
+ let mid = VZMacMachineIdentifier(dataRepresentation: midData),
11
+ let cpus = cfg["cpuCount"] as? NSNumber, let mem = cfg["memorySize"] as? NSNumber
12
+ else {
13
+ print("PROBE: unreadable config")
14
+ exit(2)
15
+ }
16
+ let platform = VZMacPlatformConfiguration()
17
+ platform.hardwareModel = hm
18
+ platform.machineIdentifier = mid
19
+ platform.auxiliaryStorage = VZMacAuxiliaryStorage(contentsOf: dir.appendingPathComponent("nvram.bin"))
20
+ let config = VZVirtualMachineConfiguration()
21
+ config.platform = platform
22
+ config.bootLoader = VZMacOSBootLoader()
23
+ config.cpuCount = cpus.intValue
24
+ config.memorySize = mem.uint64Value
25
+ guard let disk = try? VZDiskImageStorageDeviceAttachment(url: dir.appendingPathComponent("disk.img"), readOnly: false) else {
26
+ print("PROBE: unreadable disk")
27
+ exit(2)
28
+ }
29
+ config.storageDevices = [VZVirtioBlockDeviceConfiguration(attachment: disk)]
30
+ let graphics = VZMacGraphicsDeviceConfiguration()
31
+ graphics.displays = [VZMacGraphicsDisplayConfiguration(widthInPixels: 1024, heightInPixels: 768, pixelsPerInch: 80)]
32
+ config.graphicsDevices = [graphics]
33
+ do {
34
+ try config.validate()
35
+ } catch {
36
+ let e = error as NSError
37
+ print("PROBE: validate failed domain=\(e.domain) code=\(e.code) desc=\(e.localizedDescription)")
38
+ exit(3)
39
+ }
40
+ let vm = VZVirtualMachine(configuration: config)
41
+ let began = Date()
42
+ vm.start { result in
43
+ switch result {
44
+ case .success:
45
+ print("PROBE: started after \(Date().timeIntervalSince(began)) s, state=\(vm.state.rawValue); stopping")
46
+ vm.stop { stopError in
47
+ if let s = stopError as NSError? { print("PROBE: stop error domain=\(s.domain) code=\(s.code)") } else { print("PROBE: stopped") }
48
+ exit(0)
49
+ }
50
+ case .failure(let error):
51
+ let e = error as NSError
52
+ print("PROBE: start failed after \(Date().timeIntervalSince(began)) s domain=\(e.domain) code=\(e.code) desc=\(e.localizedDescription)")
53
+ exit(1)
54
+ }
55
+ }
56
+ DispatchQueue.main.asyncAfter(deadline: .now() + 60) {
57
+ print("PROBE: no completion within 60 s")
58
+ exit(4)
59
+ }
60
+ dispatchMain()