提交 7e7db4bf 编写于 作者: M Michal Privoznik

virsh: Introduce virshDomainInterfaceCompleter

For given domain fetch list of defined interfaces. This can be
used for commands like domif-getlink and others. If available,
the interface name is returned (e.g. "vnet0", usually available
only for running domains), if not the MAC address is returned.
Moreover, the detach-interface command requires only MAC address
and therefore we have new flag that forces the completer to
return just the MAC address.
Signed-off-by: NMichal Privoznik <mprivozn@redhat.com>
上级 ec3403be
......@@ -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
* <target dev=''/>. 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;
}
......@@ -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
......@@ -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,
......
......@@ -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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册