UTMAppleConfigurationVirtualization.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // Copyright © 2022 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. import Foundation
  17. import Virtualization
  18. /// Device settings.
  19. @available(iOS, unavailable, message: "Apple Virtualization not available on iOS")
  20. @available(macOS 11, *)
  21. struct UTMAppleConfigurationVirtualization: Codable {
  22. enum PointerDevice: String, CaseIterable, QEMUConstant {
  23. case disabled = "Disabled"
  24. case mouse = "Mouse"
  25. case trackpad = "Trackpad"
  26. var prettyValue: String {
  27. switch self {
  28. case .disabled: return NSLocalizedString("Disabled", comment: "UTMAppleConfigurationDevices")
  29. case .mouse: return NSLocalizedString("Mouse", comment: "UTMAppleConfigurationDevices")
  30. case .trackpad: return NSLocalizedString("Trackpad", comment: "UTMAppleConfigurationDevices")
  31. }
  32. }
  33. }
  34. var hasAudio: Bool = false
  35. var hasBalloon: Bool = true
  36. var hasEntropy: Bool = true
  37. var hasKeyboard: Bool = false
  38. var hasPointer: Bool = false
  39. var hasTrackpad: Bool = false
  40. var hasRosetta: Bool?
  41. var hasClipboardSharing: Bool = false
  42. enum CodingKeys: String, CodingKey {
  43. case hasAudio = "Audio"
  44. case hasBalloon = "Balloon"
  45. case hasEntropy = "Entropy"
  46. case hasKeyboard = "Keyboard"
  47. case hasPointer = "Pointer"
  48. case hasTrackpad = "Trackpad"
  49. case rosetta = "Rosetta"
  50. case hasClipboardSharing = "ClipboardSharing"
  51. }
  52. init() {
  53. }
  54. init(from decoder: Decoder) throws {
  55. let values = try decoder.container(keyedBy: CodingKeys.self)
  56. hasAudio = try values.decode(Bool.self, forKey: .hasAudio)
  57. hasBalloon = try values.decode(Bool.self, forKey: .hasBalloon)
  58. hasEntropy = try values.decode(Bool.self, forKey: .hasEntropy)
  59. hasKeyboard = try values.decode(Bool.self, forKey: .hasKeyboard)
  60. if let legacyPointer = try? values.decode(PointerDevice.self, forKey: .hasPointer) {
  61. hasPointer = legacyPointer != .disabled
  62. hasTrackpad = legacyPointer == .trackpad
  63. } else {
  64. hasPointer = try values.decode(Bool.self, forKey: .hasPointer)
  65. hasTrackpad = try values.decodeIfPresent(Bool.self, forKey: .hasTrackpad) ?? false
  66. }
  67. if #available(macOS 13, *) {
  68. hasRosetta = try values.decodeIfPresent(Bool.self, forKey: .rosetta)
  69. hasClipboardSharing = try values.decodeIfPresent(Bool.self, forKey: .hasClipboardSharing) ?? false
  70. }
  71. }
  72. func encode(to encoder: Encoder) throws {
  73. var container = encoder.container(keyedBy: CodingKeys.self)
  74. try container.encode(hasAudio, forKey: .hasAudio)
  75. try container.encode(hasBalloon, forKey: .hasBalloon)
  76. try container.encode(hasEntropy, forKey: .hasEntropy)
  77. try container.encode(hasKeyboard, forKey: .hasKeyboard)
  78. try container.encode(hasPointer, forKey: .hasPointer)
  79. try container.encode(hasTrackpad, forKey: .hasTrackpad)
  80. try container.encodeIfPresent(hasRosetta, forKey: .rosetta)
  81. try container.encode(hasClipboardSharing, forKey: .hasClipboardSharing)
  82. }
  83. }
  84. // MARK: - Conversion of old config format
  85. @available(iOS, unavailable, message: "Apple Virtualization not available on iOS")
  86. @available(macOS 11, *)
  87. extension UTMAppleConfigurationVirtualization {
  88. init(migrating oldConfig: UTMLegacyAppleConfiguration) {
  89. self.init()
  90. hasBalloon = oldConfig.isBalloonEnabled
  91. hasEntropy = oldConfig.isEntropyEnabled
  92. if #available(macOS 12, *) {
  93. hasAudio = oldConfig.isAudioEnabled
  94. hasKeyboard = oldConfig.isKeyboardEnabled
  95. hasPointer = oldConfig.isPointingEnabled
  96. }
  97. }
  98. }
  99. // MARK: - Creating Apple config
  100. @available(iOS, unavailable, message: "Apple Virtualization not available on iOS")
  101. @available(macOS 11, *)
  102. extension UTMAppleConfigurationVirtualization {
  103. func fillVZConfiguration(_ vzconfig: VZVirtualMachineConfiguration, isMacOSGuest: Bool = false) throws {
  104. if hasBalloon && !isMacOSGuest {
  105. vzconfig.memoryBalloonDevices = [VZVirtioTraditionalMemoryBalloonDeviceConfiguration()]
  106. }
  107. if hasEntropy {
  108. vzconfig.entropyDevices = [VZVirtioEntropyDeviceConfiguration()]
  109. }
  110. if #available(macOS 12, *) {
  111. if hasAudio {
  112. let audioInputConfiguration = VZVirtioSoundDeviceConfiguration()
  113. let audioInput = VZVirtioSoundDeviceInputStreamConfiguration()
  114. audioInput.source = VZHostAudioInputStreamSource()
  115. audioInputConfiguration.streams = [audioInput]
  116. let audioOutputConfiguration = VZVirtioSoundDeviceConfiguration()
  117. let audioOutput = VZVirtioSoundDeviceOutputStreamConfiguration()
  118. audioOutput.sink = VZHostAudioOutputStreamSink()
  119. audioOutputConfiguration.streams = [audioOutput]
  120. vzconfig.audioDevices = [audioInputConfiguration, audioOutputConfiguration]
  121. }
  122. if hasKeyboard {
  123. vzconfig.keyboards = [VZUSBKeyboardConfiguration()]
  124. #if arch(arm64)
  125. if #available(macOS 14, *), isMacOSGuest {
  126. vzconfig.keyboards = [VZMacKeyboardConfiguration()]
  127. }
  128. #endif
  129. }
  130. if hasPointer {
  131. vzconfig.pointingDevices = [VZUSBScreenCoordinatePointingDeviceConfiguration()]
  132. #if arch(arm64)
  133. if #available(macOS 13, *), isMacOSGuest && hasTrackpad {
  134. // replace with trackpad device
  135. vzconfig.pointingDevices = [VZMacTrackpadConfiguration()]
  136. }
  137. #endif
  138. }
  139. } else {
  140. if hasAudio || hasKeyboard || hasPointer {
  141. throw UTMAppleConfigurationError.featureNotSupported
  142. }
  143. }
  144. if #available(macOS 13, *) {
  145. #if arch(arm64)
  146. if hasRosetta == true {
  147. let rosettaDirectoryShare = try VZLinuxRosettaDirectoryShare()
  148. if #available(macOS 14, *) {
  149. // enable cache if possible
  150. try? rosettaDirectoryShare.setCachingOptions(.defaultUnixSocket)
  151. }
  152. let fileSystemDevice = VZVirtioFileSystemDeviceConfiguration(tag: "rosetta")
  153. fileSystemDevice.share = rosettaDirectoryShare
  154. vzconfig.directorySharingDevices.append(fileSystemDevice)
  155. }
  156. #else
  157. if hasRosetta == true {
  158. throw UTMAppleConfigurationError.rosettaNotSupported
  159. }
  160. #endif
  161. if hasClipboardSharing && !isMacOSGuest {
  162. let spiceClipboardAgent = VZSpiceAgentPortAttachment()
  163. spiceClipboardAgent.sharesClipboard = true
  164. let consolePort = VZVirtioConsolePortConfiguration()
  165. consolePort.name = VZSpiceAgentPortAttachment.spiceAgentPortName
  166. consolePort.attachment = spiceClipboardAgent
  167. consolePort.isConsole = false
  168. let consoleDevice = VZVirtioConsoleDeviceConfiguration()
  169. consoleDevice.ports[0] = consolePort
  170. vzconfig.consoleDevices.append(consoleDevice)
  171. }
  172. } else {
  173. if hasRosetta == true || hasClipboardSharing {
  174. throw UTMAppleConfigurationError.featureNotSupported
  175. }
  176. }
  177. }
  178. }
  179. // MARK: prepare save
  180. extension UTMAppleConfigurationVirtualization {
  181. func prepareSave(for packageURL: URL) async throws {
  182. if #available(macOS 13, *), hasRosetta == true {
  183. try await installRosetta()
  184. }
  185. }
  186. @available(macOS 13, *)
  187. private func installRosetta() async throws {
  188. #if arch(arm64)
  189. let rosettaAvailability = VZLinuxRosettaDirectoryShare.availability
  190. if rosettaAvailability == .notSupported {
  191. throw UTMAppleConfigurationError.rosettaNotSupported
  192. } else if rosettaAvailability == .notInstalled {
  193. try await VZLinuxRosettaDirectoryShare.installRosetta()
  194. }
  195. #else
  196. throw UTMAppleConfigurationError.rosettaNotSupported
  197. #endif
  198. }
  199. }