[PATCH libvirt v1 3/3] Improve `virsh create --console` behavior

Marc Hartmayer posted 3 patches 2 years, 4 months ago
[PATCH libvirt v1 3/3] Improve `virsh create --console` behavior
Posted by Marc Hartmayer 2 years, 4 months ago
When starting a guest via libvirt (`virsh create --console`), early
console output was missed because the guest was started first and then
the console was attached. This patch changes this to the following
sequence:

1. create a paused transient guest
2. attach the console
3. resume the guest

Reviewed-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
---
 tools/virsh-domain.c | 34 ++++++++++++++++++++++++++++------
 1 file changed, 28 insertions(+), 6 deletions(-)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 36670039444c..2f055df0d97d 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -8212,6 +8212,13 @@ static const vshCmdOptDef opts_create[] = {
     {.name = NULL}
 };
 
+
+static virshDomain *virDomainCreateXMLHelper(virConnectPtr conn, const char *xmlDesc, unsigned int nfds, int *fds, unsigned int flags) {
+  if (nfds)
+      return virDomainCreateXMLWithFiles(conn, xmlDesc, nfds, fds, flags);
+  return virDomainCreateXML(conn, xmlDesc, flags);
+}
+
 static bool
 cmdCreate(vshControl *ctl, const vshCmd *cmd)
 {
@@ -8220,6 +8227,7 @@ cmdCreate(vshControl *ctl, const vshCmd *cmd)
     g_autofree char *buffer = NULL;
 #ifndef WIN32
     bool console = vshCommandOptBool(cmd, "console");
+    bool resume_domain = false;
 #endif
     unsigned int flags = 0;
     size_t nfds = 0;
@@ -8235,8 +8243,14 @@ cmdCreate(vshControl *ctl, const vshCmd *cmd)
     if (virshFetchPassFdsList(ctl, cmd, &nfds, &fds) < 0)
         return false;
 
-    if (vshCommandOptBool(cmd, "paused"))
+    if (vshCommandOptBool(cmd, "paused")) {
         flags |= VIR_DOMAIN_START_PAUSED;
+#ifndef WIN32
+    } else if (console) {
+        flags |= VIR_DOMAIN_START_PAUSED;
+        resume_domain = true;
+#endif
+    }
     if (vshCommandOptBool(cmd, "autodestroy"))
         flags |= VIR_DOMAIN_START_AUTODESTROY;
     if (vshCommandOptBool(cmd, "validate"))
@@ -8244,10 +8258,18 @@ cmdCreate(vshControl *ctl, const vshCmd *cmd)
     if (vshCommandOptBool(cmd, "reset-nvram"))
         flags |= VIR_DOMAIN_START_RESET_NVRAM;
 
-    if (nfds)
-        dom = virDomainCreateXMLWithFiles(priv->conn, buffer, nfds, fds, flags);
-    else
-        dom = virDomainCreateXML(priv->conn, buffer, flags);
+    dom = virDomainCreateXMLHelper(priv->conn, buffer, nfds, fds, flags);
+#ifndef WIN32
+    /* If the driver does not support the paused flag, let's fallback to the old
+     * behavior without the flag. */
+    if (!dom && resume_domain && last_error && last_error->code == VIR_ERR_INVALID_ARG) {
+      vshResetLibvirtError();
+
+      flags &= ~VIR_DOMAIN_START_PAUSED;
+      resume_domain = false;
+      dom = virDomainCreateXMLHelper(priv->conn, buffer, nfds, fds, flags);
+    }
+#endif
 
     if (!dom) {
         vshError(ctl, _("Failed to create domain from %1$s"), from);
@@ -8258,7 +8280,7 @@ cmdCreate(vshControl *ctl, const vshCmd *cmd)
                   virDomainGetName(dom), from);
 #ifndef WIN32
     if (console)
-        cmdRunConsole(ctl, dom, NULL, false, 0);
+        cmdRunConsole(ctl, dom, NULL, resume_domain, 0);
 #endif
     return true;
 }
-- 
2.34.1
Re: [PATCH libvirt v1 3/3] Improve `virsh create --console` behavior
Posted by Michal Prívozník 2 years, 3 months ago
On 9/28/23 17:37, Marc Hartmayer wrote:
> When starting a guest via libvirt (`virsh create --console`), early
> console output was missed because the guest was started first and then
> the console was attached. This patch changes this to the following
> sequence:
> 
> 1. create a paused transient guest
> 2. attach the console
> 3. resume the guest
> 
> Reviewed-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
> Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
> ---
>  tools/virsh-domain.c | 34 ++++++++++++++++++++++++++++------
>  1 file changed, 28 insertions(+), 6 deletions(-)
> 
> diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
> index 36670039444c..2f055df0d97d 100644
> --- a/tools/virsh-domain.c
> +++ b/tools/virsh-domain.c
> @@ -8212,6 +8212,13 @@ static const vshCmdOptDef opts_create[] = {
>      {.name = NULL}
>  };
>  
> +
> +static virshDomain *virDomainCreateXMLHelper(virConnectPtr conn, const char *xmlDesc, unsigned int nfds, int *fds, unsigned int flags) {
> +  if (nfds)
> +      return virDomainCreateXMLWithFiles(conn, xmlDesc, nfds, fds, flags);
> +  return virDomainCreateXML(conn, xmlDesc, flags);
> +}
> +

s/virDomainCreate/virshDomainCreate/

Michal
Re: [PATCH libvirt v1 3/3] Improve `virsh create --console` behavior
Posted by Thomas Huth 2 years, 3 months ago
On 28/09/2023 17.37, Marc Hartmayer wrote:
> When starting a guest via libvirt (`virsh create --console`), early
> console output was missed because the guest was started first and then
> the console was attached. This patch changes this to the following
> sequence:
> 
> 1. create a paused transient guest
> 2. attach the console
> 3. resume the guest
> 
> Reviewed-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
> Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
> ---
>   tools/virsh-domain.c | 34 ++++++++++++++++++++++++++++------
>   1 file changed, 28 insertions(+), 6 deletions(-)

Reviewed-by: Thomas Huth <thuth@redhat.com>