On 10/31/2011 11:33 AM, zituan@taobao.com wrote:
There was a similiar patch months ago:

http://www.redhat.com/archives/libvir-list/2011-June/msg00362.html,
but I like your patch more, as it falls back to use lvchange but not
use lvchange directly before lvremove.

From: Chang Liu <lingjiao.lc@taobao.com>

We found an issue that virStorageBackendLogicalDeleteVol() could not remove
the lv with notificaton "could not remove open logical volume.", in such
situation, we should disable the lv first, then delete it. this patch fix it.

*src/storage/storage_backend_logical.c
(virStorageBackendLogicalDeleteVol):lvremove fail, lvchange the volume
and then lvremove it second.

Signed-off-by: Chang Liu <lingjiao.lc@taobao.com>
---
 configure.ac                          |    4 ++++
 src/storage/storage_backend_logical.c |   14 ++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/configure.ac b/configure.ac
index 5753c08..6092c47 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1691,6 +1691,7 @@ if test "$with_storage_lvm" = "yes" || test "$with_storage_lvm" = "check"; then
   AC_PATH_PROG([PVREMOVE], [pvremove], [], [$PATH:/sbin:/usr/sbin])
   AC_PATH_PROG([VGREMOVE], [vgremove], [], [$PATH:/sbin:/usr/sbin])
   AC_PATH_PROG([LVREMOVE], [lvremove], [], [$PATH:/sbin:/usr/sbin])
+  AC_PATH_PROG([LVCHANGE], [lvchange], [], [$PATH:/sbin:/usr/sbin])
   AC_PATH_PROG([VGCHANGE], [vgchange], [], [$PATH:/sbin:/usr/sbin])
   AC_PATH_PROG([VGSCAN], [vgscan], [], [$PATH:/sbin:/usr/sbin])
   AC_PATH_PROG([PVS], [pvs], [], [$PATH:/sbin:/usr/sbin])
@@ -1704,6 +1705,7 @@ if test "$with_storage_lvm" = "yes" || test "$with_storage_lvm" = "check"; then
     if test -z "$PVREMOVE" ; then AC_MSG_ERROR([We need pvremove for LVM storage driver]) ; fi
     if test -z "$VGREMOVE" ; then AC_MSG_ERROR([We need vgremove for LVM storage driver]) ; fi
     if test -z "$LVREMOVE" ; then AC_MSG_ERROR([We need lvremove for LVM storage driver]) ; fi
+    if test -z "$LVCHANGE" ; then AC_MSG_ERROR([We need lvchange for LVM storage driver]) ; fi
     if test -z "$VGCHANGE" ; then AC_MSG_ERROR([We need vgchange for LVM storage driver]) ; fi
     if test -z "$VGSCAN" ; then AC_MSG_ERROR([We need vgscan for LVM storage driver]) ; fi
     if test -z "$PVS" ; then AC_MSG_ERROR([We need pvs for LVM storage driver]) ; fi
@@ -1716,6 +1718,7 @@ if test "$with_storage_lvm" = "yes" || test "$with_storage_lvm" = "check"; then
     if test -z "$PVREMOVE" ; then with_storage_lvm=no ; fi
     if test -z "$VGREMOVE" ; then with_storage_lvm=no ; fi
     if test -z "$LVREMOVE" ; then with_storage_lvm=no ; fi
+    if test -z "$LVCHANGE" ; then with_storage_lvm=no ; fi
     if test -z "$VGCHANGE" ; then with_storage_lvm=no ; fi
     if test -z "$VGSCAN" ; then with_storage_lvm=no ; fi
     if test -z "$PVS" ; then with_storage_lvm=no ; fi
@@ -1733,6 +1736,7 @@ if test "$with_storage_lvm" = "yes" || test "$with_storage_lvm" = "check"; then
     AC_DEFINE_UNQUOTED([PVREMOVE],["$PVREMOVE"],[Location of pvremove program])
     AC_DEFINE_UNQUOTED([VGREMOVE],["$VGREMOVE"],[Location of vgremove program])
     AC_DEFINE_UNQUOTED([LVREMOVE],["$LVREMOVE"],[Location of lvremove program])
+    AC_DEFINE_UNQUOTED([LVCHANGE],["$LVCHANGE"],[Location of lvchange program])
     AC_DEFINE_UNQUOTED([VGCHANGE],["$VGCHANGE"],[Location of vgchange program])
     AC_DEFINE_UNQUOTED([VGSCAN],["$VGSCAN"],[Location of vgscan program])
     AC_DEFINE_UNQUOTED([PVS],["$PVS"],[Location of pvs program])
diff --git a/src/storage/storage_backend_logical.c b/src/storage/storage_backend_logical.c
index 3c3e736..4a2acf1 100644
--- a/src/storage/storage_backend_logical.c
+++ b/src/storage/storage_backend_logical.c
@@ -772,13 +772,23 @@ virStorageBackendLogicalDeleteVol(virConnectPtr conn ATTRIBUTE_UNUSED,
         LVREMOVE, "-f", vol->target.path, NULL
     };
 
+    const char *lvchange_cmd[] = {
+        LVCHANGE, "-a", "n", vol->target.path, NULL
+    };

Using virCommand APIs is prefered.

http://libvirt.org/internals/command.html

And it might need to use "-aly" in case of cluster vols.
see:
http://www.redhat.com/archives/libvir-list/2011-October/msg01116.html

+
     virCheckFlags(0, -1);
 
     virFileWaitForDevices();
 
     if (virRun(cmdargv, NULL) < 0)
-        return -1;
-
+    {
+        if(virRun(lvchange_cmd, NULL) < 0)
+            return -1;
+        else{
+            if(virRun(cmdargv,NULL) < 0)
+                return -1;
+        }
+    }
     return 0;
 }