]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
block_resize: Implement qemu monitor functions
authorOsier Yang <jyang@redhat.com>
Tue, 29 Nov 2011 07:34:53 +0000 (15:34 +0800)
committerOsier Yang <jyang@redhat.com>
Tue, 29 Nov 2011 13:45:11 +0000 (21:45 +0800)
Implements functions for both HMP and QMP mode.

For HMP mode, qemu uses "M" as the units by default, so the passed "sized"
is divided by 1024.

For QMP mode, qemu uses "Bytes" as the units by default, the passed "sized"
is multiplied by 1024.

All of the monitor functions return -1 on failure, 0 on success, or -2 if
not supported.

src/qemu/qemu_monitor.c
src/qemu/qemu_monitor.h
src/qemu/qemu_monitor_json.c
src/qemu/qemu_monitor_json.h
src/qemu/qemu_monitor_text.c
src/qemu/qemu_monitor_text.h

index 73e5ea986795f77f78277f7cc609006ab4f50e23..6423bf785d83455f1689cbbfb886427f78ff0b95 100644 (file)
@@ -1307,6 +1307,21 @@ int qemuMonitorGetBlockExtent(qemuMonitorPtr mon,
     return ret;
 }
 
+int qemuMonitorBlockResize(qemuMonitorPtr mon,
+                           const char *device,
+                           unsigned long long size)
+{
+    int ret;
+    VIR_DEBUG("mon=%p, fd=%d, devname=%p size=%llu",
+              mon, mon->fd, device, size);
+
+    if (mon->json)
+        ret = qemuMonitorJSONBlockResize(mon, device, size);
+    else
+        ret = qemuMonitorTextBlockResize(mon, device, size);
+
+    return ret;
+}
 
 int qemuMonitorSetVNCPassword(qemuMonitorPtr mon,
                               const char *password)
index 883e0aaf1e47555531b282dee3561014e917692a..41b8da2cff6f0ffc9f7b7c6ca021b9fe82343ada 100644 (file)
@@ -255,8 +255,9 @@ int qemuMonitorGetBlockStatsParamsNumber(qemuMonitorPtr mon,
 int qemuMonitorGetBlockExtent(qemuMonitorPtr mon,
                               const char *dev_name,
                               unsigned long long *extent);
-
-
+int qemuMonitorBlockResize(qemuMonitorPtr mon,
+                           const char *devname,
+                           unsigned long long size);
 int qemuMonitorSetVNCPassword(qemuMonitorPtr mon,
                               const char *password);
 int qemuMonitorSetPassword(qemuMonitorPtr mon,
index 56a62dbf416c1c1de2ab0049304588a3c3610c2a..2ea5e48ce405bea053fe5b7e76f18cb05165c232 100644 (file)
@@ -1775,6 +1775,38 @@ cleanup:
     return ret;
 }
 
+/* Return 0 on success, -1 on failure, or -2 if not supported. */
+int qemuMonitorJSONBlockResize(qemuMonitorPtr mon,
+                               const char *device,
+                               unsigned long long size)
+{
+    int ret;
+    virJSONValuePtr cmd;
+    virJSONValuePtr reply = NULL;
+
+    cmd = qemuMonitorJSONMakeCommand("block_resize",
+                                     "s:device", device,
+                                     "U:size", size * 1024,
+                                     NULL);
+    if (!cmd)
+        return -1;
+
+    ret = qemuMonitorJSONCommand(mon, cmd, &reply);
+
+    if (ret == 0) {
+        if (qemuMonitorJSONHasError(reply, "CommandNotFound")) {
+            ret = -2;
+            goto cleanup;
+        }
+
+        ret = qemuMonitorJSONCheckError(cmd, reply);
+    }
+
+cleanup:
+    virJSONValueFree(cmd);
+    virJSONValueFree(reply);
+    return ret;
+}
 
 int qemuMonitorJSONSetVNCPassword(qemuMonitorPtr mon,
                                   const char *password)
index f10d7d20ce2952816cf917ab455e2f711b74abc7..d31b32bc41c1a96df530daeb12fd23bb60b6682a 100644 (file)
@@ -81,7 +81,9 @@ int qemuMonitorJSONGetBlockStatsParamsNumber(qemuMonitorPtr mon,
 int qemuMonitorJSONGetBlockExtent(qemuMonitorPtr mon,
                                   const char *dev_name,
                                   unsigned long long *extent);
-
+int qemuMonitorJSONBlockResize(qemuMonitorPtr mon,
+                               const char *devce,
+                               unsigned long long size);
 
 int qemuMonitorJSONSetVNCPassword(qemuMonitorPtr mon,
                                   const char *password);
index 5de4d24be86a43ff3b48f993a3f9448f7e9a0d3b..728bea84b23ce61cdf8afadc6035c5657040c618 100644 (file)
@@ -1075,6 +1075,39 @@ int qemuMonitorTextGetBlockExtent(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
     return -1;
 }
 
+/* Return 0 on success, -1 on failure, or -2 if not supported. */
+int qemuMonitorTextBlockResize(qemuMonitorPtr mon,
+                               const char *device,
+                               unsigned long long size)
+{
+    char *cmd = NULL;
+    char *reply = NULL;
+    int ret = -1;
+
+    if (virAsprintf(&cmd, "block_resize %s %llu",
+                    device, VIR_DIV_UP(size, 1024)) < 0) {
+        virReportOOMError();
+        goto cleanup;
+    }
+
+    if (qemuMonitorHMPCommand(mon, cmd, &reply) < 0) {
+        qemuReportError(VIR_ERR_OPERATION_FAILED,
+                        "%s", _("failed to resize block"));
+        goto cleanup;
+    }
+
+    if (strstr(reply, "unknown command:")) {
+        ret = -2;
+        goto cleanup;
+    }
+
+    ret = 0;
+
+cleanup:
+    VIR_FREE(cmd);
+    VIR_FREE(reply);
+    return ret;
+}
 
 static int
 qemuMonitorSendVNCPassphrase(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
index f32fce0235eb2aa975e5ca8e5743c6f67c2b5d40..cece21cefc9b99edaa29508327a2fe0668510983 100644 (file)
@@ -78,7 +78,9 @@ int qemuMonitorTextGetBlockStatsParamsNumber(qemuMonitorPtr mon,
 int qemuMonitorTextGetBlockExtent(qemuMonitorPtr mon,
                                   const char *dev_name,
                                   unsigned long long *extent);
-
+int qemuMonitorTextBlockResize(qemuMonitorPtr mon,
+                               const char *device,
+                               unsigned long long size);
 int qemuMonitorTextSetVNCPassword(qemuMonitorPtr mon,
                                   const char *password);
 int qemuMonitorTextSetPassword(qemuMonitorPtr mon,