@@ -4439,7 +4439,7 @@ public static String getDatastoreName(String str) {
44394439 return str .replace ('/' , '-' );
44404440 }
44414441
4442- private String trimIqn (String iqn ) {
4442+ public static String trimIqn (String iqn ) {
44434443 String [] tmp = iqn .split ("/" );
44444444
44454445 if (tmp .length != 3 ) {
@@ -4454,36 +4454,23 @@ private String trimIqn(String iqn) {
44544454 }
44554455
44564456 @ Override
4457- public ManagedObjectReference handleDatastoreAndVmdkAttach (Command cmd , String iqn , String storageHost , int storagePort ,
4458- String initiatorUsername , String initiatorPassword , String targetUsername , String targetPassword ) throws Exception {
4457+ public void createVmdk (Command cmd , DatastoreMO dsMo , String vmdkDatastorePath , Long volumeSize ) throws Exception {
44594458 VmwareContext context = getServiceContext ();
44604459 VmwareHypervisorHost hyperHost = getHyperHost (context );
44614460
4462- ManagedObjectReference morDs = createVmfsDatastore (hyperHost , getDatastoreName (iqn ),
4463- storageHost , storagePort , trimIqn (iqn ),
4464- initiatorUsername , initiatorPassword ,
4465- targetUsername , targetPassword );
4461+ String dummyVmName = getWorkerName (context , cmd , 0 );
44664462
4467- DatastoreMO dsMo = new DatastoreMO ( context , morDs );
4463+ VirtualMachineMO vmMo = HypervisorHostHelper . createWorkerVM ( hyperHost , dsMo , dummyVmName );
44684464
4469- String volumeDatastorePath = String .format ("[%s] %s.vmdk" , dsMo .getName (), dsMo .getName ());
4470-
4471- if (!dsMo .fileExists (volumeDatastorePath )) {
4472- String dummyVmName = getWorkerName (context , cmd , 0 );
4473-
4474- VirtualMachineMO vmMo = HypervisorHostHelper .createWorkerVM (hyperHost , dsMo , dummyVmName );
4475-
4476- if (vmMo == null ) {
4477- throw new Exception ("Unable to create a dummy VM for volume creation" );
4478- }
4479-
4480- vmMo .createDisk (volumeDatastorePath , getMBsFromBytes (dsMo .getSummary ().getFreeSpace ()),
4481- morDs , vmMo .getScsiDeviceControllerKey ());
4482- vmMo .detachDisk (volumeDatastorePath , false );
4483- vmMo .destroy ();
4465+ if (vmMo == null ) {
4466+ throw new Exception ("Unable to create a dummy VM for volume creation" );
44844467 }
44854468
4486- return morDs ;
4469+ Long volumeSizeToUse = volumeSize < dsMo .getSummary ().getFreeSpace () ? volumeSize : dsMo .getSummary ().getFreeSpace ();
4470+
4471+ vmMo .createDisk (vmdkDatastorePath , getMBsFromBytes (volumeSizeToUse ), dsMo .getMor (), vmMo .getScsiDeviceControllerKey ());
4472+ vmMo .detachDisk (vmdkDatastorePath , false );
4473+ vmMo .destroy ();
44874474 }
44884475
44894476 @ Override
@@ -4516,9 +4503,16 @@ protected Answer execute(AttachVolumeCommand cmd) {
45164503 ManagedObjectReference morDs = null ;
45174504
45184505 if (cmd .getAttach () && cmd .isManaged ()) {
4519- morDs = handleDatastoreAndVmdkAttach (cmd , cmd .get_iScsiName (), cmd .getStorageHost (), cmd .getStoragePort (),
4520- cmd .getChapInitiatorUsername (), cmd .getChapInitiatorPassword (),
4521- cmd .getChapTargetUsername (), cmd .getChapTargetPassword ());
4506+ morDs = getVmfsDatastore (hyperHost , getDatastoreName (cmd .get_iScsiName ()), cmd .getStorageHost (), cmd .getStoragePort (), trimIqn (cmd .get_iScsiName ()),
4507+ cmd .getChapInitiatorUsername (), cmd .getChapInitiatorPassword (), cmd .getChapTargetUsername (), cmd .getChapTargetPassword ());
4508+
4509+ DatastoreMO dsMo = new DatastoreMO (getServiceContext (), morDs );
4510+
4511+ String volumeDatastorePath = String .format ("[%s] %s.vmdk" , dsMo .getName (), dsMo .getName ());
4512+
4513+ if (!dsMo .fileExists (volumeDatastorePath )) {
4514+ createVmdk (cmd , dsMo , VmwareResource .getDatastoreName (cmd .get_iScsiName ()), cmd .getVolumeSize ());
4515+ }
45224516 }
45234517 else {
45244518 morDs = HypervisorHostHelper .findDatastoreWithBackwardsCompatibility (hyperHost , cmd .getPoolUuid ());
@@ -4531,10 +4525,18 @@ protected Answer execute(AttachVolumeCommand cmd) {
45314525 }
45324526
45334527 DatastoreMO dsMo = new DatastoreMO (getServiceContext (), morDs );
4534- VmwareStorageLayoutHelper .syncVolumeToVmDefaultFolder (dsMo .getOwnerDatacenter ().first (), cmd .getVmName (),
4535- dsMo , cmd .getVolumePath ());
4536-
4537- String datastoreVolumePath = dsMo .searchFileInSubFolders (cmd .getVolumePath () + ".vmdk" , true );
4528+
4529+ String datastoreVolumePath = null ;
4530+
4531+ if (cmd .isManaged ()) {
4532+ datastoreVolumePath = dsMo .getDatastorePath (dsMo .getName () + ".vmdk" );
4533+ }
4534+ else {
4535+ VmwareStorageLayoutHelper .syncVolumeToVmDefaultFolder (dsMo .getOwnerDatacenter ().first (), cmd .getVmName (), dsMo , cmd .getVolumePath ());
4536+
4537+ datastoreVolumePath = dsMo .searchFileInSubFolders (cmd .getVolumePath () + ".vmdk" , true );
4538+ }
4539+
45384540 assert (datastoreVolumePath != null ) : "Virtual disk file must exist in specified datastore for attach/detach operations." ;
45394541 if (datastoreVolumePath == null ) {
45404542 throw new CloudRuntimeException ("Unable to find file " + cmd .getVolumePath () + ".vmdk in datastore " + dsMo .getName ());
@@ -4687,7 +4689,7 @@ public void run() {
46874689 }
46884690 }
46894691
4690- private ManagedObjectReference createVmfsDatastore (VmwareHypervisorHost hyperHost , String datastoreName , String storageIpAddress ,
4692+ public ManagedObjectReference getVmfsDatastore (VmwareHypervisorHost hyperHost , String datastoreName , String storageIpAddress ,
46914693 int storagePortNumber , String iqn , String chapName , String chapSecret , String mutualChapName , String mutualChapSecret ) throws Exception {
46924694 VmwareContext context = getServiceContext ();
46934695 ManagedObjectReference morCluster = hyperHost .getHyperHostCluster ();
0 commit comments