@@ -2351,51 +2351,8 @@ protected StartAnswer execute(StartCommand cmd) {
23512351 }
23522352
23532353 VirtualMachineDiskInfo matchingExistingDisk = getMatchingExistingDisk (diskInfoBuilder , vol , hyperHost , context );
2354- VolumeObjectTO volumeTO = (VolumeObjectTO ) vol .getData ();
2355- DataStoreTO primaryStore = volumeTO .getDataStore ();
2356- Map <String , String > details = vol .getDetails ();
2357- boolean managed = false ;
2358- String iScsiName = null ;
2359-
2360- if (details != null ) {
2361- managed = Boolean .parseBoolean (details .get (DiskTO .MANAGED ));
2362- iScsiName = details .get (DiskTO .IQN );
2363- }
2364-
2365- String primaryStoreUuid = primaryStore .getUuid ();
2366- // if the storage is managed, iScsiName should not be null
2367- String datastoreName = managed ? VmwareResource .getDatastoreName (iScsiName ) : primaryStoreUuid ;
2368- Pair <ManagedObjectReference , DatastoreMO > volumeDsDetails = dataStoresDetails .get (datastoreName );
2369-
2370- assert (volumeDsDetails != null );
2371- if (volumeDsDetails == null ) {
2372- throw new Exception ("Primary datastore " + primaryStore .getUuid () + " is not mounted on host." );
2373- }
2374-
2375- if (vol .getDetails ().get (DiskTO .PROTOCOL_TYPE ) != null && vol .getDetails ().get (DiskTO .PROTOCOL_TYPE ).equalsIgnoreCase ("DatastoreCluster" )) {
2376- if (diskInfoBuilder != null && matchingExistingDisk != null ) {
2377- String [] diskChain = matchingExistingDisk .getDiskChain ();
2378- if (diskChain != null && diskChain .length > 0 ) {
2379- DatastoreFile file = new DatastoreFile (diskChain [0 ]);
2380- if (!file .getFileBaseName ().equalsIgnoreCase (volumeTO .getPath ())) {
2381- if (logger .isInfoEnabled ())
2382- logger .info ("Detected disk-chain top file change on volume: " + volumeTO .getId () + " " + volumeTO .getPath () + " -> " + file .getFileBaseName ());
2383- volumeTO .setPath (file .getFileBaseName ());
2384- vol .setPath (file .getFileBaseName ());
2385- }
2386- }
2387- DatastoreMO diskDatastoreMofromVM = getDataStoreWhereDiskExists (hyperHost , context , diskInfoBuilder , vol , diskDatastores );
2388- if (diskDatastoreMofromVM != null ) {
2389- String actualPoolUuid = diskDatastoreMofromVM .getCustomFieldValue (CustomFieldConstants .CLOUD_UUID );
2390- if (actualPoolUuid != null && !actualPoolUuid .equalsIgnoreCase (primaryStore .getUuid ())) {
2391- volumeDsDetails = new Pair <>(diskDatastoreMofromVM .getMor (), diskDatastoreMofromVM );
2392- if (logger .isInfoEnabled ())
2393- logger .info ("Detected datastore uuid change on volume: " + volumeTO .getId () + " " + primaryStore .getUuid () + " -> " + actualPoolUuid );
2394- ((PrimaryDataStoreTO )primaryStore ).setUuid (actualPoolUuid );
2395- }
2396- }
2397- }
2398- }
2354+ Pair <ManagedObjectReference , DatastoreMO > volumeDsDetails = getVolumeDatastoreDetails (vol , dataStoresDetails );
2355+ syncVolumeDatastoreAndPathForDatastoreCluster (vol , diskInfoBuilder , matchingExistingDisk , volumeDsDetails , diskDatastores , hyperHost , context );
23992356
24002357 if (deployAsIs && vol .getType () == Volume .Type .ROOT ) {
24012358 rootDiskTO = vol ;
@@ -2442,6 +2399,7 @@ protected StartAnswer execute(StartCommand cmd) {
24422399 scsiUnitNumber ++;
24432400 }
24442401
2402+ VolumeObjectTO volumeTO = (VolumeObjectTO ) vol .getData ();
24452403 Long maxIops = volumeTO .getIopsWriteRate () + volumeTO .getIopsReadRate ();
24462404 VirtualDevice device = VmwareHelper .prepareDiskDevice (vmMo , null , controllerKey , diskChain , volumeDsDetails .first (), deviceNumber , i + 1 , maxIops );
24472405 logger .debug (LogUtils .logGsonWithoutException ("The following definitions will be used to start the VM: virtual device [%s], volume [%s]." , device , volumeTO ));
@@ -2715,6 +2673,64 @@ protected StartAnswer execute(StartCommand cmd) {
27152673 }
27162674 }
27172675
2676+ private Pair <ManagedObjectReference , DatastoreMO > getVolumeDatastoreDetails (DiskTO vol , HashMap <String , Pair <ManagedObjectReference , DatastoreMO >> dataStoresDetails ) throws Exception {
2677+ boolean managed = false ;
2678+ String iScsiName = null ;
2679+ Map <String , String > details = vol .getDetails ();
2680+ if (MapUtils .isNotEmpty (details )) {
2681+ managed = Boolean .parseBoolean (details .get (DiskTO .MANAGED ));
2682+ iScsiName = details .get (DiskTO .IQN );
2683+ }
2684+
2685+ VolumeObjectTO volumeTO = (VolumeObjectTO ) vol .getData ();
2686+ DataStoreTO primaryStore = volumeTO .getDataStore ();
2687+ String primaryStoreUuid = primaryStore .getUuid ();
2688+ // if the storage is managed, iScsiName should not be null
2689+ String datastoreName = managed ? VmwareResource .getDatastoreName (iScsiName ) : primaryStoreUuid ;
2690+ Pair <ManagedObjectReference , DatastoreMO > volumeDsDetails = dataStoresDetails .get (datastoreName );
2691+ if (volumeDsDetails == null ) {
2692+ throw new Exception ("Primary datastore " + primaryStore .getUuid () + " is not mounted on host." );
2693+ }
2694+
2695+ return volumeDsDetails ;
2696+ }
2697+
2698+ private void syncVolumeDatastoreAndPathForDatastoreCluster (DiskTO vol , VirtualMachineDiskInfoBuilder diskInfoBuilder , VirtualMachineDiskInfo matchingExistingDisk ,
2699+ Pair <ManagedObjectReference , DatastoreMO > volumeDsDetails , List <Pair <Integer , ManagedObjectReference >> diskDatastores ,
2700+ VmwareHypervisorHost hyperHost , VmwareContext context ) throws Exception {
2701+ if (vol .getDetails () == null || vol .getDetails ().get (DiskTO .PROTOCOL_TYPE ) == null || !vol .getDetails ().get (DiskTO .PROTOCOL_TYPE ).equalsIgnoreCase ("DatastoreCluster" )) {
2702+ return ;
2703+ }
2704+
2705+ if (diskInfoBuilder != null && matchingExistingDisk != null ) {
2706+ String [] diskChain = matchingExistingDisk .getDiskChain ();
2707+ if (diskChain != null && diskChain .length > 0 ) {
2708+ DatastoreFile file = new DatastoreFile (diskChain [0 ]);
2709+ VolumeObjectTO volumeTO = (VolumeObjectTO ) vol .getData ();
2710+ if (!file .getFileBaseName ().equalsIgnoreCase (volumeTO .getPath ())) {
2711+ if (logger .isInfoEnabled ()) {
2712+ logger .info ("Detected disk-chain top file change on volume: " + volumeTO .getId () + " " + volumeTO .getPath () + " -> " + file .getFileBaseName ());
2713+ }
2714+ volumeTO .setPath (file .getFileBaseName ());
2715+ vol .setPath (file .getFileBaseName ());
2716+ }
2717+ }
2718+ DatastoreMO diskDatastoreMofromVM = getDataStoreWhereDiskExists (hyperHost , context , diskInfoBuilder , vol , diskDatastores );
2719+ if (diskDatastoreMofromVM != null ) {
2720+ String actualPoolUuid = diskDatastoreMofromVM .getCustomFieldValue (CustomFieldConstants .CLOUD_UUID );
2721+ VolumeObjectTO volumeTO = (VolumeObjectTO ) vol .getData ();
2722+ DataStoreTO primaryStore = volumeTO .getDataStore ();
2723+ if (actualPoolUuid != null && !actualPoolUuid .equalsIgnoreCase (primaryStore .getUuid ())) {
2724+ volumeDsDetails = new Pair <>(diskDatastoreMofromVM .getMor (), diskDatastoreMofromVM );
2725+ if (logger .isInfoEnabled ()) {
2726+ logger .info ("Detected datastore uuid change on volume: " + volumeTO .getId () + " " + primaryStore .getUuid () + " -> " + actualPoolUuid );
2727+ }
2728+ ((PrimaryDataStoreTO )primaryStore ).setUuid (actualPoolUuid );
2729+ }
2730+ }
2731+ }
2732+ }
2733+
27182734 private boolean powerOnVM (final VirtualMachineMO vmMo , final String vmInternalCSName , final String vmNameOnVcenter ) throws Exception {
27192735 int retry = 20 ;
27202736 while (retry -- > 0 ) {
0 commit comments