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 <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger 2024-04-18 15:15:45 +02:00
parent 0b293c3aaa
commit 2a4b851f39
No known key found for this signature in database
GPG Key ID: EB145DD938A3CAF2
1 changed files with 10 additions and 2 deletions

View File

@ -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
}()