Skip to content

Commit e7030a7

Browse files
committed
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 8dfd125 commit e7030a7

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
@@ -3826,7 +3826,6 @@ protected Answer execute(MigrateVmToPoolCommand cmd) {
38263826

38273827
private Answer migrateAndAnswer(VirtualMachineMO vmMo, String poolUuid, VmwareHypervisorHost hyperHost, Command cmd) throws Exception {
38283828
String hostNameInTargetCluster = null;
3829-
VmwareHypervisorHost hostInTargetCluster = null;
38303829
List<Pair<VolumeTO, StorageFilerTO>> volToFiler = new ArrayList<>();
38313830
if (cmd instanceof MigrateVmToPoolCommand) {
38323831
MigrateVmToPoolCommand mcmd = (MigrateVmToPoolCommand)cmd;
@@ -3835,13 +3834,7 @@ private Answer migrateAndAnswer(VirtualMachineMO vmMo, String poolUuid, VmwareHy
38353834
} else if (cmd instanceof MigrateVolumeCommand) {
38363835
hostNameInTargetCluster = ((MigrateVolumeCommand)cmd).getHostGuidInTargetCluster();
38373836
}
3838-
if (StringUtils.isNotBlank(hostNameInTargetCluster)) {
3839-
String hostInTargetClusterMorInfo = hostNameInTargetCluster.split("@")[0];
3840-
ManagedObjectReference morHostInTargetCluster = new ManagedObjectReference();
3841-
morHostInTargetCluster.setType(hostInTargetClusterMorInfo.split(":")[0]);
3842-
morHostInTargetCluster.setValue(hostInTargetClusterMorInfo.split(":")[1]);
3843-
hostInTargetCluster = new HostMO(getServiceContext(), morHostInTargetCluster);
3844-
}
3837+
VmwareHypervisorHost hostInTargetCluster = VmwareHelper.getHostMOFromHostName(getServiceContext(), hostNameInTargetCluster);
38453838
try {
38463839
// OfflineVmwareMigration: getVolumesFromCommand(cmd);
38473840
Map<Integer, Long> volumeDeviceKey = new HashMap<>();
@@ -4020,19 +4013,11 @@ private Answer migrateVolume(MigrateVolumeCommand cmd) {
40204013

40214014
ManagedObjectReference morSourceDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, cmd.getSourcePool().getUuid());
40224015
DatastoreMO sourceDsMo = new DatastoreMO(hyperHost.getContext(), morSourceDs);
4023-
DatacenterMO dcMo = new DatacenterMO(hyperHost.getContext(), hyperHost.getHyperHostDatacenter());
40244016
String targetDsName = cmd.getTargetPool().getUuid();
4025-
String hostNameInTargetCluster = cmd.getHostGuidInTargetCluster();
4026-
VmwareHypervisorHost hostInTargetCluster = null;
4027-
if (StringUtils.isNotBlank(hostNameInTargetCluster)) {
4028-
String hostInTargetClusterMorInfo = hostNameInTargetCluster.split("@")[0];
4029-
ManagedObjectReference morHostInTargetCluster = new ManagedObjectReference();
4030-
morHostInTargetCluster.setType(hostInTargetClusterMorInfo.split(":")[0]);
4031-
morHostInTargetCluster.setValue(hostInTargetClusterMorInfo.split(":")[1]);
4032-
hostInTargetCluster = new HostMO(getServiceContext(), morHostInTargetCluster);
4033-
}
4017+
VmwareHypervisorHost hostInTargetCluster = VmwareHelper.getHostMOFromHostName(getServiceContext(),
4018+
cmd.getHostGuidInTargetCluster());
40344019
VmwareHypervisorHost dsHost = hostInTargetCluster == null ? hyperHost : hostInTargetCluster;
4035-
ManagedObjectReference morTargetDS = getTargetDatastoreMOReference(targetDsName, hyperHost);
4020+
ManagedObjectReference morTargetDS = getTargetDatastoreMOReference(targetDsName, dsHost);
40364021
if(morTargetDS == null) {
40374022
String msg = "Unable to find the target datastore: " + targetDsName + " on host: " + dsHost.getHyperHostName();
40384023
s_logger.error(msg);
@@ -6291,30 +6276,23 @@ private List<VolumeObjectTO> relocateVirtualMachine(final VmwareHypervisorHost h
62916276
VmwareHypervisorHost targetHyperHost = hostInTargetCluster;
62926277
VirtualMachineMO vmMo = null;
62936278
ManagedObjectReference morSourceHostDc = null;
6294-
ManagedObjectReference morTargetHostDc = null;
6295-
ManagedObjectReference morTargetHost = new ManagedObjectReference();
62966279
VirtualMachineRelocateSpec relocateSpec = new VirtualMachineRelocateSpec();
62976280
List<VirtualMachineRelocateSpecDiskLocator> diskLocators = new ArrayList<VirtualMachineRelocateSpecDiskLocator>();
62986281
Set<String> mountedDatastoresAtSource = new HashSet<String>();
62996282
List<VolumeObjectTO> volumeToList = new ArrayList<>();
63006283
Map<Long, Integer> volumeDeviceKey = new HashMap<Long, Integer>();
6301-
if (StringUtils.isNotBlank(targetHost)) {
6302-
String targetHostMorInfo = targetHost.split("@")[0];
6303-
morTargetHost.setType(targetHostMorInfo.split(":")[0]);
6304-
morTargetHost.setValue(targetHostMorInfo.split(":")[1]);
6305-
}
63066284

63076285
try {
63086286
if (sourceHyperHost == null) {
63096287
sourceHyperHost = getHyperHost(getServiceContext());
63106288
}
63116289
if (targetHyperHost == null && StringUtils.isNotBlank(targetHost)) {
6312-
targetHyperHost = new HostMO(getServiceContext(), morTargetHost);
6290+
targetHyperHost = VmwareHelper.getHostMOFromHostName(getServiceContext(), targetHost);
63136291
}
63146292
morSourceHostDc = sourceHyperHost.getHyperHostDatacenter();
63156293
DatacenterMO dcMo = new DatacenterMO(sourceHyperHost.getContext(), morSourceHostDc);
63166294
if (targetHyperHost != null) {
6317-
morTargetHostDc = targetHyperHost.getHyperHostDatacenter();
6295+
ManagedObjectReference morTargetHostDc = targetHyperHost.getHyperHostDatacenter();
63186296
if (!morSourceHostDc.getValue().equalsIgnoreCase(morTargetHostDc.getValue())) {
63196297
String msg = "VM " + vmName + " cannot be migrated between different datacenter";
63206298
throw new CloudRuntimeException(msg);
@@ -6405,12 +6383,12 @@ private List<VolumeObjectTO> relocateVirtualMachine(final VmwareHypervisorHost h
64056383
}
64066384

64076385
// Specific section for MigrateVmWithStorageCommand
6408-
if (vmTo != null) {
6386+
if (vmTo != null && targetHyperHost != null) {
64096387
// Prepare network at target before migration
64106388
NicTO[] nics = vmTo.getNics();
64116389
for (NicTO nic : nics) {
64126390
// prepare network on the host
6413-
prepareNetworkFromNicInfo(new HostMO(getServiceContext(), morTargetHost), nic, false, vmTo.getType());
6391+
prepareNetworkFromNicInfo((HostMO)targetHyperHost, nic, false, vmTo.getType());
64146392
}
64156393
// Ensure secondary storage mounted on target host
64166394
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
@@ -773,4 +773,18 @@ public static boolean isControllerOsRecommended(String dataDiskController) {
773773
return DiskControllerType.getType(dataDiskController) == DiskControllerType.osdefault;
774774
}
775775

776+
public static HostMO getHostMOFromHostName(final VmwareContext context, final String hostName) {
777+
HostMO host = null;
778+
if (com.cloud.utils.StringUtils.isNotBlank(hostName) && hostName.contains("@")) {
779+
String hostMorInfo = hostName.split("@")[0];
780+
if (hostMorInfo.contains(":")) {
781+
ManagedObjectReference morHost = new ManagedObjectReference();
782+
morHost.setType(hostMorInfo.split(":")[0]);
783+
morHost.setValue(hostMorInfo.split(":")[1]);
784+
host = new HostMO(context, morHost);
785+
}
786+
}
787+
return host;
788+
}
789+
776790
}

0 commit comments

Comments
 (0)