Pārlūkot izejas kodu

config: always use first available interface for bridging when unspecified

Fixes #5410
osy 2 gadi atpakaļ
vecāks
revīzija
d9d3291147

+ 9 - 5
Configuration/UTMAppleConfigurationNetwork.swift

@@ -94,12 +94,16 @@ struct UTMAppleConfigurationNetwork: Codable, Identifiable {
             config.attachment = attachment
         case .bridged:
             var found: VZBridgedNetworkInterface?
-            for interface in VZBridgedNetworkInterface.networkInterfaces.reversed() {
-                // this defaults to first interface if not found
-                found = interface
-                if interface.identifier == bridgeInterface {
-                    break
+            if let bridgeInterface = bridgeInterface {
+                for interface in VZBridgedNetworkInterface.networkInterfaces {
+                    if interface.identifier == bridgeInterface {
+                        found = interface
+                        break
+                    }
                 }
+            } else {
+                // default to first interface if unspecified
+                found = VZBridgedNetworkInterface.networkInterfaces.first
             }
             if let found = found {
                 let attachment = VZBridgedNetworkDeviceAttachment(interface: found)

+ 10 - 1
Configuration/UTMQemuConfiguration+Arguments.swift

@@ -15,6 +15,9 @@
 //
 
 import Foundation
+#if os(macOS)
+import Virtualization // for getting network interfaces
+#endif
 
 /// Build QEMU arguments from config
 @MainActor extension UTMQemuConfiguration {
@@ -706,6 +709,12 @@ import Foundation
         return (network.vlanDhcpStartAddress ?? firstAddrStr, network.vlanDhcpEndAddress ?? lastAddrStr, netmaskStr)
     }
     
+    #if os(macOS)
+    private var defaultBridgedInterface: String {
+        VZBridgedNetworkInterface.networkInterfaces.first?.identifier ?? "en0"
+    }
+    #endif
+    
     @QEMUArgumentBuilder private var networkArguments: [QEMUArgument] {
         for i in networks.indices {
             if isSparc {
@@ -733,7 +742,7 @@ import Foundation
                 useVMnet = true
                 "vmnet-bridged"
                 "id=net\(i)"
-                "ifname=\(networks[i].bridgeInterface ?? "en0")"
+                "ifname=\(networks[i].bridgeInterface ?? defaultBridgedInterface)"
             } else if networks[i].mode == .host {
                 useVMnet = true
                 "vmnet-host"

+ 11 - 2
Platform/Shared/VMConfigNetworkView.swift

@@ -15,6 +15,9 @@
 //
 
 import SwiftUI
+#if os(macOS)
+import Virtualization
+#endif
 
 struct VMConfigNetworkView: View {
     @Binding var config: UTMQemuConfigurationNetwork
@@ -28,8 +31,14 @@ struct VMConfigNetworkView: View {
                     #if os(macOS)
                     VMConfigConstantPicker("Network Mode", selection: $config.mode)
                     if config.mode == .bridged {
-                        DefaultTextField("Bridged Interface", text: $config.bridgeInterface.bound, prompt: "en0")
-                            .keyboardType(.asciiCapable)
+                        Picker("Bridged Interface", selection: $config.bridgeInterface) {
+                            Text("Automatic")
+                                .tag(nil as String?)
+                            ForEach(VZBridgedNetworkInterface.networkInterfaces, id: \.identifier) { interface in
+                                Text(interface.identifier)
+                                    .tag(interface.identifier as String?)
+                            }
+                        }
                     }
                     #endif
                     VMConfigConstantPicker("Emulated Network Card", selection: $config.hardware, type: system.architecture.networkDeviceType)

+ 3 - 1
Platform/macOS/VMConfigAppleNetworkingView.swift

@@ -40,7 +40,9 @@ struct VMConfigAppleNetworkingView: View {
             }
             if config.mode == .bridged {
                 Section(header: Text("Bridged Settings")) {
-                    Picker("Interface", selection: $config.bridgeInterface.bound) {
+                    Picker("Interface", selection: $config.bridgeInterface) {
+                        Text("Automatic")
+                            .tag(nil as String?)
                         ForEach(VZBridgedNetworkInterface.networkInterfaces, id: \.identifier) { interface in
                             Text(interface.identifier)
                                 .tag(interface.identifier as String?)