1
0
mirror of https://github.com/containers/buildah.git synced 2025-04-18 07:04:05 +03:00

Run(): always clean up options.ExternalImageMounts

Make sure that we consistently unmount the list of images that we're
told to, even in cases where we return an error before arranging to do
so in Run().

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
This commit is contained in:
Nalin Dahyabhai 2025-01-08 11:04:42 -05:00
parent b48b0fce4c
commit 9e00b6b399
3 changed files with 41 additions and 5 deletions

6
run.go
View File

@ -159,9 +159,9 @@ type RunOptions struct {
RunMounts []string
// Map of stages and container mountpoint if any from stage executor
StageMountPoints map[string]internal.StageMountDetails
// External Image mounts to be cleaned up.
// Buildah run --mount could mount image before RUN calls, RUN could cleanup
// them up as well
// IDs of mounted images to be unmounted before returning
// Deprecated: before 1.39, these images would not be consistently
// unmounted if Run() returned an error
ExternalImageMounts []string
// System context of current build
SystemContext *types.SystemContext

View File

@ -72,6 +72,24 @@ func setChildProcess() error {
}
func (b *Builder) Run(command []string, options RunOptions) error {
var runArtifacts *runMountArtifacts
if len(options.ExternalImageMounts) > 0 {
defer func() {
if runArtifacts == nil {
// we didn't add ExternalImageMounts to the
// list of images that we're going to unmount
// yet and make a deferred call that cleans
// them up, but the caller is expecting us to
// unmount these for them because we offered to
for _, image := range options.ExternalImageMounts {
if _, err := b.store.UnmountImage(image, false); err != nil {
logrus.Debugf("umounting image %q: %v", image, err)
}
}
}
}()
}
p, err := os.MkdirTemp(tmpdir.GetTempDir(), define.Package)
if err != nil {
return err
@ -262,7 +280,7 @@ func (b *Builder) Run(command []string, options RunOptions) error {
SystemContext: options.SystemContext,
}
runArtifacts, err := b.setupMounts(mountPoint, spec, path, options.Mounts, bindFiles, volumes, options.CompatBuiltinVolumes, b.CommonBuildOpts.Volumes, options.RunMounts, runMountInfo)
runArtifacts, err = b.setupMounts(mountPoint, spec, path, options.Mounts, bindFiles, volumes, options.CompatBuiltinVolumes, b.CommonBuildOpts.Volumes, options.RunMounts, runMountInfo)
if err != nil {
return fmt.Errorf("resolving mountpoints for container %q: %w", b.ContainerID, err)
}

View File

@ -166,6 +166,24 @@ func separateDevicesFromRuntimeSpec(g *generate.Generator) define.ContainerDevic
// Run runs the specified command in the container's root filesystem.
func (b *Builder) Run(command []string, options RunOptions) error {
var runArtifacts *runMountArtifacts
if len(options.ExternalImageMounts) > 0 {
defer func() {
if runArtifacts == nil {
// we didn't add ExternalImageMounts to the
// list of images that we're going to unmount
// yet and make a deferred call that cleans
// them up, but the caller is expecting us to
// unmount these for them because we offered to
for _, image := range options.ExternalImageMounts {
if _, err := b.store.UnmountImage(image, false); err != nil {
logrus.Debugf("umounting image %q: %v", image, err)
}
}
}
}()
}
if os.Getenv("container") != "" {
os, arch, variant, err := parse.Platform("")
if err != nil {
@ -499,7 +517,7 @@ rootless=%d
SystemContext: options.SystemContext,
}
runArtifacts, err := b.setupMounts(mountPoint, spec, path, options.Mounts, bindFiles, volumes, options.CompatBuiltinVolumes, b.CommonBuildOpts.Volumes, options.RunMounts, runMountInfo)
runArtifacts, err = b.setupMounts(mountPoint, spec, path, options.Mounts, bindFiles, volumes, options.CompatBuiltinVolumes, b.CommonBuildOpts.Volumes, options.RunMounts, runMountInfo)
if err != nil {
return fmt.Errorf("resolving mountpoints for container %q: %w", b.ContainerID, err)
}