diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 32c51029e8c80298121f0cee2587bb87e3801f77..48dd9fbc2bc549d9699c27fc0c5c57eeb5ce8614 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -28,6 +28,7 @@ #include "internal.h" #include "viralloc.h" #include "virstring.h" +#include "virxml.h" char ** @@ -87,3 +88,62 @@ virshDomainNameCompleter(vshControl *ctl, VIR_FREE(ret); return NULL; } + + +char ** +virshDomainInterfaceCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + xmlDocPtr xmldoc = NULL; + xmlXPathContextPtr ctxt = NULL; + int ninterfaces; + xmlNodePtr *interfaces = NULL; + size_t i; + unsigned int domainXMLFlags = 0; + char **ret = NULL; + + virCheckFlags(VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if (vshCommandOptBool(cmd, "config")) + domainXMLFlags = VIR_DOMAIN_XML_INACTIVE; + + if (virshDomainGetXML(ctl, cmd, domainXMLFlags, &xmldoc, &ctxt) < 0) + goto error; + + ninterfaces = virXPathNodeSet("./devices/interface", ctxt, &interfaces); + if (ninterfaces < 0) + goto error; + + if (VIR_ALLOC_N(ret, ninterfaces + 1) < 0) + goto error; + + for (i = 0; i < ninterfaces; i++) { + ctxt->node = interfaces[i]; + + if (!(flags & VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC) && + (ret[i] = virXPathString("string(./target/@dev)", ctxt))) + continue; + + /* In case we are dealing with inactive domain XML there's no + * . Offer MAC addresses then. */ + if (!(ret[i] = virXPathString("string(./mac/@address)", ctxt))) + goto error; + } + + VIR_FREE(interfaces); + xmlFreeDoc(xmldoc); + xmlXPathFreeContext(ctxt); + return ret; + + error: + VIR_FREE(interfaces); + xmlFreeDoc(xmldoc); + xmlXPathFreeContext(ctxt); + virStringListFree(ret); + return NULL; +} diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index 288e179096eb11d6b6f6149636ed5eab02610d27..1a2dd685fb05a27b1de1d4e836b5e6a6201ed56c 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -30,4 +30,12 @@ char ** virshDomainNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); +enum { + VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC = 1 << 0, /* Return just MACs */ +}; + +char ** virshDomainInterfaceCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + #endif diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c index a09eb010c3d871e2782dda4e23f34f306b08fcc8..32a42707eb25cf4e0faa9ce80e125d650abf2229 100644 --- a/tools/virsh-domain-monitor.c +++ b/tools/virsh-domain-monitor.c @@ -659,6 +659,7 @@ static const vshCmdOptDef opts_domif_getlink[] = { {.name = "interface", .type = VSH_OT_DATA, .flags = VSH_OFLAG_REQ, + .completer = virshDomainInterfaceCompleter, .help = N_("interface device (MAC Address)") }, {.name = "persistent", @@ -995,6 +996,7 @@ static const vshCmdOptDef opts_domifstat[] = { {.name = "interface", .type = VSH_OT_DATA, .flags = VSH_OFLAG_REQ, + .completer = virshDomainInterfaceCompleter, .help = N_("interface device specified by name or MAC Address") }, {.name = NULL} @@ -2149,6 +2151,7 @@ static const vshCmdOptDef opts_domifaddr[] = { {.name = "interface", .type = VSH_OT_STRING, .flags = VSH_OFLAG_NONE, + .completer = virshDomainInterfaceCompleter, .help = N_("network interface name")}, {.name = "full", .type = VSH_OT_BOOL, diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 13f8db3ddaded8a8f78278665c131fde523dbd0f..0f329d6d72481fe81908519cd49d4487f451391c 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -2977,6 +2977,7 @@ static const vshCmdOptDef opts_domif_setlink[] = { {.name = "interface", .type = VSH_OT_DATA, .flags = VSH_OFLAG_REQ, + .completer = virshDomainInterfaceCompleter, .help = N_("interface device (MAC Address)") }, {.name = "state", @@ -3147,6 +3148,7 @@ static const vshCmdOptDef opts_domiftune[] = { {.name = "interface", .type = VSH_OT_DATA, .flags = VSH_OFLAG_REQ, + .completer = virshDomainInterfaceCompleter, .help = N_("interface device (MAC Address)") }, {.name = "inbound", @@ -11983,6 +11985,8 @@ static const vshCmdOptDef opts_detach_interface[] = { }, {.name = "mac", .type = VSH_OT_STRING, + .completer = virshDomainInterfaceCompleter, + .completer_flags = VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC, .help = N_("MAC address") }, VIRSH_COMMON_OPT_DOMAIN_PERSISTENT,