virsh: add console --resume support

This patch adds the command line flag `--resume` to the `virsh console`
command. This resumes a paused guest after connecting to the console.
This might be handy since it's a "common" pattern to start a guest
paused, connect to the console, and then resume it so as not to miss any
console messages.

Reviewed-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Marc Hartmayer 2023-09-28 17:37:08 +02:00 committed by Michal Privoznik
parent 1622012cc4
commit 49e3e7f9e8
4 changed files with 24 additions and 5 deletions

View File

@ -1442,7 +1442,7 @@ console
:: ::
console domain [devname] [--safe] [--force] console domain [devname] [--safe] [--force] [--resume]
Connect the virtual serial console for the guest. The optional Connect the virtual serial console for the guest. The optional
*devname* parameter refers to the device alias of an alternate *devname* parameter refers to the device alias of an alternate
@ -1455,6 +1455,9 @@ the server has to ensure exclusive access to console devices. Optionally
the *--force* flag may be specified, requesting to disconnect any existing the *--force* flag may be specified, requesting to disconnect any existing
sessions, such as in a case of a broken connection. sessions, such as in a case of a broken connection.
If the flag *--resume* is specified then the guest is resumed after connecting
to the console.
cpu-stats cpu-stats
--------- ---------

View File

@ -401,6 +401,7 @@ int
virshRunConsole(vshControl *ctl, virshRunConsole(vshControl *ctl,
virDomainPtr dom, virDomainPtr dom,
const char *dev_name, const char *dev_name,
const bool resume_domain,
unsigned int flags) unsigned int flags)
{ {
virConsole *con = NULL; virConsole *con = NULL;
@ -476,6 +477,14 @@ virshRunConsole(vshControl *ctl,
goto cleanup; goto cleanup;
} }
if (resume_domain) {
if (virDomainResume(dom) != 0) {
vshError(ctl, _("Failed to resume domain '%1$s'"),
virDomainGetName(dom));
goto cleanup;
}
}
while (!con->quit) { while (!con->quit) {
if (virCondWait(&con->cond, &con->parent.lock) < 0) { if (virCondWait(&con->cond, &con->parent.lock) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",

View File

@ -27,6 +27,7 @@
int virshRunConsole(vshControl *ctl, int virshRunConsole(vshControl *ctl,
virDomainPtr dom, virDomainPtr dom,
const char *dev_name, const char *dev_name,
const bool resume_domain,
unsigned int flags); unsigned int flags);
#endif /* !WIN32 */ #endif /* !WIN32 */

View File

@ -3012,6 +3012,10 @@ static const vshCmdOptDef opts_console[] = {
.type = VSH_OT_BOOL, .type = VSH_OT_BOOL,
.help = N_("force console connection (disconnect already connected sessions)") .help = N_("force console connection (disconnect already connected sessions)")
}, },
{.name = "resume",
.type = VSH_OT_BOOL,
.help = N_("resume a paused guest after connecting to console")
},
{.name = "safe", {.name = "safe",
.type = VSH_OT_BOOL, .type = VSH_OT_BOOL,
.help = N_("only connect if safe console handling is supported") .help = N_("only connect if safe console handling is supported")
@ -3022,6 +3026,7 @@ static const vshCmdOptDef opts_console[] = {
static bool static bool
cmdRunConsole(vshControl *ctl, virDomainPtr dom, cmdRunConsole(vshControl *ctl, virDomainPtr dom,
const char *name, const char *name,
const bool resume_domain,
unsigned int flags) unsigned int flags)
{ {
int state; int state;
@ -3048,7 +3053,7 @@ cmdRunConsole(vshControl *ctl, virDomainPtr dom,
vshPrintExtra(ctl, " (Ctrl + %c)", priv->escapeChar[1]); vshPrintExtra(ctl, " (Ctrl + %c)", priv->escapeChar[1]);
vshPrintExtra(ctl, "\n"); vshPrintExtra(ctl, "\n");
fflush(stdout); fflush(stdout);
if (virshRunConsole(ctl, dom, name, flags) == 0) if (virshRunConsole(ctl, dom, name, resume_domain, flags) == 0)
return true; return true;
return false; return false;
@ -3059,6 +3064,7 @@ cmdConsole(vshControl *ctl, const vshCmd *cmd)
{ {
g_autoptr(virshDomain) dom = NULL; g_autoptr(virshDomain) dom = NULL;
bool force = vshCommandOptBool(cmd, "force"); bool force = vshCommandOptBool(cmd, "force");
bool resume = vshCommandOptBool(cmd, "resume");
bool safe = vshCommandOptBool(cmd, "safe"); bool safe = vshCommandOptBool(cmd, "safe");
unsigned int flags = 0; unsigned int flags = 0;
const char *name = NULL; const char *name = NULL;
@ -3074,7 +3080,7 @@ cmdConsole(vshControl *ctl, const vshCmd *cmd)
if (safe) if (safe)
flags |= VIR_DOMAIN_CONSOLE_SAFE; flags |= VIR_DOMAIN_CONSOLE_SAFE;
return cmdRunConsole(ctl, dom, name, flags); return cmdRunConsole(ctl, dom, name, resume, flags);
} }
#endif /* WIN32 */ #endif /* WIN32 */
@ -4136,7 +4142,7 @@ cmdStart(vshControl *ctl, const vshCmd *cmd)
vshPrintExtra(ctl, _("Domain '%1$s' started\n"), vshPrintExtra(ctl, _("Domain '%1$s' started\n"),
virDomainGetName(dom)); virDomainGetName(dom));
#ifndef WIN32 #ifndef WIN32
if (console && !cmdRunConsole(ctl, dom, NULL, 0)) if (console && !cmdRunConsole(ctl, dom, NULL, false, 0))
return false; return false;
#endif #endif
@ -8232,7 +8238,7 @@ cmdCreate(vshControl *ctl, const vshCmd *cmd)
virDomainGetName(dom), from); virDomainGetName(dom), from);
#ifndef WIN32 #ifndef WIN32
if (console) if (console)
cmdRunConsole(ctl, dom, NULL, 0); cmdRunConsole(ctl, dom, NULL, false, 0);
#endif #endif
return true; return true;
} }