virsh: Show 'connected to console' message later

Right now, we display the message before actually attempting
to connect to the VM console. That operation, however, can
fail for a number of reasons: for example, is the VM doesn't
have a serial device, the output ends up looking like

  $ virsh console cirros
  Connected to domain 'cirros'
  Escape character is ^] (Ctrl + ])
  error: internal error: cannot find character device <null>

The initial message is misleading. Change things so that it's
only printed if we actually successfully connected to the VM
console.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
Andrea Bolognani 2024-03-28 23:36:40 +01:00
parent 1e853a64dd
commit 16d37076be
2 changed files with 13 additions and 13 deletions

View File

@ -418,12 +418,6 @@ virshRunConsole(vshControl *ctl,
sigemptyset(&sighandler.sa_mask);
/* Put STDIN into raw mode so that stuff typed does not echo to the screen
* (the TTY reads will result in it being echoed back already), and also
* ensure Ctrl-C, etc is blocked, and misc other bits */
if (vshTTYMakeRaw(ctl, true) < 0)
goto resettty;
if (!(con = virConsoleNew()))
goto resettty;
@ -447,6 +441,19 @@ virshRunConsole(vshControl *ctl,
if (virDomainOpenConsole(dom, dev_name, con->st, flags) < 0)
goto cleanup;
vshPrintExtra(ctl, _("Connected to domain '%1$s'\n"), virDomainGetName(dom));
vshPrintExtra(ctl, _("Escape character is %1$s"), priv->escapeChar);
if (priv->escapeChar[0] == '^')
vshPrintExtra(ctl, " (Ctrl + %c)", priv->escapeChar[1]);
vshPrintExtra(ctl, "\n");
fflush(stdout);
/* Put STDIN into raw mode so that stuff typed does not echo to the screen
* (the TTY reads will result in it being echoed back already), and also
* ensure Ctrl-C, etc is blocked, and misc other bits */
if (vshTTYMakeRaw(ctl, true) < 0)
goto cleanup;
virObjectRef(con);
if ((con->stdinWatch = virEventAddHandle(STDIN_FILENO,
VIR_EVENT_HANDLE_READABLE,

View File

@ -3017,7 +3017,6 @@ cmdRunConsole(vshControl *ctl, virDomainPtr dom,
unsigned int flags)
{
int state;
virshControl *priv = ctl->privData;
if ((state = virshDomainState(ctl, dom, NULL)) < 0) {
vshError(ctl, "%s", _("Unable to get domain status"));
@ -3034,12 +3033,6 @@ cmdRunConsole(vshControl *ctl, virDomainPtr dom,
return false;
}
vshPrintExtra(ctl, _("Connected to domain '%1$s'\n"), virDomainGetName(dom));
vshPrintExtra(ctl, _("Escape character is %1$s"), priv->escapeChar);
if (priv->escapeChar[0] == '^')
vshPrintExtra(ctl, " (Ctrl + %c)", priv->escapeChar[1]);
vshPrintExtra(ctl, "\n");
fflush(stdout);
if (virshRunConsole(ctl, dom, name, resume_domain, flags) == 0)
return true;