-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathUTMScriptingVirtualMachineImpl.swift
More file actions
407 lines (363 loc) · 15.9 KB
/
UTMScriptingVirtualMachineImpl.swift
File metadata and controls
407 lines (363 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//
// Copyright © 2022 osy. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import QEMUKitInternal
@MainActor
@objc(UTMScriptingVirtualMachineImpl)
class UTMScriptingVirtualMachineImpl: NSObject, UTMScriptable {
@nonobjc var box: VMData
@nonobjc var data: UTMData
@nonobjc var vm: (any UTMVirtualMachine)! {
box.wrapped
}
@objc var id: String {
vm.id.uuidString
}
@objc var name: String {
box.detailsTitleLabel
}
@objc var notes: String {
box.detailsNotes ?? ""
}
@objc var machine: String {
box.detailsSystemTargetLabel
}
@objc var architecture: String {
box.detailsSystemArchitectureLabel
}
@objc var memory: String {
box.detailsSystemMemoryLabel
}
@objc var backend: UTMScriptingBackend {
if vm is UTMQemuVirtualMachine {
return .qemu
} else if vm is UTMAppleVirtualMachine {
return .apple
} else {
return .unavailable
}
}
@objc var status: UTMScriptingStatus {
switch vm.state {
case .stopped: return .stopped
case .starting: return .starting
case .started: return .started
case .pausing: return .pausing
case .paused: return .paused
case .resuming: return .resuming
case .stopping: return .stopping
case .saving: return .pausing // FIXME: new entries
case .restoring: return .resuming // FIXME: new entries
}
}
@objc var serialPorts: [UTMScriptingSerialPortImpl] {
if let config = vm.config as? UTMQemuConfiguration {
return config.serials.indices.map({ UTMScriptingSerialPortImpl(qemuSerial: config.serials[$0], parent: self, index: $0) })
} else if let config = vm.config as? UTMAppleConfiguration {
return config.serials.indices.map({ UTMScriptingSerialPortImpl(appleSerial: config.serials[$0], parent: self, index: $0) })
} else {
return []
}
}
var guestAgent: QEMUGuestAgent! {
get async {
await (vm as? UTMQemuVirtualMachine)?.guestAgent
}
}
var qemuProcess: UTMQemuSystem? {
get async {
await (vm as? UTMQemuVirtualMachine)?.system
}
}
override var objectSpecifier: NSScriptObjectSpecifier? {
let appDescription = NSApplication.classDescription() as! NSScriptClassDescription
return NSUniqueIDSpecifier(containerClassDescription: appDescription,
containerSpecifier: nil,
key: "scriptingVirtualMachines",
uniqueID: id)
}
init(for vm: VMData, data: UTMData) {
self.box = vm
self.data = data
}
@objc func start(_ command: NSScriptCommand) {
let shouldSaveState = command.evaluatedArguments?["saveFlag"] as? Bool ?? true
let bootRecoveryMode = command.evaluatedArguments?["bootRecoveryFlag"] as? Bool ?? false
withScriptCommand(command) { [self] in
var options: UTMVirtualMachineStartOptions = []
if !shouldSaveState {
guard type(of: vm).capabilities.supportsDisposibleMode else {
throw ScriptingError.operationNotSupported
}
options.insert(.bootDisposibleMode)
}
if bootRecoveryMode {
guard type(of: vm).capabilities.supportsRecoveryMode else {
throw ScriptingError.operationNotSupported
}
options.insert(.bootRecovery)
}
data.run(vm: box, startImmediately: false)
if vm.state == .stopped {
try await vm.start(options: options)
} else if vm.state == .paused {
try await vm.resume()
} else {
throw ScriptingError.operationNotAvailable
}
}
}
@objc func suspend(_ command: NSScriptCommand) {
let shouldSaveState = command.evaluatedArguments?["saveFlag"] as? Bool ?? false
withScriptCommand(command) { [self] in
guard vm.state == .started else {
throw ScriptingError.notRunning
}
try await vm.pause()
if shouldSaveState {
try await vm.saveSnapshot(name: nil)
}
}
}
@objc func stop(_ command: NSScriptCommand) {
let stopMethod: UTMScriptingStopMethod
if let stopMethodValue = command.evaluatedArguments?["stopBy"] as? AEKeyword {
stopMethod = UTMScriptingStopMethod(rawValue: stopMethodValue) ?? .force
} else {
stopMethod = .force
}
withScriptCommand(command) { [self] in
guard vm.state == .started || stopMethod == .kill else {
throw ScriptingError.notRunning
}
switch stopMethod {
case .force:
try await vm.stop(usingMethod: .force)
case .kill:
try await vm.stop(usingMethod: .kill)
case .request:
try await vm.stop(usingMethod: .request)
}
}
}
@objc func delete(_ command: NSDeleteCommand) {
withScriptCommand(command) { [self] in
guard vm.state == .stopped else {
throw ScriptingError.notStopped
}
try await data.delete(vm: box, alsoRegistry: true)
}
}
@objc func clone(_ command: NSCloneCommand) {
let properties = command.evaluatedArguments?["WithProperties"] as? [AnyHashable : Any]
withScriptCommand(command) { [self] in
guard vm.state == .stopped else {
throw ScriptingError.notStopped
}
let newVM = try await data.clone(vm: box)
if let properties = properties, let newConfiguration = properties["configuration"] as? [AnyHashable : Any] {
let wrapper = UTMScriptingConfigImpl(newVM.config!)
try wrapper.updateConfiguration(from: newConfiguration)
try await data.save(vm: newVM)
}
}
}
@objc func export(_ command: NSCloneCommand) {
let exportUrl = command.evaluatedArguments?["file"] as? URL
withScriptCommand(command) { [self] in
guard vm.state == .stopped else {
throw ScriptingError.notStopped
}
try await data.export(vm: box, to: exportUrl!)
}
}
}
// MARK: - Guest agent suite
@objc extension UTMScriptingVirtualMachineImpl {
@nonobjc private func withGuestAgent<Result>(_ block: (QEMUGuestAgent) async throws -> Result) async throws -> Result {
guard vm.state == .started else {
throw ScriptingError.notRunning
}
guard let vm = vm as? UTMQemuVirtualMachine else {
throw ScriptingError.operationNotSupported
}
guard let guestAgent = await vm.guestAgent else {
throw ScriptingError.guestAgentNotRunning
}
return try await block(guestAgent)
}
@objc func valueInOpenFilesWithUniqueID(_ id: Int) -> UTMScriptingGuestFileImpl {
UTMScriptingGuestFileImpl(from: id, parent: self)
}
@objc func openFile(_ command: NSScriptCommand) {
let path = command.evaluatedArguments?["path"] as? String
let mode = command.evaluatedArguments?["mode"] as? AEKeyword
let isUpdate = command.evaluatedArguments?["isUpdate"] as? Bool ?? false
withScriptCommand(command) { [self] in
guard let path = path else {
throw ScriptingError.invalidParameter
}
let modeValue: String
if let mode = mode {
switch UTMScriptingOpenMode(rawValue: mode) {
case .reading: modeValue = "r"
case .writing: modeValue = "w"
case .appending: modeValue = "a"
default: modeValue = "r"
}
} else {
modeValue = "r"
}
return try await withGuestAgent { guestAgent in
let handle = try await guestAgent.guestFileOpen(path, mode: modeValue + (isUpdate ? "+" : ""))
return UTMScriptingGuestFileImpl(from: handle, parent: self)
}
}
}
@objc func valueInProcessesWithUniqueID(_ id: Int) -> UTMScriptingGuestProcessImpl {
UTMScriptingGuestProcessImpl(from: id, parent: self)
}
@objc func execute(_ command: NSScriptCommand) {
let path = command.evaluatedArguments?["path"] as? String
let argv = command.evaluatedArguments?["argv"] as? [String]
let envp = command.evaluatedArguments?["envp"] as? [String]
let input = command.evaluatedArguments?["input"] as? String
let isBase64Encoded = command.evaluatedArguments?["isBase64Encoded"] as? Bool ?? false
let isCaptureOutput = command.evaluatedArguments?["isCaptureOutput"] as? Bool ?? false
let inputData = dataFromText(input, isBase64Encoded: isBase64Encoded)
withScriptCommand(command) { [self] in
guard let path = path else {
throw ScriptingError.invalidParameter
}
return try await withGuestAgent { guestAgent in
let pid = try await guestAgent.guestExec(path, argv: argv, envp: envp, input: inputData, captureOutput: isCaptureOutput)
return UTMScriptingGuestProcessImpl(from: pid, parent: self)
}
}
}
@objc func queryIp(_ command: NSScriptCommand) {
withScriptCommand(command) { [self] in
// Apple Virtualization backend: no guest agent available
if let appleVM = vm as? UTMAppleVirtualMachine {
guard appleVM.state == .started else {
throw ScriptingError.notRunning
}
guard let network = appleVM.config.networks.first else {
return []
}
let macAddress = network.macAddress.lowercased()
return Self.ipFromARP(macAddress: macAddress)
}
// Non-Apple backend (QEMU): use guest agent
return try await withGuestAgent { guestAgent in
let interfaces = try await guestAgent.guestNetworkGetInterfaces()
var ipv4: [String] = []
var ipv6: [String] = []
for interface in interfaces {
for ip in interface.ipAddresses {
if ip.isIpV6Address {
if ip.ipAddress != "::1" && ip.ipAddress != "0:0:0:0:0:0:0:1" {
ipv6.append(ip.ipAddress)
}
} else {
if ip.ipAddress != "127.0.0.1" {
ipv4.append(ip.ipAddress)
}
}
}
}
return ipv4 + ipv6
}
}
}
}
extension UTMScriptingVirtualMachineImpl {
/// Normalizes a colon-separated MAC address by stripping leading zeros from each
/// octet so that `%x`-formatted bytes compare equal to stored representations.
///
/// Example: `"ce:09:f1:ce:7f:f2"` → `"ce:9:f1:ce:7f:f2"`
private static func normalizeMac(_ mac: String) -> String {
mac.split(separator: ":").map { octet in
let stripped = octet.drop(while: { $0 == "0" })
return stripped.isEmpty ? "0" : String(stripped)
}.joined(separator: ":")
}
/// Find the IP address for the given MAC by querying the kernel ARP cache via
/// `sysctl(CTL_NET, PF_ROUTE, …, NET_RT_FLAGS, RTF_LLINFO)`.
///
/// - Parameter macAddress: Lowercase colon-separated MAC, e.g. `"ce:09:f1:ce:7f:f2"`.
/// - Returns: A single-element array with the IP, or empty if not found.
static func ipFromARP(macAddress: String) -> [String] {
var mib: [Int32] = [CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_FLAGS, RTF_LLINFO]
var needed = 0
guard sysctl(&mib, 6, nil, &needed, nil, 0) == 0, needed > 0 else { return [] }
var buf = [UInt8](repeating: 0, count: needed)
guard sysctl(&mib, 6, &buf, &needed, nil, 0) == 0 else { return [] }
let normalizedTarget = normalizeMac(macAddress)
var offset = 0
while offset + MemoryLayout<rt_msghdr>.stride <= needed {
let msglen = Int(buf.withUnsafeBytes {
$0.load(fromByteOffset: offset, as: rt_msghdr.self).rtm_msglen
})
guard msglen > 0, offset + msglen <= needed else { break }
defer { offset += msglen }
// Sockaddrs start immediately after rt_msghdr.
// First: sockaddr_in (destination IP). Layout: len(1) family(1) port(2) addr(4) …
let sinStart = offset + MemoryLayout<rt_msghdr>.stride
guard sinStart + 8 <= needed else { continue }
let sinLen = Int(buf[sinStart])
let sinFamily = buf[sinStart + 1]
guard sinFamily == UInt8(AF_INET), sinLen >= 8 else { continue }
let ipStr = buf[(sinStart + 4)..<(sinStart + 8)].map { String($0) }.joined(separator: ".")
// Second: sockaddr_dl (link-layer MAC). Padded to sizeof(long) = 8.
// Layout: len(1) family(1) index(2) type(1) nlen(1) alen(1) slen(1) data[nlen+alen…]
let sdlStart = sinStart + ((sinLen + 7) & ~7)
guard sdlStart + 8 <= needed else { continue }
let sdlFamily = buf[sdlStart + 1]
let sdlNlen = Int(buf[sdlStart + 5])
let sdlAlen = Int(buf[sdlStart + 6])
guard sdlFamily == UInt8(AF_LINK), sdlAlen == 6 else { continue }
let macStart = sdlStart + 8 + sdlNlen
guard macStart + 6 <= needed else { continue }
let mac = buf[macStart..<(macStart + 6)].map { String(format: "%x", $0) }.joined(separator: ":")
if normalizeMac(mac) == normalizedTarget {
return [ipStr]
}
}
return []
}
}
// MARK: - Errors
extension UTMScriptingVirtualMachineImpl {
enum ScriptingError: Error, LocalizedError {
case operationNotAvailable
case operationNotSupported
case notRunning
case notStopped
case guestAgentNotRunning
case invalidParameter
var errorDescription: String? {
switch self {
case .operationNotAvailable: return NSLocalizedString("Operation not available.", comment: "UTMScriptingVirtualMachineImpl")
case .operationNotSupported: return NSLocalizedString("Operation not supported by the backend.", comment: "UTMScriptingVirtualMachineImpl")
case .notRunning: return NSLocalizedString("The virtual machine is not running.", comment: "UTMScriptingVirtualMachineImpl")
case .notStopped: return NSLocalizedString("The virtual machine must be stopped before this operation can be performed.", comment: "UTMScriptingVirtualMachineImpl")
case .guestAgentNotRunning: return NSLocalizedString("The QEMU guest agent is not running or not installed on the guest.", comment: "UTMScriptingVirtualMachineImpl")
case .invalidParameter: return NSLocalizedString("One or more required parameters are missing or invalid.", comment: "UTMScriptingVirtualMachineImpl")
}
}
}
}