From nobody Sun May 19 11:36:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1531404493944563.5118992313735; Thu, 12 Jul 2018 07:08:13 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E5FC1308FBAA; Thu, 12 Jul 2018 14:08:10 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DE56A5D9CA; Thu, 12 Jul 2018 14:08:09 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id A8AF71800B69; Thu, 12 Jul 2018 14:08:08 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6CE86Pa015204 for ; Thu, 12 Jul 2018 10:08:06 -0400 Received: by smtp.corp.redhat.com (Postfix) id 95E701102E22; Thu, 12 Jul 2018 14:08:06 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.43.2.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 15CD91102E1F; Thu, 12 Jul 2018 14:08:03 +0000 (UTC) From: Simon Kobyda To: libvir-list@redhat.com Date: Thu, 12 Jul 2018 16:07:59 +0200 Message-Id: <20180712140759.3995-1-skobyda@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-loop: libvir-list@redhat.com Cc: Simon Kobyda Subject: [libvirt] [PATCH v2] New virsh feature: domif-setlink --domain --interface --state completer X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Thu, 12 Jul 2018 14:08:12 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" After you go through command mentioned above, completer finds what state the device is currently in, and suggests an opposite state. Signed-off-by: Simon Kobyda --- Changes in V2: - Added "Signed-off-by" - Changed format of commit message to make it more readable tools/virsh-completer.c | 73 ++++++++++++++++++++++++++++++++++++++++- tools/virsh-completer.h | 4 +++ tools/virsh-domain.c | 1 + 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 2327e08340..e32fd82211 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -32,6 +32,7 @@ #include "internal.h" #include "virutil.h" #include "viralloc.h" +#include "virmacaddr.h" #include "virstring.h" #include "virxml.h" =20 @@ -750,7 +751,6 @@ virshDomainEventNameCompleter(vshControl *ctl ATTRIBUTE= _UNUSED, return NULL; } =20 - char ** virshPoolEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, const vshCmd *cmd ATTRIBUTE_UNUSED, @@ -776,6 +776,77 @@ virshPoolEventNameCompleter(vshControl *ctl ATTRIBUTE_= UNUSED, return NULL; } =20 +char ** +virshDomainInterfaceStateCompleter(vshControl *ctl ATTRIBUTE_UNUSED, + const vshCmd *cmd ATTRIBUTE_UNUSED, + unsigned int flags) +{ + virshControlPtr priv =3D ctl->privData; + const char *iface =3D NULL; + char **ret =3D NULL; + xmlDocPtr xml =3D NULL; + virMacAddr macaddr; + char *state =3D NULL; + char *xpath =3D NULL; + char macstr[18] =3D ""; + xmlXPathContextPtr ctxt =3D NULL; + xmlNodePtr *interfaces =3D NULL; + int ninterfaces; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <=3D 0) + return NULL; + + if (vshCommandOptStringReq(ctl, cmd, "interface", &iface) < 0) + goto cleanup; + + if (virshDomainGetXML(ctl, cmd, flags, &xml, &ctxt) < 0) + goto cleanup; + + /* normalize the mac addr */ + if (virMacAddrParse(iface, &macaddr) =3D=3D 0) + virMacAddrFormat(&macaddr, macstr); + + if (virAsprintf(&xpath, "/domain/devices/interface[(mac/@address =3D '= %s') or " + " (target/@dev =3D '%= s')]", + macstr, iface) < 0) + goto cleanup; + + if ((ninterfaces =3D virXPathNodeSet(xpath, ctxt, &interfaces)) < 0) + goto cleanup; + + if (ninterfaces !=3D 1) + goto cleanup; + + ctxt->node =3D interfaces[0]; + + if (VIR_ALLOC_N(ret, 2) < 0) + goto error; + + if ((state =3D virXPathString("string(./link/@state)", ctxt)) && + STREQ(state, "down")) { + if (VIR_STRDUP(ret[0], "up") < 0) + goto error; + } else { + if (VIR_STRDUP(ret[0], "down") < 0) + goto error; + } + + cleanup: + VIR_FREE(state); + VIR_FREE(interfaces); + VIR_FREE(xpath); + xmlXPathFreeContext(ctxt); + xmlFreeDoc(xml); + return ret; + + error: + virStringListFree(ret); + ret =3D NULL; + goto cleanup; +} + =20 char ** virshNodedevEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED, diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index 1c14bb2a54..b4fd2a86c6 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -94,6 +94,10 @@ char ** virshPoolEventNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); =20 +char ** virshDomainInterfaceStateCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + char ** virshNodedevEventNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 3959b5475b..8adec1d9b1 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -3064,6 +3064,7 @@ static const vshCmdOptDef opts_domif_setlink[] =3D { {.name =3D "state", .type =3D VSH_OT_DATA, .flags =3D VSH_OFLAG_REQ, + .completer =3D virshDomainInterfaceStateCompleter, .help =3D N_("new state of the device") }, {.name =3D "persistent", --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list