|
| 1 | +// |
| 2 | +// Copyright © 2023 osy. All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +import Foundation |
| 18 | + |
| 19 | +@MainActor |
| 20 | +@objc extension AppDelegate: UTMScriptable { |
| 21 | + private var bytesInMib: Int { |
| 22 | + 1048576 |
| 23 | + } |
| 24 | + |
| 25 | + private var bytesInGib: Int { |
| 26 | + 1073741824 |
| 27 | + } |
| 28 | + |
| 29 | + @objc func handleCreateCommand(_ command: NSCreateCommand) { |
| 30 | + if command.createClassDescription.implementationClassName == "UTMScriptingVirtualMachineImpl" { |
| 31 | + let properties = command.resolvedKeyDictionary |
| 32 | + withScriptCommand(command) { [self] in |
| 33 | + guard let backend = properties["backend"] as? AEKeyword, let backend = UTMScriptingBackend(rawValue: backend) else { |
| 34 | + throw ScriptingError.backendNotFound |
| 35 | + } |
| 36 | + guard let configuration = properties["configuration"] as? [AnyHashable : Any] else { |
| 37 | + throw ScriptingError.configurationNotFound |
| 38 | + } |
| 39 | + if backend == .qemu { |
| 40 | + return try await createQemuVirtualMachine(from: configuration).objectSpecifier |
| 41 | + } else if backend == .apple { |
| 42 | + return try await createAppleVirtualMachine(from: configuration).objectSpecifier |
| 43 | + } else { |
| 44 | + throw ScriptingError.backendNotFound |
| 45 | + } |
| 46 | + } |
| 47 | + } else { |
| 48 | + command.performDefaultImplementation() |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private func createQemuVirtualMachine(from record: [AnyHashable : Any]) async throws -> UTMScriptingVirtualMachineImpl { |
| 53 | + guard let data = data else { |
| 54 | + throw ScriptingError.notReady |
| 55 | + } |
| 56 | + guard record["name"] as? String != nil else { |
| 57 | + throw ScriptingError.nameNotSpecified |
| 58 | + } |
| 59 | + guard let architecture = record["architecture"] as? String, let architecture = QEMUArchitecture(rawValue: architecture) else { |
| 60 | + throw ScriptingError.architectureNotSpecified |
| 61 | + } |
| 62 | + let machine = record["machine"] as? String |
| 63 | + let target = architecture.targetType.init(rawValue: machine ?? "") ?? architecture.targetType.default |
| 64 | + let config = UTMQemuConfiguration() |
| 65 | + config.system.architecture = architecture |
| 66 | + config.system.target = target |
| 67 | + config.reset(forArchitecture: architecture, target: target) |
| 68 | + config.qemu.hasHypervisor = true |
| 69 | + config.qemu.hasUefiBoot = true |
| 70 | + // add default drives |
| 71 | + config.drives.append(UTMQemuConfigurationDrive(forArchitecture: architecture, target: target, isExternal: true)) |
| 72 | + var fixed = UTMQemuConfigurationDrive(forArchitecture: architecture, target: target) |
| 73 | + fixed.sizeMib = 64 * bytesInGib / bytesInMib |
| 74 | + config.drives.append(fixed) |
| 75 | + // add a default serial device |
| 76 | + var serial = UTMQemuConfigurationSerial() |
| 77 | + serial.mode = .ptty |
| 78 | + config.serials = [serial] |
| 79 | + // remove GUI devices |
| 80 | + config.displays = [] |
| 81 | + config.sound = [] |
| 82 | + // parse the remaining config |
| 83 | + let wrapper = UTMScriptingConfigImpl(config) |
| 84 | + try wrapper.updateConfiguration(from: record) |
| 85 | + // create the vm |
| 86 | + let vm = try await data.create(config: config) |
| 87 | + return UTMScriptingVirtualMachineImpl(for: vm, data: data) |
| 88 | + } |
| 89 | + |
| 90 | + private func createAppleVirtualMachine(from record: [AnyHashable : Any]) async throws -> UTMScriptingVirtualMachineImpl { |
| 91 | + guard let data = data else { |
| 92 | + throw ScriptingError.notReady |
| 93 | + } |
| 94 | + guard #available(macOS 13, *) else { |
| 95 | + throw ScriptingError.backendNotSupported |
| 96 | + } |
| 97 | + guard record["name"] as? String != nil else { |
| 98 | + throw ScriptingError.nameNotSpecified |
| 99 | + } |
| 100 | + let config = UTMAppleConfiguration() |
| 101 | + config.system.boot = try UTMAppleConfigurationBoot(for: .linux) |
| 102 | + config.virtualization.hasBalloon = true |
| 103 | + config.virtualization.hasEntropy = true |
| 104 | + config.networks = [UTMAppleConfigurationNetwork()] |
| 105 | + // remove any display devices |
| 106 | + config.displays = [] |
| 107 | + // add a default serial device |
| 108 | + var serial = UTMAppleConfigurationSerial() |
| 109 | + serial.mode = .ptty |
| 110 | + config.serials = [serial] |
| 111 | + // add default drives |
| 112 | + config.drives.append(UTMAppleConfigurationDrive(existingURL: nil, isExternal: true)) |
| 113 | + config.drives.append(UTMAppleConfigurationDrive(newSize: 64 * bytesInGib / bytesInMib)) |
| 114 | + // parse the remaining config |
| 115 | + let wrapper = UTMScriptingConfigImpl(config) |
| 116 | + try wrapper.updateConfiguration(from: record) |
| 117 | + // create the vm |
| 118 | + let vm = try await data.create(config: config) |
| 119 | + return UTMScriptingVirtualMachineImpl(for: vm, data: data) |
| 120 | + } |
| 121 | + |
| 122 | + enum ScriptingError: Error, LocalizedError { |
| 123 | + case notReady |
| 124 | + case backendNotFound |
| 125 | + case backendNotSupported |
| 126 | + case configurationNotFound |
| 127 | + case nameNotSpecified |
| 128 | + case architectureNotSpecified |
| 129 | + |
| 130 | + var errorDescription: String? { |
| 131 | + switch self { |
| 132 | + case .notReady: return NSLocalizedString("UTM is not ready to accept commands.", comment: "UTMScriptingAppDelegate") |
| 133 | + case .backendNotFound: return NSLocalizedString("A valid backend must be specified.", comment: "UTMScriptingAppDelegate") |
| 134 | + case .backendNotSupported: return NSLocalizedString("This backend is not supported on your machine.", comment: "UTMScriptingAppDelegate") |
| 135 | + case .configurationNotFound: return NSLocalizedString("A valid configuration must be specified.", comment: "UTMScriptingAppDelegate") |
| 136 | + case .nameNotSpecified: return NSLocalizedString("No name specified in the configuration.", comment: "UTMScriptingAppDelegate") |
| 137 | + case .architectureNotSpecified: return NSLocalizedString("No architecture specified in the configuration.", comment: "UTMScriptingAppDelegate") |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments