Allow autoconnect to virsh console in create/start. Refuse to connect to remote consoles

This commit is contained in:
Daniel P. Berrange 2009-04-03 14:03:38 +00:00
parent 92ff72b52c
commit f0817018b1
2 changed files with 54 additions and 8 deletions

View File

@ -1,3 +1,9 @@
Fri Apr 3 15:03:00 BST 2009 Daniel P. Berrange <berrange@redhat.com>
* src/virsh.c: Add --console arg for create & start commands
to auto-attach to console. Print info about escape key for
console. Refuse to connect to remote consoles.
Fri Apr 3 13:41:00 BST 2009 Daniel P. Berrange <berrange@redhat.com> Fri Apr 3 13:41:00 BST 2009 Daniel P. Berrange <berrange@redhat.com>
Misc NUMA memory fixes Misc NUMA memory fixes

View File

@ -500,20 +500,30 @@ static const vshCmdOptDef opts_console[] = {
#ifndef __MINGW32__ #ifndef __MINGW32__
static int static int
cmdConsole(vshControl *ctl, const vshCmd *cmd) cmdRunConsole(vshControl *ctl, virDomainPtr dom)
{ {
xmlDocPtr xml = NULL; xmlDocPtr xml = NULL;
xmlXPathObjectPtr obj = NULL; xmlXPathObjectPtr obj = NULL;
xmlXPathContextPtr ctxt = NULL; xmlXPathContextPtr ctxt = NULL;
virDomainPtr dom;
int ret = FALSE; int ret = FALSE;
char *doc; char *doc;
char *thatHost = NULL;
char *thisHost = NULL;
if (!vshConnectionUsability(ctl, ctl->conn, TRUE)) if (!(thisHost = virGetHostname())) {
return FALSE; vshError(ctl, FALSE, "%s", _("Failed to get local hostname"));
goto cleanup;
}
if (!(dom = vshCommandOptDomain(ctl, cmd, NULL))) if (!(thatHost = virConnectGetHostname(ctl->conn))) {
return FALSE; vshError(ctl, FALSE, "%s", _("Failed to get connection hostname"));
goto cleanup;
}
if (STRNEQ(thisHost, thatHost)) {
vshError(ctl, FALSE, "%s", _("Cannot connect to a remote console device"));
goto cleanup;
}
doc = virDomainGetXMLDesc(dom, 0); doc = virDomainGetXMLDesc(dom, 0);
if (!doc) if (!doc)
@ -532,6 +542,8 @@ cmdConsole(vshControl *ctl, const vshCmd *cmd)
obj = xmlXPathEval(BAD_CAST "string(/domain/devices/console/@tty)", ctxt); obj = xmlXPathEval(BAD_CAST "string(/domain/devices/console/@tty)", ctxt);
if ((obj != NULL) && ((obj->type == XPATH_STRING) && if ((obj != NULL) && ((obj->type == XPATH_STRING) &&
(obj->stringval != NULL) && (obj->stringval[0] != 0))) { (obj->stringval != NULL) && (obj->stringval[0] != 0))) {
vshPrintExtra(ctl, _("Connected to domain %s\n"), virDomainGetName(dom));
vshPrintExtra(ctl, "%s", _("Escape character is ^]\n"));
if (vshRunConsole((const char *)obj->stringval) == 0) if (vshRunConsole((const char *)obj->stringval) == 0)
ret = TRUE; ret = TRUE;
} else { } else {
@ -543,14 +555,16 @@ cmdConsole(vshControl *ctl, const vshCmd *cmd)
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
if (xml) if (xml)
xmlFreeDoc(xml); xmlFreeDoc(xml);
virDomainFree(dom); free(thisHost);
free(thatHost);
return ret; return ret;
} }
#else /* __MINGW32__ */ #else /* __MINGW32__ */
static int static int
cmdConsole(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) cmdRunConsole(vshControl *ctl, virDomainPtr dom ATTRIBUTE_UNUSED)
{ {
vshError (ctl, FALSE, "%s", _("console not implemented on this platform")); vshError (ctl, FALSE, "%s", _("console not implemented on this platform"));
return FALSE; return FALSE;
@ -558,6 +572,24 @@ cmdConsole(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
#endif /* __MINGW32__ */ #endif /* __MINGW32__ */
static int
cmdConsole(vshControl *ctl, const vshCmd *cmd)
{
virDomainPtr dom;
int ret;
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE;
if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
return FALSE;
ret = cmdRunConsole(ctl, dom);
virDomainFree(dom);
return ret;
}
/* /*
* "list" command * "list" command
*/ */
@ -882,6 +914,7 @@ static const vshCmdInfo info_create[] = {
static const vshCmdOptDef opts_create[] = { static const vshCmdOptDef opts_create[] = {
{"file", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("file containing an XML domain description")}, {"file", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("file containing an XML domain description")},
{"console", VSH_OT_BOOL, 0, gettext_noop("attach to console after creation")},
{NULL, 0, 0, NULL} {NULL, 0, 0, NULL}
}; };
@ -893,6 +926,7 @@ cmdCreate(vshControl *ctl, const vshCmd *cmd)
int found; int found;
int ret = TRUE; int ret = TRUE;
char *buffer; char *buffer;
int console = vshCommandOptBool(cmd, "console");
if (!vshConnectionUsability(ctl, ctl->conn, TRUE)) if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE; return FALSE;
@ -910,6 +944,8 @@ cmdCreate(vshControl *ctl, const vshCmd *cmd)
if (dom != NULL) { if (dom != NULL) {
vshPrint(ctl, _("Domain %s created from %s\n"), vshPrint(ctl, _("Domain %s created from %s\n"),
virDomainGetName(dom), from); virDomainGetName(dom), from);
if (console)
cmdRunConsole(ctl, dom);
virDomainFree(dom); virDomainFree(dom);
} else { } else {
vshError(ctl, FALSE, _("Failed to create domain from %s"), from); vshError(ctl, FALSE, _("Failed to create domain from %s"), from);
@ -1030,6 +1066,7 @@ static const vshCmdInfo info_start[] = {
static const vshCmdOptDef opts_start[] = { static const vshCmdOptDef opts_start[] = {
{"domain", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("name of the inactive domain")}, {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("name of the inactive domain")},
{"console", VSH_OT_BOOL, 0, gettext_noop("attach to console after creation")},
{NULL, 0, 0, NULL} {NULL, 0, 0, NULL}
}; };
@ -1038,6 +1075,7 @@ cmdStart(vshControl *ctl, const vshCmd *cmd)
{ {
virDomainPtr dom; virDomainPtr dom;
int ret = TRUE; int ret = TRUE;
int console = vshCommandOptBool(cmd, "console");
if (!vshConnectionUsability(ctl, ctl->conn, TRUE)) if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE; return FALSE;
@ -1054,6 +1092,8 @@ cmdStart(vshControl *ctl, const vshCmd *cmd)
if (virDomainCreate(dom) == 0) { if (virDomainCreate(dom) == 0) {
vshPrint(ctl, _("Domain %s started\n"), vshPrint(ctl, _("Domain %s started\n"),
virDomainGetName(dom)); virDomainGetName(dom));
if (console)
cmdRunConsole(ctl, dom);
} else { } else {
vshError(ctl, FALSE, _("Failed to start domain %s"), vshError(ctl, FALSE, _("Failed to start domain %s"),
virDomainGetName(dom)); virDomainGetName(dom));