Skip to content

Commit 03d9cb5

Browse files
authored
Volume migration fix wrt #22 (#27)
* fix worker vm Signed-off-by: Abhishek Kumar <[email protected]> * fix Signed-off-by: Abhishek Kumar <[email protected]> * remove unwanted logs Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 4712856 commit 03d9cb5

File tree

3 files changed

+35
-42
lines changed

3 files changed

+35
-42
lines changed

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

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3387,7 +3387,6 @@ protected Answer execute(MigrateVmToPoolCommand cmd) {
33873387

33883388
private Answer migrateAndAnswer(VirtualMachineMO vmMo, String poolUuid, VmwareHypervisorHost hyperHost, Command cmd) throws Exception {
33893389
String hostNameInTargetCluster = null;
3390-
VmwareHypervisorHost hostInTargetCluster = null;
33913390
List<Pair<VolumeTO, StorageFilerTO>> volToFiler = new ArrayList<>();
33923391
if (cmd instanceof MigrateVmToPoolCommand) {
33933392
MigrateVmToPoolCommand mcmd = (MigrateVmToPoolCommand)cmd;
@@ -3396,13 +3395,7 @@ private Answer migrateAndAnswer(VirtualMachineMO vmMo, String poolUuid, VmwareHy
33963395
} else if (cmd instanceof MigrateVolumeCommand) {
33973396
hostNameInTargetCluster = ((MigrateVolumeCommand)cmd).getHostGuidInTargetCluster();
33983397
}
3399-
if (StringUtils.isNotBlank(hostNameInTargetCluster)) {
3400-
String hostInTargetClusterMorInfo = hostNameInTargetCluster.split("@")[0];
3401-
ManagedObjectReference morHostInTargetCluster = new ManagedObjectReference();
3402-
morHostInTargetCluster.setType(hostInTargetClusterMorInfo.split(":")[0]);
3403-
morHostInTargetCluster.setValue(hostInTargetClusterMorInfo.split(":")[1]);
3404-
hostInTargetCluster = new HostMO(getServiceContext(), morHostInTargetCluster);
3405-
}
3398+
VmwareHypervisorHost hostInTargetCluster = VmwareHelper.getHostMOFromHostName(getServiceContext(), hostNameInTargetCluster);
34063399
try {
34073400
// OfflineVmwareMigration: getVolumesFromCommand(cmd);
34083401
Map<Integer, Long> volumeDeviceKey = new HashMap<>();
@@ -3581,19 +3574,11 @@ private Answer migrateVolume(MigrateVolumeCommand cmd) {
35813574

35823575
ManagedObjectReference morSourceDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, cmd.getSourcePool().getUuid());
35833576
DatastoreMO sourceDsMo = new DatastoreMO(hyperHost.getContext(), morSourceDs);
3584-
DatacenterMO dcMo = new DatacenterMO(hyperHost.getContext(), hyperHost.getHyperHostDatacenter());
35853577
String targetDsName = cmd.getTargetPool().getUuid();
3586-
String hostNameInTargetCluster = cmd.getHostGuidInTargetCluster();
3587-
VmwareHypervisorHost hostInTargetCluster = null;
3588-
if (StringUtils.isNotBlank(hostNameInTargetCluster)) {
3589-
String hostInTargetClusterMorInfo = hostNameInTargetCluster.split("@")[0];
3590-
ManagedObjectReference morHostInTargetCluster = new ManagedObjectReference();
3591-
morHostInTargetCluster.setType(hostInTargetClusterMorInfo.split(":")[0]);
3592-
morHostInTargetCluster.setValue(hostInTargetClusterMorInfo.split(":")[1]);
3593-
hostInTargetCluster = new HostMO(getServiceContext(), morHostInTargetCluster);
3594-
}
3578+
VmwareHypervisorHost hostInTargetCluster = VmwareHelper.getHostMOFromHostName(getServiceContext(),
3579+
cmd.getHostGuidInTargetCluster());
35953580
VmwareHypervisorHost dsHost = hostInTargetCluster == null ? hyperHost : hostInTargetCluster;
3596-
ManagedObjectReference morTargetDS = getTargetDatastoreMOReference(targetDsName, hyperHost);
3581+
ManagedObjectReference morTargetDS = getTargetDatastoreMOReference(targetDsName, dsHost);
35973582
if(morTargetDS == null) {
35983583
String msg = "Unable to find the target datastore: " + targetDsName + " on host: " + dsHost.getHyperHostName();
35993584
s_logger.error(msg);
@@ -5731,30 +5716,23 @@ private List<VolumeObjectTO> relocateVirtualMachine(final VmwareHypervisorHost h
57315716
VmwareHypervisorHost targetHyperHost = hostInTargetCluster;
57325717
VirtualMachineMO vmMo = null;
57335718
ManagedObjectReference morSourceHostDc = null;
5734-
ManagedObjectReference morTargetHostDc = null;
5735-
ManagedObjectReference morTargetHost = new ManagedObjectReference();
57365719
VirtualMachineRelocateSpec relocateSpec = new VirtualMachineRelocateSpec();
57375720
List<VirtualMachineRelocateSpecDiskLocator> diskLocators = new ArrayList<VirtualMachineRelocateSpecDiskLocator>();
57385721
Set<String> mountedDatastoresAtSource = new HashSet<String>();
57395722
List<VolumeObjectTO> volumeToList = new ArrayList<>();
57405723
Map<Long, Integer> volumeDeviceKey = new HashMap<Long, Integer>();
5741-
if (StringUtils.isNotBlank(targetHost)) {
5742-
String targetHostMorInfo = targetHost.split("@")[0];
5743-
morTargetHost.setType(targetHostMorInfo.split(":")[0]);
5744-
morTargetHost.setValue(targetHostMorInfo.split(":")[1]);
5745-
}
57465724

57475725
try {
57485726
if (sourceHyperHost == null) {
57495727
sourceHyperHost = getHyperHost(getServiceContext());
57505728
}
57515729
if (targetHyperHost == null && StringUtils.isNotBlank(targetHost)) {
5752-
targetHyperHost = new HostMO(getServiceContext(), morTargetHost);
5730+
targetHyperHost = VmwareHelper.getHostMOFromHostName(getServiceContext(), targetHost);
57535731
}
57545732
morSourceHostDc = sourceHyperHost.getHyperHostDatacenter();
57555733
DatacenterMO dcMo = new DatacenterMO(sourceHyperHost.getContext(), morSourceHostDc);
57565734
if (targetHyperHost != null) {
5757-
morTargetHostDc = targetHyperHost.getHyperHostDatacenter();
5735+
ManagedObjectReference morTargetHostDc = targetHyperHost.getHyperHostDatacenter();
57585736
if (!morSourceHostDc.getValue().equalsIgnoreCase(morTargetHostDc.getValue())) {
57595737
String msg = "VM " + vmName + " cannot be migrated between different datacenter";
57605738
throw new CloudRuntimeException(msg);
@@ -5845,12 +5823,12 @@ private List<VolumeObjectTO> relocateVirtualMachine(final VmwareHypervisorHost h
58455823
}
58465824

58475825
// Specific section for MigrateVmWithStorageCommand
5848-
if (vmTo != null) {
5826+
if (vmTo != null && targetHyperHost != null) {
58495827
// Prepare network at target before migration
58505828
NicTO[] nics = vmTo.getNics();
58515829
for (NicTO nic : nics) {
58525830
// prepare network on the host
5853-
prepareNetworkFromNicInfo(new HostMO(getServiceContext(), morTargetHost), nic, false, vmTo.getType());
5831+
prepareNetworkFromNicInfo((HostMO)targetHyperHost, nic, false, vmTo.getType());
58545832
}
58555833
// Ensure secondary storage mounted on target host
58565834
VmwareManager mgr = targetHyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);

plugins/hypervisors/vmware/src/org/apache/cloudstack/storage/motion/VmwareStorageMotionStrategy.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,22 +190,23 @@ public void copyAsync(DataObject srcData, DataObject destData, Host destHost, As
190190
ScopeType targetScopeType = destData.getDataStore().getScope().getScopeType();
191191
Long hostId = null;
192192
String hostGuidInTargetCluster = null;
193-
// Find Volume source cluster and select any Vmware hypervisor host to attach worker VM
194-
if (ScopeType.CLUSTER.equals(sourceScopeType) && ScopeType.CLUSTER.equals(targetScopeType) && !sourcePool.getClusterId().equals(targetPool.getClusterId())) {
195-
// Without host vMotion might fail between non-shared storages with error similar to,
196-
// https://kb.vmware.com/s/article/1003795
197-
List<HostVO> hosts = hostDao.findHypervisorHostInCluster(targetPool.getClusterId());
198-
if (CollectionUtils.isNotEmpty(hosts)) {
199-
hostGuidInTargetCluster = hosts.get(0).getGuid();
200-
}
201-
if (hostGuidInTargetCluster == null) {
202-
throw new CloudRuntimeException("Offline Migration failed, unable to find suitable target host for worker VM placement while migrating between storage pools of different cluster without shared storages");
203-
}
204-
} else if (ScopeType.CLUSTER.equals(sourceScopeType)) {
193+
if (ScopeType.CLUSTER.equals(sourceScopeType)) {
194+
// Find Volume source cluster and select any Vmware hypervisor host to attach worker VM
205195
hostId = findSuitableHostIdForWorkerVmPlacement(sourcePool.getClusterId());
206196
if (hostId == null) {
207197
throw new CloudRuntimeException("Offline Migration failed, unable to find suitable host for worker VM placement in the cluster of storage pool: " + sourcePool.getName());
208198
}
199+
if (ScopeType.CLUSTER.equals(targetScopeType) && !sourcePool.getClusterId().equals(targetPool.getClusterId())) {
200+
// Without host vMotion might fail between non-shared storages with error similar to,
201+
// https://kb.vmware.com/s/article/1003795
202+
List<HostVO> hosts = hostDao.findHypervisorHostInCluster(targetPool.getClusterId());
203+
if (CollectionUtils.isNotEmpty(hosts)) {
204+
hostGuidInTargetCluster = hosts.get(0).getGuid();
205+
}
206+
if (hostGuidInTargetCluster == null) {
207+
throw new CloudRuntimeException("Offline Migration failed, unable to find suitable target host for worker VM placement while migrating between storage pools of different cluster without shared storages");
208+
}
209+
}
209210
} else if (ScopeType.CLUSTER.equals(targetScopeType)) {
210211
hostId = findSuitableHostIdForWorkerVmPlacement(targetPool.getClusterId());
211212
if (hostId == null) {

vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareHelper.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,4 +797,18 @@ public static boolean isControllerOsRecommended(String dataDiskController) {
797797
return DiskControllerType.getType(dataDiskController) == DiskControllerType.osdefault;
798798
}
799799

800+
public static HostMO getHostMOFromHostName(final VmwareContext context, final String hostName) {
801+
HostMO host = null;
802+
if (com.cloud.utils.StringUtils.isNotBlank(hostName) && hostName.contains("@")) {
803+
String hostMorInfo = hostName.split("@")[0];
804+
if (hostMorInfo.contains(":")) {
805+
ManagedObjectReference morHost = new ManagedObjectReference();
806+
morHost.setType(hostMorInfo.split(":")[0]);
807+
morHost.setValue(hostMorInfo.split(":")[1]);
808+
host = new HostMO(context, morHost);
809+
}
810+
}
811+
return host;
812+
}
813+
800814
}

0 commit comments

Comments
 (0)