[PATCH 07/10] linkstate: Add usage of the new api to virsh

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

 



---
 tools/virsh.c   |  120 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/virsh.pod |   11 +++++
 2 files changed, 131 insertions(+), 0 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 1d660d0..b4c6e21 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -1161,6 +1161,124 @@ cmdDomIfstat (vshControl *ctl, const vshCmd *cmd)
     return true;
 }

+/* "domif-setlink" command
+ */
+static const vshCmdInfo info_domif_setlink[] = {
+    {"help", N_("set link state of a virtual interface")},
+    {"desc", N_("Set link state of a domain's virtual interface.")},
+    {NULL,NULL}
+};
+
+static const vshCmdOptDef opts_domif_setlink[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {"interface", VSH_OT_DATA, VSH_OFLAG_REQ, N_("interface device (MAC Address)")},
+    {"state", VSH_OT_DATA, VSH_OFLAG_REQ, N_("new state of the device")},
+    {"persistent", VSH_OT_BOOL, 0, N_("persist interface state")},
+    {NULL, 0, 0, NULL}
+};
+
+static bool
+cmdDomIfSetLink (vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom;
+    const char *name = NULL, *device = NULL, *state_str = NULL;
+    int flags = VIR_DOMAIN_AFFECT_CURRENT;
+    int state = VIR_LINK_STATE_DEFAULT;
+
+
+    if (!vshConnectionUsability (ctl, ctl->conn))
+        return false;
+
+    if (!(dom = vshCommandOptDomain (ctl, cmd, &name)))
+        return false;
+
+    if (vshCommandOptString (cmd, "interface", &device) <= 0) {
+        virDomainFree(dom);
+        return false;
+    }
+
+    if (vshCommandOptString (cmd, "state", &state_str) <= 0) {
+        virDomainFree(dom);
+        return false;
+    }
+
+    if (vshCommandOptBool(cmd, "persistent"))
+        flags = VIR_DOMAIN_AFFECT_CONFIG;
+
+    /* parse state */
+    if (STREQ(state_str, "up"))
+        state = VIR_LINK_STATE_UP;
+    else if (STREQ(state_str, "down"))
+        state = VIR_LINK_STATE_DOWN;
+    else {
+        vshError(ctl, _("Invalid state %s"), state_str);
+        virDomainFree(dom);
+        return false;
+    }
+
+    if (virDomainInterfaceLinkSetState (dom, device, state, flags) == -1) {
+        vshError(ctl, _("Failed to set interface state %s: %s"), device, state_str);
+        virDomainFree(dom);
+        return false;
+    }
+
+    virDomainFree(dom);
+    return true;
+}
+
+/* "domif-getlink" command
+ */
+static const vshCmdInfo info_domif_getlink[] = {
+    {"help", N_("get link state of a virtual interface")},
+    {"desc", N_("Get link state of a domain's virtual interface.")},
+    {NULL,NULL}
+};
+
+static const vshCmdOptDef opts_domif_getlink[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {"interface", VSH_OT_DATA, VSH_OFLAG_REQ, N_("interface device (MAC Address)")},
+    {"persistent", VSH_OT_BOOL, 0, N_("Get persistent interface state")},
+    {NULL, 0, 0, NULL}
+};
+
+static bool
+cmdDomIfGetLink (vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom;
+    const char *name = NULL, *device = NULL;
+    int flags = VIR_DOMAIN_AFFECT_CURRENT;
+    int state;
+
+
+    if (!vshConnectionUsability (ctl, ctl->conn))
+        return false;
+
+    if (!(dom = vshCommandOptDomain (ctl, cmd, &name)))
+        return false;
+
+    if (vshCommandOptString (cmd, "interface", &device) <= 0) {
+        virDomainFree(dom);
+        return false;
+    }
+
+    if (vshCommandOptBool(cmd, "persistent"))
+        flags = VIR_DOMAIN_AFFECT_CONFIG;
+
+    if ((state = virDomainInterfaceLinkGetState (dom, device, flags)) == -1) {
+        vshError(ctl, _("Failed to get interface state %s"), device);
+        virDomainFree(dom);
+        return false;
+    }
+
+    if (state == VIR_LINK_STATE_DEFAULT || state == VIR_LINK_STATE_UP)
+        vshPrint(ctl, _("Interface %s is up.\n"), device);
+    else
+        vshPrint(ctl, _("Interface %s is down.\n"), device);
+
+    virDomainFree(dom);
+    return true;
+}
+
 /*
  * "dommemstats" command
  */
@@ -12521,6 +12639,7 @@ static const vshCmdDef domManagementCmds[] = {
     {"detach-interface", cmdDetachInterface, opts_detach_interface,
      info_detach_interface, 0},
     {"domid", cmdDomid, opts_domid, info_domid, 0},
+    {"domif-setlink", cmdDomIfSetLink, opts_domif_setlink, info_domif_setlink, 0},
     {"domjobabort", cmdDomjobabort, opts_domjobabort, info_domjobabort, 0},
     {"domjobinfo", cmdDomjobinfo, opts_domjobinfo, info_domjobinfo, 0},
     {"domname", cmdDomname, opts_domname, info_domname, 0},
@@ -12578,6 +12697,7 @@ static const vshCmdDef domMonitoringCmds[] = {
     {"domblkinfo", cmdDomblkinfo, opts_domblkinfo, info_domblkinfo, 0},
     {"domblkstat", cmdDomblkstat, opts_domblkstat, info_domblkstat, 0},
     {"domcontrol", cmdDomControl, opts_domcontrol, info_domcontrol, 0},
+    {"domif-getlink", cmdDomIfGetLink, opts_domif_getlink, info_domif_getlink, 0},
     {"domifstat", cmdDomIfstat, opts_domifstat, info_domifstat, 0},
     {"dominfo", cmdDominfo, opts_dominfo, info_dominfo, 0},
     {"dommemstat", cmdDomMemStat, opts_dommemstat, info_dommemstat, 0},
diff --git a/tools/virsh.pod b/tools/virsh.pod
index a6af1e6..915fc6d 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -422,6 +422,17 @@ Get device block stats for a running domain.

 Get network interface stats for a running domain.

+=item B<domif-setlink> I<domain> I<interface-MAC> I<state> I<--persistent>
+
+Modify link state of the domain's virtual interface. Possible values for
+state are "up" and "down. If --persistent is specified, modify
+persistent configuration of the domain.
+
+=item B<domif-setlink> I<domain> I<interface-MAC> I<--persistent>
+
+Query link state of the domain's virtual interface. If --persistent
+is specified, query the persistent configuration.
+
 =item B<dommemstat> I<domain>

 Get memory stats for a running domain.
-- 
1.7.6

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]