[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v4 6/6] undefine: Extend virsh undefine to support the new flag



If the domain has managed save image, and --managed-save is
not specified, then it fails with an error telling the user
that a managed save image still exists.

If the domain has managed save image, and --managed-save is
specified, it invokes virDomainUndefineFlags. If
virDomainUndefineFlags fails, then it tries to remove the managed
save image using virDomainManagedSaveRemove first, with
invoking virDomainUndefine following. (For compatibility between
new virsh with this patch and older libvirt without this patch).

Similarly if the domain has no managed save image. See the codes for
detail.

NOTE: Have not removing the codes checking if the domain is running
in function "cmdUndefine", it will go along with qemu driver's fix
(allow to undefine a running domain).
---
 tools/virsh.c   |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 tools/virsh.pod |   14 ++++++++++--
 2 files changed, 71 insertions(+), 4 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index a6803d8..e50fd8a 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -1420,6 +1420,7 @@ static const vshCmdInfo info_undefine[] = {
 
 static const vshCmdOptDef opts_undefine[] = {
     {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name or uuid")},
+    {"managed-save", VSH_OT_BOOL, 0, N_("remove domain managed state file")},
     {NULL, 0, 0, NULL}
 };
 
@@ -1430,6 +1431,16 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
     bool ret = true;
     const char *name = NULL;
     int id;
+    int flags = 0;
+    int managed_save = vshCommandOptBool(cmd, "managed-save");
+    int has_managed_save = 0;
+    int rc = -1;
+
+    if (managed_save)
+        flags |= VIR_DOMAIN_UNDEFINE_MANAGED_SAVE;
+
+    if (!managed_save)
+        flags = -1;
 
     if (!vshConnectionUsability(ctl, ctl->conn))
         return false;
@@ -1451,7 +1462,55 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
                                       VSH_BYNAME|VSH_BYUUID)))
         return false;
 
-    if (virDomainUndefine(dom) == 0) {
+    has_managed_save = virDomainHasManagedSaveImage(dom, 0);
+    if (has_managed_save < 0) {
+        if (last_error->code != VIR_ERR_NO_SUPPORT) {
+            virshReportError(ctl);
+            virDomainFree(dom);
+            return false;
+        } else {
+            virFreeError(last_error);
+            last_error = NULL;
+        }
+    }
+
+    if (flags == -1) {
+        if (has_managed_save == 1) {
+            vshError(ctl,
+                     _("Refusing to undefine while domain managed save "
+                       "image exists"));
+            virDomainFree(dom);
+            return false;
+        }
+
+        rc = virDomainUndefine(dom);
+    } else {
+        rc = virDomainUndefineFlags(dom, flags);
+
+        /* It might fail for virDomainUndefineFlags is not
+         * supported on older libvirt, try to undefine the
+         * domain with combo virDomainManagedSaveRemove and
+         * virDomainUndefine.
+         */
+        if (rc < 0) {
+            if (last_error->code != VIR_ERR_NO_SUPPORT) {
+                virshReportError(ctl);
+                goto end;
+            } else {
+                virFreeError(last_error);
+                last_error = NULL;
+            }
+
+            if (has_managed_save &&
+                virDomainManagedSaveRemove(dom, 0) < 0)
+                goto end;
+
+            rc = virDomainUndefine(dom);
+        }
+    }
+
+end:
+    if (rc == 0) {
         vshPrint(ctl, _("Domain %s has been undefined\n"), name);
     } else {
         vshError(ctl, _("Failed to undefine domain %s"), name);
diff --git a/tools/virsh.pod b/tools/virsh.pod
index af762ca..9cd2853 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -813,10 +813,18 @@ hypervisor.
 Output the device used for the TTY console of the domain. If the information
 is not available the processes will provide an exit code of 1.
 
-=item B<undefine> I<domain-id>
+=item B<undefine> I<domain-id> [I<--managed-save>]
 
-Undefine the configuration for an inactive domain. Since it's not running
-the domain name or UUID must be used as the I<domain-id>.
+Undefine a domain. If the domain is running, this converts it to a
+transient domain, without stopping it. If the domain is inactive,
+the domain configuration is removed.
+
+The I<--managed-save> flag guarantees that any managed save image(see
+the B<managedsave> command) is also cleaned up.  Without the flag, attempts
+to undefine a domain with a managed save image will fail.
+
+NOTE: For an inactive domain, the domain name or UUID must be used as the
+I<domain-id>.
 
 =item B<vcpucount> I<domain-id>  [{I<--maximum> | I<--current>}
 {I<--config> | I<--live>}]
-- 
1.7.6


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]