diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java index 34c610dd9a1..2ad3089cf37 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java @@ -70,6 +70,7 @@ import org.apache.cloudstack.utils.qemu.QemuImgException; import org.apache.cloudstack.utils.qemu.QemuImgFile; import org.apache.commons.collections.MapUtils; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.libvirt.Connect; import org.libvirt.Domain; @@ -1195,6 +1196,14 @@ public class KVMStorageProcessor implements StorageProcessor { } else { s_logger.debug("Detaching device: " + xml); dm.detachDevice(xml); + LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser(); + parser.parseDomainXML(dm.getXMLDesc(0)); + List disks = parser.getDisks(); + for (DiskDef diskDef : disks) { + if (StringUtils.contains(xml, diskDef.getDiskPath())) { + throw new InternalErrorException("Could not detach volume. Probably the VM is in boot state at the moment"); + } + } } } catch (final LibvirtException e) { if (attach) { diff --git a/test/integration/smoke/test_volumes.py b/test/integration/smoke/test_volumes.py index 107403e2df3..fa7bea0b9c1 100644 --- a/test/integration/smoke/test_volumes.py +++ b/test/integration/smoke/test_volumes.py @@ -385,11 +385,15 @@ class TestVolumes(cloudstackTestCase): def tearDown(self): #Clean up, terminate the created volumes if self.attached: + self.virtual_machine.get_ssh_client(reconnect = True) + self.virtual_machine.detach_volume(self.apiClient, self.volume) if self.virtual_machine.hypervisor == "KVM": self.virtual_machine.stop(self.apiClient) self.virtual_machine.start(self.apiClient) + self.virtual_machine.get_ssh_client(reconnect = True) + cleanup_resources(self.apiClient, self.cleanup) return