Skip to content

Commit e8ada29

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

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
@@ -2042,7 +2042,6 @@ protected StartAnswer execute(StartCommand cmd) {
20422042
VirtualMachineDefinedProfileSpec diskProfileSpec = null;
20432043
VirtualMachineDefinedProfileSpec vmProfileSpec = null;
20442044

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

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

20882087
VirtualMachineDiskInfoBuilder diskInfoBuilder = null;
2089-
VirtualDevice[] nicDevices = null;
20902088
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmInternalCSName);
20912089
DiskControllerType systemVmScsiControllerType = DiskControllerType.lsilogic;
20922090
int firstScsiControllerBusNum = 0;
@@ -2103,7 +2101,6 @@ protected StartAnswer execute(StartCommand cmd) {
21032101
diskDatastores = vmMo.getAllDiskDatastores();
21042102
diskInfoBuilder = vmMo.getDiskInfoBuilder();
21052103
hasSnapshot = vmMo.hasSnapshot();
2106-
nicDevices = vmMo.getNicDevices();
21072104

21082105
tearDownVmDevices(vmMo, hasSnapshot, deployAsIs);
21092106
ensureDiskControllersInternal(vmMo, systemVm, controllerInfo, systemVmScsiControllerType,
@@ -2119,17 +2116,20 @@ protected StartAnswer execute(StartCommand cmd) {
21192116
}
21202117

21212118
takeVmFromOtherHyperHost(hyperHost, vmInternalCSName);
2119+
vmMo = hyperHost.findVmOnHyperHost(vmInternalCSName);
21222120

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

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

2130-
tearDownVmDevices(vmMo, hasSnapshot, deployAsIs);
2131-
ensureDiskControllersInternal(vmMo, systemVm, controllerInfo, systemVmScsiControllerType,
2132-
numScsiControllerForSystemVm, firstScsiControllerBusNum, deployAsIs);
2129+
tearDownVmDevices(vmMo, hasSnapshot, deployAsIs);
2130+
ensureDiskControllersInternal(vmMo, systemVm, controllerInfo, systemVmScsiControllerType,
2131+
numScsiControllerForSystemVm, firstScsiControllerBusNum, deployAsIs);
2132+
}
21332133
} else {
21342134
// 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).
21352135
VirtualMachineMO existingVmInDc = dcMo.findVm(vmInternalCSName);
@@ -2146,7 +2146,7 @@ protected StartAnswer execute(StartCommand cmd) {
21462146
vmMo = hyperHost.findVmOnHyperHost(vmInternalCSName);
21472147
if (vmMo == null) {
21482148
logger.info("Cloned deploy-as-is VM " + vmInternalCSName + " is not in this host, relocating it");
2149-
vmMo = takeVmFromOtherHyperHost(hyperHost, vmInternalCSName);
2149+
takeVmFromOtherHyperHost(hyperHost, vmInternalCSName);
21502150
}
21512151
} else {
21522152
DiskTO rootDisk = null;
@@ -2256,11 +2256,11 @@ protected StartAnswer execute(StartCommand cmd) {
22562256
vmConfigSpec.setCpuHotAddEnabled(vmMo.isCpuHotAddSupported(guestOsId) && vmSpec.isEnableDynamicallyScaleVm());
22572257
}
22582258

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

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

@@ -2731,9 +2731,11 @@ private void syncVolumeDatastoreAndPathForDatastoreCluster(DiskTO vol, VirtualMa
27312731
}
27322732

27332733
private boolean powerOnVM(final VirtualMachineMO vmMo, final String vmInternalCSName, final String vmNameOnVcenter) throws Exception {
2734-
int retry = 20;
2735-
while (retry-- > 0) {
2734+
final int retry = 20;
2735+
int retryAttempt = 0;
2736+
while (++retryAttempt <= retry) {
27362737
try {
2738+
logger.debug(String.format("VM %s, powerOn attempt #%d", vmInternalCSName, retryAttempt));
27372739
return vmMo.powerOn();
27382740
} catch (Exception e) {
27392741
logger.info(String.format("Got exception while power on VM %s with hostname %s", vmInternalCSName, vmNameOnVcenter), e);
@@ -3300,7 +3302,7 @@ private void tearDownVm(VirtualMachineMO vmMo) throws Exception {
33003302

33013303
int getReservedMemoryMb(VirtualMachineTO vmSpec) {
33023304
if (vmSpec.getDetails().get(VMwareGuru.VmwareReserveMemory.key()).equalsIgnoreCase("true")) {
3303-
if(vmSpec.getDetails().get(VmDetailConstants.RAM_RESERVATION) != null){
3305+
if (vmSpec.getDetails().get(VmDetailConstants.RAM_RESERVATION) != null) {
33043306
float reservedMemory = (vmSpec.getMaxRam() * Float.parseFloat(vmSpec.getDetails().get(VmDetailConstants.RAM_RESERVATION)));
33053307
return (int) (reservedMemory / ResourceType.bytesToMiB);
33063308
}
@@ -4927,7 +4929,7 @@ private Answer migrateVolume(MigrateVolumeCommand cmd) {
49274929
VmwareHypervisorHost dsHost = hyperHostInTargetCluster == null ? hyperHost : hyperHostInTargetCluster;
49284930
String targetDsName = cmd.getTargetPool().getUuid();
49294931
morDestinationDS = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(dsHost, targetDsName);
4930-
if(morDestinationDS == null) {
4932+
if (morDestinationDS == null) {
49314933
String msg = "Unable to find the target datastore: " + targetDsName + " on host: " + dsHost.getHyperHostName();
49324934
logger.error(msg);
49334935
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
@@ -8394,6 +8394,7 @@ public Pair<UserVmVO, Volume> doInTransaction(final TransactionStatus status) th
83948394

83958395
getRootVolumeSizeForVmRestore(newVol, template, userVm, diskOffering, details, true);
83968396
volumeMgr.saveVolumeDetails(newVol.getDiskOfferingId(), newVol.getId());
8397+
newVol = _volsDao.findById(newVol.getId());
83978398

83988399
// 1. Save usage event and update resource count for user vm volumes
83998400
try {
@@ -8493,7 +8494,6 @@ public Pair<UserVmVO, Volume> doInTransaction(final TransactionStatus status) th
84938494

84948495
Long getRootVolumeSizeForVmRestore(Volume vol, VMTemplateVO template, UserVmVO userVm, DiskOffering diskOffering, Map<String, String> details, boolean update) {
84958496
VolumeVO resizedVolume = (VolumeVO) vol;
8496-
84978497
Long size = null;
84988498
if (template != null && template.getSize() != null) {
84998499
UserVmDetailVO vmRootDiskSizeDetail = userVmDetailsDao.findDetail(userVm.getId(), VmDetailConstants.ROOT_DISK_SIZE);

0 commit comments

Comments
 (0)