mirror of https://github.com/cirruslabs/tart.git
Smart generation of new MAC addresses (#45)
* Introduce --new-mac-address flag for Clone command Related to https://github.com/cirruslabs/tart/issues/20#issuecomment-1116732505 * Smart generation of new MAC addresses
This commit is contained in:
parent
d5e3a7718e
commit
f20a98bf01
|
|
@ -13,13 +13,21 @@ struct Clone: AsyncParsableCommand {
|
|||
|
||||
func run() async throws {
|
||||
do {
|
||||
// Pull the VM in case it's OCI-based and doesn't exist locally yet
|
||||
if let remoteName = try? RemoteName(sourceName), !VMStorageOCI().exists(remoteName) {
|
||||
let registry = try Registry(host: remoteName.host, namespace: remoteName.namespace)
|
||||
try await VMStorageOCI().pull(remoteName, registry: registry)
|
||||
}
|
||||
if let remoteName = try? RemoteName(sourceName) {
|
||||
if !VMStorageOCI().exists(remoteName) {
|
||||
// Pull the VM in case it's OCI-based and doesn't exist locally yet
|
||||
let registry = try Registry(host: remoteName.host, namespace: remoteName.namespace)
|
||||
try await VMStorageOCI().pull(remoteName, registry: registry)
|
||||
}
|
||||
let removeVM = try VMStorageHelper.open(sourceName)
|
||||
|
||||
try VMStorageHelper.open(sourceName).clone(to: VMStorageLocal().create(newName))
|
||||
let removeConfig = try VMConfig.init(fromURL: removeVM.configURL)
|
||||
let needToGenerateNewMAC = try localVMExistsWith(macAddress: removeConfig.macAddress.string)
|
||||
|
||||
try removeVM.clone(to: VMStorageLocal().create(newName), generateMAC: needToGenerateNewMAC)
|
||||
} else {
|
||||
try VMStorageHelper.open(sourceName).clone(to: VMStorageLocal().create(newName), generateMAC: true)
|
||||
}
|
||||
|
||||
Foundation.exit(0)
|
||||
} catch {
|
||||
|
|
@ -28,4 +36,15 @@ struct Clone: AsyncParsableCommand {
|
|||
Foundation.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
private func localVMExistsWith(macAddress: String) throws -> Bool {
|
||||
var needToGenerateNewMAC = false
|
||||
for (_, localDir) in try VMStorageLocal().list() {
|
||||
let localConfig = try VMConfig.init(fromURL: localDir.configURL)
|
||||
if localConfig.macAddress.string == macAddress {
|
||||
needToGenerateNewMAC = true
|
||||
}
|
||||
}
|
||||
return needToGenerateNewMAC
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,14 +48,16 @@ struct VMDirectory {
|
|||
}
|
||||
}
|
||||
|
||||
func clone(to: VMDirectory) throws {
|
||||
func clone(to: VMDirectory, generateMAC: Bool) throws {
|
||||
try FileManager.default.copyItem(at: configURL, to: to.configURL)
|
||||
try FileManager.default.copyItem(at: nvramURL, to: to.nvramURL)
|
||||
try FileManager.default.copyItem(at: diskURL, to: to.diskURL)
|
||||
|
||||
// Re-generate MAC address
|
||||
var newVMConfig = try VMConfig(fromURL: to.configURL)
|
||||
newVMConfig.macAddress = VZMACAddress.randomLocallyAdministered()
|
||||
if generateMAC {
|
||||
newVMConfig.macAddress = VZMACAddress.randomLocallyAdministered()
|
||||
}
|
||||
try newVMConfig.save(toURL: to.configURL)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue