From: Nitesh_Konkar <niteshkonkar.libvirt(a)gmail.com>
---
tools/virsh-domain.c | 103 +++++++++++++++++++++++++++++++++------------------
1 file changed, 64 insertions(+), 39 deletions(-)
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 979f115..a15ef40 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
-static bool
-cmdDetachInterface(vshControl *ctl, const vshCmd *cmd)
+static bool
+vshDomainDetachInterface(char *doc, unsigned int flags, virDomainPtr dom, vshControl
*ctl, const vshCmd *cmd)
{
- virDomainPtr dom = NULL;
xmlDocPtr xml = NULL;
xmlXPathObjectPtr obj = NULL;
xmlXPathContextPtr ctxt = NULL;
xmlNodePtr cur = NULL, matchNode = NULL;
- char *detach_xml = NULL;
const char *mac = NULL, *type = NULL;
- char *doc = NULL;
+ char *detach_xml = NULL;
+ bool current = vshCommandOptBool(cmd, "current");
char buf[64];
int diff_mac;
size_t i;
int ret;
bool functionReturn = false;
- unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
- bool current = vshCommandOptBool(cmd, "current");
- bool config = vshCommandOptBool(cmd, "config");
- bool live = vshCommandOptBool(cmd, "live");
- bool persistent = vshCommandOptBool(cmd, "persistent");
-
- VSH_EXCLUSIVE_OPTIONS_VAR(persistent, current);
-
- VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
- VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
-
- if (config || persistent)
- flags |= VIR_DOMAIN_AFFECT_CONFIG;
- if (live)
- flags |= VIR_DOMAIN_AFFECT_LIVE;
-
- if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
- return false;
if (vshCommandOptStringReq(ctl, cmd, "type", &type) < 0)
goto cleanup;
@@ -10859,15 +10823,6 @@ cmdDetachInterface(vshControl *ctl, const vshCmd *cmd)
if (vshCommandOptStringReq(ctl, cmd, "mac", &mac) < 0)
goto cleanup;
- if (persistent &&
- virDomainIsActive(dom) == 1)
- flags |= VIR_DOMAIN_AFFECT_LIVE;
-
- if (flags & VIR_DOMAIN_AFFECT_CONFIG)
- doc = virDomainGetXMLDesc(dom, VIR_DOMAIN_XML_INACTIVE);
- else
- doc = virDomainGetXMLDesc(dom, 0);
-
if (!doc)
goto cleanup;
@@ -10935,20 +10890,72 @@ cmdDetachInterface(vshControl *ctl, const vshCmd *cmd)
else
ret = virDomainDetachDevice(dom, detach_xml);
- if (ret != 0) {
- vshError(ctl, "%s", _("Failed to detach interface"));
- } else {
- vshPrint(ctl, "%s", _("Interface detached successfully\n"));
+ if (ret == 0)
functionReturn = true;
- }
cleanup:
- VIR_FREE(doc);
VIR_FREE(detach_xml);
- virDomainFree(dom);
+ xmlFreeDoc(xml);
xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt);
- xmlFreeDoc(xml);
+ return functionReturn;
+}
+
+static bool
+cmdDetachInterface(vshControl *ctl, const vshCmd *cmd)
+{
+ virDomainPtr dom = NULL;
+ unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
+ char *doc_live = NULL, *doc_config = NULL;
+ bool current = vshCommandOptBool(cmd, "current");
+ bool config = vshCommandOptBool(cmd, "config");
+ bool live = vshCommandOptBool(cmd, "live");
+ bool persistent = vshCommandOptBool(cmd, "persistent");
+ bool functionReturn = false;
+
+ VSH_EXCLUSIVE_OPTIONS_VAR(persistent, current);
+
+ VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
+ VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
+
+ if (config || persistent)
+ flags |= VIR_DOMAIN_AFFECT_CONFIG;
+
+ if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
+ return false;
+
+ if (persistent &&
+ virDomainIsActive(dom) == 1)
+ flags |= VIR_DOMAIN_AFFECT_LIVE;
+
+ if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
+ doc_config = virDomainGetXMLDesc(dom, VIR_DOMAIN_XML_INACTIVE);
+ if (!(functionReturn = vshDomainDetachInterface(doc_config, flags, dom, ctl,
cmd)))
+ goto cleanup;
+ }
+
+ if (live || (!live && !config))
+ flags |= VIR_DOMAIN_AFFECT_LIVE;
+
+ if (flags & VIR_DOMAIN_AFFECT_LIVE) {
+ doc_live = virDomainGetXMLDesc(dom, 0);
+
+ if (flags & VIR_DOMAIN_AFFECT_CONFIG)
+ flags &= (~VIR_DOMAIN_AFFECT_CONFIG);
+
+ functionReturn = vshDomainDetachInterface(doc_live, flags, dom, ctl, cmd);
+ }
+
+ cleanup:
+ if (functionReturn == false) {
+ vshError(ctl, "%s", _("Failed to detach interface"));
+ } else {
+ vshPrint(ctl, "%s", _("Interface detached successfully\n"));
+ functionReturn = true;
+ }
+ VIR_FREE(doc_live);
+ VIR_FREE(doc_config);
+ virDomainFree(dom);
return functionReturn;
}