2
0

VMWizardOSLinuxView.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. Section {
  44. Toggle("Boot from kernel image", isOn: $wizardState.useLinuxKernel)
  45. .help("If set, boot directly from a raw kernel image and initrd. Otherwise, boot from a supported ISO.")
  46. .disabled(wizardState.useAppleVirtualization && !hasVenturaFeatures)
  47. if !wizardState.useLinuxKernel {
  48. if wizardState.useAppleVirtualization {
  49. Link(destination: URL(string: "https://docs.getutm.app/guides/debian/")!) {
  50. Label("Debian Install Guide", systemImage: "link")
  51. }.buttonStyle(.borderless)
  52. } else {
  53. Link(destination: URL(string: "https://docs.getutm.app/guides/ubuntu/")!) {
  54. Label("Ubuntu Install Guide", systemImage: "link")
  55. }.buttonStyle(.borderless)
  56. }
  57. }
  58. } header: {
  59. Text("Boot Image Type")
  60. }
  61. #if arch(arm64)
  62. if #available(macOS 13, *), wizardState.useAppleVirtualization {
  63. Section {
  64. Toggle("Enable Rosetta (x86_64 Emulation)", isOn: $wizardState.linuxHasRosetta)
  65. Link(destination: URL(string: "https://docs.getutm.app/advanced/rosetta/")!) {
  66. Label("Installation Instructions", systemImage: "link")
  67. }.buttonStyle(.borderless)
  68. } header: {
  69. Text("Additional Options")
  70. }
  71. }
  72. #endif
  73. if wizardState.useLinuxKernel {
  74. Section {
  75. FileBrowseField(url: $wizardState.linuxKernelURL, isFileImporterPresented: $isFileImporterPresented, hasClearButton: false) {
  76. selectImage = .kernel
  77. }
  78. } header: {
  79. if wizardState.useAppleVirtualization {
  80. Text("Uncompressed Linux kernel (required)")
  81. } else {
  82. Text("Linux kernel (required)")
  83. }
  84. }
  85. Section {
  86. FileBrowseField(url: $wizardState.linuxInitialRamdiskURL, isFileImporterPresented: $isFileImporterPresented) {
  87. selectImage = .initialRamdisk
  88. }
  89. } header: {
  90. if wizardState.useAppleVirtualization {
  91. Text("Uncompressed Linux initial ramdisk (optional)")
  92. } else {
  93. Text("Linux initial ramdisk (optional)")
  94. }
  95. }
  96. Section {
  97. FileBrowseField(url: $wizardState.linuxRootImageURL, isFileImporterPresented: $isFileImporterPresented) {
  98. selectImage = .rootImage
  99. }
  100. } header: {
  101. Text("Linux Root FS Image (optional)")
  102. }
  103. Section {
  104. FileBrowseField(url: $wizardState.bootImageURL, isFileImporterPresented: $isFileImporterPresented) {
  105. selectImage = .bootImage
  106. }
  107. } header: {
  108. Text("Boot ISO Image (optional)")
  109. }
  110. Section {
  111. TextField("Boot Arguments", text: $wizardState.linuxBootArguments)
  112. } header: {
  113. Text("Boot Arguments")
  114. }
  115. } else {
  116. Section {
  117. FileBrowseField(url: $wizardState.bootImageURL, isFileImporterPresented: $isFileImporterPresented) {
  118. selectImage = .bootImage
  119. }
  120. } header: {
  121. Text("Boot ISO Image")
  122. }
  123. }
  124. if wizardState.isBusy {
  125. Spinner(size: .large)
  126. }
  127. }
  128. .fileImporter(isPresented: $isFileImporterPresented, allowedContentTypes: [.data], onCompletion: processImage)
  129. }
  130. private func processImage(_ result: Result<URL, Error>) {
  131. wizardState.busyWorkAsync {
  132. let url = try result.get()
  133. await MainActor.run {
  134. switch selectImage {
  135. case .kernel:
  136. wizardState.linuxKernelURL = url
  137. case .initialRamdisk:
  138. wizardState.linuxInitialRamdiskURL = url
  139. case .rootImage:
  140. wizardState.linuxRootImageURL = url
  141. case .bootImage:
  142. wizardState.bootImageURL = url
  143. wizardState.isSkipBootImage = false
  144. }
  145. }
  146. }
  147. }
  148. }
  149. struct VMWizardOSLinuxView_Previews: PreviewProvider {
  150. @StateObject static var wizardState = VMWizardState()
  151. static var previews: some View {
  152. VMWizardOSLinuxView(wizardState: wizardState)
  153. }
  154. }