Skip to content

Commit ea13e08

Browse files
committed
CLOUDSTACK-4519: reimplementdestroy-volume to take consideration of on-disk snapshot
1 parent a4762b0 commit ea13e08

4 files changed

Lines changed: 106 additions & 34 deletions

File tree

‎plugins/hypervisors/vmware/src/com/cloud/hypervisor/guru/VMwareGuru.java‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,6 @@ private static String resolveNameInGuid(String guid) {
416416
return tokens[0] + "@" + vCenterIp;
417417
}
418418

419-
@Override
420-
public List<Command> finalizeExpunge(VirtualMachine vm) {
421-
UnregisterVMCommand unregisterVMCommand = new UnregisterVMCommand(vm.getInstanceName());
422-
List<Command> commands = new ArrayList<Command>();
423-
commands.add(unregisterVMCommand);
424-
return commands;
425-
}
426-
427419
@Override
428420
public List<Command> finalizeExpungeNics(VirtualMachine vm, List<NicProfile> nics) {
429421
List<Command> commands = new ArrayList<Command>();

‎plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareStorageLayoutHelper.java‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// under the License.
1717
package com.cloud.storage.resource;
1818

19+
import java.util.List;
20+
1921
import org.apache.log4j.Logger;
2022

2123
import com.cloud.hypervisor.vmware.mo.DatacenterMO;
@@ -166,6 +168,40 @@ public static void syncVolumeToRootFolder(DatacenterMO dcMo, DatastoreMO ds, Str
166168
s_logger.info("Fixup folder-synchronization. move " + fileDsFullPath + " -> " + targetPath);
167169
ds.moveDatastoreFile(fileDsFullPath, dcMo.getMor(), ds.getMor(), targetPath, dcMo.getMor(), true);
168170
}
171+
172+
public static void moveVolumeToRootFolder(DatacenterMO dcMo, List<String> detachedDisks) throws Exception {
173+
if(detachedDisks.size() > 0) {
174+
for(String fileFullDsPath : detachedDisks) {
175+
DatastoreFile file = new DatastoreFile(fileFullDsPath);
176+
177+
s_logger.info("Check if we need to move " + fileFullDsPath + " to its root location");
178+
DatastoreMO dsMo = new DatastoreMO(dcMo.getContext(), dcMo.findDatastore(file.getDatastoreName()));
179+
if(dsMo.getMor() != null) {
180+
DatastoreFile targetFile = new DatastoreFile(file.getDatastoreName(), file.getFileName());
181+
if(!targetFile.getPath().equalsIgnoreCase(file.getPath())) {
182+
s_logger.info("Move " + file.getPath() + " -> " + targetFile.getPath());
183+
dsMo.moveDatastoreFile(file.getPath(), dcMo.getMor(), dsMo.getMor(), targetFile.getPath(), dcMo.getMor(), true);
184+
185+
String pairSrcFilePath = file.getCompanionPath(file.getFileBaseName() + "-flat.vmdk");
186+
String pairTargetFilePath = targetFile.getCompanionPath(file.getFileBaseName() + "-flat.vmdk");
187+
if(dsMo.fileExists(pairSrcFilePath)) {
188+
s_logger.info("Move " + pairSrcFilePath + " -> " + pairTargetFilePath);
189+
dsMo.moveDatastoreFile(pairSrcFilePath, dcMo.getMor(), dsMo.getMor(), pairTargetFilePath, dcMo.getMor(), true);
190+
}
191+
192+
pairSrcFilePath = file.getCompanionPath(file.getFileBaseName() + "-delta.vmdk");
193+
pairTargetFilePath = targetFile.getCompanionPath(file.getFileBaseName() + "-delta.vmdk");
194+
if(dsMo.fileExists(pairSrcFilePath)) {
195+
s_logger.info("Move " + pairSrcFilePath + " -> " + pairTargetFilePath);
196+
dsMo.moveDatastoreFile(pairSrcFilePath, dcMo.getMor(), dsMo.getMor(), pairTargetFilePath, dcMo.getMor(), true);
197+
}
198+
}
199+
} else {
200+
s_logger.warn("Datastore for " + fileFullDsPath + " no longer exists, we have to skip");
201+
}
202+
}
203+
}
204+
}
169205

170206
public static String getTemplateOnSecStorageFilePath(String secStorageMountPoint, String templateRelativeFolderPath,
171207
String templateName, String fileExtension) {
@@ -227,7 +263,7 @@ public static void deleteVolumeVmdkFiles(DatastoreMO dsMo, String volumeName, Da
227263
if(!dsMo.fileExists(fileFullPath))
228264
fileFullPath = dsMo.searchFileInSubFolders(fileName, false);
229265
if(fileFullPath != null) {
230-
dsMo.deleteFile(fileFullPath, dcMo.getMor(), false);
266+
dsMo.deleteFile(fileFullPath, dcMo.getMor(), true);
231267
} else {
232268
s_logger.warn("Unable to locate VMDK file: " + fileName);
233269
}
@@ -237,7 +273,7 @@ public static void deleteVolumeVmdkFiles(DatastoreMO dsMo, String volumeName, Da
237273
if(!dsMo.fileExists(fileFullPath))
238274
fileFullPath = dsMo.searchFileInSubFolders(fileName, false);
239275
if(fileFullPath != null) {
240-
dsMo.deleteFile(fileFullPath, dcMo.getMor(), false);
276+
dsMo.deleteFile(fileFullPath, dcMo.getMor(), true);
241277
} else {
242278
s_logger.warn("Unable to locate VMDK file: " + fileName);
243279
}
@@ -247,7 +283,7 @@ public static void deleteVolumeVmdkFiles(DatastoreMO dsMo, String volumeName, Da
247283
if(!dsMo.fileExists(fileFullPath))
248284
fileFullPath = dsMo.searchFileInSubFolders(fileName, false);
249285
if(fileFullPath != null) {
250-
dsMo.deleteFile(fileFullPath, dcMo.getMor(), false);
286+
dsMo.deleteFile(fileFullPath, dcMo.getMor(), true);
251287
} else {
252288
s_logger.warn("Unable to locate VMDK file: " + fileName);
253289
}

‎plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareStorageProcessor.java‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import com.cloud.hypervisor.vmware.mo.HostMO;
7272
import com.cloud.hypervisor.vmware.mo.HypervisorHostHelper;
7373
import com.cloud.hypervisor.vmware.mo.NetworkDetails;
74+
import com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfo;
7475
import com.cloud.hypervisor.vmware.mo.VirtualMachineMO;
7576
import com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost;
7677
import com.cloud.hypervisor.vmware.resource.VmwareResource;
@@ -1308,7 +1309,7 @@ private Answer attachIso(DiskTO disk, boolean isAttach, String vmName) {
13081309
} else {
13091310
try{
13101311
vmMo.unmountToolsInstaller();
1311-
}catch(Throwable e){
1312+
} catch(Throwable e){
13121313
vmMo.detachIso(null);
13131314
}
13141315
}
@@ -1447,18 +1448,6 @@ public Answer deleteVolume(DeleteCommand cmd) {
14471448
s_logger.info("Executing resource DestroyCommand: " + _gson.toJson(cmd));
14481449
}
14491450

1450-
/*
1451-
* DestroyCommand content example
1452-
*
1453-
* {"volume": {"id":5,"name":"Volume1", "mountPoint":"/export/home/kelven/vmware-test/primary",
1454-
* "path":"6bb8762f-c34c-453c-8e03-26cc246ceec4", "size":0,"type":"DATADISK","resourceType":
1455-
* "STORAGE_POOL","storagePoolType":"NetworkFilesystem", "poolId":0,"deviceId":0 } }
1456-
*
1457-
* {"volume": {"id":1, "name":"i-2-1-KY-ROOT", "mountPoint":"/export/home/kelven/vmware-test/primary",
1458-
* "path":"i-2-1-KY-ROOT","size":0,"type":"ROOT", "resourceType":"STORAGE_POOL", "storagePoolType":"NetworkFilesystem",
1459-
* "poolId":0,"deviceId":0 } }
1460-
*/
1461-
14621451
try {
14631452
VmwareContext context = hostService.getServiceContext(null);
14641453
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
@@ -1479,6 +1468,7 @@ public Answer deleteVolume(DeleteCommand cmd) {
14791468
ClusterMO clusterMo = new ClusterMO(context, morCluster);
14801469

14811470
if (vol.getVolumeType() == Volume.Type.ROOT) {
1471+
14821472
String vmName = vol.getVmName();
14831473
if (vmName != null) {
14841474
VirtualMachineMO vmMo = clusterMo.findVmOnHyperHost(vmName);
@@ -1487,14 +1477,27 @@ public Answer deleteVolume(DeleteCommand cmd) {
14871477
s_logger.info("Destroy root volume and VM itself. vmName " + vmName);
14881478
}
14891479

1480+
// Remove all snapshots to consolidate disks for removal
1481+
vmMo.removeAllSnapshots();
1482+
1483+
VirtualMachineDiskInfo diskInfo = null;
1484+
if(vol.getChainInfo() != null)
1485+
diskInfo = _gson.fromJson(vol.getChainInfo(), VirtualMachineDiskInfo.class);
1486+
1487+
14901488
HostMO hostMo = vmMo.getRunningHost();
14911489
List<NetworkDetails> networks = vmMo.getNetworksWithDetails();
14921490

14931491
// tear down all devices first before we destroy the VM to avoid accidently delete disk backing files
14941492
if (this.resource.getVmState(vmMo) != State.Stopped) {
14951493
vmMo.safePowerOff(_shutdown_waitMs);
14961494
}
1497-
vmMo.tearDownDevices(new Class<?>[] { VirtualDisk.class, VirtualEthernetCard.class });
1495+
1496+
List<String> detachedDisks = vmMo.detachAllDisksExcept(vol.getPath(), diskInfo != null ? diskInfo.getDiskDeviceBusName() : null);
1497+
VmwareStorageLayoutHelper.moveVolumeToRootFolder(new DatacenterMO(context, morDc), detachedDisks);
1498+
1499+
// let vmMo.destroy to delete volume for us
1500+
// vmMo.tearDownDevices(new Class<?>[] { VirtualDisk.class, VirtualEthernetCard.class });
14981501
vmMo.destroy();
14991502

15001503
for (NetworkDetails netDetails : networks) {
@@ -1506,10 +1509,13 @@ public Answer deleteVolume(DeleteCommand cmd) {
15061509
}
15071510
}
15081511

1509-
if (s_logger.isInfoEnabled())
1512+
/*
1513+
if (s_logger.isInfoEnabled()) {
15101514
s_logger.info("Destroy volume by original name: " + vol.getPath() + ".vmdk");
1511-
1512-
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vol.getPath(), new DatacenterMO(context, morDc));
1515+
}
1516+
1517+
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vol.getPath(), new DatacenterMO(context, morDc));
1518+
*/
15131519
return new Answer(cmd, true, "Success");
15141520
}
15151521

‎vmware-base/src/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java‎

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -991,13 +991,11 @@ public void attachDisk(String[] vmdkDatastorePathChain, ManagedObjectReference m
991991
VirtualDevice newDisk = VmwareHelper.prepareDiskDevice(this, getScsiDeviceControllerKey(),
992992
vmdkDatastorePathChain, morDs, -1, 1);
993993
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
994-
//VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
995994
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
996995

997996
deviceConfigSpec.setDevice(newDisk);
998997
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
999998

1000-
//deviceConfigSpecArray[0] = deviceConfigSpec;
1001999
reConfigSpec.getDeviceChange().add(deviceConfigSpec);
10021000

10031001
ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec);
@@ -1024,13 +1022,11 @@ public void attachDisk(Pair<String, ManagedObjectReference>[] vmdkDatastorePathC
10241022
VirtualDevice newDisk = VmwareHelper.prepareDiskDevice(this, controllerKey,
10251023
vmdkDatastorePathChain, -1, 1);
10261024
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
1027-
//VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
10281025
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
10291026

10301027
deviceConfigSpec.setDevice(newDisk);
10311028
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
10321029

1033-
//deviceConfigSpecArray[0] = deviceConfigSpec;
10341030
reConfigSpec.getDeviceChange().add(deviceConfigSpec);
10351031

10361032
ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec);
@@ -1067,7 +1063,6 @@ public List<Pair<String, ManagedObjectReference>> detachDisk(String vmdkDatastor
10671063
List<Pair<String, ManagedObjectReference>> chain = getDiskDatastorePathChain(deviceInfo.first(), true);
10681064

10691065
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
1070-
//VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
10711066
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
10721067

10731068
deviceConfigSpec.setDevice(deviceInfo.first());
@@ -1076,7 +1071,6 @@ public List<Pair<String, ManagedObjectReference>> detachDisk(String vmdkDatastor
10761071
}
10771072
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
10781073

1079-
//deviceConfigSpecArray[0] = deviceConfigSpec;
10801074
reConfigSpec.getDeviceChange().add(deviceConfigSpec);
10811075

10821076
ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec);
@@ -1964,6 +1958,50 @@ private String getDeviceBusName(List<VirtualDevice> allDevices, VirtualDevice th
19641958
throw new Exception("Unable to find device controller");
19651959
}
19661960

1961+
public List<String> detachAllDisksExcept(String vmdkBaseName, String deviceBusName) throws Exception {
1962+
List<VirtualDevice> devices = (List<VirtualDevice>)_context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
1963+
1964+
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
1965+
List<String> detachedDiskFiles = new ArrayList<String>();
1966+
1967+
for(VirtualDevice device : devices) {
1968+
if(device instanceof VirtualDisk) {
1969+
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
1970+
1971+
VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)device.getBacking();
1972+
1973+
DatastoreFile dsBackingFile = new DatastoreFile(diskBackingInfo.getFileName());
1974+
String backingBaseName = dsBackingFile.getFileBaseName();
1975+
String deviceNumbering = getDeviceBusName(devices, device);
1976+
if(backingBaseName.equalsIgnoreCase(vmdkBaseName) || (deviceBusName != null && deviceBusName.equals(deviceNumbering))) {
1977+
continue;
1978+
} else {
1979+
s_logger.info("Detach " + diskBackingInfo.getFileName() + " from " + getName());
1980+
1981+
detachedDiskFiles.add(diskBackingInfo.getFileName());
1982+
1983+
deviceConfigSpec.setDevice(device);
1984+
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
1985+
1986+
reConfigSpec.getDeviceChange().add(deviceConfigSpec);
1987+
}
1988+
}
1989+
}
1990+
1991+
if(detachedDiskFiles.size() > 0) {
1992+
ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec);
1993+
boolean result = _context.getVimClient().waitForTask(morTask);
1994+
if(result) {
1995+
_context.waitForTaskProgressDone(morTask);
1996+
} else {
1997+
s_logger.warn("Unable to reconfigure the VM to detach disks");
1998+
throw new Exception("Unable to reconfigure the VM to detach disks");
1999+
}
2000+
}
2001+
2002+
return detachedDiskFiles;
2003+
}
2004+
19672005
public VirtualDisk[] getAllDiskDevice() throws Exception {
19682006
List<VirtualDisk> deviceList = new ArrayList<VirtualDisk>();
19692007
List<VirtualDevice> devices = (List<VirtualDevice>)_context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");

0 commit comments

Comments
 (0)