Skip to content

Commit 89dbd1e

Browse files
Likitha Shettyyadvr
authored andcommitted
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. (cherry picked from commit adc836c) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent 9565ad2 commit 89dbd1e

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
@@ -3024,15 +3024,16 @@ protected Answer execute(MigrateWithStorageCommand cmd) {
30243024
ManagedObjectReference morDc = null;
30253025
ManagedObjectReference morDcOfTargetHost = null;
30263026
ManagedObjectReference morTgtHost = new ManagedObjectReference();
3027+
ManagedObjectReference morTgtDatastore = new ManagedObjectReference();
30273028
VirtualMachineRelocateSpec relocateSpec = new VirtualMachineRelocateSpec();
30283029
List<VirtualMachineRelocateSpecDiskLocator> diskLocators = new ArrayList<VirtualMachineRelocateSpecDiskLocator>();
30293030
VirtualMachineRelocateSpecDiskLocator diskLocator = null;
30303031

30313032
boolean isFirstDs = true;
30323033
String tgtDsName = "";
3033-
String tgtDsNfsHost;
3034-
String tgtDsNfsPath;
3035-
int tgtDsNfsPort;
3034+
String tgtDsHost;
3035+
String tgtDsPath;
3036+
int tgtDsPort;
30363037
VolumeTO volume;
30373038
StorageFilerTO filerTo;
30383039
Set<String> mountedDatastoresAtSource = new HashSet<String>();
@@ -3055,6 +3056,7 @@ protected Answer execute(MigrateWithStorageCommand cmd) {
30553056
throw new CloudRuntimeException(msg);
30563057
}
30573058
VmwareManager mgr = tgtHyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
3059+
String srcHostApiVersion = ((HostMO)srcHyperHost).getHostAboutInfo().getApiVersion();
30583060

30593061
// find VM through datacenter (VM is not at the target host yet)
30603062
vmMo = srcHyperHost.findVmOnPeerHyperHost(vmName);
@@ -3065,47 +3067,68 @@ protected Answer execute(MigrateWithStorageCommand cmd) {
30653067
}
30663068
vmName = vmMo.getName();
30673069

3068-
// Get details of each target datastore & attach to source host.
3070+
// Specify destination datastore location for each volume
30693071
for (Entry<VolumeTO, StorageFilerTO> entry : volToFiler.entrySet()) {
30703072
volume = entry.getKey();
30713073
filerTo = entry.getValue();
30723074

3073-
tgtDsName = filerTo.getUuid().replace("-", "");
3074-
tgtDsNfsHost = filerTo.getHost();
3075-
tgtDsNfsPath = filerTo.getPath();
3076-
tgtDsNfsPort = filerTo.getPort();
3077-
30783075
s_logger.debug("Preparing spec for volume : " + volume.getName());
30793076
morDsAtTarget = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(tgtHyperHost, filerTo.getUuid());
3077+
morDsAtSource = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(srcHyperHost, filerTo.getUuid());
30803078
if (morDsAtTarget == null) {
30813079
String msg = "Unable to find the mounted datastore with uuid " + morDsAtTarget + " to execute MigrateWithStorageCommand";
30823080
s_logger.error(msg);
30833081
throw new Exception(msg);
30843082
}
3085-
morDsAtSource = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(srcHyperHost, filerTo.getUuid());
3086-
if (morDsAtSource == null) {
3087-
morDsAtSource = srcHyperHost.mountDatastore(false, tgtDsNfsHost, tgtDsNfsPort, tgtDsNfsPath, tgtDsName);
3088-
if (morDsAtSource == null) {
3089-
throw new Exception("Unable to mount datastore " + tgtDsNfsHost + ":/" + tgtDsNfsPath + " on " + _hostName);
3083+
morTgtDatastore = morDsAtTarget;
3084+
3085+
// If host version is below 5.1 then simultaneous change of VM's datastore and host is not supported.
3086+
// So since only the datastore will be changed first, ensure the target datastore is mounted on source host.
3087+
if (srcHostApiVersion.compareTo("5.1") < 0) {
3088+
tgtDsName = filerTo.getUuid().replace("-", "");
3089+
tgtDsHost = filerTo.getHost();
3090+
tgtDsPath = filerTo.getPath();
3091+
tgtDsPort = filerTo.getPort();
3092+
3093+
// If datastore is NFS and target datastore is not already mounted on source host then mount the datastore.
3094+
if (filerTo.getType().equals(StoragePoolType.NetworkFilesystem)) {
3095+
if (morDsAtSource == null) {
3096+
morDsAtSource = srcHyperHost.mountDatastore(false, tgtDsHost, tgtDsPort, tgtDsPath, tgtDsName);
3097+
if (morDsAtSource == null) {
3098+
throw new Exception("Unable to mount NFS datastore " + tgtDsHost + ":/" + tgtDsPath + " on " + _hostName);
3099+
}
3100+
mountedDatastoresAtSource.add(tgtDsName);
3101+
s_logger.debug("Mounted datastore " + tgtDsHost + ":/" + tgtDsPath + " on " + _hostName);
3102+
}
30903103
}
3091-
mountedDatastoresAtSource.add(tgtDsName);
3092-
s_logger.debug("Mounted datastore " + tgtDsNfsHost + ":/" + tgtDsNfsPath + " on " + _hostName);
3104+
3105+
// If datastore is VMFS and target datastore is not mounted or accessible to source host then fail migration.
3106+
if (filerTo.getType().equals(StoragePoolType.VMFS)) {
3107+
if (morDsAtSource == null) {
3108+
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.");
3109+
throw new Exception("Target VMFS datastore: " + tgtDsPath + " is not mounted on source host: " + _hostName);
3110+
}
3111+
DatastoreMO dsAtSourceMo = new DatastoreMO(getServiceContext(), morDsAtSource);
3112+
String srcHostValue = srcHyperHost.getMor().getValue();
3113+
if(!dsAtSourceMo.isAccessibleToHost(srcHostValue)) {
3114+
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.");
3115+
throw new Exception("Target VMFS datastore: " + tgtDsPath + " is not accessible on source host: " + _hostName);
3116+
}
3117+
}
3118+
morTgtDatastore = morDsAtSource;
30933119
}
30943120

30953121
if (isFirstDs) {
3096-
relocateSpec.setDatastore(morDsAtSource);
3122+
relocateSpec.setDatastore(morTgtDatastore);
30973123
isFirstDs = false;
30983124
}
3099-
VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(new DatastoreMO(srcHyperHost.getContext(), morDsAtSource), vmName, volume.getPath() +
3100-
".vmdk");
31013125
diskLocator = new VirtualMachineRelocateSpecDiskLocator();
3102-
diskLocator.setDatastore(morDsAtSource);
3126+
diskLocator.setDatastore(morTgtDatastore);
31033127
int diskId = getVirtualDiskInfo(vmMo, volume.getPath() + ".vmdk");
31043128
diskLocator.setDiskId(diskId);
31053129

31063130
diskLocators.add(diskLocator);
31073131
volumeDeviceKey.put(volume.getId(), diskId);
3108-
31093132
}
31103133
relocateSpec.getDisk().addAll(diskLocators);
31113134

@@ -3129,23 +3152,39 @@ protected Answer execute(MigrateWithStorageCommand cmd) {
31293152
throw new Exception(msg);
31303153
}
31313154

3132-
// Change datastore
3133-
if (!vmMo.changeDatastore(relocateSpec)) {
3134-
throw new Exception("Change datastore operation failed during storage migration");
3155+
if (srcHostApiVersion.compareTo("5.1") < 0) {
3156+
// Migrate VM's volumes to target datastore(s).
3157+
if (!vmMo.changeDatastore(relocateSpec)) {
3158+
throw new Exception("Change datastore operation failed during storage migration");
3159+
} else {
3160+
s_logger.debug("Successfully migrated storage of VM " + vmName + " to target datastore(s)");
3161+
}
3162+
3163+
// Migrate VM to target host.
3164+
ManagedObjectReference morPool = tgtHyperHost.getHyperHostOwnerResourcePool();
3165+
if (!vmMo.migrate(morPool, tgtHyperHost.getMor())) {
3166+
throw new Exception("VM migration to target host failed during storage migration");
3167+
} else {
3168+
s_logger.debug("Successfully migrated VM " + vmName + " from " + _hostName + " to " + tgtHyperHost.getHyperHostName());
3169+
}
31353170
} else {
3136-
s_logger.debug("Successfully migrated storage of VM " + vmName + " to target datastore(s)");
3171+
// Simultaneously migrate VM's volumes to target datastore and VM to target host.
3172+
relocateSpec.setHost(tgtHyperHost.getMor());
3173+
relocateSpec.setPool(tgtHyperHost.getHyperHostOwnerResourcePool());
3174+
if (!vmMo.changeDatastore(relocateSpec)) {
3175+
throw new Exception("Change datastore operation failed during storage migration");
3176+
} else {
3177+
s_logger.debug("Successfully migrated VM " + vmName + " from " + _hostName + " to " + tgtHyperHost.getHyperHostName() +
3178+
" and its storage to target datastore(s)");
3179+
}
31373180
}
31383181

31393182
// Consolidate VM disks.
3140-
// In case of a linked clone VM, if VM's disks are not consolidated,
3141-
// further VM operations such as volume snapshot, VM snapshot etc. will result in DB inconsistencies.
3142-
String apiVersion = HypervisorHostHelper.getVcenterApiVersion(vmMo.getContext());
3143-
if (apiVersion.compareTo("5.0") >= 0) {
3144-
if (!vmMo.consolidateVmDisks()) {
3145-
s_logger.warn("VM disk consolidation failed after storage migration. Yet proceeding with VM migration.");
3146-
} else {
3147-
s_logger.debug("Successfully consolidated disks of VM " + vmName + ".");
3148-
}
3183+
// 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.
3184+
if (!vmMo.consolidateVmDisks()) {
3185+
s_logger.warn("VM disk consolidation failed after storage migration. Yet proceeding with VM migration.");
3186+
} else {
3187+
s_logger.debug("Successfully consolidated disks of VM " + vmName + ".");
31493188
}
31503189

31513190
// Update and return volume path for every disk because that could have changed after migration
@@ -3164,14 +3203,6 @@ protected Answer execute(MigrateWithStorageCommand cmd) {
31643203
}
31653204
}
31663205

3167-
// Change host
3168-
ManagedObjectReference morPool = tgtHyperHost.getHyperHostOwnerResourcePool();
3169-
if (!vmMo.migrate(morPool, tgtHyperHost.getMor())) {
3170-
throw new Exception("Change datastore operation failed during storage migration");
3171-
} else {
3172-
s_logger.debug("Successfully relocated VM " + vmName + " from " + _hostName + " to " + tgtHyperHost.getHyperHostName());
3173-
}
3174-
31753206
return new MigrateWithStorageAnswer(cmd, volumeToList);
31763207
} catch (Throwable e) {
31773208
if (e instanceof RemoteException) {
@@ -3221,7 +3252,7 @@ private Answer execute(MigrateVolumeCommand cmd) {
32213252
try {
32223253
srcHyperHost = getHyperHost(getServiceContext());
32233254
morDc = srcHyperHost.getHyperHostDatacenter();
3224-
tgtDsName = poolTo.getUuid().replace("-", "");
3255+
tgtDsName = poolTo.getUuid();
32253256

32263257
// find VM in this datacenter not just in this cluster.
32273258
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)