Explorar el Código

settings: option to randomize MAC on clone

Resolves #7035
osy hace 3 días
padre
commit
acd7534ed5
Se han modificado 2 ficheros con 13 adiciones y 0 borrados
  1. 8 0
      Platform/UTMData.swift
  2. 5 0
      Platform/macOS/SettingsView.swift

+ 8 - 0
Platform/UTMData.swift

@@ -516,12 +516,20 @@ enum AlertItem: Identifiable {
     @discardableResult func clone(vm: VMData) async throws -> VMData {
         let newName: String = newDefaultVMName(base: vm.detailsTitleLabel)
         let newPath = ConcreteVirtualMachine.virtualMachinePath(for: newName, in: documentsURL)
+        let isRegenerateMACOnClone = UserDefaults.standard.bool(forKey: "IsRegenerateMACOnClone")
 
         try await copyItemWithCopyfile(at: vm.pathUrl, to: newPath)
         guard let newVM = try? VMData(url: newPath) else {
             throw UTMDataError.cloneFailed
         }
         newVM.wrapped!.changeUuid(to: UUID(), name: newName, copyingEntry: nil)
+        if isRegenerateMACOnClone {
+            if let config = newVM.wrapped!.config as? UTMQemuConfiguration {
+                for i in config.networks.indices {
+                    config.networks[i].macAddress = UTMQemuConfigurationNetwork.randomMacAddress()
+                }
+            }
+        }
         try await newVM.save()
         var index = virtualMachines.firstIndex(of: vm)
         if index != nil {

+ 5 - 0
Platform/macOS/SettingsView.swift

@@ -335,6 +335,7 @@ struct InputSettingsView: View {
 
 @available(macOS 12, *)
 struct NetworkSettingsView: View {
+    @AppStorage("IsRegenerateMACOnClone") var isRegenerateMACOnClone = false
     @AppStorage("HostNetworks") var hostNetworksData: Data = Data()
     @State private var hostNetworks: [UTMConfigurationHostNetwork] = []
     @State private var selectedID: UUID?
@@ -350,6 +351,10 @@ struct NetworkSettingsView: View {
     
     var body: some View {
         Form {
+            Section(header: Text("Cloning")) {
+                Toggle("Regenerate MAC addresses on clone", isOn: $isRegenerateMACOnClone)
+                    .help("When cloning a VM, regenerate MAC addresses on every network interface to prevent conflicts.")
+            }
             Section(header: Text("Host Networks")) {
                 Table($hostNetworks, selection: $selectedID) {
                     TableColumn("Name") { $network in