[libvirt] [PATCH 5/5] virsh: Expose virDomainDetachDeviceAlias

Michal Privoznik mprivozn at redhat.com
Mon May 21 16:08:02 UTC 2018


Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 tools/virsh-domain.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/virsh.pod      | 15 ++++++++++
 2 files changed, 94 insertions(+)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index cfbbf5a7bc..b41e76b3d8 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -11878,6 +11878,79 @@ cmdDetachDevice(vshControl *ctl, const vshCmd *cmd)
     return funcRet;
 }
 
+
+/*
+ * "detach-device-alias" command
+ */
+static const vshCmdInfo info_detach_device_alias[] = {
+    {.name = "help",
+     .data = N_("detach device from an alias")
+    },
+    {.name = "desc",
+     .data = N_("Detach device from domain using given alias to identify device")
+    },
+    {.name = NULL}
+};
+
+static const vshCmdOptDef opts_detach_device_alias[] = {
+    VIRSH_COMMON_OPT_DOMAIN_FULL(0),
+    {.name = "alias",
+     .type = VSH_OT_DATA,
+     .flags = VSH_OFLAG_REQ,
+     .help = N_("device alias")
+    },
+    VIRSH_COMMON_OPT_DOMAIN_PERSISTENT,
+    VIRSH_COMMON_OPT_DOMAIN_CONFIG,
+    VIRSH_COMMON_OPT_DOMAIN_LIVE,
+    VIRSH_COMMON_OPT_DOMAIN_CURRENT,
+    {.name = NULL}
+};
+
+static bool
+cmdDetachDeviceAlias(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom = NULL;
+    const char *alias = NULL;
+    bool current = vshCommandOptBool(cmd, "current");
+    bool config = vshCommandOptBool(cmd, "config");
+    bool live = vshCommandOptBool(cmd, "live");
+    bool persistent = vshCommandOptBool(cmd, "persistent");
+    unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
+    bool ret = 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 (live)
+        flags |= VIR_DOMAIN_AFFECT_LIVE;
+
+    if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
+        return false;
+
+    if (persistent &&
+        virDomainIsActive(dom) == 1)
+        flags |= VIR_DOMAIN_AFFECT_LIVE;
+
+    if (vshCommandOptStringReq(ctl, cmd, "alias", &alias) < 0)
+        goto cleanup;
+
+    if (virDomainDetachDeviceAlias(dom, alias, flags) < 0) {
+        vshError(ctl, _("Failed to detach device with alias %s"), alias);
+        goto cleanup;
+    }
+
+    vshPrintExtra(ctl, "%s", _("Device detached successfully\n"));
+    ret = true;
+
+ cleanup:
+    virshDomainFree(dom);
+    return ret;
+}
+
+
 /*
  * "update-device" command
  */
@@ -13999,6 +14072,12 @@ const vshCmdDef domManagementCmds[] = {
      .info = info_detach_device,
      .flags = 0
     },
+    {.name = "detach-device-alias",
+     .handler = cmdDetachDeviceAlias,
+     .opts = opts_detach_device_alias,
+     .info = info_detach_device_alias,
+     .flags = 0
+    },
     {.name = "detach-disk",
      .handler = cmdDetachDisk,
      .opts = opts_detach_disk,
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 929958a953..b6e60ce2f7 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -3112,6 +3112,21 @@ an offline domain, and like I<--live> I<--config> for a running domain.
 Note that older versions of virsh used I<--config> as an alias for
 I<--persistent>.
 
+=item B<detach-device-alias> I<domain> I<alias>
+[[[I<--live>] [I<--config>] | [I<--current>]] | [I<--persistent>]]
+
+Detach a device with given I<alias> from the I<domain>.
+
+If I<--live> is specified, affect a running domain.
+If I<--config> is specified, affect the next startup of a persistent domain.
+If I<--current> is specified, affect the current domain state.
+Both I<--live> and I<--config> flags may be given, but I<--current> is
+exclusive. When no flag is specified legacy API is used whose behavior depends
+on the hypervisor driver.
+
+For compatibility purposes, I<--persistent> behaves like I<--config> for
+an offline domain, and like I<--live> I<--config> for a running domain.
+
 =item B<detach-disk> I<domain> I<target>
 [[[I<--live>] [I<--config>] | [I<--current>]] | [I<--persistent>]]
 [I<--print-xml>]
-- 
2.16.1




More information about the libvir-list mailing list