@@ -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 (s_logger .isInfoEnabled ())
2382- s_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 (s_logger .isInfoEnabled ())
2393- s_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 s_logger .debug (LogUtils .logGsonWithoutException ("The following definitions will be used to start the VM: virtual device [%s], volume [%s]." , device , volumeTO ));
@@ -2710,6 +2668,64 @@ protected StartAnswer execute(StartCommand cmd) {
27102668 }
27112669 }
27122670
2671+ private Pair <ManagedObjectReference , DatastoreMO > getVolumeDatastoreDetails (DiskTO vol , HashMap <String , Pair <ManagedObjectReference , DatastoreMO >> dataStoresDetails ) throws Exception {
2672+ boolean managed = false ;
2673+ String iScsiName = null ;
2674+ Map <String , String > details = vol .getDetails ();
2675+ if (MapUtils .isNotEmpty (details )) {
2676+ managed = Boolean .parseBoolean (details .get (DiskTO .MANAGED ));
2677+ iScsiName = details .get (DiskTO .IQN );
2678+ }
2679+
2680+ VolumeObjectTO volumeTO = (VolumeObjectTO ) vol .getData ();
2681+ DataStoreTO primaryStore = volumeTO .getDataStore ();
2682+ String primaryStoreUuid = primaryStore .getUuid ();
2683+ // if the storage is managed, iScsiName should not be null
2684+ String datastoreName = managed ? VmwareResource .getDatastoreName (iScsiName ) : primaryStoreUuid ;
2685+ Pair <ManagedObjectReference , DatastoreMO > volumeDsDetails = dataStoresDetails .get (datastoreName );
2686+ if (volumeDsDetails == null ) {
2687+ throw new Exception ("Primary datastore " + primaryStore .getUuid () + " is not mounted on host." );
2688+ }
2689+
2690+ return volumeDsDetails ;
2691+ }
2692+
2693+ private void syncVolumeDatastoreAndPathForDatastoreCluster (DiskTO vol , VirtualMachineDiskInfoBuilder diskInfoBuilder , VirtualMachineDiskInfo matchingExistingDisk ,
2694+ Pair <ManagedObjectReference , DatastoreMO > volumeDsDetails , List <Pair <Integer , ManagedObjectReference >> diskDatastores ,
2695+ VmwareHypervisorHost hyperHost , VmwareContext context ) throws Exception {
2696+ if (vol .getDetails () == null || vol .getDetails ().get (DiskTO .PROTOCOL_TYPE ) == null || !vol .getDetails ().get (DiskTO .PROTOCOL_TYPE ).equalsIgnoreCase ("DatastoreCluster" )) {
2697+ return ;
2698+ }
2699+
2700+ if (diskInfoBuilder != null && matchingExistingDisk != null ) {
2701+ String [] diskChain = matchingExistingDisk .getDiskChain ();
2702+ if (diskChain != null && diskChain .length > 0 ) {
2703+ DatastoreFile file = new DatastoreFile (diskChain [0 ]);
2704+ VolumeObjectTO volumeTO = (VolumeObjectTO ) vol .getData ();
2705+ if (!file .getFileBaseName ().equalsIgnoreCase (volumeTO .getPath ())) {
2706+ if (s_logger .isInfoEnabled ()) {
2707+ s_logger .info ("Detected disk-chain top file change on volume: " + volumeTO .getId () + " " + volumeTO .getPath () + " -> " + file .getFileBaseName ());
2708+ }
2709+ volumeTO .setPath (file .getFileBaseName ());
2710+ vol .setPath (file .getFileBaseName ());
2711+ }
2712+ }
2713+ DatastoreMO diskDatastoreMofromVM = getDataStoreWhereDiskExists (hyperHost , context , diskInfoBuilder , vol , diskDatastores );
2714+ if (diskDatastoreMofromVM != null ) {
2715+ String actualPoolUuid = diskDatastoreMofromVM .getCustomFieldValue (CustomFieldConstants .CLOUD_UUID );
2716+ VolumeObjectTO volumeTO = (VolumeObjectTO ) vol .getData ();
2717+ DataStoreTO primaryStore = volumeTO .getDataStore ();
2718+ if (actualPoolUuid != null && !actualPoolUuid .equalsIgnoreCase (primaryStore .getUuid ())) {
2719+ volumeDsDetails = new Pair <>(diskDatastoreMofromVM .getMor (), diskDatastoreMofromVM );
2720+ if (s_logger .isInfoEnabled ()) {
2721+ s_logger .info ("Detected datastore uuid change on volume: " + volumeTO .getId () + " " + primaryStore .getUuid () + " -> " + actualPoolUuid );
2722+ }
2723+ ((PrimaryDataStoreTO )primaryStore ).setUuid (actualPoolUuid );
2724+ }
2725+ }
2726+ }
2727+ }
2728+
27132729 private boolean powerOnVM (final VirtualMachineMO vmMo , final String vmInternalCSName , final String vmNameOnVcenter ) throws Exception {
27142730 int retry = 20 ;
27152731 while (retry -- > 0 ) {
0 commit comments