2828import java .util .Collection ;
2929import java .util .Collections ;
3030import java .util .Comparator ;
31+ import java .util .concurrent .*;
3132import java .util .Date ;
3233import java .util .GregorianCalendar ;
3334import java .util .HashMap ;
@@ -4114,7 +4115,7 @@ public ManagedObjectReference handleDatastoreAndVmdkAttach(Command cmd, String i
41144115
41154116 String volumeDatastorePath = String .format ("[%s] %s.vmdk" , dsMo .getName (), dsMo .getName ());
41164117
4117- if (!datastoreFileExists ( dsMo , volumeDatastorePath )) {
4118+ if (!dsMo . fileExists ( volumeDatastorePath )) {
41184119 String dummyVmName = getWorkerName (context , cmd , 0 );
41194120
41204121 VirtualMachineMO vmMo = prepareVolumeHostDummyVm (hyperHost , dsMo , dummyVmName );
@@ -4204,49 +4205,80 @@ protected Answer execute(AttachVolumeCommand cmd) {
42044205 }
42054206 }
42064207
4207- private ManagedObjectReference createVmfsDatastore ( VmwareHypervisorHost hyperHost , String datastoreName , String storageIpAddress ,
4208- int storagePortNumber , String iqn , String chapName , String chapSecret , String mutualChapName , String mutualChapSecret ) throws Exception {
4208+ private void addRemoveInternetScsiTargetsToAllHosts ( final boolean add , final List < HostInternetScsiHbaStaticTarget > lstTargets ,
4209+ final List < Pair < ManagedObjectReference , String >> lstHosts ) throws Exception {
42094210 VmwareContext context = getServiceContext ();
4210- ManagedObjectReference morCluster = hyperHost .getHyperHostCluster ();
4211- ClusterMO cluster = new ClusterMO (context , morCluster );
4212- List <Pair <ManagedObjectReference , String >> lstHosts = cluster .getClusterHosts ();
42134211
4214- HostInternetScsiHbaStaticTarget target = new HostInternetScsiHbaStaticTarget ( );
4212+ ExecutorService executorService = Executors . newFixedThreadPool ( lstHosts . size () );
42154213
4216- target .setAddress (storageIpAddress );
4217- target .setPort (storagePortNumber );
4218- target .setIScsiName (iqn );
4214+ final List <Exception > exceptions = new ArrayList <Exception >();
42194215
4220- HostInternetScsiHbaAuthenticationProperties auth = new HostInternetScsiHbaAuthenticationProperties ();
4216+ for (Pair <ManagedObjectReference , String > hostPair : lstHosts ) {
4217+ HostMO host = new HostMO (context , hostPair .first ());
4218+ HostStorageSystemMO hostStorageSystem = host .getHostStorageSystemMO ();
42214219
4222- String strAuthType = "chapRequired" ;
4220+ boolean iScsiHbaConfigured = false ;
42234221
4224- auth .setChapAuthEnabled (true );
4225- auth .setChapInherited (false );
4226- auth .setChapAuthenticationType (strAuthType );
4227- auth .setChapName (chapName );
4228- auth .setChapSecret (chapSecret );
4229- auth .setMutualChapInherited (false );
4230- auth .setMutualChapAuthenticationType (strAuthType );
4231- auth .setMutualChapName (mutualChapName );
4232- auth .setMutualChapSecret (mutualChapSecret );
4222+ for (HostHostBusAdapter hba : hostStorageSystem .getStorageDeviceInfo ().getHostBusAdapter ()) {
4223+ if (hba instanceof HostInternetScsiHba ) {
4224+ // just finding an instance of HostInternetScsiHba means that we have found at least one configured iSCSI HBA
4225+ // at least one iSCSI HBA must be configured before a CloudStack user can use this host for iSCSI storage
4226+ iScsiHbaConfigured = true ;
42334227
4234- target . setAuthenticationProperties ( auth );
4228+ final String iScsiHbaDevice = hba . getDevice ( );
42354229
4236- final List < HostInternetScsiHbaStaticTarget > lstTargets = new ArrayList < HostInternetScsiHbaStaticTarget >() ;
4230+ final HostStorageSystemMO hss = hostStorageSystem ;
42374231
4238- lstTargets .add (target );
4232+ executorService .submit (new Thread () {
4233+ @ Override
4234+ public void run () {
4235+ try {
4236+ if (add ) {
4237+ hss .addInternetScsiStaticTargets (iScsiHbaDevice , lstTargets );
4238+ }
4239+ else {
4240+ hss .removeInternetScsiStaticTargets (iScsiHbaDevice , lstTargets );
4241+ }
4242+
4243+ hss .rescanHba (iScsiHbaDevice );
4244+ hss .rescanVmfs ();
4245+ }
4246+ catch (Exception ex ) {
4247+ synchronized (exceptions ) {
4248+ exceptions .add (ex );
4249+ }
4250+ }
4251+ }
4252+ });
4253+ }
4254+ }
4255+
4256+ if (!iScsiHbaConfigured ) {
4257+ throw new Exception ("An iSCSI HBA must be configured before a host can use iSCSI storage." );
4258+ }
4259+ }
4260+
4261+ executorService .shutdown ();
4262+
4263+ if (!executorService .awaitTermination (Long .MAX_VALUE , TimeUnit .MINUTES )) {
4264+ throw new Exception ("The system timed out before completing the task 'rescanAllHosts'." );
4265+ }
4266+
4267+ if (exceptions .size () > 0 ) {
4268+ throw new Exception (exceptions .get (0 ).getMessage ());
4269+ }
4270+ }
42394271
4240- HostDatastoreSystemMO hostDatastoreSystem = null ;
4241- HostStorageSystemMO hostStorageSystem = null ;
4272+ private void rescanAllHosts (final List <Pair <ManagedObjectReference , String >> lstHosts ) throws Exception {
4273+ VmwareContext context = getServiceContext ();
4274+
4275+ ExecutorService executorService = Executors .newFixedThreadPool (lstHosts .size ());
42424276
4243- final List <Thread > threads = new ArrayList <Thread >();
42444277 final List <Exception > exceptions = new ArrayList <Exception >();
42454278
42464279 for (Pair <ManagedObjectReference , String > hostPair : lstHosts ) {
42474280 HostMO host = new HostMO (context , hostPair .first ());
4248- hostDatastoreSystem = host .getHostDatastoreSystemMO ();
4249- hostStorageSystem = host .getHostStorageSystemMO ();
4281+ HostStorageSystemMO hostStorageSystem = host .getHostStorageSystemMO ();
42504282
42514283 boolean iScsiHbaConfigured = false ;
42524284
@@ -4260,12 +4292,10 @@ private ManagedObjectReference createVmfsDatastore(VmwareHypervisorHost hyperHos
42604292
42614293 final HostStorageSystemMO hss = hostStorageSystem ;
42624294
4263- threads . add (new Thread () {
4295+ executorService . submit (new Thread () {
42644296 @ Override
42654297 public void run () {
42664298 try {
4267- hss .addInternetScsiStaticTargets (iScsiHbaDevice , lstTargets );
4268-
42694299 hss .rescanHba (iScsiHbaDevice );
42704300 hss .rescanVmfs ();
42714301 }
@@ -4284,33 +4314,90 @@ public void run() {
42844314 }
42854315 }
42864316
4287- for (Thread thread : threads ) {
4288- thread .start ();
4289- }
4317+ executorService .shutdown ();
42904318
4291- for ( Thread thread : threads ) {
4292- thread . join ( );
4319+ if (! executorService . awaitTermination ( Long . MAX_VALUE , TimeUnit . MINUTES ) ) {
4320+ throw new Exception ( "The system timed out before completing the task 'rescanAllHosts'." );
42934321 }
42944322
42954323 if (exceptions .size () > 0 ) {
42964324 throw new Exception (exceptions .get (0 ).getMessage ());
42974325 }
4326+ }
4327+
4328+ private ManagedObjectReference createVmfsDatastore (VmwareHypervisorHost hyperHost , String datastoreName , String storageIpAddress ,
4329+ int storagePortNumber , String iqn , String chapName , String chapSecret , String mutualChapName , String mutualChapSecret ) throws Exception {
4330+ VmwareContext context = getServiceContext ();
4331+ ManagedObjectReference morCluster = hyperHost .getHyperHostCluster ();
4332+ ClusterMO cluster = new ClusterMO (context , morCluster );
4333+ List <Pair <ManagedObjectReference , String >> lstHosts = cluster .getClusterHosts ();
4334+
4335+ HostInternetScsiHbaStaticTarget target = new HostInternetScsiHbaStaticTarget ();
4336+
4337+ target .setAddress (storageIpAddress );
4338+ target .setPort (storagePortNumber );
4339+ target .setIScsiName (iqn );
4340+
4341+ HostInternetScsiHbaAuthenticationProperties auth = new HostInternetScsiHbaAuthenticationProperties ();
4342+
4343+ String strAuthType = "chapRequired" ;
4344+
4345+ auth .setChapAuthEnabled (true );
4346+ auth .setChapInherited (false );
4347+ auth .setChapAuthenticationType (strAuthType );
4348+ auth .setChapName (chapName );
4349+ auth .setChapSecret (chapSecret );
4350+ auth .setMutualChapInherited (false );
4351+ auth .setMutualChapAuthenticationType (strAuthType );
4352+ auth .setMutualChapName (mutualChapName );
4353+ auth .setMutualChapSecret (mutualChapSecret );
4354+
4355+ target .setAuthenticationProperties (auth );
4356+
4357+ final List <HostInternetScsiHbaStaticTarget > lstTargets = new ArrayList <HostInternetScsiHbaStaticTarget >();
4358+
4359+ lstTargets .add (target );
4360+
4361+ addRemoveInternetScsiTargetsToAllHosts (true , lstTargets , lstHosts );
4362+
4363+ rescanAllHosts (lstHosts );
4364+
4365+ HostMO host = new HostMO (context , lstHosts .get (0 ).first ());
4366+ HostDatastoreSystemMO hostDatastoreSystem = host .getHostDatastoreSystemMO ();
42984367
42994368 ManagedObjectReference morDs = hostDatastoreSystem .findDatastoreByName (datastoreName );
43004369
43014370 if (morDs != null ) {
43024371 return morDs ;
43034372 }
43044373
4374+ rescanAllHosts (lstHosts );
4375+
4376+ HostStorageSystemMO hostStorageSystem = host .getHostStorageSystemMO ();
43054377 List <HostScsiDisk > lstHostScsiDisks = hostDatastoreSystem .queryAvailableDisksForVmfs ();
43064378
43074379 HostScsiDisk hostScsiDisk = getHostScsiDisk (hostStorageSystem .getStorageDeviceInfo ().getScsiTopology (), lstHostScsiDisks , iqn );
43084380
43094381 if (hostScsiDisk == null ) {
4382+ // check to see if the datastore actually does exist already
4383+ morDs = hostDatastoreSystem .findDatastoreByName (datastoreName );
4384+
4385+ if (morDs != null ) {
4386+ return morDs ;
4387+ }
4388+
43104389 throw new Exception ("A relevant SCSI disk could not be located to use to create a datastore." );
43114390 }
43124391
4313- return hostDatastoreSystem .createVmfsDatastore (datastoreName , hostScsiDisk );
4392+ morDs = hostDatastoreSystem .createVmfsDatastore (datastoreName , hostScsiDisk );
4393+
4394+ if (morDs != null ) {
4395+ rescanAllHosts (lstHosts );
4396+
4397+ return morDs ;
4398+ }
4399+
4400+ throw new Exception ("Unable to create a datastore" );
43144401 }
43154402
43164403 // the purpose of this method is to find the HostScsiDisk in the passed-in array that exists (if any) because
@@ -4358,46 +4445,9 @@ private void deleteVmfsDatastore(VmwareHypervisorHost hyperHost, String volumeUu
43584445
43594446 lstTargets .add (target );
43604447
4361- final List <Thread > threads = new ArrayList <Thread >();
4362- final List <Exception > exceptions = new ArrayList <Exception >();
4363-
4364- for (Pair <ManagedObjectReference , String > hostPair : lstHosts ) {
4365- final HostMO host = new HostMO (context , hostPair .first ());
4366- final HostStorageSystemMO hostStorageSystem = host .getHostStorageSystemMO ();
4448+ addRemoveInternetScsiTargetsToAllHosts (false , lstTargets , lstHosts );
43674449
4368- for (HostHostBusAdapter hba : hostStorageSystem .getStorageDeviceInfo ().getHostBusAdapter ()) {
4369- if (hba instanceof HostInternetScsiHba ) {
4370- final String iScsiHbaDevice = hba .getDevice ();
4371-
4372- Thread thread = new Thread () {
4373- @ Override
4374- public void run () {
4375- try {
4376- hostStorageSystem .removeInternetScsiStaticTargets (iScsiHbaDevice , lstTargets );
4377-
4378- hostStorageSystem .rescanHba (iScsiHbaDevice );
4379- hostStorageSystem .rescanVmfs ();
4380- }
4381- catch (Exception ex ) {
4382- exceptions .add (ex );
4383- }
4384- }
4385- };
4386-
4387- threads .add (thread );
4388-
4389- thread .start ();
4390- }
4391- }
4392- }
4393-
4394- for (Thread thread : threads ) {
4395- thread .join ();
4396- }
4397-
4398- if (exceptions .size () > 0 ) {
4399- throw new Exception (exceptions .get (0 ).getMessage ());
4400- }
4450+ rescanAllHosts (lstHosts );
44014451 }
44024452
44034453 protected Answer execute (AttachIsoCommand cmd ) {
0 commit comments