@@ -2807,7 +2807,8 @@ protected StartAnswer execute(StartCommand cmd) {
28072807 if (vol .getType () == Volume .Type .ISO )
28082808 continue ;
28092809
2810- controllerKey = getDiskController (vol , vmSpec , ideControllerKey , scsiControllerKey );
2810+ VirtualMachineDiskInfo matchingExistingDisk = getMatchingExistingDisk (diskInfoBuilder , vol );
2811+ controllerKey = getDiskController (matchingExistingDisk , vol , vmSpec , ideControllerKey , scsiControllerKey );
28112812
28122813 VolumeObjectTO volumeTO = (VolumeObjectTO )vol .getData ();
28132814 PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO )volumeTO .getDataStore ();
@@ -2816,19 +2817,16 @@ protected StartAnswer execute(StartCommand cmd) {
28162817 VirtualDevice device ;
28172818
28182819 String [] diskChain = syncDiskChain (dcMo , vmMo , vmSpec ,
2819- vol , diskInfoBuilder ,
2820- dataStoresDetails ,
2821- (controllerKey == ideControllerKey ) ? true : false ,
2822- 0 , // currently only support bus 0
2823- (controllerKey == ideControllerKey ) ? ideUnitNumber : scsiUnitNumber );
2820+ vol , matchingExistingDisk ,
2821+ dataStoresDetails );
28242822
2825- device = VmwareHelper .prepareDiskDevice (vmMo , controllerKey ,
2823+ device = VmwareHelper .prepareDiskDevice (vmMo , null , controllerKey ,
28262824 diskChain ,
28272825 volumeDsDetails .first (),
28282826 (controllerKey == ideControllerKey ) ? ideUnitNumber ++ : scsiUnitNumber ++, i + 1 );
28292827
28302828 deviceConfigSpecArray [i ].setDevice (device );
2831- deviceConfigSpecArray [i ].setOperation (VirtualDeviceConfigSpecOperation .ADD );
2829+ deviceConfigSpecArray [i ].setOperation (VirtualDeviceConfigSpecOperation .ADD );
28322830
28332831 if (s_logger .isDebugEnabled ())
28342832 s_logger .debug ("Prepare volume at new device " + _gson .toJson (device ));
@@ -2951,55 +2949,37 @@ protected StartAnswer execute(StartCommand cmd) {
29512949
29522950 // return the finalized disk chain for startup, from top to bottom
29532951 private String [] syncDiskChain (DatacenterMO dcMo , VirtualMachineMO vmMo , VirtualMachineTO vmSpec ,
2954- DiskTO vol , VirtualMachineDiskInfoBuilder diskInfoBuilder ,
2955- HashMap <String ,Pair <ManagedObjectReference , DatastoreMO >> dataStoresDetails ,
2956- boolean ideController , int deviceBusNumber , int deviceUnitNumber ) throws Exception {
2952+ DiskTO vol , VirtualMachineDiskInfo diskInfo ,
2953+ HashMap <String ,Pair <ManagedObjectReference , DatastoreMO >> dataStoresDetails
2954+ ) throws Exception {
29572955
29582956 VolumeObjectTO volumeTO = (VolumeObjectTO )vol .getData ();
29592957 PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO )volumeTO .getDataStore ();
29602958
2961- String deviceBusName ;
2962- if (ideController )
2963- deviceBusName = String .format ("ide%d:%d" , deviceBusNumber , deviceUnitNumber );
2964- else
2965- deviceBusName = String .format ("scsi%d:%d" , deviceBusNumber , deviceUnitNumber );
2966-
29672959 Pair <ManagedObjectReference , DatastoreMO > volumeDsDetails = dataStoresDetails .get (primaryStore .getUuid ());
29682960 if (volumeDsDetails == null )
29692961 throw new Exception ("Primary datastore " + primaryStore .getUuid () + " is not mounted on host." );
29702962 DatastoreMO dsMo = volumeDsDetails .second ();
29712963
29722964 // we will honor vCenter's meta if it exists
2973- if (diskInfoBuilder != null && diskInfoBuilder . getDiskCount () > 0 ) {
2974- // we will always on-disk info from vCenter in this case
2975- VirtualMachineDiskInfo diskInfo = diskInfoBuilder . getDiskInfoByDeviceBusName ( deviceBusName );
2976- if ( diskInfo != null ) {
2977- if ( s_logger . isInfoEnabled ())
2978- s_logger . info ( "Volume " + volumeTO . getId () + " does not seem to exist on datastore. use on-disk chain: " +
2979- _gson . toJson ( diskInfo ) );
2980-
2981- return diskInfo . getDiskChain ( );
2982- } else {
2983- s_logger . warn ( "Volume " + volumeTO . getId () + " does not seem to exist on datastore. on-disk may be out of sync as well. disk device info: " + deviceBusName );
2984- }
2985- }
2965+ if (diskInfo != null ) {
2966+ // to deal with run-time upgrade to maintain the new datastore folder structure
2967+ String disks [] = diskInfo . getDiskChain ( );
2968+ for ( int i = 0 ; i < disks . length ; i ++ ) {
2969+ DatastoreFile file = new DatastoreFile ( disks [ i ]);
2970+ if ( file . getDir () != null && file . getDir (). isEmpty ()) {
2971+ s_logger . info ( "Perform run-time datastore folder upgrade. sync " + disks [ i ] + " to VM folder" );
2972+ disks [ i ] = VmwareStorageLayoutHelper . syncVolumeToVmDefaultFolder (
2973+ dcMo , vmMo . getName (), dsMo , file . getFileBaseName () );
2974+ }
2975+ }
2976+ return disks ;
2977+ }
29862978
29872979 String datastoreDiskPath = VmwareStorageLayoutHelper .syncVolumeToVmDefaultFolder (
29882980 dcMo , vmMo .getName (), dsMo , volumeTO .getPath ());
29892981 if (!dsMo .fileExists (datastoreDiskPath )) {
2990- if (s_logger .isInfoEnabled ())
2991- s_logger .info ("Volume " + volumeTO .getId () + " does not seem to exist on datastore, out of sync? path: " + datastoreDiskPath );
2992-
2993- // last resort, try chain info stored in DB
2994- if (volumeTO .getChainInfo () != null ) {
2995- VirtualMachineDiskInfo diskInfo = _gson .fromJson (volumeTO .getChainInfo (), VirtualMachineDiskInfo .class );
2996- if (diskInfo != null ) {
2997- s_logger .info ("Use chain info from DB: " + volumeTO .getChainInfo ());
2998- return diskInfo .getDiskChain ();
2999- }
3000-
3001- throw new Exception ("Volume " + volumeTO .getId () + " does not seem to exist on datastore. Broken disk chain" );
3002- }
2982+ s_logger .warn ("Volume " + volumeTO .getId () + " does not seem to exist on datastore, out of sync? path: " + datastoreDiskPath );
30032983 }
30042984
30052985 return new String [] { datastoreDiskPath };
@@ -3209,21 +3189,80 @@ private static void postNvpConfigBeforeStart(VirtualMachineMO vmMo, VirtualMachi
32093189 }
32103190 }
32113191
3212- private static int getDiskController (DiskTO vol , VirtualMachineTO vmSpec , int ideControllerKey , int scsiControllerKey ) {
3192+ private VirtualMachineDiskInfo getMatchingExistingDisk (VirtualMachineDiskInfoBuilder diskInfoBuilder , DiskTO vol ) {
3193+ if (diskInfoBuilder != null ) {
3194+ VolumeObjectTO volume = (VolumeObjectTO )vol .getData ();
3195+
3196+ VirtualMachineDiskInfo diskInfo = diskInfoBuilder .getDiskInfoByBackingFileBaseName (volume .getPath ());
3197+ if (diskInfo != null ) {
3198+ s_logger .info ("Found existing disk info from volume path: " + volume .getPath ());
3199+ return diskInfo ;
3200+ } else {
3201+ String chainInfo = volume .getChainInfo ();
3202+ if (chainInfo != null ) {
3203+ VirtualMachineDiskInfo infoInChain = _gson .fromJson (chainInfo , VirtualMachineDiskInfo .class );
3204+ if (infoInChain != null ) {
3205+ String [] disks = infoInChain .getDiskChain ();
3206+ if (disks .length > 0 ) {
3207+ for (String diskPath : disks ) {
3208+ DatastoreFile file = new DatastoreFile (diskPath );
3209+ diskInfo = diskInfoBuilder .getDiskInfoByBackingFileBaseName (file .getFileBaseName ());
3210+ if (diskInfo != null ) {
3211+ s_logger .info ("Found existing disk from chain info: " + diskPath );
3212+ return diskInfo ;
3213+ }
3214+ }
3215+ }
3216+
3217+ if (diskInfo == null ) {
3218+ diskInfo = diskInfoBuilder .getDiskInfoByDeviceBusName (infoInChain .getDiskDeviceBusName ());
3219+ if (diskInfo != null ) {
3220+ s_logger .info ("Found existing disk from from chain device bus information: " + infoInChain .getDiskDeviceBusName ());
3221+ return diskInfo ;
3222+ }
3223+ }
3224+ }
3225+ }
3226+ }
3227+ }
3228+
3229+ return null ;
3230+ }
3231+
3232+ private int getDiskController (VirtualMachineDiskInfo matchingExistingDisk , DiskTO vol ,
3233+ VirtualMachineTO vmSpec , int ideControllerKey , int scsiControllerKey ) {
3234+
32133235 int controllerKey ;
3236+ if (matchingExistingDisk != null ) {
3237+ s_logger .info ("Chose disk controller based on existing information: " + matchingExistingDisk .getDiskDeviceBusName ());
3238+ if (matchingExistingDisk .getDiskDeviceBusName ().startsWith ("ide" ))
3239+ return ideControllerKey ;
3240+ else
3241+ return scsiControllerKey ;
3242+ }
32143243
32153244 if (vol .getType () == Volume .Type .ROOT ) {
3216- if (vmSpec .getDetails () != null && vmSpec .getDetails ().get (VmDetailConstants .ROOK_DISK_CONTROLLER ) != null )
3245+ Map <String , String > vmDetails = vmSpec .getDetails ();
3246+ if (vmDetails != null && vmDetails .get (VmDetailConstants .ROOK_DISK_CONTROLLER ) != null )
32173247 {
3218- if (vmSpec .getDetails ().get (VmDetailConstants .ROOK_DISK_CONTROLLER ).equalsIgnoreCase ("scsi" ))
3248+ if (vmDetails .get (VmDetailConstants .ROOK_DISK_CONTROLLER ).equalsIgnoreCase ("scsi" )) {
3249+ s_logger .info ("Chose disk controller for vol " + vol .getType () + " -> scsi, based on root disk controller settings: "
3250+ + vmDetails .get (VmDetailConstants .ROOK_DISK_CONTROLLER ));
32193251 controllerKey = scsiControllerKey ;
3220- else
3252+ }
3253+ else {
3254+ s_logger .info ("Chose disk controller for vol " + vol .getType () + " -> ide, based on root disk controller settings: "
3255+ + vmDetails .get (VmDetailConstants .ROOK_DISK_CONTROLLER ));
32213256 controllerKey = ideControllerKey ;
3257+ }
32223258 } else {
3259+ s_logger .info ("Chose disk controller for vol " + vol .getType () + " -> scsi. due to null root disk controller setting" );
32233260 controllerKey = scsiControllerKey ;
32243261 }
3262+
32253263 } else {
32263264 // DATA volume always use SCSI device
3265+ s_logger .info ("Chose disk controller for vol " + vol .getType () + " -> scsi" );
32273266 controllerKey = scsiControllerKey ;
32283267 }
32293268
@@ -3234,25 +3273,14 @@ private void postDiskConfigBeforeStart(VirtualMachineMO vmMo, VirtualMachineTO v
32343273 int ideControllerKey , int scsiControllerKey ) throws Exception {
32353274
32363275 VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo .getDiskInfoBuilder ();
3237- int controllerKey ;
3238- int ideUnitNumber = 1 ; // we always count in IDE device first
3239- int scsiUnitNumber = 0 ;
32403276
32413277 for (DiskTO vol : sortedDisks ) {
32423278 if (vol .getType () == Volume .Type .ISO )
32433279 continue ;
32443280
32453281 VolumeObjectTO volumeTO = (VolumeObjectTO )vol .getData ();
32463282
3247- controllerKey = getDiskController (vol , vmSpec , ideControllerKey , scsiControllerKey );
3248-
3249- String deviceBusName ;
3250- if (controllerKey == ideControllerKey )
3251- deviceBusName = String .format ("ide%d:%d" , 0 , ideUnitNumber ++);
3252- else
3253- deviceBusName = String .format ("scsi%d:%d" , 0 , scsiUnitNumber ++);
3254-
3255- VirtualMachineDiskInfo diskInfo = diskInfoBuilder .getDiskInfoByDeviceBusName (deviceBusName );
3283+ VirtualMachineDiskInfo diskInfo = getMatchingExistingDisk (diskInfoBuilder , vol );
32563284 assert (diskInfo != null );
32573285
32583286 String [] diskChain = diskInfo .getDiskChain ();
0 commit comments