From 2a4b851f399e118b7df93de03e1220faf40a27f8 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Thu, 18 Apr 2024 15:15:45 +0200 Subject: [PATCH] composefs: return mkcomposefs stderr as part of error Writing the error to stderr is not helpful when using the podman API. The error would only be visable on the server which is bad and likely is not noticed at all. Also for the regular podman cli it would be hard for a user to know if the stderr from mkcompose is related to the returned podman error. To fix this capture the stderr and append it to the error as string, this should be fine assuming mkcomposefs doesn't print a ton on stderr which I assume is not the case. Signed-off-by: Paul Holzinger --- drivers/overlay/composefs.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/overlay/composefs.go b/drivers/overlay/composefs.go index baa9d7befe..8f07c23602 100644 --- a/drivers/overlay/composefs.go +++ b/drivers/overlay/composefs.go @@ -4,12 +4,14 @@ package overlay import ( + "bytes" "encoding/binary" "errors" "fmt" "os" "os/exec" "path/filepath" + "strings" "sync" "github.com/containers/storage/pkg/chunked/dump" @@ -70,12 +72,18 @@ func generateComposeFsBlob(verityDigests map[string]string, toc interface{}, com // a scope to close outFd before setting fsverity on the read-only fd. defer outFd.Close() + errBuf := &bytes.Buffer{} cmd := exec.Command(writerJson, "--from-file", "-", "/proc/self/fd/3") cmd.ExtraFiles = []*os.File{outFd} - cmd.Stderr = os.Stderr + cmd.Stderr = errBuf cmd.Stdin = dumpReader if err := cmd.Run(); err != nil { - return fmt.Errorf("failed to convert json to erofs: %w", err) + rErr := fmt.Errorf("failed to convert json to erofs: %w", err) + exitErr := &exec.ExitError{} + if errors.As(err, &exitErr) { + return fmt.Errorf("%w: %s", rErr, strings.TrimSpace(errBuf.String())) + } + return rErr } return nil }()