From 0cbafdec62da7fdec64814baf1bb45f20d48d2f0 Mon Sep 17 00:00:00 2001 From: Rene Peinthor Date: Sat, 13 Jul 2024 09:30:54 +0200 Subject: [PATCH] libvirtstorageadaptor: better handle failed destroy --- .../kvm/storage/LibvirtStorageAdaptor.java | 58 +++++++++++++------ 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java index cecabc96b9f4..aeba713b7f97 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java @@ -741,6 +741,44 @@ public KVMStoragePool createStoragePool(String name, String host, int port, Stri } } + private boolean destroyPool(Connect conn, String uuid) throws LibvirtException + { + StoragePool sp; + try { + sp = conn.storagePoolLookupByUUIDString(uuid); + } catch (LibvirtException exc) { + s_logger.warn("Storage pool " + uuid + " doesn't exist in libvirt. Assuming it is already removed"); + return true; + } + + if (sp != null) { + if (sp.isPersistent() == 1) { + sp.destroy(); + sp.undefine(); + } else { + sp.destroy(); + } + sp.free(); + + return true; + } else { + s_logger.warn("Storage pool " + uuid + " doesn't exist in libvirt. Assuming it is already removed"); + return false; + } + } + + private boolean destroyPoolHandleException(Connect conn, String uuid) + { + try { + return destroyPool(conn, uuid); + } + catch (LibvirtException e) + { + s_logger.error(String.format("Failed to destroy libvirt pool %s: %s", uuid, e)); + } + return false; + } + @Override public boolean deleteStoragePool(String uuid) { s_logger.info("Attempting to remove storage pool " + uuid + " from libvirt"); @@ -750,23 +788,14 @@ public boolean deleteStoragePool(String uuid) { return true; } - Connect conn = null; + Connect conn; try { conn = LibvirtConnection.getConnection(); } catch (LibvirtException e) { throw new CloudRuntimeException(e.toString()); } - StoragePool sp = null; Secret s = null; - - try { - sp = conn.storagePoolLookupByUUIDString(uuid); - } catch (LibvirtException e) { - s_logger.warn("Storage pool " + uuid + " doesn't exist in libvirt. Assuming it is already removed"); - return true; - } - /* * Some storage pools, like RBD also have 'secret' information stored in libvirt * Destroy them if they exist @@ -778,13 +807,7 @@ public boolean deleteStoragePool(String uuid) { } try { - if (sp.isPersistent() == 1) { - sp.destroy(); - sp.undefine(); - } else { - sp.destroy(); - } - sp.free(); + destroyPool(conn, uuid); if (s != null) { s.undefine(); s.free(); @@ -802,6 +825,7 @@ public boolean deleteStoragePool(String uuid) { String result = Script.runSimpleBashScript("sleep 5 && umount " + targetPath); if (result == null) { s_logger.info("Succeeded in unmounting " + targetPath); + destroyPoolHandleException(conn, uuid); return true; } s_logger.error("Failed to unmount " + targetPath);