Skip to content

Commit 2976d9c

Browse files
committed
CLOUDSTACK-4802: Change VM start flow to better support VM snapshot on certian version of vSphere, (vSphere 5.0 Update 2). If we detect that VM has pending VM snapshot, we will fully honor VM disk info from vCenter, since in some version of vSphere (vSphere 5.0 U2) does not allow disk-editting when VM has pending snapshot.
1 parent cd797d5 commit 2976d9c

2 files changed

Lines changed: 75 additions & 31 deletions

File tree

‎plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java‎

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,14 +2661,19 @@ protected StartAnswer execute(StartCommand cmd) {
26612661
DatacenterMO dcMo = new DatacenterMO(hyperHost.getContext(), hyperHost.getHyperHostDatacenter());
26622662
VirtualMachineDiskInfoBuilder diskInfoBuilder = null;
26632663
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmInternalCSName);
2664+
boolean hasSnapshot = false;
26642665
if (vmMo != null) {
26652666
s_logger.info("VM " + vmInternalCSName + " already exists, tear down devices for reconfiguration");
26662667
if (getVmState(vmMo) != State.Stopped)
26672668
vmMo.safePowerOff(_shutdown_waitMs);
26682669

26692670
// retrieve disk information before we tear down
26702671
diskInfoBuilder = vmMo.getDiskInfoBuilder();
2671-
vmMo.tearDownDevices(new Class<?>[] { VirtualDisk.class, VirtualEthernetCard.class });
2672+
hasSnapshot = vmMo.hasSnapshot();
2673+
if(!hasSnapshot)
2674+
vmMo.tearDownDevices(new Class<?>[] { VirtualDisk.class, VirtualEthernetCard.class });
2675+
else
2676+
vmMo.tearDownDevices(new Class<?>[] { VirtualEthernetCard.class });
26722677
vmMo.ensureScsiDeviceController();
26732678
} else {
26742679
ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter();
@@ -2686,7 +2691,11 @@ protected StartAnswer execute(StartCommand cmd) {
26862691
vmMo.safePowerOff(_shutdown_waitMs);
26872692

26882693
diskInfoBuilder = vmMo.getDiskInfoBuilder();
2689-
vmMo.tearDownDevices(new Class<?>[] { VirtualDisk.class, VirtualEthernetCard.class });
2694+
hasSnapshot = vmMo.hasSnapshot();
2695+
if(!hasSnapshot)
2696+
vmMo.tearDownDevices(new Class<?>[] { VirtualDisk.class, VirtualEthernetCard.class });
2697+
else
2698+
vmMo.tearDownDevices(new Class<?>[] { VirtualEthernetCard.class });
26902699
vmMo.ensureScsiDeviceController();
26912700
} else {
26922701
Pair<ManagedObjectReference, DatastoreMO> rootDiskDataStoreDetails = null;
@@ -2840,37 +2849,45 @@ protected StartAnswer execute(StartCommand cmd) {
28402849
//
28412850
DiskTO[] sortedDisks = sortVolumesByDeviceId(disks);
28422851
for (DiskTO vol : sortedDisks) {
2843-
deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec();
2844-
28452852
if (vol.getType() == Volume.Type.ISO)
28462853
continue;
2847-
2854+
28482855
VirtualMachineDiskInfo matchingExistingDisk = getMatchingExistingDisk(diskInfoBuilder, vol);
28492856
controllerKey = getDiskController(matchingExistingDisk, vol, vmSpec, ideControllerKey, scsiControllerKey);
28502857

2851-
VolumeObjectTO volumeTO = (VolumeObjectTO)vol.getData();
2852-
PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)volumeTO.getDataStore();
2853-
Pair<ManagedObjectReference, DatastoreMO> volumeDsDetails = dataStoresDetails.get(primaryStore.getUuid());
2854-
assert (volumeDsDetails != null);
2855-
VirtualDevice device;
2856-
2857-
String[] diskChain = syncDiskChain(dcMo, vmMo, vmSpec,
2858-
vol, matchingExistingDisk,
2859-
dataStoresDetails);
2860-
if(controllerKey == scsiControllerKey && VmwareHelper.isReservedScsiDeviceNumber(scsiUnitNumber))
2861-
scsiUnitNumber++;
2862-
device = VmwareHelper.prepareDiskDevice(vmMo, null, controllerKey,
2863-
diskChain,
2864-
volumeDsDetails.first(),
2865-
(controllerKey == ideControllerKey) ? ideUnitNumber++ : scsiUnitNumber++, i + 1);
2866-
2867-
deviceConfigSpecArray[i].setDevice(device);
2868-
deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.ADD);
2869-
2870-
if(s_logger.isDebugEnabled())
2871-
s_logger.debug("Prepare volume at new device " + _gson.toJson(device));
2872-
2873-
i++;
2858+
if(!hasSnapshot) {
2859+
deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec();
2860+
2861+
VolumeObjectTO volumeTO = (VolumeObjectTO)vol.getData();
2862+
PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)volumeTO.getDataStore();
2863+
Pair<ManagedObjectReference, DatastoreMO> volumeDsDetails = dataStoresDetails.get(primaryStore.getUuid());
2864+
assert (volumeDsDetails != null);
2865+
2866+
String[] diskChain = syncDiskChain(dcMo, vmMo, vmSpec,
2867+
vol, matchingExistingDisk,
2868+
dataStoresDetails);
2869+
if(controllerKey == scsiControllerKey && VmwareHelper.isReservedScsiDeviceNumber(scsiUnitNumber))
2870+
scsiUnitNumber++;
2871+
VirtualDevice device = VmwareHelper.prepareDiskDevice(vmMo, null, controllerKey,
2872+
diskChain,
2873+
volumeDsDetails.first(),
2874+
(controllerKey == ideControllerKey) ? ideUnitNumber++ : scsiUnitNumber++, i + 1);
2875+
2876+
deviceConfigSpecArray[i].setDevice(device);
2877+
deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.ADD);
2878+
2879+
if(s_logger.isDebugEnabled())
2880+
s_logger.debug("Prepare volume at new device " + _gson.toJson(device));
2881+
2882+
i++;
2883+
} else {
2884+
if(controllerKey == scsiControllerKey && VmwareHelper.isReservedScsiDeviceNumber(scsiUnitNumber))
2885+
scsiUnitNumber++;
2886+
if(controllerKey == ideControllerKey)
2887+
ideUnitNumber++;
2888+
else
2889+
scsiUnitNumber++;
2890+
}
28742891
}
28752892

28762893
//
@@ -2917,7 +2934,8 @@ protected StartAnswer execute(StartCommand cmd) {
29172934
nicCount++;
29182935
}
29192936

2920-
vmConfigSpec.getDeviceChange().addAll(Arrays.asList(deviceConfigSpecArray));
2937+
for(int j = 0; j < i; j++)
2938+
vmConfigSpec.getDeviceChange().add(deviceConfigSpecArray[j]);
29212939

29222940
//
29232941
// Setup VM options
@@ -5479,7 +5497,7 @@ public Answer execute(DeleteCommand cmd) {
54795497
// tear down all devices first before we destroy the VM to avoid accidently delete disk backing files
54805498
if (getVmState(vmMo) != State.Stopped)
54815499
vmMo.safePowerOff(_shutdown_waitMs);
5482-
vmMo.tearDownDevices(new Class<?>[] { VirtualDisk.class, VirtualEthernetCard.class });
5500+
vmMo.tearDownDevices(new Class<?>[] { /* VirtualDisk.class, */ VirtualEthernetCard.class });
54835501
vmMo.destroy();
54845502

54855503
for (NetworkDetails netDetails : networks) {

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,14 @@ public ManagedObjectReference getSnapshotMor(String snapshotName) throws Excepti
577577
}
578578
return null;
579579
}
580+
581+
public boolean hasSnapshot() throws Exception {
582+
VirtualMachineSnapshotInfo info = getSnapshotInfo();
583+
if(info != null) {
584+
return info.getCurrentSnapshot() != null;
585+
}
586+
return false;
587+
}
580588

581589
public boolean createFullClone(String cloneName, ManagedObjectReference morFolder, ManagedObjectReference morResourcePool,
582590
ManagedObjectReference morDs) throws Exception {
@@ -1629,6 +1637,7 @@ public void tearDownDevice(VirtualDevice device) throws Exception {
16291637

16301638
public void tearDownDevices(Class<?>[] deviceClasses) throws Exception {
16311639
VirtualDevice[] devices = getMatchedDevices(deviceClasses);
1640+
16321641
if(devices.length > 0) {
16331642
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
16341643
VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[devices.length];
@@ -1637,9 +1646,9 @@ public void tearDownDevices(Class<?>[] deviceClasses) throws Exception {
16371646
deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec();
16381647
deviceConfigSpecArray[i].setDevice(devices[i]);
16391648
deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
1649+
vmConfigSpec.getDeviceChange().add(deviceConfigSpecArray[i]);
16401650
}
16411651

1642-
vmConfigSpec.getDeviceChange().addAll(Arrays.asList(deviceConfigSpecArray));
16431652
if(!configureVm(vmConfigSpec)) {
16441653
throw new Exception("Failed to detach devices");
16451654
}
@@ -2021,6 +2030,10 @@ public List<String> detachAllDisksExcept(String vmdkBaseName, String deviceBusNa
20212030
return detachedDiskFiles;
20222031
}
20232032

2033+
public List<VirtualDevice> getAllDeviceList() throws Exception {
2034+
return (List<VirtualDevice>)_context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
2035+
}
2036+
20242037
public VirtualDisk[] getAllDiskDevice() throws Exception {
20252038
List<VirtualDisk> deviceList = new ArrayList<VirtualDisk>();
20262039
List<VirtualDevice> devices = (List<VirtualDevice>)_context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
@@ -2034,6 +2047,19 @@ public VirtualDisk[] getAllDiskDevice() throws Exception {
20342047

20352048
return deviceList.toArray(new VirtualDisk[0]);
20362049
}
2050+
2051+
public VirtualDisk getDiskDeviceByBusName(List<VirtualDevice> allDevices, String busName) throws Exception {
2052+
for(VirtualDevice device : allDevices ) {
2053+
if(device instanceof VirtualDisk) {
2054+
VirtualDisk disk = (VirtualDisk)device;
2055+
String diskBusName = getDeviceBusName(allDevices, (VirtualDevice)disk);
2056+
if(busName.equalsIgnoreCase(diskBusName))
2057+
return disk;
2058+
}
2059+
}
2060+
2061+
return null;
2062+
}
20372063

20382064
public VirtualDisk[] getAllIndependentDiskDevice() throws Exception {
20392065
List<VirtualDisk> independentDisks = new ArrayList<VirtualDisk>();

0 commit comments

Comments
 (0)