From 8fbf3299b22dec36275b0ce28617ef4e1a3e3cff Mon Sep 17 00:00:00 2001 From: Ryan Harper Date: Tue, 11 Jun 2024 16:50:59 -0500 Subject: [PATCH] fix: check bind source file/dir exists (#624) Stacker does not check if a bind mount source exists and if the source is a directory then the container crashes with a stack trace that does not indicate that the missing source dir is the issue. Check if the source exists and return an error indicating the source is missing instead of the container stack trace. Signed-off-by: Ryan Harper --- pkg/container/container.go | 3 +++ pkg/stacker/build.go | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/container/container.go b/pkg/container/container.go index cd6ade01..a0789183 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -65,6 +65,9 @@ func New(sc types.StackerConfig, name string) (*Container, error) { func (c *Container) BindMount(source string, dest string, extraOpts string) error { createOpt := "create=dir" stat, err := os.Stat(source) + if os.IsNotExist(err) { + return errors.Errorf("bind mount source %q does not exist, refusing bind mount: %s", source, err) + } if err == nil && !stat.IsDir() { createOpt = "create=file" } diff --git a/pkg/stacker/build.go b/pkg/stacker/build.go index c1d30f76..8771e75d 100644 --- a/pkg/stacker/build.go +++ b/pkg/stacker/build.go @@ -820,9 +820,10 @@ func SetupLayerConfig(config types.StackerConfig, c *container.Container, l type } for _, bind := range l.Binds { + log.Debugf("bind mounting %q into container at %q", bind.Source, bind.Dest) err = c.BindMount(bind.Source, bind.Dest, "") if err != nil { - return err + return errors.Errorf("failed to bind mount %q at %q: %s", bind.Source, bind.Dest, err) } }