[PATCH 6/8] ch_process: Handle enabled console devices

William Douglas posted 8 patches 4 years, 5 months ago
There is a newer version of this series
[PATCH 6/8] ch_process: Handle enabled console devices
Posted by William Douglas 4 years, 5 months ago
Add functionality to allow libvirt console to connect to the
cloud-hypervisor created PTY associated with a VM by updating the
domain with console path information. This has to be run after the VM
is created by cloud-hypervisor.

Signed-off-by: William Douglas <william.douglas@intel.com>
---
 src/ch/ch_process.c | 67 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)

diff --git a/src/ch/ch_process.c b/src/ch/ch_process.c
index 93b1f7f97e..f6ae1677fd 100644
--- a/src/ch/ch_process.c
+++ b/src/ch/ch_process.c
@@ -28,6 +28,7 @@
 #include "ch_process.h"
 #include "viralloc.h"
 #include "virerror.h"
+#include "virjson.h"
 #include "virlog.h"
 
 #define VIR_FROM_THIS VIR_FROM_CH
@@ -52,6 +53,69 @@ virCHProcessConnectMonitor(virCHDriver *driver,
     return monitor;
 }
 
+static void
+virCHProcessUpdateConsoleDevice(virDomainObj *vm,
+                                virJSONValue *config,
+                                const char *device)
+{
+    const char *path;
+    virDomainChrDef *chr = NULL;
+    virJSONValue *dev, *file;
+
+    if (!config)
+        return;
+
+    dev = virJSONValueObjectGet(config, device);
+    if (!dev)
+        return;
+
+    file = virJSONValueObjectGet(dev, "file");
+    if (!file)
+        return;
+
+    path = virJSONValueGetString(file);
+    if (!path)
+        return;
+
+    if (STREQ(device, "console")) {
+        chr = vm->def->consoles[0];
+    } else if (STREQ(device, "serial")) {
+        chr = vm->def->serials[0];
+    }
+
+    if (chr && chr->source)
+        chr->source->data.file.path = g_strdup(path);
+}
+
+static void
+virCHProcessUpdateConsole(virDomainObj *vm,
+                          virJSONValue *info)
+{
+    virJSONValue *config;
+
+    config = virJSONValueObjectGet(info, "config");
+    if (!config)
+        return;
+
+    virCHProcessUpdateConsoleDevice(vm, config, "console");
+    virCHProcessUpdateConsoleDevice(vm, config, "serial");
+}
+
+static int
+virCHProcessUpdateInfo(virDomainObj *vm)
+{
+    virJSONValue *info;
+    virCHDomainObjPrivate *priv = vm->privateData;
+    if (virCHMonitorGetInfo(priv->monitor, &info) < 0)
+        return -1;
+
+    virCHProcessUpdateConsole(vm, info);
+
+    virJSONValueFree(info);
+
+    return 0;
+}
+
 /**
  * virCHProcessStart:
  * @driver: pointer to driver structure
@@ -92,6 +156,9 @@ int virCHProcessStart(virCHDriver *driver,
 
     vm->pid = priv->monitor->pid;
     vm->def->id = vm->pid;
+
+    virCHProcessUpdateInfo(vm);
+
     virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, reason);
 
     return 0;
-- 
2.31.1

Re: [PATCH 6/8] ch_process: Handle enabled console devices
Posted by Daniel P. Berrangé 4 years, 5 months ago
On Thu, Aug 26, 2021 at 02:59:20PM -0700, William Douglas wrote:
> Add functionality to allow libvirt console to connect to the
> cloud-hypervisor created PTY associated with a VM by updating the
> domain with console path information. This has to be run after the VM
> is created by cloud-hypervisor.
> 
> Signed-off-by: William Douglas <william.douglas@intel.com>
> ---
>  src/ch/ch_process.c | 67 +++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 67 insertions(+)
> 
> diff --git a/src/ch/ch_process.c b/src/ch/ch_process.c
> index 93b1f7f97e..f6ae1677fd 100644
> --- a/src/ch/ch_process.c
> +++ b/src/ch/ch_process.c
> @@ -28,6 +28,7 @@
>  #include "ch_process.h"
>  #include "viralloc.h"
>  #include "virerror.h"
> +#include "virjson.h"
>  #include "virlog.h"
>  
>  #define VIR_FROM_THIS VIR_FROM_CH
> @@ -52,6 +53,69 @@ virCHProcessConnectMonitor(virCHDriver *driver,
>      return monitor;
>  }
>  
> +static void
> +virCHProcessUpdateConsoleDevice(virDomainObj *vm,
> +                                virJSONValue *config,
> +                                const char *device)
> +{
> +    const char *path;
> +    virDomainChrDef *chr = NULL;
> +    virJSONValue *dev, *file;
> +
> +    if (!config)
> +        return;
> +
> +    dev = virJSONValueObjectGet(config, device);
> +    if (!dev)
> +        return;
> +
> +    file = virJSONValueObjectGet(dev, "file");
> +    if (!file)
> +        return;
> +
> +    path = virJSONValueGetString(file);
> +    if (!path)
> +        return;
> +
> +    if (STREQ(device, "console")) {
> +        chr = vm->def->consoles[0];
> +    } else if (STREQ(device, "serial")) {
> +        chr = vm->def->serials[0];
> +    }
> +
> +    if (chr && chr->source)
> +        chr->source->data.file.path = g_strdup(path);
> +}

All the error paths here are silent. I wonder if we shouldn't be
reporting proper errors if the data in the JSON reply doesn't
match the config we passed into cloudhypervisor when creating
the VM

> +
> +static void
> +virCHProcessUpdateConsole(virDomainObj *vm,
> +                          virJSONValue *info)
> +{
> +    virJSONValue *config;
> +
> +    config = virJSONValueObjectGet(info, "config");
> +    if (!config)
> +        return;
> +
> +    virCHProcessUpdateConsoleDevice(vm, config, "console");
> +    virCHProcessUpdateConsoleDevice(vm, config, "serial");
> +}
> +
> +static int
> +virCHProcessUpdateInfo(virDomainObj *vm)
> +{
> +    virJSONValue *info;
> +    virCHDomainObjPrivate *priv = vm->privateData;
> +    if (virCHMonitorGetInfo(priv->monitor, &info) < 0)
> +        return -1;
> +
> +    virCHProcessUpdateConsole(vm, info);
> +
> +    virJSONValueFree(info);
> +
> +    return 0;
> +}
> +
>  /**
>   * virCHProcessStart:
>   * @driver: pointer to driver structure
> @@ -92,6 +156,9 @@ int virCHProcessStart(virCHDriver *driver,
>  
>      vm->pid = priv->monitor->pid;
>      vm->def->id = vm->pid;
> +
> +    virCHProcessUpdateInfo(vm);
> +
>      virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, reason);
>  
>      return 0;
> -- 
> 2.31.1
> 

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|