@@ -3023,7 +3023,6 @@ protected Answer execute(MigrateWithStorageCommand cmd) {
30233023 List <VirtualMachineRelocateSpecDiskLocator > diskLocators = new ArrayList <VirtualMachineRelocateSpecDiskLocator >();
30243024 VirtualMachineRelocateSpecDiskLocator diskLocator = null ;
30253025
3026- boolean isFirstDs = true ;
30273026 String tgtDsName = "" ;
30283027 String tgtDsHost ;
30293028 String tgtDsPath ;
@@ -3112,9 +3111,8 @@ protected Answer execute(MigrateWithStorageCommand cmd) {
31123111 morTgtDatastore = morDsAtSource ;
31133112 }
31143113
3115- if (isFirstDs ) {
3114+ if (volume . getType () == Volume . Type . ROOT ) {
31163115 relocateSpec .setDatastore (morTgtDatastore );
3117- isFirstDs = false ;
31183116 }
31193117 diskLocator = new VirtualMachineRelocateSpecDiskLocator ();
31203118 diskLocator .setDatastore (morTgtDatastore );
@@ -3124,6 +3122,18 @@ protected Answer execute(MigrateWithStorageCommand cmd) {
31243122 diskLocators .add (diskLocator );
31253123 volumeDeviceKey .put (volume .getId (), diskId );
31263124 }
3125+ // If a target datastore is provided for the VM, then by default all volumes associated with the VM will be migrated to that target datastore.
3126+ // Hence set the existing datastore as target datastore for volumes that are not to be migrated.
3127+ List <Pair <Integer , ManagedObjectReference >> diskDatastores = vmMo .getAllDiskDatastores ();
3128+ for (Pair <Integer , ManagedObjectReference > diskDatastore : diskDatastores ) {
3129+ if (!volumeDeviceKey .containsValue (diskDatastore .first ().intValue ())) {
3130+ diskLocator = new VirtualMachineRelocateSpecDiskLocator ();
3131+ diskLocator .setDiskId (diskDatastore .first ().intValue ());
3132+ diskLocator .setDatastore (diskDatastore .second ());
3133+ diskLocators .add (diskLocator );
3134+ }
3135+ }
3136+
31273137 relocateSpec .getDisk ().addAll (diskLocators );
31283138
31293139 // Prepare network at target before migration
@@ -3225,6 +3235,7 @@ protected Answer execute(MigrateWithStorageCommand cmd) {
32253235 private Answer execute (MigrateVolumeCommand cmd ) {
32263236 String volumePath = cmd .getVolumePath ();
32273237 StorageFilerTO poolTo = cmd .getPool ();
3238+ Volume .Type volumeType = cmd .getVolumeType ();
32283239
32293240 if (s_logger .isInfoEnabled ()) {
32303241 s_logger .info ("Executing resource MigrateVolumeCommand: " + _gson .toJson (cmd ));
@@ -3271,9 +3282,23 @@ private Answer execute(MigrateVolumeCommand cmd) {
32713282 diskLocator = new VirtualMachineRelocateSpecDiskLocator ();
32723283 diskLocator .setDatastore (morDs );
32733284 diskLocator .setDiskId (diskId );
3274-
32753285 diskLocators .add (diskLocator );
3276- relocateSpec .getDisk ().add (diskLocator );
3286+ if (cmd .getVolumeType () == Volume .Type .ROOT ) {
3287+ relocateSpec .setDatastore (morDs );
3288+ // If a target datastore is provided for the VM, then by default all volumes associated with the VM will be migrated to that target datastore.
3289+ // Hence set the existing datastore as target datastore for volumes that are not to be migrated.
3290+ List <Pair <Integer , ManagedObjectReference >> diskDatastores = vmMo .getAllDiskDatastores ();
3291+ for (Pair <Integer , ManagedObjectReference > diskDatastore : diskDatastores ) {
3292+ if (diskDatastore .first ().intValue () != diskId ) {
3293+ diskLocator = new VirtualMachineRelocateSpecDiskLocator ();
3294+ diskLocator .setDiskId (diskDatastore .first ().intValue ());
3295+ diskLocator .setDatastore (diskDatastore .second ());
3296+ diskLocators .add (diskLocator );
3297+ }
3298+ }
3299+ }
3300+
3301+ relocateSpec .getDisk ().addAll (diskLocators );
32773302
32783303 // Change datastore
32793304 if (!vmMo .changeDatastore (relocateSpec )) {
@@ -3286,12 +3311,10 @@ private Answer execute(MigrateVolumeCommand cmd) {
32863311 // In case of a linked clone VM, if VM's disks are not consolidated,
32873312 // further volume operations on the ROOT volume such as volume snapshot etc. will result in DB inconsistencies.
32883313 String apiVersion = HypervisorHostHelper .getVcenterApiVersion (vmMo .getContext ());
3289- if (apiVersion .compareTo ("5.0" ) >= 0 ) {
3290- if (!vmMo .consolidateVmDisks ()) {
3291- s_logger .warn ("VM disk consolidation failed after storage migration." );
3292- } else {
3293- s_logger .debug ("Successfully consolidated disks of VM " + vmName + "." );
3294- }
3314+ if (!vmMo .consolidateVmDisks ()) {
3315+ s_logger .warn ("VM disk consolidation failed after storage migration." );
3316+ } else {
3317+ s_logger .debug ("Successfully consolidated disks of VM " + vmName + "." );
32953318 }
32963319
32973320 // Update and return volume path because that could have changed after migration
0 commit comments