From 280f7626d586f2c5184577c6feaaf5a3a9d3dce9 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Wed, 20 Mar 2024 13:07:29 +0100 Subject: [PATCH] bib: check /var/lib/containers/storage when using "--local" Ensure to error early when the user is not passing the required ``` -v /var/lib/containers/storage:/var/lib/containers/storage ``` when doing a local build. --- bib/internal/setup/setup.go | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/bib/internal/setup/setup.go b/bib/internal/setup/setup.go index 74f60fc..404f424 100644 --- a/bib/internal/setup/setup.go +++ b/bib/internal/setup/setup.go @@ -3,8 +3,10 @@ package setup import ( "fmt" "os" + "os/exec" "path/filepath" + "github.com/moby/sys/mountinfo" "golang.org/x/sys/unix" "github.com/osbuild/bootc-image-builder/bib/internal/podmanutil" @@ -97,3 +99,39 @@ func Validate() error { return nil } + +// ValidateHasContainerStorageMounted checks that the container storage +// is mounted inside the container +func ValidateHasContainerStorageMounted() error { + if err := exec.Command("systemd-detect-virt", "-c").Run(); err != nil { + if exitErr, ok := err.(*exec.ExitError); ok { + // not running in a container, just exit + if exitErr.ExitCode() == 1 { + return nil + } + } + return err + } + + containersStorage := "/var/lib/containers/storage" + containerStorageMountFound := false + + mnts, err := mountinfo.GetMounts(nil) + if err != nil { + return err + } + for _, mnt := range mnts { + if mnt.Mountpoint != containersStorage { + continue + } + containerStorageMountFound = true + if mnt.Root != containersStorage { + return fmt.Errorf("not the host /var/lib/storage/containers but %q", mnt.Root) + } + } + if !containerStorageMountFound { + return fmt.Errorf("cannot find mount for %q", containersStorage) + } + + return nil +}