Fix clone while running

This commit is contained in:
f-trycua
2025-04-24 16:49:00 -07:00
parent 4b2cc9218f
commit 182e8c69ad
2 changed files with 27 additions and 3 deletions

View File

@@ -158,7 +158,8 @@ final class Home {
let sourceDir = try getVMDirectory(sourceName, storage: sourceLocation)
let destDir = try getVMDirectory(destName, storage: destLocation)
if destDir.initialized() {
// Check if destination directory exists at all
if destDir.exists() {
throw HomeError.directoryAlreadyExists(path: destDir.dir.path)
}

View File

@@ -82,6 +82,28 @@ final class LumeController {
// Validate source VM exists
_ = try self.validateVMExists(normalizedName, storage: sourceLocation)
// Get the source VM and check if it's running
let sourceVM = try get(name: normalizedName, storage: sourceLocation)
if sourceVM.details.status == "running" {
Logger.error("Cannot clone a running VM", metadata: ["source": normalizedName])
throw VMError.alreadyRunning(normalizedName)
}
// Check if destination already exists
do {
let destDir = try home.getVMDirectory(normalizedNewName, storage: destLocation)
if destDir.exists() {
Logger.error(
"Destination VM already exists",
metadata: ["destination": normalizedNewName])
throw HomeError.directoryAlreadyExists(path: destDir.dir.path)
}
} catch VMLocationError.locationNotFound {
// Location not found is okay, we'll create it
} catch VMError.notFound {
// VM not found is okay, we'll create it
}
// Copy the VM directory
try home.copyVMDirectory(
from: normalizedName,
@@ -93,9 +115,10 @@ final class LumeController {
// Update MAC address in the cloned VM to ensure uniqueness
let clonedVM = try get(name: normalizedNewName, storage: destLocation)
try clonedVM.setMacAddress(VZMACAddress.randomLocallyAdministered().string)
// Update MAC Identifier in the cloned VM to ensure uniqueness
try clonedVM.setMachineIdentifier(DarwinVirtualizationService.generateMachineIdentifier())
try clonedVM.setMachineIdentifier(
DarwinVirtualizationService.generateMachineIdentifier())
Logger.info(
"VM cloned successfully",