-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathUTMQemuConfiguration.swift
More file actions
373 lines (322 loc) · 11.9 KB
/
Copy pathUTMQemuConfiguration.swift
File metadata and controls
373 lines (322 loc) · 11.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
//
// 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
/// Settings for a QEMU configuration
final class UTMQemuConfiguration: UTMConfiguration {
/// Basic information and icon
@Published var _information: UTMConfigurationInfo = .init()
/// System settings
@Published var _system: UTMQemuConfigurationSystem = .init()
/// Additional QEMU tweaks
@Published var _qemu: UTMQemuConfigurationQEMU = .init()
/// Input settings
@Published var _input: UTMQemuConfigurationInput = .init()
/// Sharing settings
@Published var _sharing: UTMQemuConfigurationSharing = .init()
/// All displays
@Published var _displays: [UTMQemuConfigurationDisplay] = []
/// All drives
@Published var _drives: [UTMQemuConfigurationDrive] = []
/// All network adapters
@Published var _networks: [UTMQemuConfigurationNetwork] = []
/// All serial ouputs
@Published var _serials: [UTMQemuConfigurationSerial] = []
/// All audio devices
@Published var _sound: [UTMQemuConfigurationSound] = []
/// True if configuration is migrated from a legacy config. Not saved.
private(set) var isLegacy: Bool = false
var backend: UTMBackend {
.qemu
}
enum CodingKeys: String, CodingKey {
case information = "Information"
case system = "System"
case qemu = "QEMU"
case input = "Input"
case sharing = "Sharing"
case displays = "Display"
case drives = "Drive"
case networks = "Network"
case serials = "Serial"
case sound = "Sound"
case backend = "Backend"
case configurationVersion = "ConfigurationVersion"
}
init() {
reset()
}
required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let backend = try values.decodeIfPresent(UTMBackend.self, forKey: .backend) ?? .qemu
guard backend == .qemu else {
throw UTMConfigurationError.invalidBackend
}
let version = try values.decodeIfPresent(Int.self, forKey: .configurationVersion) ?? 0
guard version >= Self.oldestVersion else {
throw UTMConfigurationError.versionTooLow
}
guard version <= Self.currentVersion else {
throw UTMConfigurationError.versionTooHigh
}
_information = try values.decode(UTMConfigurationInfo.self, forKey: .information)
_system = try values.decode(UTMQemuConfigurationSystem.self, forKey: .system)
_qemu = try values.decode(UTMQemuConfigurationQEMU.self, forKey: .qemu)
_input = try values.decode(UTMQemuConfigurationInput.self, forKey: .input)
_sharing = try values.decode(UTMQemuConfigurationSharing.self, forKey: .sharing)
_displays = try values.decode([UTMQemuConfigurationDisplay].self, forKey: .displays)
_drives = try values.decode([UTMQemuConfigurationDrive].self, forKey: .drives)
_networks = try values.decode([UTMQemuConfigurationNetwork].self, forKey: .networks)
_serials = try values.decode([UTMQemuConfigurationSerial].self, forKey: .serials)
_sound = try values.decode([UTMQemuConfigurationSound].self, forKey: .sound)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(_information, forKey: .information)
try container.encode(_system, forKey: .system)
try container.encode(_qemu, forKey: .qemu)
try container.encode(_input, forKey: .input)
try container.encode(_sharing, forKey: .sharing)
try container.encode(_displays, forKey: .displays)
try container.encode(_drives, forKey: .drives)
try container.encode(_networks, forKey: .networks)
try container.encode(_serials, forKey: .serials)
try container.encode(_sound, forKey: .sound)
try container.encode(UTMBackend.qemu, forKey: .backend)
try container.encode(Self.currentVersion, forKey: .configurationVersion)
}
}
enum UTMQemuConfigurationError: Error {
case migrationFailed
case uefiNotSupported
}
extension UTMQemuConfigurationError: LocalizedError {
var errorDescription: String? {
switch self {
case .migrationFailed:
return NSLocalizedString("Failed to migrate configuration from a previous UTM version.", comment: "UTMQemuConfigurationError")
case .uefiNotSupported:
return NSLocalizedString("UEFI is not supported with this architecture.", comment: "UTMQemuConfigurationError")
}
}
}
// MARK: - Public accessors
@MainActor extension UTMQemuConfiguration {
var information: UTMConfigurationInfo {
get {
_information
}
set {
_information = newValue
}
}
var system: UTMQemuConfigurationSystem {
get {
_system
}
set {
_system = newValue
}
}
var qemu: UTMQemuConfigurationQEMU {
get {
_qemu
}
set {
_qemu = newValue
}
}
var input: UTMQemuConfigurationInput {
get {
_input
}
set {
_input = newValue
}
}
var sharing: UTMQemuConfigurationSharing {
get {
_sharing
}
set {
_sharing = newValue
}
}
var displays: [UTMQemuConfigurationDisplay] {
get {
_displays
}
set {
_displays = newValue
}
}
var drives: [UTMQemuConfigurationDrive] {
get {
_drives
}
set {
_drives = newValue
}
}
var networks: [UTMQemuConfigurationNetwork] {
get {
_networks
}
set {
_networks = newValue
}
}
var serials: [UTMQemuConfigurationSerial] {
get {
_serials
}
set {
_serials = newValue
}
}
var sound: [UTMQemuConfigurationSound] {
get {
_sound
}
set {
_sound = newValue
}
}
}
// MARK: - Defaults
extension UTMQemuConfiguration {
private func reset(all: Bool = true) {
if all {
_information = .init()
_system = .init()
_drives = []
}
_qemu = .init()
_input = .init()
_sharing = .init()
_displays = []
_networks = []
_serials = []
_sound = []
}
@MainActor func reset(forArchitecture architecture: QEMUArchitecture, target: any QEMUTarget) {
reset(all: false)
qemu = .init(forArchitecture: architecture, target: target)
input = .init(forArchitecture: architecture, target: target)
sharing = .init(forArchitecture: architecture, target: target)
system.cpu = architecture.cpuType.default
if let fixedMemorySize = target.fixedMemorySize {
system.memorySize = fixedMemorySize
}
if let display = UTMQemuConfigurationDisplay(forArchitecture: architecture, target: target) {
displays = [display]
} else {
serials = [UTMQemuConfigurationSerial()]
}
if let network = UTMQemuConfigurationNetwork(forArchitecture: architecture, target: target) {
networks = [network]
}
if let _sound = UTMQemuConfigurationSound(forArchitecture: architecture, target: target) {
sound = [_sound]
}
}
}
// MARK: - Conversion of old config format
extension UTMQemuConfiguration {
convenience init(migrating oldConfig: UTMLegacyQemuConfiguration) {
self.init()
isLegacy = true
_information = .init(migrating: oldConfig)
_system = .init(migrating: oldConfig)
_qemu = .init(migrating: oldConfig)
_input = .init(migrating: oldConfig)
_sharing = .init(migrating: oldConfig)
if let display = UTMQemuConfigurationDisplay(migrating: oldConfig) {
_displays = [display]
}
_drives = (0..<oldConfig.countDrives).map({ i in UTMQemuConfigurationDrive(migrating: oldConfig, at: i) })
// remove efi_vars which is no longer stored as a drive
_drives.removeAll { drive in
drive.imageName == QEMUPackageFileName.efiVariables.rawValue
}
if let network = UTMQemuConfigurationNetwork(migrating: oldConfig) {
_networks = [network]
}
if let serial = UTMQemuConfigurationSerial(migrating: oldConfig) {
_serials = [serial]
}
if let __sound = UTMQemuConfigurationSound(migrating: oldConfig) {
_sound = [__sound]
}
}
}
// MARK: - Saving data
@MainActor extension UTMQemuConfiguration {
func prepareSave(for packageURL: URL) async throws {
guard isLegacy else {
return // nothing needed
}
// move Images to Data
let fileManager = FileManager.default
let imagesURL = packageURL.appendingPathComponent(QEMUPackageFileName.images.rawValue)
let dataURL = packageURL.appendingPathComponent(Self.dataDirectoryName)
if fileManager.fileExists(atPath: imagesURL.path) {
guard !fileManager.fileExists(atPath: dataURL.path) else {
throw UTMQemuConfigurationError.migrationFailed
}
try await Task.detached {
try FileManager.default.moveItem(at: imagesURL, to: dataURL)
}.value
}
// update any drives
for i in 0..<drives.count {
if !drives[i].isExternal, let oldImageURL = drives[i].imageURL {
drives[i].imageURL = dataURL.appendingPathComponent(oldImageURL.lastPathComponent)
}
}
// move icon
if information.isIconCustom, let oldIconURL = information.iconURL {
let newIconURL = dataURL.appendingPathComponent(oldIconURL.lastPathComponent)
try await Task.detached {
try FileManager.default.moveItem(at: oldIconURL, to: newIconURL)
}.value
information.iconURL = newIconURL
}
// move debug log
if let oldLogURL = qemu.debugLogURL, fileManager.fileExists(atPath: oldLogURL.path) {
let newLogURL = dataURL.appendingPathComponent(oldLogURL.lastPathComponent)
await Task.detached {
do {
try FileManager.default.moveItem(at: oldLogURL, to: newLogURL)
} catch {
// okay to fail
try? FileManager.default.removeItem(at: oldLogURL)
}
}.value
qemu.debugLogURL = newLogURL
}
// move efi variables
qemu.efiVarsURL = nil // will be set at saveData
}
func saveData(to dataURL: URL) async throws -> [URL] {
var existingDataURLs = [URL]()
existingDataURLs += try await _information.saveData(to: dataURL)
existingDataURLs += try await _qemu.saveData(to: dataURL, for: system)
for i in 0..<drives.count {
existingDataURLs += try await _drives[i].saveData(to: dataURL)
}
return existingDataURLs
}
}