[PATCH 0/3] lxc: Fix reporting of startup errors with debug logging enabled

Few issues with propagating error to the user when debug logging is enabled, Peter Krempa (3): virLXCControllerSetupUsernsMap: Modify debug logging for clean startup errors virLXCProcessReadLogOutputData: Refill buffer after filtering out noise virLXCProcessReportStartupLogError: Strip trailing newline from error src/lxc/lxc_controller.c | 8 +++++--- src/lxc/lxc_process.c | 12 ++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) -- 2.41.0

Avoid logging multiline debug logs so that the function which attempts to extract a non-debug log error message can work properly. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- src/lxc/lxc_controller.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index 86dcd880e8..ba7f15ad24 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -1359,11 +1359,13 @@ virLXCControllerSetupUsernsMap(virDomainIdMapEntry *map, return -1; } - for (i = 0; i < num; i++) + VIR_DEBUG("Set '%s' mappings to:", path); + + for (i = 0; i < num; i++) { + VIR_DEBUG("%u %u %u", map[i].start, map[i].target, map[i].count); virBufferAsprintf(&map_value, "%u %u %u\n", map[i].start, map[i].target, map[i].count); - - VIR_DEBUG("Set '%s' to '%s'", path, virBufferCurrentContent(&map_value)); + } if (virFileWriteStr(path, virBufferCurrentContent(&map_value), 0) < 0) { virReportSystemError(errno, _("unable write to %1$s"), path); -- 2.41.0

The caller passes in a 1k buffer, which when debug logging is in use is easily filled with debug messages only. Thus after the first pass which is common if the controller process already terminated the buffer will not contain the real error, but rather a truncated debug message, which will result in an error such as: error: internal error: guest failed to start: 2023-08-01 12:58:31.948+0000: 798195: i instead of the proper error: error: internal error: guest failed to start: Failure in libvirt_lxc startup: Failed to create /home/rootfs/.oldroot: Permission denied To fix the above retry the reading loop if the filtering function made space in the buffer. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- src/lxc/lxc_process.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index d003742fa1..6b79bd737b 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -1011,6 +1011,7 @@ virLXCProcessReadLogOutputData(virDomainObj *vm, int retries = 10; int got = 0; char *filter_next = buf; + bool filtered; buf[0] = '\0'; @@ -1036,11 +1037,13 @@ virLXCProcessReadLogOutputData(virDomainObj *vm, buf[got] = '\0'; /* Filter out debug messages from intermediate libvirt process */ + filtered = false; while ((eol = strchr(filter_next, '\n'))) { *eol = '\0'; if (virLXCProcessIgnorableLogLine(filter_next)) { memmove(filter_next, eol + 1, got - (eol - buf)); got -= eol + 1 - filter_next; + filtered = true; } else { filter_next = eol + 1; *eol = '\n'; @@ -1054,6 +1057,9 @@ virLXCProcessReadLogOutputData(virDomainObj *vm, return -1; } + if (filtered) + continue; + if (isdead) return got; -- 2.41.0

Since the error message originates from a log file it contains a trailing newline. Strip it as all error handling adds it's own newline. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- src/lxc/lxc_process.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index 6b79bd737b..04642b56dd 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -1124,6 +1124,7 @@ virLXCProcessReportStartupLogError(virDomainObj *vm, { size_t buflen = 1024; g_autofree char *errbuf = g_new0(char, buflen); + char *p; int rc; if ((rc = virLXCProcessReadLogOutput(vm, logfile, pos, errbuf, buflen)) < 0) @@ -1132,6 +1133,11 @@ virLXCProcessReportStartupLogError(virDomainObj *vm, if (rc == 0) return 0; + /* strip last newline */ + if ((p = strrchr(errbuf, '\n')) && + p[1] == '\0') + *p = '\0'; + virReportError(VIR_ERR_INTERNAL_ERROR, _("guest failed to start: %1$s"), errbuf); -- 2.41.0

On Wed, Aug 02, 2023 at 09:29:39AM +0200, Peter Krempa wrote:
Few issues with propagating error to the user when debug logging is enabled,
Peter Krempa (3): virLXCControllerSetupUsernsMap: Modify debug logging for clean startup errors virLXCProcessReadLogOutputData: Refill buffer after filtering out noise virLXCProcessReportStartupLogError: Strip trailing newline from error
src/lxc/lxc_controller.c | 8 +++++--- src/lxc/lxc_process.c | 12 ++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-)
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
-- 2.41.0
participants (2)
-
Martin Kletzander
-
Peter Krempa