1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

Add the monitor type to the domain state XML

There are no functional changes in this patch apart from adding the
monitor type to the state XML.

The patch mostly consists of switching to use virDomainChrDef every
where to describe the monitor.

* src/domain_conf.h: replace monitorpath with monitor_chr

* src/domain_conf.c: handle parsing the monitor type and initializing
  monitor chr

* src/qemu_conf.[ch]: make qemudBuildCommandLine take a
  virDomainChrDefPtr and use that to build the -monitor parameter

* src/qemu_driver.c: split pty specific and common code from
  qemudOpenMonitor, have qemudStartVMDaemon() initialize monitor_chr

* tests/qemuxml2argvtest.c: update for qemudBuildCommandLine() change
This commit is contained in:
Mark McLoughlin 2009-07-09 18:06:38 +01:00
parent 1f4ec305f0
commit 05d377bdd2
6 changed files with 127 additions and 36 deletions

View File

@ -516,7 +516,8 @@ void virDomainObjFree(virDomainObjPtr dom)
virDomainDefFree(dom->def); virDomainDefFree(dom->def);
virDomainDefFree(dom->newDef); virDomainDefFree(dom->newDef);
VIR_FREE(dom->monitorpath); virDomainChrDefFree(dom->monitor_chr);
VIR_FREE(dom->vcpupids); VIR_FREE(dom->vcpupids);
virMutexDestroy(&dom->lock); virMutexDestroy(&dom->lock);
@ -2890,6 +2891,7 @@ static virDomainObjPtr virDomainObjParseXML(virConnectPtr conn,
xmlNodePtr config; xmlNodePtr config;
xmlNodePtr oldnode; xmlNodePtr oldnode;
virDomainObjPtr obj; virDomainObjPtr obj;
char *monitorpath;
if (!(obj = virDomainObjNew(conn))) if (!(obj = virDomainObjNew(conn)))
return NULL; return NULL;
@ -2927,16 +2929,41 @@ static virDomainObjPtr virDomainObjParseXML(virConnectPtr conn,
} }
obj->pid = (pid_t)val; obj->pid = (pid_t)val;
if(!(obj->monitorpath = if (VIR_ALLOC(obj->monitor_chr) < 0) {
virReportOOMError(conn);
goto error;
}
if (!(monitorpath =
virXPathString(conn, "string(./monitor[1]/@path)", ctxt))) { virXPathString(conn, "string(./monitor[1]/@path)", ctxt))) {
virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR, virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR,
"%s", _("no monitor path")); "%s", _("no monitor path"));
goto error; goto error;
} }
tmp = virXPathString(conn, "string(./monitor[1]/@type)", ctxt);
if (tmp)
obj->monitor_chr->type = virDomainChrTypeFromString(tmp);
else
obj->monitor_chr->type = VIR_DOMAIN_CHR_TYPE_PTY;
VIR_FREE(tmp);
switch (obj->monitor_chr->type) {
case VIR_DOMAIN_CHR_TYPE_PTY:
obj->monitor_chr->data.file.path = monitorpath;
break;
default:
VIR_FREE(monitorpath);
virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR,
_("unsupported monitor type '%s'"),
virDomainChrTypeToString(obj->monitor_chr->type));
break;
}
return obj; return obj;
error: error:
virDomainChrDefFree(obj->monitor_chr);
virDomainObjFree(obj); virDomainObjFree(obj);
return NULL; return NULL;
} }
@ -4134,11 +4161,22 @@ char *virDomainObjFormat(virConnectPtr conn,
{ {
char *config_xml = NULL, *xml = NULL; char *config_xml = NULL, *xml = NULL;
virBuffer buf = VIR_BUFFER_INITIALIZER; virBuffer buf = VIR_BUFFER_INITIALIZER;
const char *monitorpath;
virBufferVSprintf(&buf, "<domstatus state='%s' pid='%d'>\n", virBufferVSprintf(&buf, "<domstatus state='%s' pid='%d'>\n",
virDomainStateTypeToString(obj->state), virDomainStateTypeToString(obj->state),
obj->pid); obj->pid);
virBufferEscapeString(&buf, " <monitor path='%s'/>\n", obj->monitorpath);
switch (obj->monitor_chr->type) {
default:
case VIR_DOMAIN_CHR_TYPE_PTY:
monitorpath = obj->monitor_chr->data.file.path;
break;
}
virBufferEscapeString(&buf, " <monitor path='%s'", monitorpath);
virBufferVSprintf(&buf, " type='%s'/>\n",
virDomainChrTypeToString(obj->monitor_chr->type));
if (!(config_xml = virDomainDefFormat(conn, if (!(config_xml = virDomainDefFormat(conn,
obj->def, obj->def,

View File

@ -538,7 +538,7 @@ struct _virDomainObj {
virMutex lock; virMutex lock;
int monitor; int monitor;
char *monitorpath; virDomainChrDefPtr monitor_chr;
int monitorWatch; int monitorWatch;
int pid; int pid;
int state; int state;

View File

@ -888,6 +888,7 @@ static int qemudBuildCommandLineChrDevStr(virDomainChrDefPtr dev,
int qemudBuildCommandLine(virConnectPtr conn, int qemudBuildCommandLine(virConnectPtr conn,
struct qemud_driver *driver, struct qemud_driver *driver,
virDomainDefPtr def, virDomainDefPtr def,
virDomainChrDefPtr monitor_chr,
unsigned int qemuCmdFlags, unsigned int qemuCmdFlags,
const char ***retargv, const char ***retargv,
const char ***retenv, const char ***retenv,
@ -1118,8 +1119,15 @@ int qemudBuildCommandLine(virConnectPtr conn,
if (!def->graphics) if (!def->graphics)
ADD_ARG_LIT("-nographic"); ADD_ARG_LIT("-nographic");
if (monitor_chr) {
char buf[4096];
if (qemudBuildCommandLineChrDevStr(monitor_chr, buf, sizeof(buf)) < 0)
goto error;
ADD_ARG_LIT("-monitor"); ADD_ARG_LIT("-monitor");
ADD_ARG_LIT("pty"); ADD_ARG_LIT(buf);
}
if (def->localtime) if (def->localtime)
ADD_ARG_LIT("-localtime"); ADD_ARG_LIT("-localtime");

View File

@ -129,6 +129,7 @@ int qemudParseHelpStr (const char *str,
int qemudBuildCommandLine (virConnectPtr conn, int qemudBuildCommandLine (virConnectPtr conn,
struct qemud_driver *driver, struct qemud_driver *driver,
virDomainDefPtr def, virDomainDefPtr def,
virDomainChrDefPtr monitor_chr,
unsigned int qemuCmdFlags, unsigned int qemuCmdFlags,
const char ***retargv, const char ***retargv,
const char ***retenv, const char ***retenv,

View File

@ -283,7 +283,6 @@ cleanup:
static int qemudOpenMonitor(virConnectPtr conn, static int qemudOpenMonitor(virConnectPtr conn,
struct qemud_driver* driver, struct qemud_driver* driver,
virDomainObjPtr vm, virDomainObjPtr vm,
const char *monitor,
int reconnect); int reconnect);
@ -297,7 +296,7 @@ qemuReconnectDomain(struct qemud_driver *driver,
{ {
int rc; int rc;
if ((rc = qemudOpenMonitor(NULL, driver, obj, obj->monitorpath, 1)) != 0) { if ((rc = qemudOpenMonitor(NULL, driver, obj, 1)) != 0) {
VIR_ERROR(_("Failed to reconnect monitor for %s: %d\n"), VIR_ERROR(_("Failed to reconnect monitor for %s: %d\n"),
obj->def->name, rc); obj->def->name, rc);
goto error; goto error;
@ -821,30 +820,24 @@ qemudCheckMonitorPrompt(virConnectPtr conn ATTRIBUTE_UNUSED,
} }
static int static int
qemudOpenMonitor(virConnectPtr conn, qemudOpenMonitorCommon(virConnectPtr conn,
struct qemud_driver* driver, struct qemud_driver* driver,
virDomainObjPtr vm, virDomainObjPtr vm,
const char *monitor, int monfd,
int reconnect) int reconnect)
{ {
int monfd;
char buf[1024]; char buf[1024];
int ret = -1; int ret;
if ((monfd = open(monitor, O_RDWR)) < 0) {
qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
_("Unable to open monitor path %s"), monitor);
return -1;
}
if (virSetCloseExec(monfd) < 0) { if (virSetCloseExec(monfd) < 0) {
qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
"%s", _("Unable to set monitor close-on-exec flag")); "%s", _("Unable to set monitor close-on-exec flag"));
goto error; return -1;
} }
if (virSetNonBlock(monfd) < 0) { if (virSetNonBlock(monfd) < 0) {
qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
"%s", _("Unable to put monitor into non-blocking mode")); "%s", _("Unable to put monitor into non-blocking mode"));
goto error; return -1;
} }
if (!reconnect) { if (!reconnect) {
@ -862,21 +855,58 @@ qemudOpenMonitor(virConnectPtr conn,
} }
if (ret != 0) if (ret != 0)
goto error; return ret;
if ((vm->monitorWatch = virEventAddHandle(vm->monitor, 0, if ((vm->monitorWatch = virEventAddHandle(vm->monitor, 0,
qemudDispatchVMEvent, qemudDispatchVMEvent,
driver, NULL)) < 0) driver, NULL)) < 0)
return -1;
return 0;
}
static int
qemudOpenMonitorPty(virConnectPtr conn,
struct qemud_driver* driver,
virDomainObjPtr vm,
const char *monitor,
int reconnect)
{
int monfd;
if ((monfd = open(monitor, O_RDWR)) < 0) {
qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
_("Unable to open monitor path %s"), monitor);
return -1;
}
if (qemudOpenMonitorCommon(conn, driver, vm, monfd, reconnect) < 0)
goto error; goto error;
return 0;
/* Keep monitor open upon success */ error:
if (ret == 0)
return ret;
error:
close(monfd); close(monfd);
return ret; return -1;
}
static int
qemudOpenMonitor(virConnectPtr conn,
struct qemud_driver *driver,
virDomainObjPtr vm,
int reconnect)
{
switch (vm->monitor_chr->type) {
case VIR_DOMAIN_CHR_TYPE_PTY:
return qemudOpenMonitorPty(conn, driver, vm,
vm->monitor_chr->data.file.path,
reconnect);
default:
qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
_("unable to handle monitor type: %s"),
virDomainChrTypeToString(vm->monitor_chr->type));
return -1;
}
} }
/* Returns -1 for error, 0 success, 1 continue reading */ /* Returns -1 for error, 0 success, 1 continue reading */
@ -966,10 +996,11 @@ qemudFindCharDevicePTYs(virConnectPtr conn,
} }
/* Got them all, so now open the monitor console */ /* Got them all, so now open the monitor console */
if ((ret = qemudOpenMonitor(conn, driver, vm, monitor, 0)) != 0) vm->monitor_chr->data.file.path = monitor;
goto cleanup; monitor = NULL;
vm->monitorpath = monitor; if ((ret = qemudOpenMonitor(conn, driver, vm, 0)) != 0)
goto cleanup;
return 0; return 0;
@ -1414,6 +1445,13 @@ static int qemudStartVMDaemon(virConnectPtr conn,
if (qemuPrepareHostDevices(conn, vm->def) < 0) if (qemuPrepareHostDevices(conn, vm->def) < 0)
goto cleanup; goto cleanup;
if (VIR_ALLOC(vm->monitor_chr) < 0) {
virReportOOMError(conn);
goto cleanup;
}
vm->monitor_chr->type = VIR_DOMAIN_CHR_TYPE_PTY;
if ((ret = virFileDeletePid(driver->stateDir, vm->def->name)) != 0) { if ((ret = virFileDeletePid(driver->stateDir, vm->def->name)) != 0) {
virReportSystemError(conn, ret, virReportSystemError(conn, ret,
_("Cannot remove stale PID file for %s"), _("Cannot remove stale PID file for %s"),
@ -1428,7 +1466,7 @@ static int qemudStartVMDaemon(virConnectPtr conn,
} }
vm->def->id = driver->nextvmid++; vm->def->id = driver->nextvmid++;
if (qemudBuildCommandLine(conn, driver, vm->def, if (qemudBuildCommandLine(conn, driver, vm->def, vm->monitor_chr,
qemuCmdFlags, &argv, &progenv, qemuCmdFlags, &argv, &progenv,
&tapfds, &ntapfds, migrateFrom) < 0) &tapfds, &ntapfds, migrateFrom) < 0)
goto cleanup; goto cleanup;
@ -3405,6 +3443,7 @@ static char *qemuDomainXMLToNative(virConnectPtr conn,
unsigned int flags ATTRIBUTE_UNUSED) { unsigned int flags ATTRIBUTE_UNUSED) {
struct qemud_driver *driver = conn->privateData; struct qemud_driver *driver = conn->privateData;
virDomainDefPtr def = NULL; virDomainDefPtr def = NULL;
virDomainChrDef monitor_chr;
const char *emulator; const char *emulator;
unsigned int qemuCmdFlags; unsigned int qemuCmdFlags;
struct stat sb; struct stat sb;
@ -3483,9 +3522,10 @@ static char *qemuDomainXMLToNative(virConnectPtr conn,
goto cleanup; goto cleanup;
} }
monitor_chr.type = VIR_DOMAIN_CHR_TYPE_PTY;
if (qemudBuildCommandLine(conn, driver, def, if (qemudBuildCommandLine(conn, driver, def,
qemuCmdFlags, &monitor_chr, qemuCmdFlags,
&retargv, &retenv, &retargv, &retenv,
NULL, NULL, /* Don't want it to create TAP devices */ NULL, NULL, /* Don't want it to create TAP devices */
NULL) < 0) { NULL) < 0) {

View File

@ -34,6 +34,7 @@ static int testCompareXMLToArgvFiles(const char *xml,
const char **tmp = NULL; const char **tmp = NULL;
int ret = -1, len, flags; int ret = -1, len, flags;
virDomainDefPtr vmdef = NULL; virDomainDefPtr vmdef = NULL;
virDomainChrDef monitor_chr;
if (virtTestLoadFile(cmd, &expectargv, MAX_FILE) < 0) if (virtTestLoadFile(cmd, &expectargv, MAX_FILE) < 0)
goto fail; goto fail;
@ -47,12 +48,15 @@ static int testCompareXMLToArgvFiles(const char *xml,
else else
vmdef->id = -1; vmdef->id = -1;
monitor_chr.type = VIR_DOMAIN_CHR_TYPE_PTY;
flags = QEMUD_CMD_FLAG_VNC_COLON | flags = QEMUD_CMD_FLAG_VNC_COLON |
QEMUD_CMD_FLAG_NO_REBOOT | QEMUD_CMD_FLAG_NO_REBOOT |
extraFlags; extraFlags;
if (qemudBuildCommandLine(NULL, &driver, if (qemudBuildCommandLine(NULL, &driver,
vmdef, flags, &argv, &qenv, vmdef, &monitor_chr, flags,
&argv, &qenv,
NULL, NULL, migrateFrom) < 0) NULL, NULL, migrateFrom) < 0)
goto fail; goto fail;