Skip to content

Commit f0d113e

Browse files
Update full clone flag when restoring vm using root disk offering with more size than the template size
1 parent f0f1e03 commit f0d113e

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,7 +2043,6 @@ protected StartAnswer execute(StartCommand cmd) {
20432043
VirtualMachineDefinedProfileSpec diskProfileSpec = null;
20442044
VirtualMachineDefinedProfileSpec vmProfileSpec = null;
20452045

2046-
20472046
DeployAsIsInfoTO deployAsIsInfo = vmSpec.getDeployAsIsInfo();
20482047
boolean deployAsIs = deployAsIsInfo != null;
20492048

@@ -2087,7 +2086,6 @@ protected StartAnswer execute(StartCommand cmd) {
20872086
}
20882087

20892088
VirtualMachineDiskInfoBuilder diskInfoBuilder = null;
2090-
VirtualDevice[] nicDevices = null;
20912089
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmInternalCSName);
20922090
DiskControllerType systemVmScsiControllerType = DiskControllerType.lsilogic;
20932091
int firstScsiControllerBusNum = 0;
@@ -2104,7 +2102,6 @@ protected StartAnswer execute(StartCommand cmd) {
21042102
diskDatastores = vmMo.getAllDiskDatastores();
21052103
diskInfoBuilder = vmMo.getDiskInfoBuilder();
21062104
hasSnapshot = vmMo.hasSnapshot();
2107-
nicDevices = vmMo.getNicDevices();
21082105

21092106
tearDownVmDevices(vmMo, hasSnapshot, deployAsIs);
21102107
ensureDiskControllersInternal(vmMo, systemVm, controllerInfo, systemVmScsiControllerType,
@@ -2120,17 +2117,20 @@ protected StartAnswer execute(StartCommand cmd) {
21202117
}
21212118

21222119
takeVmFromOtherHyperHost(hyperHost, vmInternalCSName);
2120+
vmMo = hyperHost.findVmOnHyperHost(vmInternalCSName);
21232121

2124-
if (getVmPowerState(vmMo) != PowerState.PowerOff)
2125-
vmMo.safePowerOff(_shutdownWaitMs);
2122+
if (vmMo != null) {
2123+
if (getVmPowerState(vmMo) != PowerState.PowerOff)
2124+
vmMo.safePowerOff(_shutdownWaitMs);
21262125

2127-
diskInfoBuilder = vmMo.getDiskInfoBuilder();
2128-
hasSnapshot = vmMo.hasSnapshot();
2129-
diskDatastores = vmMo.getAllDiskDatastores();
2126+
diskInfoBuilder = vmMo.getDiskInfoBuilder();
2127+
hasSnapshot = vmMo.hasSnapshot();
2128+
diskDatastores = vmMo.getAllDiskDatastores();
21302129

2131-
tearDownVmDevices(vmMo, hasSnapshot, deployAsIs);
2132-
ensureDiskControllersInternal(vmMo, systemVm, controllerInfo, systemVmScsiControllerType,
2133-
numScsiControllerForSystemVm, firstScsiControllerBusNum, deployAsIs);
2130+
tearDownVmDevices(vmMo, hasSnapshot, deployAsIs);
2131+
ensureDiskControllersInternal(vmMo, systemVm, controllerInfo, systemVmScsiControllerType,
2132+
numScsiControllerForSystemVm, firstScsiControllerBusNum, deployAsIs);
2133+
}
21342134
} else {
21352135
// If a VM with the same name is found in a different cluster in the DC, unregister the old VM and configure a new VM (cold-migration).
21362136
VirtualMachineMO existingVmInDc = dcMo.findVm(vmInternalCSName);
@@ -2147,7 +2147,7 @@ protected StartAnswer execute(StartCommand cmd) {
21472147
vmMo = hyperHost.findVmOnHyperHost(vmInternalCSName);
21482148
if (vmMo == null) {
21492149
logger.info("Cloned deploy-as-is VM " + vmInternalCSName + " is not in this host, relocating it");
2150-
vmMo = takeVmFromOtherHyperHost(hyperHost, vmInternalCSName);
2150+
takeVmFromOtherHyperHost(hyperHost, vmInternalCSName);
21512151
}
21522152
} else {
21532153
DiskTO rootDisk = null;
@@ -2257,11 +2257,11 @@ protected StartAnswer execute(StartCommand cmd) {
22572257
vmConfigSpec.setCpuHotAddEnabled(vmMo.isCpuHotAddSupported(guestOsId) && vmSpec.isEnableDynamicallyScaleVm());
22582258
}
22592259

2260-
if(!vmMo.isMemoryHotAddSupported(guestOsId) && vmSpec.isEnableDynamicallyScaleVm()){
2260+
if (!vmMo.isMemoryHotAddSupported(guestOsId) && vmSpec.isEnableDynamicallyScaleVm()) {
22612261
logger.warn("hotadd of memory is not supported, dynamic scaling feature can not be applied to vm: " + vmInternalCSName);
22622262
}
22632263

2264-
if(!vmMo.isCpuHotAddSupported(guestOsId) && vmSpec.isEnableDynamicallyScaleVm()){
2264+
if (!vmMo.isCpuHotAddSupported(guestOsId) && vmSpec.isEnableDynamicallyScaleVm()) {
22652265
logger.warn("hotadd of cpu is not supported, dynamic scaling feature can not be applied to vm: " + vmInternalCSName);
22662266
}
22672267

@@ -2715,9 +2715,11 @@ protected StartAnswer execute(StartCommand cmd) {
27152715
}
27162716

27172717
private boolean powerOnVM(final VirtualMachineMO vmMo, final String vmInternalCSName, final String vmNameOnVcenter) throws Exception {
2718-
int retry = 20;
2719-
while (retry-- > 0) {
2718+
final int retry = 20;
2719+
int retryAttempt = 0;
2720+
while (++retryAttempt <= retry) {
27202721
try {
2722+
logger.debug(String.format("VM %s, powerOn attempt #%d", vmInternalCSName, retryAttempt));
27212723
return vmMo.powerOn();
27222724
} catch (Exception e) {
27232725
logger.info(String.format("Got exception while power on VM %s with hostname %s", vmInternalCSName, vmNameOnVcenter), e);
@@ -3284,7 +3286,7 @@ private void tearDownVm(VirtualMachineMO vmMo) throws Exception {
32843286

32853287
int getReservedMemoryMb(VirtualMachineTO vmSpec) {
32863288
if (vmSpec.getDetails().get(VMwareGuru.VmwareReserveMemory.key()).equalsIgnoreCase("true")) {
3287-
if(vmSpec.getDetails().get(VmDetailConstants.RAM_RESERVATION) != null){
3289+
if (vmSpec.getDetails().get(VmDetailConstants.RAM_RESERVATION) != null) {
32883290
float reservedMemory = (vmSpec.getMaxRam() * Float.parseFloat(vmSpec.getDetails().get(VmDetailConstants.RAM_RESERVATION)));
32893291
return (int) (reservedMemory / ResourceType.bytesToMiB);
32903292
}
@@ -4913,7 +4915,7 @@ private Answer migrateVolume(MigrateVolumeCommand cmd) {
49134915
VmwareHypervisorHost dsHost = hyperHostInTargetCluster == null ? hyperHost : hyperHostInTargetCluster;
49144916
String targetDsName = cmd.getTargetPool().getUuid();
49154917
morDestinationDS = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(dsHost, targetDsName);
4916-
if(morDestinationDS == null) {
4918+
if (morDestinationDS == null) {
49174919
String msg = "Unable to find the target datastore: " + targetDsName + " on host: " + dsHost.getHyperHostName();
49184920
logger.error(msg);
49194921
throw new CloudRuntimeException(msg);

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ public Answer cloneVolumeFromBaseTemplate(CopyCommand cmd) {
814814
existingVm.detachAllDisksAndDestroy();
815815
}
816816
logger.info("ROOT Volume from deploy-as-is template, cloning template");
817-
cloneVMFromTemplate(hyperHost, template.getPath(), vmName, primaryStore.getUuid());
817+
cloneVMFromTemplate(hyperHost, template, volume, vmName, primaryStore.getUuid());
818818
} else {
819819
logger.info("ROOT Volume from deploy-as-is template, volume already created at this point");
820820
}
@@ -1222,10 +1222,10 @@ private Ternary<String, Long, Long> createTemplateFromVolume(VmwareContext conte
12221222
// Get VMDK filename
12231223
String templateVMDKName = "";
12241224
File[] files = new File(installFullPath).listFiles();
1225-
if(files != null) {
1225+
if (files != null) {
12261226
for(File file : files) {
12271227
String fileName = file.getName();
1228-
if(fileName.toLowerCase().startsWith(templateUniqueName) && fileName.toLowerCase().endsWith(".vmdk")) {
1228+
if (fileName.toLowerCase().startsWith(templateUniqueName) && fileName.toLowerCase().endsWith(".vmdk")) {
12291229
templateVMDKName += fileName;
12301230
break;
12311231
}
@@ -1856,16 +1856,16 @@ public Answer backupSnapshot(CopyCommand cmd) {
18561856
CopyCmdAnswer answer = null;
18571857

18581858
try {
1859-
if(vmName != null) {
1859+
if (vmName != null) {
18601860
vmMo = hyperHost.findVmOnHyperHost(vmName);
18611861
if (vmMo == null) {
1862-
if(logger.isDebugEnabled()) {
1862+
if (logger.isDebugEnabled()) {
18631863
logger.debug("Unable to find owner VM for BackupSnapshotCommand on host " + hyperHost.getHyperHostName() + ", will try within datacenter");
18641864
}
18651865
vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
18661866
}
18671867
}
1868-
if(vmMo == null) {
1868+
if (vmMo == null) {
18691869
dsMo = new DatastoreMO(hyperHost.getContext(), morDs);
18701870
workerVMName = hostService.getWorkerName(context, cmd, 0, dsMo);
18711871
vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVMName, null);
@@ -1899,10 +1899,10 @@ public Answer backupSnapshot(CopyCommand cmd) {
18991899
String secondaryMountPoint = mountService.getMountPoint(secondaryStorageUrl, _nfsVersion);
19001900
String snapshotDir = destSnapshot.getPath() + "/" + snapshotBackupUuid;
19011901
File[] files = new File(secondaryMountPoint + "/" + snapshotDir).listFiles();
1902-
if(files != null) {
1902+
if (files != null) {
19031903
for(File file : files) {
19041904
String fileName = file.getName();
1905-
if(fileName.toLowerCase().startsWith(snapshotBackupUuid) && fileName.toLowerCase().endsWith(".vmdk")) {
1905+
if (fileName.toLowerCase().startsWith(snapshotBackupUuid) && fileName.toLowerCase().endsWith(".vmdk")) {
19061906
physicalSize = new File(secondaryMountPoint + "/" + snapshotDir + "/" + fileName).length();
19071907
break;
19081908
}
@@ -3651,7 +3651,7 @@ private Long restoreVolumeFromSecStorage(VmwareHypervisorHost hyperHost, Datasto
36513651
}
36523652
workerVm.tagAsWorkerVM();
36533653

3654-
if(!primaryDsMo.getDatastoreType().equalsIgnoreCase("VVOL")) {
3654+
if (!primaryDsMo.getDatastoreType().equalsIgnoreCase("VVOL")) {
36553655
HypervisorHostHelper.createBaseFolderInDatastore(primaryDsMo, primaryDsMo.getDataCenterMor());
36563656
workerVm.moveAllVmDiskFiles(primaryDsMo, HypervisorHostHelper.VSPHERE_DATASTORE_BASE_FOLDER, false);
36573657
}
@@ -3811,8 +3811,9 @@ public Answer copyVolumeFromPrimaryToPrimary(CopyCommand cmd) {
38113811
/**
38123812
* Return the cloned VM from the template
38133813
*/
3814-
public VirtualMachineMO cloneVMFromTemplate(VmwareHypervisorHost hyperHost, String templateName, String cloneName, String templatePrimaryStoreUuid) {
3814+
public VirtualMachineMO cloneVMFromTemplate(VmwareHypervisorHost hyperHost, TemplateObjectTO template, VolumeObjectTO volume, String cloneName, String templatePrimaryStoreUuid) {
38153815
try {
3816+
String templateName = template.getPath();
38163817
VmwareContext context = hyperHost.getContext();
38173818
DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
38183819
VirtualMachineMO templateMo = dcMo.findVm(templateName);
@@ -3826,6 +3827,9 @@ public VirtualMachineMO cloneVMFromTemplate(VmwareHypervisorHost hyperHost, Stri
38263827
throw new CloudRuntimeException("Unable to find datastore in vSphere");
38273828
}
38283829
logger.info("Cloning VM " + cloneName + " from template " + templateName + " into datastore " + templatePrimaryStoreUuid);
3830+
if (template.getSize() != null) {
3831+
_fullCloneFlag = volume.getSize() > template.getSize() ? true : _fullCloneFlag;
3832+
}
38293833
if (!_fullCloneFlag) {
38303834
createVMLinkedClone(templateMo, dcMo, cloneName, morDatastore, morPool, null);
38313835
} else {

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8391,6 +8391,7 @@ public Pair<UserVmVO, Volume> doInTransaction(final TransactionStatus status) th
83918391

83928392
getRootVolumeSizeForVmRestore(newVol, template, userVm, diskOffering, details, true);
83938393
volumeMgr.saveVolumeDetails(newVol.getDiskOfferingId(), newVol.getId());
8394+
newVol = _volsDao.findById(newVol.getId());
83948395

83958396
// 1. Save usage event and update resource count for user vm volumes
83968397
try {
@@ -8490,7 +8491,6 @@ public Pair<UserVmVO, Volume> doInTransaction(final TransactionStatus status) th
84908491

84918492
Long getRootVolumeSizeForVmRestore(Volume vol, VMTemplateVO template, UserVmVO userVm, DiskOffering diskOffering, Map<String, String> details, boolean update) {
84928493
VolumeVO resizedVolume = (VolumeVO) vol;
8493-
84948494
Long size = null;
84958495
if (template != null && template.getSize() != null) {
84968496
UserVmDetailVO vmRootDiskSizeDetail = userVmDetailsDao.findDetail(userVm.getId(), VmDetailConstants.ROOT_DISK_SIZE);

0 commit comments

Comments
 (0)