2
0

VMWizardOSLinuxView.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // Copyright © 2021 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 SwiftUI
  17. struct VMWizardOSLinuxView: View {
  18. private enum SelectImage {
  19. case kernel
  20. case initialRamdisk
  21. case rootImage
  22. case bootImage
  23. }
  24. @ObservedObject var wizardState: VMWizardState
  25. @State private var isFileImporterPresented: Bool = false
  26. @State private var selectImage: SelectImage = .kernel
  27. private var hasVenturaFeatures: Bool {
  28. if #available(macOS 13, *) {
  29. return true
  30. } else {
  31. return false
  32. }
  33. }
  34. var body: some View {
  35. VMWizardContent("Linux") {
  36. #if os(macOS)
  37. if wizardState.useVirtualization {
  38. DetailedSection("Virtualization Engine", description: "Apple Virtualization is experimental and only for advanced use cases. Leave unchecked to use QEMU, which is recommended.") {
  39. Toggle("Use Apple Virtualization", isOn: $wizardState.useAppleVirtualization)
  40. }
  41. }
  42. #endif
  43. Picker("Boot Image Type", selection: $wizardState.bootDevice) {
  44. Text("Boot from kernel image").tag(VMBootDevice.kernel)
  45. if !wizardState.useAppleVirtualization || hasVenturaFeatures {
  46. Text("Boot from ISO image").tag(VMBootDevice.cd)
  47. Text("Import existing drive").tag(VMBootDevice.drive)
  48. }
  49. }.pickerStyle(.inline)
  50. .onAppear {
  51. if ![.kernel, .cd, .drive].contains(wizardState.bootDevice) {
  52. wizardState.bootDevice = .cd
  53. }
  54. }
  55. if wizardState.bootDevice != .kernel {
  56. if wizardState.useAppleVirtualization {
  57. Link(destination: URL(string: "https://docs.getutm.app/guides/debian/")!) {
  58. Label("Debian Install Guide", systemImage: "link")
  59. }.buttonStyle(.borderless)
  60. } else {
  61. Link(destination: URL(string: "https://docs.getutm.app/guides/ubuntu/")!) {
  62. Label("Ubuntu Install Guide", systemImage: "link")
  63. }.buttonStyle(.borderless)
  64. }
  65. }
  66. #if arch(arm64)
  67. if #available(macOS 13, *), wizardState.useAppleVirtualization {
  68. Section {
  69. Toggle("Enable Rosetta (x86_64 Emulation)", isOn: $wizardState.linuxHasRosetta)
  70. Link(destination: URL(string: "https://docs.getutm.app/advanced/rosetta/")!) {
  71. Label("Installation Instructions", systemImage: "link")
  72. }.buttonStyle(.borderless)
  73. } header: {
  74. Text("Additional Options")
  75. }
  76. }
  77. #endif
  78. if wizardState.bootDevice == .kernel {
  79. Section {
  80. FileBrowseField(url: $wizardState.linuxKernelURL, isFileImporterPresented: $isFileImporterPresented, hasClearButton: false) {
  81. selectImage = .kernel
  82. }
  83. } header: {
  84. if wizardState.useAppleVirtualization {
  85. Text("Uncompressed Linux kernel (required)")
  86. } else {
  87. Text("Linux kernel (required)")
  88. }
  89. }
  90. Section {
  91. FileBrowseField(url: $wizardState.linuxInitialRamdiskURL, isFileImporterPresented: $isFileImporterPresented) {
  92. selectImage = .initialRamdisk
  93. }
  94. } header: {
  95. if wizardState.useAppleVirtualization {
  96. Text("Uncompressed Linux initial ramdisk (optional)")
  97. } else {
  98. Text("Linux initial ramdisk (optional)")
  99. }
  100. }
  101. Section {
  102. FileBrowseField(url: $wizardState.linuxRootImageURL, isFileImporterPresented: $isFileImporterPresented) {
  103. selectImage = .rootImage
  104. }
  105. } header: {
  106. Text("Linux Root FS Image (optional)")
  107. }
  108. Section {
  109. FileBrowseField(url: $wizardState.bootImageURL, isFileImporterPresented: $isFileImporterPresented) {
  110. selectImage = .bootImage
  111. }
  112. } header: {
  113. Text("Boot ISO Image (optional)")
  114. }
  115. Section {
  116. TextField("Boot Arguments", text: $wizardState.linuxBootArguments)
  117. } header: {
  118. Text("Boot Arguments")
  119. }
  120. } else {
  121. Section {
  122. FileBrowseField(url: $wizardState.bootImageURL, isFileImporterPresented: $isFileImporterPresented) {
  123. selectImage = .bootImage
  124. }
  125. } header: {
  126. if wizardState.bootDevice == .drive {
  127. Text("Import Disk Image")
  128. } else {
  129. Text("Boot ISO Image")
  130. }
  131. }
  132. }
  133. if wizardState.isBusy {
  134. Spinner(size: .large)
  135. }
  136. }
  137. .fileImporter(isPresented: $isFileImporterPresented, allowedContentTypes: [.data], onCompletion: processImage)
  138. }
  139. private func processImage(_ result: Result<URL, Error>) {
  140. wizardState.busyWorkAsync {
  141. let url = try result.get()
  142. await MainActor.run {
  143. switch selectImage {
  144. case .kernel:
  145. wizardState.linuxKernelURL = url
  146. case .initialRamdisk:
  147. wizardState.linuxInitialRamdiskURL = url
  148. case .rootImage:
  149. wizardState.linuxRootImageURL = url
  150. case .bootImage:
  151. wizardState.bootImageURL = url
  152. }
  153. }
  154. }
  155. }
  156. }
  157. struct VMWizardOSLinuxView_Previews: PreviewProvider {
  158. @StateObject static var wizardState = VMWizardState()
  159. static var previews: some View {
  160. VMWizardOSLinuxView(wizardState: wizardState)
  161. }
  162. }