]> xenbits.xensource.com Git - libvirt.git/commitdiff
Introduce a update-device command in virsh
authorDaniel P. Berrange <berrange@redhat.com>
Mon, 22 Mar 2010 18:45:09 +0000 (18:45 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Fri, 26 Mar 2010 14:17:35 +0000 (14:17 +0000)
Support the new virDomainUpdateDeviceFlags API in virsh by adding
a new 'update-device' command. In the future this should be augmented
with an explicit 'change-disk' command for media change to make it
end user discoverable, as attach-disk is.

* tools/virsh.c: Add 'update-device' command

tools/virsh.c

index 32895b26acf0589d28406086bd6c6c1d606665a6..35bdbf1b8cabac68225752f04256033e2d4299f7 100644 (file)
@@ -6676,6 +6676,73 @@ cmdDetachDevice(vshControl *ctl, const vshCmd *cmd)
 }
 
 
+/*
+ * "update-device" command
+ */
+static const vshCmdInfo info_update_device[] = {
+    {"help", N_("update device from an XML file")},
+    {"desc", N_("Update device from an XML <file>.")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_update_device[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {"file",   VSH_OT_DATA, VSH_OFLAG_REQ, N_("XML file")},
+    {"persistent", VSH_OT_BOOL, 0, N_("persist device update")},
+    {NULL, 0, 0, NULL}
+};
+
+static int
+cmdUpdateDevice(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom;
+    char *from;
+    char *buffer;
+    int ret = TRUE;
+    int found;
+    unsigned int flags;
+
+    if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
+        return FALSE;
+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
+        return FALSE;
+
+    from = vshCommandOptString(cmd, "file", &found);
+    if (!found) {
+        vshError(ctl, "%s", _("update-device: Missing <file> option"));
+        virDomainFree(dom);
+        return FALSE;
+    }
+
+    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0) {
+        virDomainFree(dom);
+        return FALSE;
+    }
+
+    if (vshCommandOptBool(cmd, "persistent")) {
+        flags = VIR_DOMAIN_DEVICE_MODIFY_CONFIG;
+        if (virDomainIsActive(dom) == 1)
+           flags |= VIR_DOMAIN_DEVICE_MODIFY_LIVE;
+    } else {
+        flags = VIR_DOMAIN_DEVICE_MODIFY_LIVE;
+    }
+    ret = virDomainUpdateDeviceFlags(dom, buffer, flags);
+    VIR_FREE(buffer);
+
+    if (ret < 0) {
+        vshError(ctl, _("Failed to update device from %s"), from);
+        virDomainFree(dom);
+        return FALSE;
+    } else {
+        vshPrint(ctl, "%s", _("Device updated successfully\n"));
+    }
+
+    virDomainFree(dom);
+    return TRUE;
+}
+
+
 /*
  * "attach-interface" command
  */
@@ -7891,6 +7958,7 @@ static const vshCmdDef commands[] = {
     {"suspend", cmdSuspend, opts_suspend, info_suspend},
     {"ttyconsole", cmdTTYConsole, opts_ttyconsole, info_ttyconsole},
     {"undefine", cmdUndefine, opts_undefine, info_undefine},
+    {"update-device", cmdUpdateDevice, opts_update_device, info_update_device},
     {"uri", cmdURI, NULL, info_uri},
 
     {"vol-create", cmdVolCreate, opts_vol_create, info_vol_create},