block_resize: Implement qemu monitor functions

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.
This commit is contained in:
Osier Yang 2011-11-29 15:34:53 +08:00
parent 7c80d07414
commit 4fa36f1392
6 changed files with 89 additions and 4 deletions

View 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)

View 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,

View 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)

View 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);

View 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,

View 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,