[libvirt PATCH v5 15/32] qemu: log error output from nbdkit

Jonathon Jongsma posted 32 patches 2 years, 12 months ago
There is a newer version of this series
[libvirt PATCH v5 15/32] qemu: log error output from nbdkit
Posted by Jonathon Jongsma 2 years, 12 months ago
log stderr and stdout from nbdkit into its own log so that
nbdkit-related issues can be debugged more easily.

Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
---
 src/qemu/qemu_nbdkit.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/src/qemu/qemu_nbdkit.c b/src/qemu/qemu_nbdkit.c
index 5abccf1536..f2ff9552a3 100644
--- a/src/qemu/qemu_nbdkit.c
+++ b/src/qemu/qemu_nbdkit.c
@@ -850,15 +850,28 @@ qemuNbdkitProcessStart(qemuNbdkitProcess *proc,
     int rc;
     int exitstatus = 0;
     int cmdret = 0;
-    g_autofree char *errbuf = NULL;
     virTimeBackOffVar timebackoff;
     bool socketCreated = false;
+    g_autofree char *basename = g_strdup_printf("%s-nbdkit-%i", vm->def->name, proc->source->id);
+    int logfd = -1;
+    g_autoptr(qemuLogContext) logContext = NULL;
+    g_autofree char *errmsg = NULL;
+    g_autoptr(virURI) uri = NULL;
+    g_autofree char *uristring = NULL;
 
     if (!(cmd = qemuNbdkitProcessBuildCommand(proc)))
         return -1;
 
+    if (!(logContext = qemuLogContextNew(driver, vm, basename))) {
+        virLastErrorPrefixMessage("%s", _("can't connect to virtlogd"));
+        return -1;
+    }
+
+    logfd = qemuLogContextGetWriteFD(logContext);
+
     VIR_DEBUG("starting nbdkit process for %s", proc->source->nodestorage);
-    virCommandSetErrorBuffer(cmd, &errbuf);
+    virCommandSetErrorFD(cmd, &logfd);
+    virCommandSetOutputFD(cmd, &logfd);
     virCommandSetPidFile(cmd, proc->pidfile);
 
     if (qemuExtDeviceLogCommand(driver, vm, cmd, "nbdkit") < 0)
@@ -902,9 +915,16 @@ qemuNbdkitProcessStart(qemuNbdkitProcess *proc,
     return 0;
 
  error:
-    if (errbuf)
-        virReportError(VIR_ERR_OPERATION_FAILED,
-                       _("nbdkit failed to start and reported: %s"), errbuf);
+    if (qemuLogContextReadFiltered(logContext, &errmsg, 1024) < 0)
+        VIR_WARN("Unable to read from nbdkit log");
+
+    if ((uri = qemuBlockStorageSourceGetURI(proc->source)))
+        uristring = virURIFormat(uri);
+
+    virReportError(VIR_ERR_INTERNAL_ERROR,
+                   _("Failed to connect to nbdkit for '%s': %s"),
+                   NULLSTR(uristring), NULLSTR(errmsg));
+
     qemuNbdkitProcessStop(proc);
     return -1;
 }
-- 
2.39.1
Re: [libvirt PATCH v5 15/32] qemu: log error output from nbdkit
Posted by Peter Krempa 2 years, 11 months ago
On Tue, Feb 14, 2023 at 11:08:02 -0600, Jonathon Jongsma wrote:
> log stderr and stdout from nbdkit into its own log so that
> nbdkit-related issues can be debugged more easily.
> 
> Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
> ---
>  src/qemu/qemu_nbdkit.c | 30 +++++++++++++++++++++++++-----
>  1 file changed, 25 insertions(+), 5 deletions(-)
> 
> diff --git a/src/qemu/qemu_nbdkit.c b/src/qemu/qemu_nbdkit.c
> index 5abccf1536..f2ff9552a3 100644
> --- a/src/qemu/qemu_nbdkit.c
> +++ b/src/qemu/qemu_nbdkit.c

[...]

> @@ -902,9 +915,16 @@ qemuNbdkitProcessStart(qemuNbdkitProcess *proc,
>      return 0;
>  
>   error:
> -    if (errbuf)
> -        virReportError(VIR_ERR_OPERATION_FAILED,
> -                       _("nbdkit failed to start and reported: %s"), errbuf);
> +    if (qemuLogContextReadFiltered(logContext, &errmsg, 1024) < 0)
> +        VIR_WARN("Unable to read from nbdkit log");
> +
> +    if ((uri = qemuBlockStorageSourceGetURI(proc->source)))
> +        uristring = virURIFormat(uri);
> +
> +    virReportError(VIR_ERR_INTERNAL_ERROR,

VIR_ERR_INTERNAL_ERROR is not appropriate for message which in most
cases involves user missconfig e.g:

  error: internal error: Failed to connect to nbdkit for 'http://storage.example.com:10809/ppc64dev': nbdkit: curl[1]: error: problem doing HEAD request to fetch size of URL [http://storage.example.com:10809/ppc64dev]: Couldn't resolve host name: Could not resolve host: storage.example.com

IMO 'VIR_ERR_OPERATION_FAILED' will be better. It says "operation
failed"


> +                   _("Failed to connect to nbdkit for '%s': %s"),
> +                   NULLSTR(uristring), NULLSTR(errmsg));
> +

Reviewed-by: Peter Krempa <pkrempa@redhat.com>