@@ -4069,13 +4069,44 @@ protected Answer execute(DeleteStoragePoolCommand cmd) {
40694069 }
40704070 }
40714071
4072+ public static String getDatastoreName (String str ) {
4073+ return str .replace ('/' , '-' );
4074+ }
4075+
4076+ // This methd can be used to determine if the datastore is active yet.
4077+ // When an iSCSI target is created on a host and that target already contains
4078+ // the metadata that represents a datastore, the datastore shows up within
4079+ // vCenter as existent, but not necessarily active.
4080+ // Call this method and pass in the datastore's name to wait, if necessary,
4081+ // for the datastore to become active.
4082+ private boolean datastoreFileExists (DatastoreMO dsMo , String volumeDatastorePath ) {
4083+ for (int i = 0 ; i < 10 ; i ++) {
4084+ try {
4085+ return dsMo .fileExists (volumeDatastorePath );
4086+ }
4087+ catch (Exception e ) {
4088+ if (!e .getMessage ().contains ("is not accessible" )) {
4089+ break ;
4090+ }
4091+ }
4092+
4093+ try {
4094+ Thread .sleep (5000 );
4095+ }
4096+ catch (Exception e ) {
4097+ }
4098+ }
4099+
4100+ return false ;
4101+ }
4102+
40724103 @ Override
40734104 public ManagedObjectReference handleDatastoreAndVmdkAttach (Command cmd , String iqn , String storageHost , int storagePort ,
40744105 String initiatorUsername , String initiatorPassword , String targetUsername , String targetPassword ) throws Exception {
40754106 VmwareContext context = getServiceContext ();
40764107 VmwareHypervisorHost hyperHost = getHyperHost (context );
40774108
4078- ManagedObjectReference morDs = createVmfsDatastore (hyperHost , iqn ,
4109+ ManagedObjectReference morDs = createVmfsDatastore (hyperHost , getDatastoreName ( iqn ) ,
40794110 storageHost , storagePort , iqn ,
40804111 initiatorUsername , initiatorPassword ,
40814112 targetUsername , targetPassword );
@@ -4084,7 +4115,7 @@ public ManagedObjectReference handleDatastoreAndVmdkAttach(Command cmd, String i
40844115
40854116 String volumeDatastorePath = String .format ("[%s] %s.vmdk" , dsMo .getName (), dsMo .getName ());
40864117
4087- if (!dsMo . fileExists ( volumeDatastorePath )) {
4118+ if (!datastoreFileExists ( dsMo , volumeDatastorePath )) {
40884119 String dummyVmName = getWorkerName (context , cmd , 0 );
40894120
40904121 VirtualMachineMO vmMo = prepareVolumeHostDummyVm (hyperHost , dsMo , dummyVmName );
@@ -4096,6 +4127,7 @@ public ManagedObjectReference handleDatastoreAndVmdkAttach(Command cmd, String i
40964127 vmMo .createDisk (volumeDatastorePath , getMBsFromBytes (dsMo .getSummary ().getFreeSpace ()),
40974128 morDs , vmMo .getScsiDeviceControllerKey ());
40984129 vmMo .detachDisk (volumeDatastorePath , false );
4130+ vmMo .destroy ();
40994131 }
41004132
41014133 return morDs ;
@@ -4106,7 +4138,7 @@ public void handleDatastoreAndVmdkDetach(String iqn, String storageHost, int sto
41064138 VmwareContext context = getServiceContext ();
41074139 VmwareHypervisorHost hyperHost = getHyperHost (context );
41084140
4109- deleteVmfsDatastore (hyperHost , iqn , storageHost , storagePort , iqn );
4141+ deleteVmfsDatastore (hyperHost , getDatastoreName ( iqn ) , storageHost , storagePort , iqn );
41104142 }
41114143
41124144 protected Answer execute (AttachVolumeCommand cmd ) {
@@ -4240,6 +4272,7 @@ public void run() {
42404272 hss .addInternetScsiStaticTargets (iScsiHbaDevice , lstTargets );
42414273
42424274 hss .rescanHba (iScsiHbaDevice );
4275+ hss .rescanVmfs ();
42434276 }
42444277 catch (Exception ex ) {
42454278 synchronized (exceptions ) {
@@ -4268,7 +4301,7 @@ public void run() {
42684301 throw new Exception (exceptions .get (0 ).getMessage ());
42694302 }
42704303
4271- ManagedObjectReference morDs = hostDatastoreSystem .findDatastore (iqn );
4304+ ManagedObjectReference morDs = hostDatastoreSystem .findDatastoreByName (iqn );
42724305
42734306 if (morDs != null ) {
42744307 return morDs ;
@@ -4348,6 +4381,7 @@ public void run() {
43484381 hostStorageSystem .removeInternetScsiStaticTargets (iScsiHbaDevice , lstTargets );
43494382
43504383 hostStorageSystem .rescanHba (iScsiHbaDevice );
4384+ hostStorageSystem .rescanVmfs ();
43514385 }
43524386 catch (Exception ex ) {
43534387 exceptions .add (ex );
0 commit comments