2011-10-05 17:31:54 +00:00
|
|
|
/*
|
2014-03-07 13:38:51 +00:00
|
|
|
* qemu_agent.c: interaction with QEMU guest agent
|
2011-10-05 17:31:54 +00:00
|
|
|
*
|
2014-03-18 08:15:21 +00:00
|
|
|
* Copyright (C) 2006-2014 Red Hat, Inc.
|
2011-10-05 17:31:54 +00:00
|
|
|
* Copyright (C) 2006 Daniel P. Berrange
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2011-10-05 17:31:54 +00:00
|
|
|
*
|
|
|
|
* Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include "qemu_agent.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-12-12 17:53:50 +00:00
|
|
|
#include "virjson.h"
|
2011-10-05 17:31:54 +00:00
|
|
|
#include "virfile.h"
|
2012-09-24 16:54:51 +00:00
|
|
|
#include "virprocess.h"
|
2012-02-01 14:44:53 +00:00
|
|
|
#include "virtime.h"
|
2012-07-11 13:35:47 +00:00
|
|
|
#include "virobject.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_QEMU
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("qemu.qemu_agent");
|
|
|
|
|
2011-10-05 17:31:54 +00:00
|
|
|
#define LINE_ENDING "\n"
|
|
|
|
|
|
|
|
#define DEBUG_IO 0
|
|
|
|
#define DEBUG_RAW_IO 0
|
|
|
|
|
|
|
|
/* When you are the first to uncomment this,
|
|
|
|
* don't forget to uncomment the corresponding
|
|
|
|
* part in qemuAgentIOProcessEvent as well.
|
|
|
|
*
|
|
|
|
static struct {
|
|
|
|
const char *type;
|
|
|
|
void (*handler)(qemuAgentPtr mon, virJSONValuePtr data);
|
|
|
|
} eventHandlers[] = {
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct _qemuAgentMessage qemuAgentMessage;
|
|
|
|
typedef qemuAgentMessage *qemuAgentMessagePtr;
|
|
|
|
|
|
|
|
struct _qemuAgentMessage {
|
|
|
|
char *txBuffer;
|
|
|
|
int txOffset;
|
|
|
|
int txLength;
|
|
|
|
|
|
|
|
/* Used by the JSON monitor to hold reply / error */
|
|
|
|
char *rxBuffer;
|
|
|
|
int rxLength;
|
|
|
|
void *rxObject;
|
|
|
|
|
|
|
|
/* True if rxBuffer / rxObject are ready, or a
|
|
|
|
* fatal error occurred on the monitor channel
|
|
|
|
*/
|
|
|
|
bool finished;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct _qemuAgent {
|
2013-01-09 21:00:32 +00:00
|
|
|
virObjectLockable parent;
|
2012-07-11 13:35:47 +00:00
|
|
|
|
2011-10-05 17:31:54 +00:00
|
|
|
virCond notify;
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
int watch;
|
|
|
|
|
|
|
|
bool connectPending;
|
|
|
|
|
|
|
|
virDomainObjPtr vm;
|
|
|
|
|
|
|
|
qemuAgentCallbacksPtr cb;
|
|
|
|
|
|
|
|
/* If there's a command being processed this will be
|
|
|
|
* non-NULL */
|
|
|
|
qemuAgentMessagePtr msg;
|
|
|
|
|
|
|
|
/* Buffer incoming data ready for Agent monitor
|
|
|
|
* code to process & find message boundaries */
|
|
|
|
size_t bufferOffset;
|
|
|
|
size_t bufferLength;
|
|
|
|
char *buffer;
|
|
|
|
|
|
|
|
/* If anything went wrong, this will be fed back
|
|
|
|
* the next monitor msg */
|
|
|
|
virError lastError;
|
2012-06-15 16:00:13 +00:00
|
|
|
|
|
|
|
/* Some guest agent commands don't return anything
|
|
|
|
* but fire up an event on qemu monitor instead.
|
|
|
|
* Take that as indication of successful completion */
|
|
|
|
qemuAgentEvent await_event;
|
2011-10-05 17:31:54 +00:00
|
|
|
};
|
|
|
|
|
2012-07-11 13:35:47 +00:00
|
|
|
static virClassPtr qemuAgentClass;
|
|
|
|
static void qemuAgentDispose(void *obj);
|
|
|
|
|
|
|
|
static int qemuAgentOnceInit(void)
|
|
|
|
{
|
2013-01-09 21:00:32 +00:00
|
|
|
if (!(qemuAgentClass = virClassNew(virClassForObjectLockable(),
|
2013-01-09 17:37:27 +00:00
|
|
|
"qemuAgent",
|
2012-07-11 13:35:47 +00:00
|
|
|
sizeof(qemuAgent),
|
|
|
|
qemuAgentDispose)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_ONCE_GLOBAL_INIT(qemuAgent)
|
|
|
|
|
|
|
|
|
2011-10-05 17:31:54 +00:00
|
|
|
#if DEBUG_RAW_IO
|
|
|
|
# include <c-ctype.h>
|
|
|
|
static char *
|
|
|
|
qemuAgentEscapeNonPrintable(const char *text)
|
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2011-10-05 17:31:54 +00:00
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
2013-05-21 07:21:20 +00:00
|
|
|
for (i = 0; text[i] != '\0'; i++) {
|
2011-10-05 17:31:54 +00:00
|
|
|
if (text[i] == '\\')
|
|
|
|
virBufferAddLit(&buf, "\\\\");
|
|
|
|
else if (c_isprint(text[i]) || text[i] == '\n' ||
|
|
|
|
(text[i] == '\r' && text[i+1] == '\n'))
|
|
|
|
virBufferAddChar(&buf, text[i]);
|
|
|
|
else
|
|
|
|
virBufferAsprintf(&buf, "\\x%02x", text[i]);
|
|
|
|
}
|
|
|
|
return virBufferContentAndReset(&buf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-07-11 13:35:47 +00:00
|
|
|
static void qemuAgentDispose(void *obj)
|
2011-10-05 17:31:54 +00:00
|
|
|
{
|
2012-07-11 13:35:47 +00:00
|
|
|
qemuAgentPtr mon = obj;
|
2011-10-05 17:31:54 +00:00
|
|
|
VIR_DEBUG("mon=%p", mon);
|
|
|
|
if (mon->cb && mon->cb->destroy)
|
|
|
|
(mon->cb->destroy)(mon, mon->vm);
|
2013-02-07 14:03:17 +00:00
|
|
|
virCondDestroy(&mon->notify);
|
2011-10-05 17:31:54 +00:00
|
|
|
VIR_FREE(mon->buffer);
|
2013-12-03 02:59:00 +00:00
|
|
|
virResetError(&mon->lastError);
|
2011-10-05 17:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qemuAgentOpenUnix(const char *monitor, pid_t cpid, bool *inProgress)
|
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
int monfd;
|
|
|
|
int timeout = 3; /* In seconds */
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
int ret;
|
|
|
|
size_t i = 0;
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
*inProgress = false;
|
|
|
|
|
|
|
|
if ((monfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
"%s", _("failed to create socket"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virSetNonBlock(monfd) < 0) {
|
2012-08-27 11:49:21 +00:00
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to put monitor "
|
|
|
|
"into non-blocking mode"));
|
2011-10-05 17:31:54 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virSetCloseExec(monfd) < 0) {
|
2012-08-27 11:49:21 +00:00
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to set monitor "
|
|
|
|
"close-on-exec flag"));
|
2011-10-05 17:31:54 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
if (virStrcpyStatic(addr.sun_path, monitor) == NULL) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Agent path %s too big for destination"), monitor);
|
2011-10-05 17:31:54 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = connect(monfd, (struct sockaddr *) &addr, sizeof(addr));
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((errno == ENOENT || errno == ECONNREFUSED) &&
|
2012-09-24 15:29:20 +00:00
|
|
|
virProcessKill(cpid, 0) == 0) {
|
2011-10-05 17:31:54 +00:00
|
|
|
/* ENOENT : Socket may not have shown up yet
|
|
|
|
* ECONNREFUSED : Leftover socket hasn't been removed yet */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((errno == EINPROGRESS) ||
|
|
|
|
(errno == EAGAIN)) {
|
|
|
|
VIR_DEBUG("Connection attempt continuing in background");
|
|
|
|
*inProgress = true;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("failed to connect to monitor socket"));
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
} while ((++i <= timeout*5) && (usleep(.2 * 1000000) <= 0));
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
2012-11-29 13:15:35 +00:00
|
|
|
_("monitor socket did not show up"));
|
2011-10-05 17:31:54 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return monfd;
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
error:
|
2011-10-05 17:31:54 +00:00
|
|
|
VIR_FORCE_CLOSE(monfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qemuAgentOpenPty(const char *monitor)
|
|
|
|
{
|
|
|
|
int monfd;
|
|
|
|
|
|
|
|
if ((monfd = open(monitor, O_RDWR | O_NONBLOCK)) < 0) {
|
2012-08-27 11:49:21 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to open monitor path %s"), monitor);
|
2011-10-05 17:31:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virSetCloseExec(monfd) < 0) {
|
2012-08-27 11:49:21 +00:00
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to set monitor close-on-exec flag"));
|
2011-10-05 17:31:54 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return monfd;
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
error:
|
2011-10-05 17:31:54 +00:00
|
|
|
VIR_FORCE_CLOSE(monfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
qemuAgentIOProcessEvent(qemuAgentPtr mon,
|
|
|
|
virJSONValuePtr obj)
|
|
|
|
{
|
|
|
|
const char *type;
|
|
|
|
VIR_DEBUG("mon=%p obj=%p", mon, obj);
|
|
|
|
|
|
|
|
type = virJSONValueObjectGetString(obj, "event");
|
|
|
|
if (!type) {
|
|
|
|
VIR_WARN("missing event type in message");
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-05-21 07:21:20 +00:00
|
|
|
for (i = 0; i < ARRAY_CARDINALITY(eventHandlers); i++) {
|
2011-10-05 17:31:54 +00:00
|
|
|
if (STREQ(eventHandlers[i].type, type)) {
|
|
|
|
virJSONValuePtr data = virJSONValueObjectGet(obj, "data");
|
|
|
|
VIR_DEBUG("handle %s handler=%p data=%p", type,
|
|
|
|
eventHandlers[i].handler, data);
|
|
|
|
(eventHandlers[i].handler)(mon, data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qemuAgentIOProcessLine(qemuAgentPtr mon,
|
|
|
|
const char *line,
|
|
|
|
qemuAgentMessagePtr msg)
|
|
|
|
{
|
|
|
|
virJSONValuePtr obj = NULL;
|
|
|
|
int ret = -1;
|
2012-02-01 14:44:53 +00:00
|
|
|
unsigned long long id;
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Line [%s]", line);
|
|
|
|
|
|
|
|
if (!(obj = virJSONValueFromString(line)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (obj->type != VIR_JSON_TYPE_OBJECT) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Parsed JSON reply '%s' isn't an object"), line);
|
2011-10-05 17:31:54 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virJSONValueObjectHasKey(obj, "QMP") == 1) {
|
|
|
|
ret = 0;
|
|
|
|
} else if (virJSONValueObjectHasKey(obj, "event") == 1) {
|
|
|
|
ret = qemuAgentIOProcessEvent(mon, obj);
|
|
|
|
} else if (virJSONValueObjectHasKey(obj, "error") == 1 ||
|
|
|
|
virJSONValueObjectHasKey(obj, "return") == 1) {
|
|
|
|
if (msg) {
|
|
|
|
msg->rxObject = obj;
|
|
|
|
msg->finished = 1;
|
|
|
|
obj = NULL;
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
2012-02-01 14:44:53 +00:00
|
|
|
/* If we've received something like:
|
|
|
|
* {"return": 1234}
|
|
|
|
* it is likely that somebody started GA
|
|
|
|
* which is now processing our previous
|
|
|
|
* guest-sync commands. Check if this is
|
|
|
|
* the case and don't report an error but
|
|
|
|
* return silently.
|
|
|
|
*/
|
|
|
|
if (virJSONValueObjectGetNumberUlong(obj, "return", &id) == 0) {
|
|
|
|
VIR_DEBUG("Ignoring delayed reply to guest-sync: %llu", id);
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unexpected JSON reply '%s'"), line);
|
2011-10-05 17:31:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unknown JSON reply '%s'"), line);
|
2011-10-05 17:31:54 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
cleanup:
|
2011-10-05 17:31:54 +00:00
|
|
|
virJSONValueFree(obj);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qemuAgentIOProcessData(qemuAgentPtr mon,
|
|
|
|
char *data,
|
|
|
|
size_t len,
|
|
|
|
qemuAgentMessagePtr msg)
|
|
|
|
{
|
|
|
|
int used = 0;
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i = 0;
|
2011-10-05 17:31:54 +00:00
|
|
|
#if DEBUG_IO
|
|
|
|
# if DEBUG_RAW_IO
|
|
|
|
char *str1 = qemuAgentEscapeNonPrintable(data);
|
|
|
|
VIR_ERROR("[%s]", str1);
|
|
|
|
VIR_FREE(str1);
|
|
|
|
# else
|
|
|
|
VIR_DEBUG("Data %zu bytes [%s]", len, data);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (used < len) {
|
|
|
|
char *nl = strstr(data + used, LINE_ENDING);
|
|
|
|
|
|
|
|
if (nl) {
|
|
|
|
int got = nl - (data + used);
|
|
|
|
for (i = 0; i < strlen(LINE_ENDING); i++)
|
|
|
|
data[used + got + i] = '\0';
|
2014-11-13 14:25:30 +00:00
|
|
|
if (qemuAgentIOProcessLine(mon, data + used, msg) < 0)
|
2011-10-05 17:31:54 +00:00
|
|
|
return -1;
|
|
|
|
used += got + strlen(LINE_ENDING);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("Total used %d bytes out of %zd available in buffer", used, len);
|
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This method processes data that has been received
|
|
|
|
* from the monitor. Looking for async events and
|
|
|
|
* replies/errors.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
qemuAgentIOProcess(qemuAgentPtr mon)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
qemuAgentMessagePtr msg = NULL;
|
|
|
|
|
|
|
|
/* See if there's a message ready for reply; that is,
|
|
|
|
* one that has completed writing all its data.
|
|
|
|
*/
|
|
|
|
if (mon->msg && mon->msg->txOffset == mon->msg->txLength)
|
|
|
|
msg = mon->msg;
|
|
|
|
|
|
|
|
#if DEBUG_IO
|
|
|
|
# if DEBUG_RAW_IO
|
|
|
|
char *str1 = qemuAgentEscapeNonPrintable(msg ? msg->txBuffer : "");
|
|
|
|
char *str2 = qemuAgentEscapeNonPrintable(mon->buffer);
|
|
|
|
VIR_ERROR(_("Process %zu %p %p [[[%s]]][[[%s]]]"),
|
|
|
|
mon->bufferOffset, mon->msg, msg, str1, str2);
|
|
|
|
VIR_FREE(str1);
|
|
|
|
VIR_FREE(str2);
|
|
|
|
# else
|
|
|
|
VIR_DEBUG("Process %zu", mon->bufferOffset);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
len = qemuAgentIOProcessData(mon,
|
|
|
|
mon->buffer, mon->bufferOffset,
|
|
|
|
msg);
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (len < mon->bufferOffset) {
|
|
|
|
memmove(mon->buffer, mon->buffer + len, mon->bufferOffset - len);
|
|
|
|
mon->bufferOffset -= len;
|
|
|
|
} else {
|
|
|
|
VIR_FREE(mon->buffer);
|
|
|
|
mon->bufferOffset = mon->bufferLength = 0;
|
|
|
|
}
|
|
|
|
#if DEBUG_IO
|
|
|
|
VIR_DEBUG("Process done %zu used %d", mon->bufferOffset, len);
|
|
|
|
#endif
|
|
|
|
if (msg && msg->finished)
|
|
|
|
virCondBroadcast(&mon->notify);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
qemuAgentIOConnect(qemuAgentPtr mon)
|
|
|
|
{
|
|
|
|
int optval;
|
|
|
|
socklen_t optlen;
|
|
|
|
|
|
|
|
VIR_DEBUG("Checking on background connection status");
|
|
|
|
|
|
|
|
mon->connectPending = false;
|
|
|
|
|
|
|
|
optlen = sizeof(optval);
|
|
|
|
|
|
|
|
if (getsockopt(mon->fd, SOL_SOCKET, SO_ERROR,
|
|
|
|
&optval, &optlen) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Cannot check socket connection status"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optval != 0) {
|
|
|
|
virReportSystemError(optval, "%s",
|
|
|
|
_("Cannot connect to agent socket"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("Agent is now connected");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when the monitor is able to write data
|
|
|
|
* Call this function while holding the monitor lock.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
qemuAgentIOWrite(qemuAgentPtr mon)
|
|
|
|
{
|
|
|
|
int done;
|
|
|
|
|
|
|
|
/* If no active message, or fully transmitted, then no-op */
|
|
|
|
if (!mon->msg || mon->msg->txOffset == mon->msg->txLength)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
done = safewrite(mon->fd,
|
|
|
|
mon->msg->txBuffer + mon->msg->txOffset,
|
|
|
|
mon->msg->txLength - mon->msg->txOffset);
|
|
|
|
|
|
|
|
if (done < 0) {
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to write to monitor"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mon->msg->txOffset += done;
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when the monitor has incoming data to read
|
|
|
|
* Call this function while holding the monitor lock.
|
|
|
|
*
|
|
|
|
* Returns -1 on error, or number of bytes read
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
qemuAgentIORead(qemuAgentPtr mon)
|
|
|
|
{
|
|
|
|
size_t avail = mon->bufferLength - mon->bufferOffset;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (avail < 1024) {
|
|
|
|
if (VIR_REALLOC_N(mon->buffer,
|
2013-07-04 10:14:12 +00:00
|
|
|
mon->bufferLength + 1024) < 0)
|
2011-10-05 17:31:54 +00:00
|
|
|
return -1;
|
|
|
|
mon->bufferLength += 1024;
|
|
|
|
avail += 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read as much as we can get into our buffer,
|
|
|
|
until we block on EAGAIN, or hit EOF */
|
|
|
|
while (avail > 1) {
|
|
|
|
int got;
|
|
|
|
got = read(mon->fd,
|
|
|
|
mon->buffer + mon->bufferOffset,
|
|
|
|
avail - 1);
|
|
|
|
if (got < 0) {
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
break;
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to read from monitor"));
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (got == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ret += got;
|
|
|
|
avail -= got;
|
|
|
|
mon->bufferOffset += got;
|
|
|
|
mon->buffer[mon->bufferOffset] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_IO
|
|
|
|
VIR_DEBUG("Now read %zu bytes of data", mon->bufferOffset);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void qemuAgentUpdateWatch(qemuAgentPtr mon)
|
|
|
|
{
|
|
|
|
int events =
|
|
|
|
VIR_EVENT_HANDLE_HANGUP |
|
|
|
|
VIR_EVENT_HANDLE_ERROR;
|
|
|
|
|
|
|
|
if (mon->lastError.code == VIR_ERR_OK) {
|
|
|
|
events |= VIR_EVENT_HANDLE_READABLE;
|
|
|
|
|
|
|
|
if (mon->msg && mon->msg->txOffset < mon->msg->txLength)
|
|
|
|
events |= VIR_EVENT_HANDLE_WRITABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virEventUpdateHandle(mon->watch, events);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2014-03-18 08:15:21 +00:00
|
|
|
qemuAgentIO(int watch, int fd, int events, void *opaque)
|
|
|
|
{
|
2011-10-05 17:31:54 +00:00
|
|
|
qemuAgentPtr mon = opaque;
|
|
|
|
bool error = false;
|
|
|
|
bool eof = false;
|
|
|
|
|
2012-07-11 13:35:47 +00:00
|
|
|
virObjectRef(mon);
|
2011-10-05 17:31:54 +00:00
|
|
|
/* lock access to the monitor and protect fd */
|
2013-01-09 21:00:32 +00:00
|
|
|
virObjectLock(mon);
|
2011-10-05 17:31:54 +00:00
|
|
|
#if DEBUG_IO
|
|
|
|
VIR_DEBUG("Agent %p I/O on watch %d fd %d events %d", mon, watch, fd, events);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mon->fd != fd || mon->watch != watch) {
|
|
|
|
if (events & (VIR_EVENT_HANDLE_HANGUP | VIR_EVENT_HANDLE_ERROR))
|
|
|
|
eof = true;
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("event from unexpected fd %d!=%d / watch %d!=%d"),
|
|
|
|
mon->fd, fd, mon->watch, watch);
|
2011-10-05 17:31:54 +00:00
|
|
|
error = true;
|
|
|
|
} else if (mon->lastError.code != VIR_ERR_OK) {
|
|
|
|
if (events & (VIR_EVENT_HANDLE_HANGUP | VIR_EVENT_HANDLE_ERROR))
|
|
|
|
eof = true;
|
|
|
|
error = true;
|
|
|
|
} else {
|
|
|
|
if (events & VIR_EVENT_HANDLE_WRITABLE) {
|
|
|
|
if (mon->connectPending) {
|
|
|
|
if (qemuAgentIOConnect(mon) < 0)
|
|
|
|
error = true;
|
|
|
|
} else {
|
|
|
|
if (qemuAgentIOWrite(mon) < 0)
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
events &= ~VIR_EVENT_HANDLE_WRITABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!error &&
|
|
|
|
events & VIR_EVENT_HANDLE_READABLE) {
|
|
|
|
int got = qemuAgentIORead(mon);
|
|
|
|
events &= ~VIR_EVENT_HANDLE_READABLE;
|
|
|
|
if (got < 0) {
|
|
|
|
error = true;
|
|
|
|
} else if (got == 0) {
|
|
|
|
eof = true;
|
|
|
|
} else {
|
|
|
|
/* Ignore hangup/error events if we read some data, to
|
|
|
|
* give time for that data to be consumed */
|
|
|
|
events = 0;
|
|
|
|
|
|
|
|
if (qemuAgentIOProcess(mon) < 0)
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!error &&
|
|
|
|
events & VIR_EVENT_HANDLE_HANGUP) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("End of file from monitor"));
|
2013-05-24 10:14:02 +00:00
|
|
|
eof = true;
|
2011-10-05 17:31:54 +00:00
|
|
|
events &= ~VIR_EVENT_HANDLE_HANGUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!error && !eof &&
|
|
|
|
events & VIR_EVENT_HANDLE_ERROR) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Invalid file descriptor while waiting for monitor"));
|
2013-05-24 10:14:02 +00:00
|
|
|
eof = true;
|
2011-10-05 17:31:54 +00:00
|
|
|
events &= ~VIR_EVENT_HANDLE_ERROR;
|
|
|
|
}
|
|
|
|
if (!error && events) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unhandled event %d for monitor fd %d"),
|
|
|
|
events, mon->fd);
|
2013-05-24 10:14:02 +00:00
|
|
|
error = true;
|
2011-10-05 17:31:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error || eof) {
|
|
|
|
if (mon->lastError.code != VIR_ERR_OK) {
|
|
|
|
/* Already have an error, so clear any new error */
|
|
|
|
virResetLastError();
|
|
|
|
} else {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
if (!err)
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Error while processing monitor IO"));
|
2011-10-05 17:31:54 +00:00
|
|
|
virCopyLastError(&mon->lastError);
|
|
|
|
virResetLastError();
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("Error on monitor %s", NULLSTR(mon->lastError.message));
|
|
|
|
/* If IO process resulted in an error & we have a message,
|
|
|
|
* then wakeup that waiter */
|
|
|
|
if (mon->msg && !mon->msg->finished) {
|
|
|
|
mon->msg->finished = 1;
|
|
|
|
virCondSignal(&mon->notify);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qemuAgentUpdateWatch(mon);
|
|
|
|
|
|
|
|
/* We have to unlock to avoid deadlock against command thread,
|
|
|
|
* but is this safe ? I think it is, because the callback
|
|
|
|
* will try to acquire the virDomainObjPtr mutex next */
|
|
|
|
if (eof) {
|
|
|
|
void (*eofNotify)(qemuAgentPtr, virDomainObjPtr)
|
|
|
|
= mon->cb->eofNotify;
|
|
|
|
virDomainObjPtr vm = mon->vm;
|
|
|
|
|
|
|
|
/* Make sure anyone waiting wakes up now */
|
|
|
|
virCondSignal(&mon->notify);
|
2013-01-09 21:00:32 +00:00
|
|
|
virObjectUnlock(mon);
|
2012-07-11 13:35:47 +00:00
|
|
|
virObjectUnref(mon);
|
2011-10-05 17:31:54 +00:00
|
|
|
VIR_DEBUG("Triggering EOF callback");
|
|
|
|
(eofNotify)(mon, vm);
|
|
|
|
} else if (error) {
|
|
|
|
void (*errorNotify)(qemuAgentPtr, virDomainObjPtr)
|
|
|
|
= mon->cb->errorNotify;
|
|
|
|
virDomainObjPtr vm = mon->vm;
|
|
|
|
|
|
|
|
/* Make sure anyone waiting wakes up now */
|
|
|
|
virCondSignal(&mon->notify);
|
2013-01-09 21:00:32 +00:00
|
|
|
virObjectUnlock(mon);
|
2012-07-11 13:35:47 +00:00
|
|
|
virObjectUnref(mon);
|
2011-10-05 17:31:54 +00:00
|
|
|
VIR_DEBUG("Triggering error callback");
|
|
|
|
(errorNotify)(mon, vm);
|
|
|
|
} else {
|
2013-01-09 21:00:32 +00:00
|
|
|
virObjectUnlock(mon);
|
2012-07-11 13:35:47 +00:00
|
|
|
virObjectUnref(mon);
|
2011-10-05 17:31:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
qemuAgentPtr
|
|
|
|
qemuAgentOpen(virDomainObjPtr vm,
|
|
|
|
virDomainChrSourceDefPtr config,
|
|
|
|
qemuAgentCallbacksPtr cb)
|
|
|
|
{
|
|
|
|
qemuAgentPtr mon;
|
|
|
|
|
|
|
|
if (!cb || !cb->eofNotify) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("EOF notify callback must be supplied"));
|
2011-10-05 17:31:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-11 13:35:47 +00:00
|
|
|
if (qemuAgentInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2013-01-09 21:00:32 +00:00
|
|
|
if (!(mon = virObjectLockableNew(qemuAgentClass)))
|
2011-10-05 17:31:54 +00:00
|
|
|
return NULL;
|
|
|
|
|
2013-01-09 21:00:32 +00:00
|
|
|
mon->fd = -1;
|
2011-10-05 17:31:54 +00:00
|
|
|
if (virCondInit(&mon->notify) < 0) {
|
2012-08-27 11:49:21 +00:00
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("cannot initialize monitor condition"));
|
2013-01-09 21:00:32 +00:00
|
|
|
virObjectUnref(mon);
|
2011-10-05 17:31:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
mon->vm = vm;
|
|
|
|
mon->cb = cb;
|
|
|
|
|
|
|
|
switch (config->type) {
|
|
|
|
case VIR_DOMAIN_CHR_TYPE_UNIX:
|
|
|
|
mon->fd = qemuAgentOpenUnix(config->data.nix.path, vm->pid,
|
|
|
|
&mon->connectPending);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_DOMAIN_CHR_TYPE_PTY:
|
|
|
|
mon->fd = qemuAgentOpenPty(config->data.file.path);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("unable to handle monitor type: %s"),
|
|
|
|
virDomainChrTypeToString(config->type));
|
2011-10-05 17:31:54 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mon->fd == -1)
|
|
|
|
goto cleanup;
|
|
|
|
|
2013-01-09 21:00:32 +00:00
|
|
|
virObjectRef(mon);
|
2011-10-05 17:31:54 +00:00
|
|
|
if ((mon->watch = virEventAddHandle(mon->fd,
|
|
|
|
VIR_EVENT_HANDLE_HANGUP |
|
|
|
|
VIR_EVENT_HANDLE_ERROR |
|
|
|
|
VIR_EVENT_HANDLE_READABLE |
|
|
|
|
(mon->connectPending ?
|
|
|
|
VIR_EVENT_HANDLE_WRITABLE :
|
|
|
|
0),
|
|
|
|
qemuAgentIO,
|
2012-07-11 13:35:47 +00:00
|
|
|
mon,
|
|
|
|
virObjectFreeCallback)) < 0) {
|
2013-01-09 21:00:32 +00:00
|
|
|
virObjectUnref(mon);
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("unable to register monitor events"));
|
2011-10-05 17:31:54 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("New mon %p fd =%d watch=%d", mon, mon->fd, mon->watch);
|
|
|
|
|
|
|
|
return mon;
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
cleanup:
|
2011-10-05 17:31:54 +00:00
|
|
|
/* We don't want the 'destroy' callback invoked during
|
|
|
|
* cleanup from construction failure, because that can
|
|
|
|
* give a double-unref on virDomainObjPtr in the caller,
|
|
|
|
* so kill the callbacks now.
|
|
|
|
*/
|
|
|
|
mon->cb = NULL;
|
|
|
|
qemuAgentClose(mon);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void qemuAgentClose(qemuAgentPtr mon)
|
|
|
|
{
|
|
|
|
if (!mon)
|
|
|
|
return;
|
|
|
|
|
|
|
|
VIR_DEBUG("mon=%p", mon);
|
|
|
|
|
2013-01-09 21:00:32 +00:00
|
|
|
virObjectLock(mon);
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
if (mon->fd >= 0) {
|
|
|
|
if (mon->watch)
|
|
|
|
virEventRemoveHandle(mon->watch);
|
|
|
|
VIR_FORCE_CLOSE(mon->fd);
|
|
|
|
}
|
|
|
|
|
2012-06-15 16:00:13 +00:00
|
|
|
/* If there is somebody waiting for a message
|
|
|
|
* wake him up. No message will arrive anyway. */
|
|
|
|
if (mon->msg && !mon->msg->finished) {
|
|
|
|
mon->msg->finished = 1;
|
|
|
|
virCondSignal(&mon->notify);
|
|
|
|
}
|
2013-01-09 21:00:32 +00:00
|
|
|
virObjectUnlock(mon);
|
2012-06-15 16:00:13 +00:00
|
|
|
|
2012-07-11 13:35:47 +00:00
|
|
|
virObjectUnref(mon);
|
2011-10-05 17:31:54 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 12:19:02 +00:00
|
|
|
#define QEMU_AGENT_WAIT_TIME 5
|
2011-10-05 17:31:54 +00:00
|
|
|
|
2012-02-01 14:44:53 +00:00
|
|
|
/**
|
|
|
|
* qemuAgentSend:
|
|
|
|
* @mon: Monitor
|
|
|
|
* @msg: Message
|
2012-08-30 12:19:02 +00:00
|
|
|
* @seconds: number of seconds to wait for the result, it can be either
|
|
|
|
* -2, -1, 0 or positive.
|
2012-02-01 14:44:53 +00:00
|
|
|
*
|
2012-08-30 12:19:02 +00:00
|
|
|
* Send @msg to agent @mon. If @seconds is equal to
|
|
|
|
* VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK(-2), this function will block forever
|
|
|
|
* waiting for the result. The value of
|
|
|
|
* VIR_DOMAIN_QEMU_AGENT_COMMAND_DEFAULT(-1) means use default timeout value
|
|
|
|
* and VIR_DOMAIN_QEMU_AGENT_COMMAND_NOWAIT(0) makes this this function return
|
|
|
|
* immediately without waiting. Any positive value means the number of seconds
|
|
|
|
* to wait for the result.
|
2012-02-01 14:44:53 +00:00
|
|
|
*
|
|
|
|
* Returns: 0 on success,
|
|
|
|
* -2 on timeout,
|
|
|
|
* -1 otherwise
|
|
|
|
*/
|
2011-10-05 17:31:54 +00:00
|
|
|
static int qemuAgentSend(qemuAgentPtr mon,
|
2012-02-01 14:44:53 +00:00
|
|
|
qemuAgentMessagePtr msg,
|
2012-08-23 03:29:21 +00:00
|
|
|
int seconds)
|
2011-10-05 17:31:54 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
2012-08-30 12:19:02 +00:00
|
|
|
unsigned long long then = 0;
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
/* Check whether qemu quit unexpectedly */
|
|
|
|
if (mon->lastError.code != VIR_ERR_OK) {
|
|
|
|
VIR_DEBUG("Attempt to send command while error is set %s",
|
|
|
|
NULLSTR(mon->lastError.message));
|
|
|
|
virSetError(&mon->lastError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-08-30 12:19:02 +00:00
|
|
|
if (seconds > VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) {
|
|
|
|
unsigned long long now;
|
2012-02-01 14:44:53 +00:00
|
|
|
if (virTimeMillisNow(&now) < 0)
|
|
|
|
return -1;
|
2012-08-30 12:19:02 +00:00
|
|
|
if (seconds == VIR_DOMAIN_QEMU_AGENT_COMMAND_DEFAULT)
|
|
|
|
seconds = QEMU_AGENT_WAIT_TIME;
|
|
|
|
then = now + seconds * 1000ull;
|
2012-02-01 14:44:53 +00:00
|
|
|
}
|
|
|
|
|
2011-10-05 17:31:54 +00:00
|
|
|
mon->msg = msg;
|
|
|
|
qemuAgentUpdateWatch(mon);
|
|
|
|
|
|
|
|
while (!mon->msg->finished) {
|
2013-01-09 21:00:32 +00:00
|
|
|
if ((then && virCondWaitUntil(&mon->notify, &mon->parent.lock, then) < 0) ||
|
|
|
|
(!then && virCondWait(&mon->notify, &mon->parent.lock) < 0)) {
|
2012-02-01 14:44:53 +00:00
|
|
|
if (errno == ETIMEDOUT) {
|
2012-08-27 10:24:59 +00:00
|
|
|
virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s",
|
2012-07-18 15:22:03 +00:00
|
|
|
_("Guest agent not available for now"));
|
2012-02-01 14:44:53 +00:00
|
|
|
ret = -2;
|
|
|
|
} else {
|
2012-08-27 11:49:21 +00:00
|
|
|
virReportSystemError(errno, "%s",
|
2012-11-28 12:30:40 +00:00
|
|
|
_("Unable to wait on agent monitor "
|
|
|
|
"condition"));
|
2012-02-01 14:44:53 +00:00
|
|
|
}
|
2011-10-05 17:31:54 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mon->lastError.code != VIR_ERR_OK) {
|
|
|
|
VIR_DEBUG("Send command resulted in error %s",
|
|
|
|
NULLSTR(mon->lastError.message));
|
|
|
|
virSetError(&mon->lastError);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
cleanup:
|
2011-10-05 17:31:54 +00:00
|
|
|
mon->msg = NULL;
|
|
|
|
qemuAgentUpdateWatch(mon);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-01 14:44:53 +00:00
|
|
|
/**
|
|
|
|
* qemuAgentGuestSync:
|
|
|
|
* @mon: Monitor
|
|
|
|
*
|
|
|
|
* Send guest-sync with unique ID
|
|
|
|
* and wait for reply. If we get one, check if
|
|
|
|
* received ID is equal to given.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success,
|
|
|
|
* -1 otherwise
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
qemuAgentGuestSync(qemuAgentPtr mon)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
int send_ret;
|
|
|
|
unsigned long long id, id_ret;
|
|
|
|
qemuAgentMessage sync_msg;
|
|
|
|
|
|
|
|
memset(&sync_msg, 0, sizeof(sync_msg));
|
|
|
|
|
|
|
|
if (virTimeMillisNow(&id) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virAsprintf(&sync_msg.txBuffer,
|
|
|
|
"{\"execute\":\"guest-sync\", "
|
2013-07-24 15:52:33 +00:00
|
|
|
"\"arguments\":{\"id\":%llu}}\n", id) < 0)
|
2012-02-01 14:44:53 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
sync_msg.txLength = strlen(sync_msg.txBuffer);
|
|
|
|
|
|
|
|
VIR_DEBUG("Sending guest-sync command with ID: %llu", id);
|
|
|
|
|
2012-08-30 12:19:02 +00:00
|
|
|
send_ret = qemuAgentSend(mon, &sync_msg,
|
2012-08-23 03:29:21 +00:00
|
|
|
VIR_DOMAIN_QEMU_AGENT_COMMAND_DEFAULT);
|
2012-02-01 14:44:53 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("qemuAgentSend returned: %d", send_ret);
|
|
|
|
|
2013-07-25 08:59:21 +00:00
|
|
|
if (send_ret < 0)
|
2012-02-01 14:44:53 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!sync_msg.rxObject) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Missing monitor reply object"));
|
2012-02-01 14:44:53 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virJSONValueObjectGetNumberUlong(sync_msg.rxObject,
|
|
|
|
"return", &id_ret) < 0) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Malformed return value"));
|
2012-02-01 14:44:53 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("Guest returned ID: %llu", id_ret);
|
|
|
|
if (id_ret != id) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Guest agent returned ID: %llu instead of %llu"),
|
|
|
|
id_ret, id);
|
2012-02-01 14:44:53 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
cleanup:
|
2012-02-01 14:44:53 +00:00
|
|
|
virJSONValueFree(sync_msg.rxObject);
|
|
|
|
VIR_FREE(sync_msg.txBuffer);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-04-12 14:35:24 +00:00
|
|
|
static const char *
|
|
|
|
qemuAgentStringifyErrorClass(const char *klass)
|
|
|
|
{
|
|
|
|
if (STREQ_NULLABLE(klass, "BufferOverrun"))
|
|
|
|
return "Buffer overrun";
|
|
|
|
else if (STREQ_NULLABLE(klass, "CommandDisabled"))
|
|
|
|
return "The command has been disabled for this instance";
|
|
|
|
else if (STREQ_NULLABLE(klass, "CommandNotFound"))
|
|
|
|
return "The command has not been found";
|
|
|
|
else if (STREQ_NULLABLE(klass, "FdNotFound"))
|
|
|
|
return "File descriptor not found";
|
|
|
|
else if (STREQ_NULLABLE(klass, "InvalidParameter"))
|
|
|
|
return "Invalid parameter";
|
|
|
|
else if (STREQ_NULLABLE(klass, "InvalidParameterType"))
|
|
|
|
return "Invalid parameter type";
|
|
|
|
else if (STREQ_NULLABLE(klass, "InvalidParameterValue"))
|
|
|
|
return "Invalid parameter value";
|
|
|
|
else if (STREQ_NULLABLE(klass, "OpenFileFailed"))
|
|
|
|
return "Cannot open file";
|
|
|
|
else if (STREQ_NULLABLE(klass, "QgaCommandFailed"))
|
|
|
|
return "Guest agent command failed";
|
|
|
|
else if (STREQ_NULLABLE(klass, "QMPBadInputObjectMember"))
|
|
|
|
return "Bad QMP input object member";
|
|
|
|
else if (STREQ_NULLABLE(klass, "QMPExtraInputObjectMember"))
|
|
|
|
return "Unexpected extra object member";
|
|
|
|
else if (STREQ_NULLABLE(klass, "UndefinedError"))
|
|
|
|
return "An undefined error has occurred";
|
|
|
|
else if (STREQ_NULLABLE(klass, "Unsupported"))
|
|
|
|
return "this feature or command is not currently supported";
|
|
|
|
else if (klass)
|
|
|
|
return klass;
|
|
|
|
else
|
|
|
|
return "unknown QEMU command error";
|
|
|
|
}
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
/* Ignoring OOM in this method, since we're already reporting
|
|
|
|
* a more important error
|
|
|
|
*
|
|
|
|
* XXX see qerror.h for different klasses & fill out useful params
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
qemuAgentStringifyError(virJSONValuePtr error)
|
|
|
|
{
|
|
|
|
const char *klass = virJSONValueObjectGetString(error, "class");
|
2014-10-28 02:41:27 +00:00
|
|
|
const char *detail = virJSONValueObjectGetString(error, "desc");
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
/* The QMP 'desc' field is usually sufficient for our generic
|
2014-10-28 02:41:27 +00:00
|
|
|
* error reporting needs. However, if not present, translate
|
|
|
|
* the class into something readable.
|
2011-10-05 17:31:54 +00:00
|
|
|
*/
|
|
|
|
if (!detail)
|
2012-04-12 14:35:24 +00:00
|
|
|
detail = qemuAgentStringifyErrorClass(klass);
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
return detail;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
qemuAgentCommandName(virJSONValuePtr cmd)
|
|
|
|
{
|
|
|
|
const char *name = virJSONValueObjectGetString(cmd, "execute");
|
|
|
|
if (name)
|
|
|
|
return name;
|
|
|
|
else
|
|
|
|
return "<unknown>";
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qemuAgentCheckError(virJSONValuePtr cmd,
|
|
|
|
virJSONValuePtr reply)
|
|
|
|
{
|
|
|
|
if (virJSONValueObjectHasKey(reply, "error")) {
|
|
|
|
virJSONValuePtr error = virJSONValueObjectGet(reply, "error");
|
2012-08-09 10:46:29 +00:00
|
|
|
char *cmdstr = virJSONValueToString(cmd, false);
|
|
|
|
char *replystr = virJSONValueToString(reply, false);
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
/* Log the full JSON formatted command & error */
|
2012-11-28 12:30:40 +00:00
|
|
|
VIR_DEBUG("unable to execute QEMU agent command %s: %s",
|
2013-02-22 16:41:38 +00:00
|
|
|
NULLSTR(cmdstr), NULLSTR(replystr));
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
/* Only send the user the command name + friendly error */
|
|
|
|
if (!error)
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2012-11-28 12:30:40 +00:00
|
|
|
_("unable to execute QEMU agent command '%s'"),
|
2012-07-18 15:22:03 +00:00
|
|
|
qemuAgentCommandName(cmd));
|
2011-10-05 17:31:54 +00:00
|
|
|
else
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2012-11-28 12:30:40 +00:00
|
|
|
_("unable to execute QEMU agent command '%s': %s"),
|
2012-07-18 15:22:03 +00:00
|
|
|
qemuAgentCommandName(cmd),
|
|
|
|
qemuAgentStringifyError(error));
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
VIR_FREE(cmdstr);
|
|
|
|
VIR_FREE(replystr);
|
|
|
|
return -1;
|
|
|
|
} else if (!virJSONValueObjectHasKey(reply, "return")) {
|
2012-08-09 10:46:29 +00:00
|
|
|
char *cmdstr = virJSONValueToString(cmd, false);
|
|
|
|
char *replystr = virJSONValueToString(reply, false);
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Neither 'return' nor 'error' is set in the JSON reply %s: %s",
|
2013-02-22 16:41:38 +00:00
|
|
|
NULLSTR(cmdstr), NULLSTR(replystr));
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2012-11-28 12:30:40 +00:00
|
|
|
_("unable to execute QEMU agent command '%s'"),
|
2012-07-18 15:22:03 +00:00
|
|
|
qemuAgentCommandName(cmd));
|
2011-10-05 17:31:54 +00:00
|
|
|
VIR_FREE(cmdstr);
|
|
|
|
VIR_FREE(replystr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-02 06:57:59 +00:00
|
|
|
static int
|
|
|
|
qemuAgentCommand(qemuAgentPtr mon,
|
|
|
|
virJSONValuePtr cmd,
|
|
|
|
virJSONValuePtr *reply,
|
2014-04-03 05:20:25 +00:00
|
|
|
bool needReply,
|
2014-04-02 06:57:59 +00:00
|
|
|
int seconds)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
qemuAgentMessage msg;
|
|
|
|
char *cmdstr = NULL;
|
|
|
|
int await_event = mon->await_event;
|
|
|
|
|
|
|
|
*reply = NULL;
|
|
|
|
|
|
|
|
if (qemuAgentGuestSync(mon) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
|
|
|
|
if (!(cmdstr = virJSONValueToString(cmd, false)))
|
|
|
|
goto cleanup;
|
|
|
|
if (virAsprintf(&msg.txBuffer, "%s" LINE_ENDING, cmdstr) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
msg.txLength = strlen(msg.txBuffer);
|
|
|
|
|
|
|
|
VIR_DEBUG("Send command '%s' for write, seconds = %d", cmdstr, seconds);
|
|
|
|
|
|
|
|
ret = qemuAgentSend(mon, &msg, seconds);
|
|
|
|
|
|
|
|
VIR_DEBUG("Receive command reply ret=%d rxObject=%p",
|
|
|
|
ret, msg.rxObject);
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
/* If we haven't obtained any reply but we wait for an
|
|
|
|
* event, then don't report this as error */
|
|
|
|
if (!msg.rxObject) {
|
2014-04-03 05:20:25 +00:00
|
|
|
if (await_event && !needReply) {
|
2014-04-02 06:57:59 +00:00
|
|
|
VIR_DEBUG("Woken up by event %d", await_event);
|
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Missing monitor reply object"));
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*reply = msg.rxObject;
|
|
|
|
ret = qemuAgentCheckError(cmd, *reply);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(cmdstr);
|
|
|
|
VIR_FREE(msg.txBuffer);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-05 17:31:54 +00:00
|
|
|
static virJSONValuePtr ATTRIBUTE_SENTINEL
|
|
|
|
qemuAgentMakeCommand(const char *cmdname,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
virJSONValuePtr obj;
|
|
|
|
virJSONValuePtr jargs = NULL;
|
|
|
|
va_list args;
|
|
|
|
char *key;
|
|
|
|
|
|
|
|
va_start(args, cmdname);
|
|
|
|
|
|
|
|
if (!(obj = virJSONValueNewObject()))
|
2013-07-04 10:14:12 +00:00
|
|
|
goto error;
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
if (virJSONValueObjectAppendString(obj, "execute", cmdname) < 0)
|
2013-07-04 10:14:12 +00:00
|
|
|
goto error;
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
while ((key = va_arg(args, char *)) != NULL) {
|
|
|
|
int ret;
|
|
|
|
char type;
|
|
|
|
|
|
|
|
if (strlen(key) < 3) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("argument key '%s' is too short, missing type prefix"),
|
|
|
|
key);
|
2011-10-05 17:31:54 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keys look like s:name the first letter is a type code */
|
|
|
|
type = key[0];
|
|
|
|
key += 2;
|
|
|
|
|
|
|
|
if (!jargs &&
|
|
|
|
!(jargs = virJSONValueNewObject()))
|
2013-07-04 10:14:12 +00:00
|
|
|
goto error;
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
/* This doesn't support maps/arrays. This hasn't
|
|
|
|
* proved to be a problem..... yet :-) */
|
|
|
|
switch (type) {
|
|
|
|
case 's': {
|
|
|
|
char *val = va_arg(args, char *);
|
|
|
|
ret = virJSONValueObjectAppendString(jargs, key, val);
|
|
|
|
} break;
|
|
|
|
case 'i': {
|
|
|
|
int val = va_arg(args, int);
|
|
|
|
ret = virJSONValueObjectAppendNumberInt(jargs, key, val);
|
|
|
|
} break;
|
|
|
|
case 'u': {
|
|
|
|
unsigned int val = va_arg(args, unsigned int);
|
|
|
|
ret = virJSONValueObjectAppendNumberUint(jargs, key, val);
|
|
|
|
} break;
|
|
|
|
case 'I': {
|
|
|
|
long long val = va_arg(args, long long);
|
|
|
|
ret = virJSONValueObjectAppendNumberLong(jargs, key, val);
|
|
|
|
} break;
|
|
|
|
case 'U': {
|
|
|
|
/* qemu silently truncates numbers larger than LLONG_MAX,
|
|
|
|
* so passing the full range of unsigned 64 bit integers
|
|
|
|
* is not safe here. Pass them as signed 64 bit integers
|
|
|
|
* instead.
|
|
|
|
*/
|
|
|
|
long long val = va_arg(args, long long);
|
|
|
|
ret = virJSONValueObjectAppendNumberLong(jargs, key, val);
|
|
|
|
} break;
|
|
|
|
case 'd': {
|
|
|
|
double val = va_arg(args, double);
|
|
|
|
ret = virJSONValueObjectAppendNumberDouble(jargs, key, val);
|
|
|
|
} break;
|
|
|
|
case 'b': {
|
|
|
|
int val = va_arg(args, int);
|
|
|
|
ret = virJSONValueObjectAppendBoolean(jargs, key, val);
|
|
|
|
} break;
|
|
|
|
case 'n': {
|
|
|
|
ret = virJSONValueObjectAppendNull(jargs, key);
|
|
|
|
} break;
|
2013-04-15 15:00:04 +00:00
|
|
|
case 'a': {
|
|
|
|
virJSONValuePtr val = va_arg(args, virJSONValuePtr);
|
|
|
|
ret = virJSONValueObjectAppend(jargs, key, val);
|
|
|
|
} break;
|
2011-10-05 17:31:54 +00:00
|
|
|
default:
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("unsupported data type '%c' for arg '%s'"), type, key - 2);
|
2011-10-05 17:31:54 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (ret < 0)
|
2013-07-04 10:14:12 +00:00
|
|
|
goto error;
|
2011-10-05 17:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (jargs &&
|
|
|
|
virJSONValueObjectAppend(obj, "arguments", jargs) < 0)
|
2013-07-04 10:14:12 +00:00
|
|
|
goto error;
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
error:
|
2011-10-05 17:31:54 +00:00
|
|
|
virJSONValueFree(obj);
|
|
|
|
virJSONValueFree(jargs);
|
|
|
|
va_end(args);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-02 00:06:19 +00:00
|
|
|
static virJSONValuePtr
|
|
|
|
qemuAgentMakeStringsArray(const char **strings, unsigned int len)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
virJSONValuePtr ret = virJSONValueNewArray(), str;
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
str = virJSONValueNewString(strings[i]);
|
|
|
|
if (!str)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (virJSONValueArrayAppend(ret, str) < 0) {
|
|
|
|
virJSONValueFree(str);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
error:
|
|
|
|
virJSONValueFree(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-15 16:00:13 +00:00
|
|
|
void qemuAgentNotifyEvent(qemuAgentPtr mon,
|
|
|
|
qemuAgentEvent event)
|
|
|
|
{
|
|
|
|
VIR_DEBUG("mon=%p event=%d", mon, event);
|
|
|
|
if (mon->await_event == event) {
|
|
|
|
VIR_DEBUG("Waking up a tragedian");
|
|
|
|
mon->await_event = QEMU_AGENT_EVENT_NONE;
|
|
|
|
/* somebody waiting for this event, wake him up. */
|
|
|
|
if (mon->msg && !mon->msg->finished) {
|
|
|
|
mon->msg->finished = 1;
|
|
|
|
virCondSignal(&mon->notify);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* shouldn't happen but one never knows */
|
|
|
|
VIR_WARN("Received unexpected event %d", event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-05 17:31:54 +00:00
|
|
|
VIR_ENUM_DECL(qemuAgentShutdownMode);
|
|
|
|
|
|
|
|
VIR_ENUM_IMPL(qemuAgentShutdownMode,
|
|
|
|
QEMU_AGENT_SHUTDOWN_LAST,
|
|
|
|
"powerdown", "reboot", "halt");
|
|
|
|
|
|
|
|
int qemuAgentShutdown(qemuAgentPtr mon,
|
|
|
|
qemuAgentShutdownMode mode)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
virJSONValuePtr cmd;
|
|
|
|
virJSONValuePtr reply = NULL;
|
|
|
|
|
|
|
|
cmd = qemuAgentMakeCommand("guest-shutdown",
|
|
|
|
"s:mode", qemuAgentShutdownModeTypeToString(mode),
|
|
|
|
NULL);
|
|
|
|
if (!cmd)
|
|
|
|
return -1;
|
|
|
|
|
2012-09-04 10:01:43 +00:00
|
|
|
if (mode == QEMU_AGENT_SHUTDOWN_REBOOT)
|
|
|
|
mon->await_event = QEMU_AGENT_EVENT_RESET;
|
|
|
|
else
|
|
|
|
mon->await_event = QEMU_AGENT_EVENT_SHUTDOWN;
|
2014-04-03 05:20:25 +00:00
|
|
|
ret = qemuAgentCommand(mon, cmd, &reply, false,
|
2012-08-30 12:40:01 +00:00
|
|
|
VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK);
|
2011-10-05 17:31:54 +00:00
|
|
|
|
|
|
|
virJSONValueFree(cmd);
|
|
|
|
virJSONValueFree(reply);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-01-24 20:13:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* qemuAgentFSFreeze:
|
|
|
|
* @mon: Agent
|
2014-05-02 00:06:19 +00:00
|
|
|
* @mountpoints: Array of mountpoint paths to be frozen, or NULL for all
|
|
|
|
* @nmountpoints: Number of mountpoints to be frozen, or 0 for all
|
2012-01-24 20:13:40 +00:00
|
|
|
*
|
|
|
|
* Issue guest-fsfreeze-freeze command to guest agent,
|
2014-05-02 00:06:19 +00:00
|
|
|
* which freezes file systems mounted on specified mountpoints
|
|
|
|
* (or all file systems when @mountpoints is NULL), and returns
|
2012-01-24 20:13:40 +00:00
|
|
|
* number of frozen file systems on success.
|
|
|
|
*
|
|
|
|
* Returns: number of file system frozen on success,
|
|
|
|
* -1 on error.
|
|
|
|
*/
|
2014-05-02 00:06:19 +00:00
|
|
|
int qemuAgentFSFreeze(qemuAgentPtr mon, const char **mountpoints,
|
|
|
|
unsigned int nmountpoints)
|
2012-01-24 20:13:40 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
2014-08-27 18:42:41 +00:00
|
|
|
virJSONValuePtr cmd, arg = NULL;
|
2012-01-24 20:13:40 +00:00
|
|
|
virJSONValuePtr reply = NULL;
|
|
|
|
|
2014-05-02 00:06:19 +00:00
|
|
|
if (mountpoints && nmountpoints) {
|
|
|
|
arg = qemuAgentMakeStringsArray(mountpoints, nmountpoints);
|
|
|
|
if (!arg)
|
|
|
|
return -1;
|
|
|
|
|
2014-08-08 20:03:41 +00:00
|
|
|
cmd = qemuAgentMakeCommand("guest-fsfreeze-freeze-list",
|
2014-05-02 00:06:19 +00:00
|
|
|
"a:mountpoints", arg, NULL);
|
|
|
|
} else {
|
|
|
|
cmd = qemuAgentMakeCommand("guest-fsfreeze-freeze", NULL);
|
|
|
|
}
|
2012-01-24 20:13:40 +00:00
|
|
|
|
|
|
|
if (!cmd)
|
2014-08-27 18:42:41 +00:00
|
|
|
goto cleanup;
|
|
|
|
arg = NULL;
|
2012-01-24 20:13:40 +00:00
|
|
|
|
2014-04-03 05:20:25 +00:00
|
|
|
if (qemuAgentCommand(mon, cmd, &reply, true,
|
2014-04-01 12:58:56 +00:00
|
|
|
VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) < 0)
|
2012-01-24 20:13:40 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virJSONValueObjectGetNumberInt(reply, "return", &ret) < 0) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("malformed return value"));
|
2012-01-24 20:13:40 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
cleanup:
|
2014-08-27 18:42:41 +00:00
|
|
|
virJSONValueFree(arg);
|
2012-01-24 20:13:40 +00:00
|
|
|
virJSONValueFree(cmd);
|
|
|
|
virJSONValueFree(reply);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* qemuAgentFSThaw:
|
|
|
|
* @mon: Agent
|
|
|
|
*
|
|
|
|
* Issue guest-fsfreeze-thaw command to guest agent,
|
|
|
|
* which unfreezes all mounted file systems and returns
|
|
|
|
* number of thawed file systems on success.
|
|
|
|
*
|
|
|
|
* Returns: number of file system thawed on success,
|
|
|
|
* -1 on error.
|
|
|
|
*/
|
|
|
|
int qemuAgentFSThaw(qemuAgentPtr mon)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
virJSONValuePtr cmd;
|
|
|
|
virJSONValuePtr reply = NULL;
|
|
|
|
|
|
|
|
cmd = qemuAgentMakeCommand("guest-fsfreeze-thaw", NULL);
|
|
|
|
|
|
|
|
if (!cmd)
|
|
|
|
return -1;
|
|
|
|
|
2014-04-03 05:20:25 +00:00
|
|
|
if (qemuAgentCommand(mon, cmd, &reply, true,
|
2014-04-01 12:58:56 +00:00
|
|
|
VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) < 0)
|
2012-01-24 20:13:40 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virJSONValueObjectGetNumberInt(reply, "return", &ret) < 0) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("malformed return value"));
|
2012-01-24 20:13:40 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
cleanup:
|
2012-01-24 20:13:40 +00:00
|
|
|
virJSONValueFree(cmd);
|
|
|
|
virJSONValueFree(reply);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-02-13 11:27:25 +00:00
|
|
|
|
|
|
|
VIR_ENUM_DECL(qemuAgentSuspendMode);
|
|
|
|
|
|
|
|
VIR_ENUM_IMPL(qemuAgentSuspendMode,
|
|
|
|
VIR_NODE_SUSPEND_TARGET_LAST,
|
|
|
|
"guest-suspend-ram",
|
|
|
|
"guest-suspend-disk",
|
|
|
|
"guest-suspend-hybrid");
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuAgentSuspend(qemuAgentPtr mon,
|
|
|
|
unsigned int target)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
virJSONValuePtr cmd;
|
|
|
|
virJSONValuePtr reply = NULL;
|
|
|
|
|
|
|
|
cmd = qemuAgentMakeCommand(qemuAgentSuspendModeTypeToString(target),
|
|
|
|
NULL);
|
|
|
|
if (!cmd)
|
|
|
|
return -1;
|
|
|
|
|
2012-06-15 16:00:13 +00:00
|
|
|
mon->await_event = QEMU_AGENT_EVENT_SUSPEND;
|
2014-04-03 05:20:25 +00:00
|
|
|
ret = qemuAgentCommand(mon, cmd, &reply, false,
|
2012-08-30 12:40:01 +00:00
|
|
|
VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK);
|
2012-02-13 11:27:25 +00:00
|
|
|
|
|
|
|
virJSONValueFree(cmd);
|
|
|
|
virJSONValueFree(reply);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-08-23 03:29:22 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
qemuAgentArbitraryCommand(qemuAgentPtr mon,
|
|
|
|
const char *cmd_str,
|
|
|
|
char **result,
|
|
|
|
int timeout)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
2013-06-03 13:58:31 +00:00
|
|
|
virJSONValuePtr cmd = NULL;
|
2012-08-23 03:29:22 +00:00
|
|
|
virJSONValuePtr reply = NULL;
|
|
|
|
|
|
|
|
*result = NULL;
|
2013-06-03 13:58:31 +00:00
|
|
|
if (timeout < VIR_DOMAIN_QEMU_AGENT_COMMAND_MIN) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
_("guest agent timeout '%d' is "
|
|
|
|
"less than the minimum '%d'"),
|
|
|
|
timeout, VIR_DOMAIN_QEMU_AGENT_COMMAND_MIN);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-08-23 03:29:22 +00:00
|
|
|
|
2013-06-03 13:58:31 +00:00
|
|
|
if (!(cmd = virJSONValueFromString(cmd_str)))
|
|
|
|
goto cleanup;
|
|
|
|
|
2014-04-03 05:20:25 +00:00
|
|
|
if ((ret = qemuAgentCommand(mon, cmd, &reply, true, timeout)) < 0)
|
2013-06-03 13:58:31 +00:00
|
|
|
goto cleanup;
|
2012-08-23 03:29:22 +00:00
|
|
|
|
2013-06-03 13:58:31 +00:00
|
|
|
if (!(*result = virJSONValueToString(reply, false)))
|
|
|
|
ret = -1;
|
2012-08-23 03:29:22 +00:00
|
|
|
|
2013-06-03 13:58:31 +00:00
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
cleanup:
|
2012-08-23 03:29:22 +00:00
|
|
|
virJSONValueFree(cmd);
|
|
|
|
virJSONValueFree(reply);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-11-20 16:10:29 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
qemuAgentFSTrim(qemuAgentPtr mon,
|
|
|
|
unsigned long long minimum)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
virJSONValuePtr cmd;
|
|
|
|
virJSONValuePtr reply = NULL;
|
|
|
|
|
|
|
|
cmd = qemuAgentMakeCommand("guest-fstrim",
|
|
|
|
"U:minimum", minimum,
|
|
|
|
NULL);
|
|
|
|
if (!cmd)
|
|
|
|
return ret;
|
|
|
|
|
2014-04-03 05:20:25 +00:00
|
|
|
ret = qemuAgentCommand(mon, cmd, &reply, false,
|
2012-11-20 16:10:29 +00:00
|
|
|
VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK);
|
|
|
|
|
|
|
|
virJSONValueFree(cmd);
|
|
|
|
virJSONValueFree(reply);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-04-12 10:14:02 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
qemuAgentGetVCPUs(qemuAgentPtr mon,
|
|
|
|
qemuAgentCPUInfoPtr *info)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2013-04-12 10:14:02 +00:00
|
|
|
virJSONValuePtr cmd;
|
|
|
|
virJSONValuePtr reply = NULL;
|
|
|
|
virJSONValuePtr data = NULL;
|
|
|
|
int ndata;
|
|
|
|
|
|
|
|
if (!(cmd = qemuAgentMakeCommand("guest-get-vcpus", NULL)))
|
|
|
|
return -1;
|
|
|
|
|
2014-04-03 05:20:25 +00:00
|
|
|
if (qemuAgentCommand(mon, cmd, &reply, true,
|
2014-04-01 12:58:56 +00:00
|
|
|
VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) < 0)
|
2013-04-12 10:14:02 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!(data = virJSONValueObjectGet(reply, "return"))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("guest-get-vcpus reply was missing return data"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->type != VIR_JSON_TYPE_ARRAY) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("guest-get-vcpus return information was not an array"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ndata = virJSONValueArraySize(data);
|
|
|
|
|
2013-07-04 10:14:12 +00:00
|
|
|
if (VIR_ALLOC_N(*info, ndata) < 0)
|
2013-04-12 10:14:02 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
for (i = 0; i < ndata; i++) {
|
|
|
|
virJSONValuePtr entry = virJSONValueArrayGet(data, i);
|
|
|
|
qemuAgentCPUInfoPtr in = *info + i;
|
|
|
|
|
|
|
|
if (!entry) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("array element missing in guest-get-vcpus return "
|
|
|
|
"value"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virJSONValueObjectGetNumberUint(entry, "logical-id", &in->id) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("'logical-id' missing in reply of guest-get-vcpus"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virJSONValueObjectGetBoolean(entry, "online", &in->online) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("'online' missing in reply of guest-get-vcpus"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virJSONValueObjectGetBoolean(entry, "can-offline",
|
|
|
|
&in->offlinable) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("'can-offline' missing in reply of guest-get-vcpus"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ndata;
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
cleanup:
|
2013-04-12 10:14:02 +00:00
|
|
|
virJSONValueFree(cmd);
|
|
|
|
virJSONValueFree(reply);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the VCPU state using guest agent.
|
|
|
|
*
|
|
|
|
* Returns -1 on error, ninfo in case everything was successful and less than
|
|
|
|
* ninfo on a partial failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
qemuAgentSetVCPUs(qemuAgentPtr mon,
|
|
|
|
qemuAgentCPUInfoPtr info,
|
|
|
|
size_t ninfo)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
virJSONValuePtr cmd = NULL;
|
|
|
|
virJSONValuePtr reply = NULL;
|
|
|
|
virJSONValuePtr cpus = NULL;
|
|
|
|
virJSONValuePtr cpu = NULL;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* create the key data array */
|
|
|
|
if (!(cpus = virJSONValueNewArray()))
|
2013-07-04 10:14:12 +00:00
|
|
|
goto cleanup;
|
2013-04-12 10:14:02 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ninfo; i++) {
|
|
|
|
qemuAgentCPUInfoPtr in = &info[i];
|
|
|
|
|
|
|
|
/* create single cpu object */
|
|
|
|
if (!(cpu = virJSONValueNewObject()))
|
2013-07-04 10:14:12 +00:00
|
|
|
goto cleanup;
|
2013-04-12 10:14:02 +00:00
|
|
|
|
|
|
|
if (virJSONValueObjectAppendNumberInt(cpu, "logical-id", in->id) < 0)
|
2013-07-04 10:14:12 +00:00
|
|
|
goto cleanup;
|
2013-04-12 10:14:02 +00:00
|
|
|
|
|
|
|
if (virJSONValueObjectAppendBoolean(cpu, "online", in->online) < 0)
|
2013-07-04 10:14:12 +00:00
|
|
|
goto cleanup;
|
2013-04-12 10:14:02 +00:00
|
|
|
|
|
|
|
if (virJSONValueArrayAppend(cpus, cpu) < 0)
|
2013-07-04 10:14:12 +00:00
|
|
|
goto cleanup;
|
2013-04-12 10:14:02 +00:00
|
|
|
|
|
|
|
cpu = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(cmd = qemuAgentMakeCommand("guest-set-vcpus",
|
|
|
|
"a:vcpus", cpus,
|
|
|
|
NULL)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
cpus = NULL;
|
|
|
|
|
2014-04-03 05:20:25 +00:00
|
|
|
if (qemuAgentCommand(mon, cmd, &reply, true,
|
2014-04-01 12:58:56 +00:00
|
|
|
VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) < 0)
|
2013-04-12 10:14:02 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virJSONValueObjectGetNumberInt(reply, "return", &ret) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("malformed return value"));
|
|
|
|
}
|
|
|
|
|
2014-03-25 06:49:44 +00:00
|
|
|
cleanup:
|
2013-04-12 10:14:02 +00:00
|
|
|
virJSONValueFree(cmd);
|
|
|
|
virJSONValueFree(reply);
|
|
|
|
virJSONValueFree(cpu);
|
|
|
|
virJSONValueFree(cpus);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-07-30 10:04:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* modify the cpu info structure to set the correct amount of cpus */
|
|
|
|
int
|
|
|
|
qemuAgentUpdateCPUInfo(unsigned int nvcpus,
|
|
|
|
qemuAgentCPUInfoPtr cpuinfo,
|
|
|
|
int ncpuinfo)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int nonline = 0;
|
|
|
|
int nofflinable = 0;
|
|
|
|
|
|
|
|
/* count the active and offlinable cpus */
|
|
|
|
for (i = 0; i < ncpuinfo; i++) {
|
|
|
|
if (cpuinfo[i].online)
|
|
|
|
nonline++;
|
|
|
|
|
|
|
|
if (cpuinfo[i].offlinable && cpuinfo[i].online)
|
|
|
|
nofflinable++;
|
|
|
|
|
|
|
|
/* This shouldn't happen, but we can't trust the guest agent */
|
|
|
|
if (!cpuinfo[i].online && !cpuinfo[i].offlinable) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Invalid data provided by guest agent"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the guest agent reported less cpus than requested */
|
|
|
|
if (nvcpus > ncpuinfo) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("guest agent reports less cpu than requested"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not enough offlinable CPUs to support the request */
|
|
|
|
if (nvcpus < nonline - nofflinable) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG, "%s",
|
|
|
|
_("Cannot offline enough CPUs"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ncpuinfo; i++) {
|
|
|
|
if (nvcpus < nonline) {
|
|
|
|
/* unplug */
|
|
|
|
if (cpuinfo[i].offlinable && cpuinfo[i].online) {
|
|
|
|
cpuinfo[i].online = false;
|
|
|
|
nonline--;
|
|
|
|
}
|
|
|
|
} else if (nvcpus > nonline) {
|
|
|
|
/* plug */
|
|
|
|
if (!cpuinfo[i].online) {
|
|
|
|
cpuinfo[i].online = true;
|
|
|
|
nonline++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* done */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-02 17:05:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuAgentGetTime(qemuAgentPtr mon,
|
|
|
|
long long *seconds,
|
|
|
|
unsigned int *nseconds)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
unsigned long long json_time;
|
|
|
|
virJSONValuePtr cmd;
|
|
|
|
virJSONValuePtr reply = NULL;
|
|
|
|
|
|
|
|
cmd = qemuAgentMakeCommand("guest-get-time",
|
|
|
|
NULL);
|
|
|
|
if (!cmd)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (qemuAgentCommand(mon, cmd, &reply, true,
|
|
|
|
VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virJSONValueObjectGetNumberUlong(reply, "return", &json_time) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("malformed return value"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* guest agent returns time in nanoseconds,
|
|
|
|
* we need it in seconds here */
|
|
|
|
*seconds = json_time / 1000000000LL;
|
|
|
|
*nseconds = json_time % 1000000000LL;
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virJSONValueFree(cmd);
|
|
|
|
virJSONValueFree(reply);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* qemuAgentSetTime:
|
|
|
|
* @setTime: time to set
|
|
|
|
* @sync: let guest agent to read domain's RTC (@setTime is ignored)
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
qemuAgentSetTime(qemuAgentPtr mon,
|
|
|
|
long long seconds,
|
|
|
|
unsigned int nseconds,
|
2014-05-19 14:36:55 +00:00
|
|
|
bool rtcSync)
|
2014-04-02 17:05:42 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
virJSONValuePtr cmd;
|
|
|
|
virJSONValuePtr reply = NULL;
|
|
|
|
|
2014-05-19 14:36:55 +00:00
|
|
|
if (rtcSync) {
|
2014-04-02 17:05:42 +00:00
|
|
|
cmd = qemuAgentMakeCommand("guest-set-time", NULL);
|
|
|
|
} else {
|
|
|
|
/* guest agent expect time with nanosecond granularity.
|
|
|
|
* Impressing. */
|
|
|
|
long long json_time;
|
|
|
|
|
|
|
|
/* Check if we overflow. For some reason qemu doesn't handle unsigned
|
|
|
|
* long long on the monitor well as it silently truncates numbers to
|
|
|
|
* signed long long. Therefore we must check overflow against LLONG_MAX
|
|
|
|
* not ULLONG_MAX. */
|
|
|
|
if (seconds > LLONG_MAX / 1000000000LL) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
_("Time '%lld' is too big for guest agent"),
|
|
|
|
seconds);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_time = seconds * 1000000000LL;
|
|
|
|
json_time += nseconds;
|
|
|
|
cmd = qemuAgentMakeCommand("guest-set-time",
|
|
|
|
"I:time", json_time,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cmd)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (qemuAgentCommand(mon, cmd, &reply, true,
|
|
|
|
VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virJSONValueFree(cmd);
|
|
|
|
virJSONValueFree(reply);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-11-22 01:27:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuAgentGetFSInfo(qemuAgentPtr mon, virDomainFSInfoPtr **info,
|
|
|
|
virDomainDefPtr vmdef)
|
|
|
|
{
|
|
|
|
size_t i, j, k;
|
|
|
|
int ret = -1;
|
|
|
|
int ndata = 0, ndisk;
|
|
|
|
char **alias;
|
|
|
|
virJSONValuePtr cmd;
|
|
|
|
virJSONValuePtr reply = NULL;
|
|
|
|
virJSONValuePtr data;
|
|
|
|
virDomainFSInfoPtr *info_ret = NULL;
|
|
|
|
virDevicePCIAddress pci_address;
|
|
|
|
|
|
|
|
cmd = qemuAgentMakeCommand("guest-get-fsinfo", NULL);
|
|
|
|
if (!cmd)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (qemuAgentCommand(mon, cmd, &reply, true,
|
|
|
|
VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!(data = virJSONValueObjectGet(reply, "return"))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("guest-get-fsinfo reply was missing return data"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->type != VIR_JSON_TYPE_ARRAY) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("guest-get-fsinfo return information was not "
|
|
|
|
"an array"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ndata = virJSONValueArraySize(data);
|
|
|
|
if (!ndata) {
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (VIR_ALLOC_N(info_ret, ndata) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
for (i = 0; i < ndata; i++) {
|
|
|
|
/* Reverse the order to arrange in mount order */
|
|
|
|
virJSONValuePtr entry = virJSONValueArrayGet(data, ndata - 1 - i);
|
|
|
|
|
|
|
|
if (!entry) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("array element '%zd' of '%d' missing in "
|
|
|
|
"guest-get-fsinfo return data"),
|
|
|
|
i, ndata);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_ALLOC(info_ret[i]) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (VIR_STRDUP(info_ret[i]->mountpoint,
|
|
|
|
virJSONValueObjectGetString(entry, "mountpoint")) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("'mountpoint' missing in reply of "
|
|
|
|
"guest-get-fsinfo"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_STRDUP(info_ret[i]->name,
|
|
|
|
virJSONValueObjectGetString(entry, "name")) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("'name' missing in reply of guest-get-fsinfo"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_STRDUP(info_ret[i]->fstype,
|
|
|
|
virJSONValueObjectGetString(entry, "type")) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("'type' missing in reply of guest-get-fsinfo"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(entry = virJSONValueObjectGet(entry, "disk"))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("'disk' missing in reply of guest-get-fsinfo"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->type != VIR_JSON_TYPE_ARRAY) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("guest-get-fsinfo 'disk' data was not an array"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ndisk = virJSONValueArraySize(entry);
|
|
|
|
if (!ndisk)
|
|
|
|
continue;
|
|
|
|
if (VIR_ALLOC_N(info_ret[i]->devAlias, ndisk) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
alias = info_ret[i]->devAlias;
|
|
|
|
info_ret[i]->ndevAlias = 0;
|
|
|
|
for (j = 0; j < ndisk; j++) {
|
|
|
|
virJSONValuePtr disk = virJSONValueArrayGet(entry, j);
|
|
|
|
virJSONValuePtr pci;
|
|
|
|
int diskaddr[3], pciaddr[4], idx;
|
|
|
|
const char *diskaddr_comp[] = {"bus", "target", "unit"};
|
|
|
|
const char *pciaddr_comp[] = {"domain", "bus", "slot", "function"};
|
|
|
|
|
|
|
|
if (!disk) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("array element '%zd' of '%d' missing in "
|
|
|
|
"guest-get-fsinfo 'disk' data"),
|
|
|
|
j, ndisk);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(pci = virJSONValueObjectGet(disk, "pci-controller"))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("'pci-controller' missing in guest-get-fsinfo "
|
|
|
|
"'disk' data"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (k = 0; k < 3; k++) {
|
|
|
|
if (virJSONValueObjectGetNumberInt(
|
|
|
|
disk, diskaddr_comp[k], &diskaddr[k]) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("'%s' missing in guest-get-fsinfo "
|
|
|
|
"'disk' data"), diskaddr_comp[k]);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (k = 0; k < 4; k++) {
|
|
|
|
if (virJSONValueObjectGetNumberInt(
|
|
|
|
pci, pciaddr_comp[k], &pciaddr[k]) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("'%s' missing in guest-get-fsinfo "
|
|
|
|
"'pci-address' data"), pciaddr_comp[k]);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pci_address.domain = pciaddr[0];
|
|
|
|
pci_address.bus = pciaddr[1];
|
|
|
|
pci_address.slot = pciaddr[2];
|
|
|
|
pci_address.function = pciaddr[3];
|
|
|
|
if ((idx = virDomainDiskIndexByAddress(
|
|
|
|
vmdef, &pci_address,
|
|
|
|
diskaddr[0], diskaddr[1], diskaddr[2])) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (VIR_STRDUP(*alias, vmdef->disks[idx]->dst) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (*alias) {
|
|
|
|
alias++;
|
|
|
|
info_ret[i]->ndevAlias++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*info = info_ret;
|
|
|
|
info_ret = NULL;
|
|
|
|
ret = ndata;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (info_ret) {
|
|
|
|
for (i = 0; i < ndata; i++)
|
|
|
|
virDomainFSInfoFree(info_ret[i]);
|
|
|
|
VIR_FREE(info_ret);
|
|
|
|
}
|
|
|
|
virJSONValueFree(cmd);
|
|
|
|
virJSONValueFree(reply);
|
|
|
|
return ret;
|
|
|
|
}
|