@@ -1888,6 +1888,10 @@ private Volume orchestrateDetachVolumeFromVM(long vmId, long volumeId) {
18881888 }
18891889 }
18901890
1891+ if (volumePool == null ) {
1892+ sendCommand = false ;
1893+ }
1894+
18911895 Answer answer = null ;
18921896
18931897 if (sendCommand ) {
@@ -1920,14 +1924,14 @@ private Volume orchestrateDetachVolumeFromVM(long vmId, long volumeId) {
19201924 // Mark the volume as detached
19211925 _volsDao .detachVolume (volume .getId ());
19221926
1923- // volumePool() should be null if the VM we are detaching the disk from has never been started before
1924- // only revoke access on volumes that are actually on a datastore
1925- if (volumePool != null ) {
1927+ // volume.getPoolId() should be null if the VM we are detaching the disk from has never been started before
1928+ if (volume .getPoolId () != null ) {
19261929 DataStore dataStore = dataStoreMgr .getDataStore (volume .getPoolId (), DataStoreRole .Primary );
19271930 volService .revokeAccess (volFactory .getVolume (volume .getId ()), host , dataStore );
1931+ }
1932+ if (volumePool != null && hostId != null ) {
19281933 handleTargetsForVMware (hostId , volumePool .getHostAddress (), volumePool .getPort (), volume .get_iScsiName ());
19291934 }
1930-
19311935 return _volsDao .findById (volumeId );
19321936 } else {
19331937
@@ -2641,13 +2645,19 @@ private synchronized void checkAndSetAttaching(Long volumeId) {
26412645 if (volumeToAttach .isAttachedVM ()) {
26422646 throw new CloudRuntimeException ("volume: " + volumeToAttach .getName () + " is already attached to a VM: " + volumeToAttach .getAttachedVmName ());
26432647 }
2644- if (volumeToAttach .getState ().equals (Volume .State .Ready )) {
2648+
2649+ if (Volume .State .Allocated .equals (volumeToAttach .getState ())) {
2650+ return ;
2651+ }
2652+
2653+ if (Volume .State .Ready .equals (volumeToAttach .getState ())) {
26452654 volumeToAttach .stateTransit (Volume .Event .AttachRequested );
2646- } else if (!volumeToAttach .getState ().equals (Volume .State .Allocated )) {
2647- final String error = "Volume: " + volumeToAttach .getName () + " is in " + volumeToAttach .getState () + ". It should be in Ready or Allocated state" ;
2648- s_logger .error (error );
2649- throw new CloudRuntimeException (error );
2655+ return ;
26502656 }
2657+
2658+ final String error = "Volume: " + volumeToAttach .getName () + " is in " + volumeToAttach .getState () + ". It should be in Ready or Allocated state" ;
2659+ s_logger .error (error );
2660+ throw new CloudRuntimeException (error );
26512661 }
26522662
26532663 private VolumeVO sendAttachVolumeCommand (UserVmVO vm , VolumeVO volumeToAttach , Long deviceId ) {
@@ -2769,9 +2779,9 @@ private VolumeVO sendAttachVolumeCommand(UserVmVO vm, VolumeVO volumeToAttach, L
27692779
27702780 volumeToAttach = _volsDao .findById (volumeToAttach .getId ());
27712781
2772- if (volumeToAttach . getState (). equals ( Volume . State . Ready ) &&
2773- vm . getHypervisorType () == HypervisorType . KVM &&
2774- volumeToAttachStoragePool . isManaged () && volumeToAttach .getPath () = = null ) {
2782+ if (vm . getHypervisorType () == HypervisorType . KVM &&
2783+ volumeToAttachStoragePool != null && volumeToAttachStoragePool . isManaged () &&
2784+ volumeToAttach . getPath () == null && volumeToAttach .get_iScsiName () ! = null ) {
27752785 volumeToAttach .setPath (volumeToAttach .get_iScsiName ());
27762786 _volsDao .update (volumeToAttach .getId (), volumeToAttach );
27772787 }
@@ -2798,19 +2808,15 @@ private VolumeVO sendAttachVolumeCommand(UserVmVO vm, VolumeVO volumeToAttach, L
27982808 throw new CloudRuntimeException (errorMsg );
27992809 }
28002810 } finally {
2801- final VolumeInfo volInfo = volFactory .getVolume (volumeToAttach .getId ());
2802- // Transit events are only fired when volume was allocated at some point of time.
2803- if (volInfo .getPoolId () != null || volInfo .getLastPoolId () != null ) {
2804- final Volume .Event ev ;
2805- if (attached ) {
2806- ev = Volume .Event .OperationSucceeded ;
2807- s_logger .debug ("Volume: " + volInfo .getName () + " successfully attached to VM: " + volInfo .getAttachedVmName ());
2808- } else {
2809- ev = Volume .Event .OperationFailed ;
2810- s_logger .debug ("Volume: " + volInfo .getName () + " failed to attach to VM: " + volInfo .getAttachedVmName ());
2811- }
2812- volInfo .stateTransit (ev );
2811+ Volume .Event ev = Volume .Event .OperationFailed ;
2812+ VolumeInfo volInfo = volFactory .getVolume (volumeToAttach .getId ());
2813+ if (attached ) {
2814+ ev = Volume .Event .OperationSucceeded ;
2815+ s_logger .debug ("Volume: " + volInfo .getName () + " successfully attached to VM: " + volInfo .getAttachedVmName ());
2816+ } else {
2817+ s_logger .debug ("Volume: " + volInfo .getName () + " failed to attach to VM: " + volInfo .getAttachedVmName ());
28132818 }
2819+ volInfo .stateTransit (ev );
28142820 }
28152821 return _volsDao .findById (volumeToAttach .getId ());
28162822 }
0 commit comments