|
| 1 | +package imagebuildah |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io/fs" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "slices" |
| 10 | + |
| 11 | + "github.com/containers/buildah/define" |
| 12 | + "github.com/containers/buildah/internal/tmpdir" |
| 13 | + "github.com/containers/buildah/pkg/overlay" |
| 14 | + "github.com/containers/storage" |
| 15 | + "github.com/opencontainers/selinux/go-selinux/label" |
| 16 | + "github.com/sirupsen/logrus" |
| 17 | + "golang.org/x/sys/unix" |
| 18 | +) |
| 19 | + |
| 20 | +// platformSetupContextDirectoryOverlay() sets up an overlay _over_ the build |
| 21 | +// context directory, and sorts out labeling. Returns the location which |
| 22 | +// should be used as the default build context; the process label and mount |
| 23 | +// label for the build, if any; a boolean value that indicates whether we did, |
| 24 | +// in fact, mount an overlay; and a cleanup function which should be called |
| 25 | +// when the location is no longer needed (on success). Returned errors should |
| 26 | +// be treated as fatal. |
| 27 | +func platformSetupContextDirectoryOverlay(store storage.Store, options *define.BuildOptions) (string, string, string, bool, func(), error) { |
| 28 | + var succeeded bool |
| 29 | + var tmpDir, contentDir string |
| 30 | + cleanup := func() { |
| 31 | + if contentDir != "" { |
| 32 | + if err := overlay.CleanupContent(tmpDir); err != nil { |
| 33 | + logrus.Debugf("cleaning up overlay scaffolding for build context directory: %v", err) |
| 34 | + } |
| 35 | + } |
| 36 | + if tmpDir != "" { |
| 37 | + if err := os.Remove(tmpDir); err != nil && !errors.Is(err, fs.ErrNotExist) { |
| 38 | + logrus.Debugf("removing should-be-empty temporary directory %q: %v", tmpDir, err) |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + defer func() { |
| 43 | + if !succeeded { |
| 44 | + cleanup() |
| 45 | + } |
| 46 | + }() |
| 47 | + // double-check that the context directory location is an absolute path |
| 48 | + contextDirectoryAbsolute, err := filepath.Abs(options.ContextDirectory) |
| 49 | + if err != nil { |
| 50 | + return "", "", "", false, nil, fmt.Errorf("determining absolute path of %q: %w", options.ContextDirectory, err) |
| 51 | + } |
| 52 | + var st unix.Stat_t |
| 53 | + if err := unix.Stat(contextDirectoryAbsolute, &st); err != nil { |
| 54 | + return "", "", "", false, nil, fmt.Errorf("checking ownership of %q: %w", options.ContextDirectory, err) |
| 55 | + } |
| 56 | + // figure out the labeling situation |
| 57 | + processLabel, mountLabel, err := label.InitLabels(options.CommonBuildOpts.LabelOpts) |
| 58 | + if err != nil { |
| 59 | + return "", "", "", false, nil, err |
| 60 | + } |
| 61 | + // create a temporary directory |
| 62 | + tmpDir, err = os.MkdirTemp(tmpdir.GetTempDir(), "buildah-context-") |
| 63 | + if err != nil { |
| 64 | + return "", "", "", false, nil, fmt.Errorf("creating temporary directory: %w", err) |
| 65 | + } |
| 66 | + // create the scaffolding for an overlay mount under it |
| 67 | + contentDir, err = overlay.TempDir(tmpDir, 0, 0) |
| 68 | + if err != nil { |
| 69 | + return "", "", "", false, nil, fmt.Errorf("creating overlay scaffolding for build context directory: %w", err) |
| 70 | + } |
| 71 | + // mount an overlay that uses it as a lower |
| 72 | + overlayOptions := overlay.Options{ |
| 73 | + GraphOpts: slices.Clone(store.GraphOptions()), |
| 74 | + ForceMount: true, |
| 75 | + MountLabel: mountLabel, |
| 76 | + } |
| 77 | + targetDir := filepath.Join(contentDir, "target") |
| 78 | + contextDirMountSpec, err := overlay.MountWithOptions(contentDir, contextDirectoryAbsolute, targetDir, &overlayOptions) |
| 79 | + if err != nil { |
| 80 | + return "", "", "", false, nil, fmt.Errorf("creating overlay scaffolding for build context directory: %w", err) |
| 81 | + } |
| 82 | + // going forward, pretend that the merged directory is the actual context directory |
| 83 | + logrus.Debugf("mounted an overlay at %q over %q", contextDirMountSpec.Source, contextDirectoryAbsolute) |
| 84 | + succeeded = true |
| 85 | + return contextDirMountSpec.Source, processLabel, mountLabel, true, cleanup, nil |
| 86 | +} |
0 commit comments