@@ -2985,7 +2985,9 @@ public UserVm destroyVm(DestroyVMCmd cmd) throws ResourceUnavailableException, C
29852985
29862986 stopVirtualMachine (vmId , VmDestroyForcestop .value ());
29872987
2988- detachVolumesFromVm (volumesToBeDeleted );
2988+ // Detach all data disks from VM
2989+ List <VolumeVO > dataVols = _volsDao .findByInstanceAndType (vmId , Volume .Type .DATADISK );
2990+ detachVolumesFromVm (dataVols );
29892991
29902992 UserVm destroyedVm = destroyVm (vmId , expunge );
29912993 if (expunge ) {
@@ -2994,7 +2996,7 @@ public UserVm destroyVm(DestroyVMCmd cmd) throws ResourceUnavailableException, C
29942996 }
29952997 }
29962998
2997- deleteVolumesFromVm (volumesToBeDeleted );
2999+ deleteVolumesFromVm (volumesToBeDeleted , expunge );
29983000
29993001 return destroyedVm ;
30003002 }
@@ -7322,13 +7324,13 @@ private void checkForUnattachedVolumes(long vmId, List<VolumeVO> volumes) {
73227324 StringBuilder sb = new StringBuilder ();
73237325
73247326 for (VolumeVO volume : volumes ) {
7325- if (volume .getInstanceId () == null || vmId != volume .getInstanceId ()) {
7327+ if (volume .getInstanceId () == null || vmId != volume .getInstanceId () || volume . getVolumeType () != Volume . Type . DATADISK ) {
73267328 sb .append (volume .toString () + "; " );
73277329 }
73287330 }
73297331
73307332 if (!StringUtils .isEmpty (sb .toString ())) {
7331- throw new InvalidParameterValueException ("The following supplied volumes are not attached to the VM: " + sb .toString ());
7333+ throw new InvalidParameterValueException ("The following supplied volumes are not DATADISK attached to the VM: " + sb .toString ());
73327334 }
73337335 }
73347336
@@ -7353,13 +7355,13 @@ private void detachVolumesFromVm(List<VolumeVO> volumes) {
73537355 }
73547356 }
73557357
7356- private void deleteVolumesFromVm (List <VolumeVO > volumes ) {
7358+ private void deleteVolumesFromVm (List <VolumeVO > volumes , boolean expunge ) {
73577359
73587360 for (VolumeVO volume : volumes ) {
73597361
7360- boolean deleteResult = _volumeService .deleteVolume (volume .getId (), CallContext .current ().getCallingAccount ());
7362+ Volume result = _volumeService .destroyVolume (volume .getId (), CallContext .current ().getCallingAccount (), expunge , false );
73617363
7362- if (! deleteResult ) {
7364+ if (result == null ) {
73637365 s_logger .error ("DestroyVM remove volume - failed to delete volume " + volume .getInstanceId () + " from instance " + volume .getId ());
73647366 }
73657367 }
0 commit comments