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

[libvirt] [PATCH 3/4] virsh: New command cmdChangeMedia



One could use it to eject, insert, or update media of the CDROM
or floppy drive. See the documents for more details.
---
 tools/virsh.c |  134 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 134 insertions(+), 0 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 5dad1d6..95bda67 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -14492,6 +14492,139 @@ cmdDetachDisk(vshControl *ctl, const vshCmd *cmd)
 }
 
 /*
+ * "change-media" command
+ */
+static const vshCmdInfo info_change_media[] = {
+    {"help", N_("insert media into CD or floppy drive")},
+    {"desc", N_("Insert media into CD or floppy drive.")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_change_media[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {"target", VSH_OT_DATA, VSH_OFLAG_REQ, N_("target of disk device")},
+    {"source", VSH_OT_DATA, 0, N_("source of the media")},
+    {"eject", VSH_OT_BOOL, 0, N_("Eject the media")},
+    {"insert", VSH_OT_BOOL, 0, N_("Insert the media")},
+    {"update", VSH_OT_BOOL, 0, N_("Update the media")},
+    {"current", VSH_OT_BOOL, 0, N_("can be either or both of --live and --config, "
+                                   "depends on implementation of hypervisor driver")},
+    {"live", VSH_OT_BOOL, 0, N_("alter live configuration of running domain")},
+    {"config", VSH_OT_BOOL, 0, N_("alter persistent configuration, effect observed on next boot")},
+    {"force",  VSH_OT_BOOL, 0, N_("force media insertion")},
+    {NULL, 0, 0, NULL}
+};
+
+static bool
+cmdChangeMedia(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom = NULL;
+    const char *source = NULL;
+    const char *target = NULL;
+    const char *doc = NULL;
+    xmlNodePtr disk_node = NULL;
+    const char *disk_xml = NULL;
+    int flags = 0;
+    int config, live, current, force = 0;
+    int eject, insert, update = 0;
+    bool ret = false;
+    unsigned int prepare_flags = 0;
+
+    config = vshCommandOptBool(cmd, "config");
+    live = vshCommandOptBool(cmd, "live");
+    current = vshCommandOptBool(cmd, "current");
+    force = vshCommandOptBool(cmd, "force");
+    eject = vshCommandOptBool(cmd, "eject");
+    insert = vshCommandOptBool(cmd, "insert");
+    update = vshCommandOptBool(cmd, "update");
+
+    if ((eject && insert) ||
+        (insert && update) ||
+        (eject && update)) {
+        vshError(ctl, "%s", _("--eject, --insert, and --update must be specified "
+                            "exclusively."));
+        return false;
+    }
+
+    if (!eject && !insert && !update) {
+        vshError(ctl, "%s", _("One of --eject, --insert, and --update should be "
+                              "specified"));
+        return false;
+    }
+
+    if (eject)
+        prepare_flags |= VSH_PREPARE_DISK_XML_EJECT;
+
+    if (insert)
+        prepare_flags |= VSH_PREPARE_DISK_XML_INSERT;
+
+    if (update)
+        prepare_flags |= VSH_PREPARE_DISK_XML_UPDATE;
+
+    if (current) {
+        if (live || config) {
+            vshError(ctl, "%s", _("--current must be specified exclusively"));
+            return false;
+        }
+        flags = VIR_DOMAIN_AFFECT_CURRENT;
+    } else {
+        if (config)
+            flags |= VIR_DOMAIN_AFFECT_CONFIG;
+        if (live)
+            flags |= VIR_DOMAIN_AFFECT_LIVE;
+    }
+
+    if (force)
+        flags |= VIR_DOMAIN_DEVICE_MODIFY_FORCE;
+
+    if (!vshConnectionUsability(ctl, ctl->conn))
+        goto cleanup;
+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
+        goto cleanup;
+
+    if (vshCommandOptString(cmd, "target", &target) <= 0)
+        goto cleanup;
+
+    if (vshCommandOptString(cmd, "source", &source) < 0)
+        goto cleanup;
+
+    if (insert && !source) {
+        vshError(ctl, "%s", _("No disk source specified for inserting"));
+        goto cleanup;
+    }
+
+    if (flags & VIR_DOMAIN_AFFECT_CONFIG)
+        doc = virDomainGetXMLDesc(dom, VIR_DOMAIN_XML_INACTIVE);
+    else
+        doc = virDomainGetXMLDesc(dom, 0);
+    if (!doc)
+        goto cleanup;
+
+    if (!(disk_node = vshFindDisk(doc, target, VSH_FIND_DISK_CHANGEABLE)))
+        goto cleanup;
+
+    if (!(disk_xml = vshPrepareDiskXML(disk_node, source, target, prepare_flags)))
+        goto cleanup;
+
+    if (virDomainUpdateDeviceFlags(dom, disk_xml, flags) != 0) {
+        vshError(ctl, "%s", _("Failed to insert media"));
+        goto cleanup;
+    }
+
+    vshPrint(ctl, "%s", _("Media inserted successfully\n"));
+    ret = true;
+
+cleanup:
+    VIR_FREE(doc);
+    xmlFreeNode(disk_node);
+    VIR_FREE(disk_xml);
+    if (dom)
+        virDomainFree(dom);
+    return ret;
+}
+
+/*
  * "cpu-compare" command
  */
 static const vshCmdInfo info_cpu_compare[] = {
@@ -16584,6 +16717,7 @@ static const vshCmdDef domManagementCmds[] = {
     {"blockpull", cmdBlockPull, opts_block_pull, info_block_pull, 0},
     {"blockjob", cmdBlockJob, opts_block_job, info_block_job, 0},
     {"blockresize", cmdBlockResize, opts_block_resize, info_block_resize, 0},
+    {"change-media", cmdChangeMedia, opts_change_media, info_change_media, 0},
 #ifndef WIN32
     {"console", cmdConsole, opts_console, info_console, 0},
 #endif
-- 
1.7.7.3


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