Skip to content

Commit adc836c

Browse files
author
Likitha Shetty
committed
CLOUDSTACK-8318. Storage vMotion support for VMFS.
MigrateVMWithVolumes- 1. If ESXi host version is below 5.1, ensure destination datastore(s) is mounted on the source host, then migrate the storage and then finally migrate the VM. If destination storage(s) is not mounted on the source host, - In case of NFS storage mount the storage(s). - In case of VMFS storage fail the request for migration. 2. If EXi host version is 5.1 or above, simultaneously migrate the VM and its storage to the destination host and storage(s) respectively for both NFS and VMFS storage.
1 parent c27c694 commit adc836c

2 files changed

Lines changed: 94 additions & 43 deletions

File tree

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

Lines changed: 74 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3018,15 +3018,16 @@ protected Answer execute(MigrateWithStorageCommand cmd) {
30183018
ManagedObjectReference morDc = null;
30193019
ManagedObjectReference morDcOfTargetHost = null;
30203020
ManagedObjectReference morTgtHost = new ManagedObjectReference();
3021+
ManagedObjectReference morTgtDatastore = new ManagedObjectReference();
30213022
VirtualMachineRelocateSpec relocateSpec = new VirtualMachineRelocateSpec();
30223023
List<VirtualMachineRelocateSpecDiskLocator> diskLocators = new ArrayList<VirtualMachineRelocateSpecDiskLocator>();
30233024
VirtualMachineRelocateSpecDiskLocator diskLocator = null;
30243025

30253026
boolean isFirstDs = true;
30263027
String tgtDsName = "";
3027-
String tgtDsNfsHost;
3028-
String tgtDsNfsPath;
3029-
int tgtDsNfsPort;
3028+
String tgtDsHost;
3029+
String tgtDsPath;
3030+
int tgtDsPort;
30303031
VolumeTO volume;
30313032
StorageFilerTO filerTo;
30323033
Set<String> mountedDatastoresAtSource = new HashSet<String>();
@@ -3049,6 +3050,7 @@ protected Answer execute(MigrateWithStorageCommand cmd) {
30493050
throw new CloudRuntimeException(msg);
30503051
}
30513052
VmwareManager mgr = tgtHyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
3053+
String srcHostApiVersion = ((HostMO)srcHyperHost).getHostAboutInfo().getApiVersion();
30523054

30533055
// find VM through datacenter (VM is not at the target host yet)
30543056
vmMo = srcHyperHost.findVmOnPeerHyperHost(vmName);
@@ -3059,47 +3061,68 @@ protected Answer execute(MigrateWithStorageCommand cmd) {
30593061
}
30603062
vmName = vmMo.getName();
30613063

3062-
// Get details of each target datastore & attach to source host.
3064+
// Specify destination datastore location for each volume
30633065
for (Entry<VolumeTO, StorageFilerTO> entry : volToFiler.entrySet()) {
30643066
volume = entry.getKey();
30653067
filerTo = entry.getValue();
30663068

3067-
tgtDsName = filerTo.getUuid().replace("-", "");
3068-
tgtDsNfsHost = filerTo.getHost();
3069-
tgtDsNfsPath = filerTo.getPath();
3070-
tgtDsNfsPort = filerTo.getPort();
3071-
30723069
s_logger.debug("Preparing spec for volume : " + volume.getName());
30733070
morDsAtTarget = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(tgtHyperHost, filerTo.getUuid());
3071+
morDsAtSource = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(srcHyperHost, filerTo.getUuid());
30743072
if (morDsAtTarget == null) {
30753073
String msg = "Unable to find the mounted datastore with uuid " + morDsAtTarget + " to execute MigrateWithStorageCommand";
30763074
s_logger.error(msg);
30773075
throw new Exception(msg);
30783076
}
3079-
morDsAtSource = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(srcHyperHost, filerTo.getUuid());
3080-
if (morDsAtSource == null) {
3081-
morDsAtSource = srcHyperHost.mountDatastore(false, tgtDsNfsHost, tgtDsNfsPort, tgtDsNfsPath, tgtDsName);
3082-
if (morDsAtSource == null) {
3083-
throw new Exception("Unable to mount datastore " + tgtDsNfsHost + ":/" + tgtDsNfsPath + " on " + _hostName);
3077+
morTgtDatastore = morDsAtTarget;
3078+
3079+
// If host version is below 5.1 then simultaneous change of VM's datastore and host is not supported.
3080+
// So since only the datastore will be changed first, ensure the target datastore is mounted on source host.
3081+
if (srcHostApiVersion.compareTo("5.1") < 0) {
3082+
tgtDsName = filerTo.getUuid().replace("-", "");
3083+
tgtDsHost = filerTo.getHost();
3084+
tgtDsPath = filerTo.getPath();
3085+
tgtDsPort = filerTo.getPort();
3086+
3087+
// If datastore is NFS and target datastore is not already mounted on source host then mount the datastore.
3088+
if (filerTo.getType().equals(StoragePoolType.NetworkFilesystem)) {
3089+
if (morDsAtSource == null) {
3090+
morDsAtSource = srcHyperHost.mountDatastore(false, tgtDsHost, tgtDsPort, tgtDsPath, tgtDsName);
3091+
if (morDsAtSource == null) {
3092+
throw new Exception("Unable to mount NFS datastore " + tgtDsHost + ":/" + tgtDsPath + " on " + _hostName);
3093+
}
3094+
mountedDatastoresAtSource.add(tgtDsName);
3095+
s_logger.debug("Mounted datastore " + tgtDsHost + ":/" + tgtDsPath + " on " + _hostName);
3096+
}
30843097
}
3085-
mountedDatastoresAtSource.add(tgtDsName);
3086-
s_logger.debug("Mounted datastore " + tgtDsNfsHost + ":/" + tgtDsNfsPath + " on " + _hostName);
3098+
3099+
// If datastore is VMFS and target datastore is not mounted or accessible to source host then fail migration.
3100+
if (filerTo.getType().equals(StoragePoolType.VMFS)) {
3101+
if (morDsAtSource == null) {
3102+
s_logger.warn("If host version is below 5.1, then target VMFS datastore(s) need to manually mounted on source host for a successful live storage migration.");
3103+
throw new Exception("Target VMFS datastore: " + tgtDsPath + " is not mounted on source host: " + _hostName);
3104+
}
3105+
DatastoreMO dsAtSourceMo = new DatastoreMO(getServiceContext(), morDsAtSource);
3106+
String srcHostValue = srcHyperHost.getMor().getValue();
3107+
if(!dsAtSourceMo.isAccessibleToHost(srcHostValue)) {
3108+
s_logger.warn("If host version is below 5.1, then target VMFS datastore(s) need to accessible to source host for a successful live storage migration.");
3109+
throw new Exception("Target VMFS datastore: " + tgtDsPath + " is not accessible on source host: " + _hostName);
3110+
}
3111+
}
3112+
morTgtDatastore = morDsAtSource;
30873113
}
30883114

30893115
if (isFirstDs) {
3090-
relocateSpec.setDatastore(morDsAtSource);
3116+
relocateSpec.setDatastore(morTgtDatastore);
30913117
isFirstDs = false;
30923118
}
3093-
VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(new DatastoreMO(srcHyperHost.getContext(), morDsAtSource), vmName, volume.getPath() +
3094-
".vmdk");
30953119
diskLocator = new VirtualMachineRelocateSpecDiskLocator();
3096-
diskLocator.setDatastore(morDsAtSource);
3120+
diskLocator.setDatastore(morTgtDatastore);
30973121
int diskId = getVirtualDiskInfo(vmMo, volume.getPath() + ".vmdk");
30983122
diskLocator.setDiskId(diskId);
30993123

31003124
diskLocators.add(diskLocator);
31013125
volumeDeviceKey.put(volume.getId(), diskId);
3102-
31033126
}
31043127
relocateSpec.getDisk().addAll(diskLocators);
31053128

@@ -3123,23 +3146,39 @@ protected Answer execute(MigrateWithStorageCommand cmd) {
31233146
throw new Exception(msg);
31243147
}
31253148

3126-
// Change datastore
3127-
if (!vmMo.changeDatastore(relocateSpec)) {
3128-
throw new Exception("Change datastore operation failed during storage migration");
3149+
if (srcHostApiVersion.compareTo("5.1") < 0) {
3150+
// Migrate VM's volumes to target datastore(s).
3151+
if (!vmMo.changeDatastore(relocateSpec)) {
3152+
throw new Exception("Change datastore operation failed during storage migration");
3153+
} else {
3154+
s_logger.debug("Successfully migrated storage of VM " + vmName + " to target datastore(s)");
3155+
}
3156+
3157+
// Migrate VM to target host.
3158+
ManagedObjectReference morPool = tgtHyperHost.getHyperHostOwnerResourcePool();
3159+
if (!vmMo.migrate(morPool, tgtHyperHost.getMor())) {
3160+
throw new Exception("VM migration to target host failed during storage migration");
3161+
} else {
3162+
s_logger.debug("Successfully migrated VM " + vmName + " from " + _hostName + " to " + tgtHyperHost.getHyperHostName());
3163+
}
31293164
} else {
3130-
s_logger.debug("Successfully migrated storage of VM " + vmName + " to target datastore(s)");
3165+
// Simultaneously migrate VM's volumes to target datastore and VM to target host.
3166+
relocateSpec.setHost(tgtHyperHost.getMor());
3167+
relocateSpec.setPool(tgtHyperHost.getHyperHostOwnerResourcePool());
3168+
if (!vmMo.changeDatastore(relocateSpec)) {
3169+
throw new Exception("Change datastore operation failed during storage migration");
3170+
} else {
3171+
s_logger.debug("Successfully migrated VM " + vmName + " from " + _hostName + " to " + tgtHyperHost.getHyperHostName() +
3172+
" and its storage to target datastore(s)");
3173+
}
31313174
}
31323175

31333176
// Consolidate VM disks.
3134-
// In case of a linked clone VM, if VM's disks are not consolidated,
3135-
// further VM operations such as volume snapshot, VM snapshot etc. will result in DB inconsistencies.
3136-
String apiVersion = HypervisorHostHelper.getVcenterApiVersion(vmMo.getContext());
3137-
if (apiVersion.compareTo("5.0") >= 0) {
3138-
if (!vmMo.consolidateVmDisks()) {
3139-
s_logger.warn("VM disk consolidation failed after storage migration. Yet proceeding with VM migration.");
3140-
} else {
3141-
s_logger.debug("Successfully consolidated disks of VM " + vmName + ".");
3142-
}
3177+
// In case of a linked clone VM, if VM's disks are not consolidated, further VM operations such as volume snapshot, VM snapshot etc. will result in DB inconsistencies.
3178+
if (!vmMo.consolidateVmDisks()) {
3179+
s_logger.warn("VM disk consolidation failed after storage migration. Yet proceeding with VM migration.");
3180+
} else {
3181+
s_logger.debug("Successfully consolidated disks of VM " + vmName + ".");
31433182
}
31443183

31453184
// Update and return volume path for every disk because that could have changed after migration
@@ -3158,14 +3197,6 @@ protected Answer execute(MigrateWithStorageCommand cmd) {
31583197
}
31593198
}
31603199

3161-
// Change host
3162-
ManagedObjectReference morPool = tgtHyperHost.getHyperHostOwnerResourcePool();
3163-
if (!vmMo.migrate(morPool, tgtHyperHost.getMor())) {
3164-
throw new Exception("Change datastore operation failed during storage migration");
3165-
} else {
3166-
s_logger.debug("Successfully relocated VM " + vmName + " from " + _hostName + " to " + tgtHyperHost.getHyperHostName());
3167-
}
3168-
31693200
return new MigrateWithStorageAnswer(cmd, volumeToList);
31703201
} catch (Throwable e) {
31713202
if (e instanceof RemoteException) {
@@ -3215,7 +3246,7 @@ private Answer execute(MigrateVolumeCommand cmd) {
32153246
try {
32163247
srcHyperHost = getHyperHost(getServiceContext());
32173248
morDc = srcHyperHost.getHyperHostDatacenter();
3218-
tgtDsName = poolTo.getUuid().replace("-", "");
3249+
tgtDsName = poolTo.getUuid();
32193250

32203251
// find VM in this datacenter not just in this cluster.
32213252
DatacenterMO dcMo = new DatacenterMO(getServiceContext(), morDc);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121

2222
import org.apache.log4j.Logger;
2323

24+
import com.vmware.vim25.DatastoreHostMount;
2425
import com.vmware.vim25.DatastoreSummary;
2526
import com.vmware.vim25.FileInfo;
2627
import com.vmware.vim25.HostDatastoreBrowserSearchResults;
28+
import com.vmware.vim25.HostMountInfo;
2729
import com.vmware.vim25.ManagedObjectReference;
2830
import com.vmware.vim25.ObjectContent;
2931
import com.vmware.vim25.ObjectSpec;
@@ -66,6 +68,10 @@ public HostDatastoreBrowserMO getHostDatastoreBrowserMO() throws Exception {
6668
return new HostDatastoreBrowserMO(_context, (ManagedObjectReference)_context.getVimClient().getDynamicProperty(_mor, "browser"));
6769
}
6870

71+
public List<DatastoreHostMount> getHostMounts() throws Exception {
72+
return _context.getVimClient().getDynamicProperty(_mor, "host");
73+
}
74+
6975
public String getInventoryPath() throws Exception {
7076
Pair<DatacenterMO, String> dcInfo = getOwnerDatacenter();
7177
return dcInfo.second() + "/" + getName();
@@ -380,4 +386,18 @@ public String searchFileInSubFolders(String fileName, boolean caseInsensitive) t
380386
}
381387
return absoluteFileName;
382388
}
389+
390+
public boolean isAccessibleToHost(String hostValue) throws Exception {
391+
boolean isAccessible = true;
392+
List<DatastoreHostMount> hostMounts = getHostMounts();
393+
for (DatastoreHostMount hostMount : hostMounts) {
394+
String hostMountValue = hostMount.getKey().getValue();
395+
if (hostMountValue.equalsIgnoreCase(hostValue)) {
396+
HostMountInfo mountInfo = hostMount.getMountInfo();
397+
isAccessible = mountInfo.isAccessible();
398+
break;
399+
}
400+
}
401+
return isAccessible;
402+
}
383403
}

0 commit comments

Comments
 (0)