@@ -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 );
0 commit comments