Merge pull request #6340 from nalind/mount-image-fails-with-imagestores

Mount image fails with imagestores
This commit is contained in:
openshift-merge-bot[bot] 2025-08-14 15:02:31 +00:00 committed by GitHub
commit e1bbbc2747
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 8 deletions

View File

@ -787,20 +787,37 @@ func (mb *ociManifestBuilder) manifestAndConfig() ([]byte, []byte, error) {
}
// filterExclusionsByImage returns a slice of the members of "exclusions" which are present in the image with the specified ID
func (i containerImageRef) filterExclusionsByImage(exclusions []copier.EnsureParentPath, imageID string) ([]copier.EnsureParentPath, error) {
func (i containerImageRef) filterExclusionsByImage(ctx context.Context, exclusions []copier.EnsureParentPath, imageID string) ([]copier.EnsureParentPath, error) {
if len(exclusions) == 0 || imageID == "" {
return nil, nil
}
var paths []copier.EnsureParentPath
mountPoint, err := i.store.MountImage(imageID, nil, i.mountLabel)
if err != nil {
return nil, err
}
defer func() {
cleanup := func() {
if _, err := i.store.UnmountImage(imageID, false); err != nil {
logrus.Debugf("unmounting image %q: %v", imageID, err)
}
}()
}
if err != nil && errors.Is(err, storage.ErrLayerUnknown) {
// if an imagestore is being used, this could be expected
if b, err2 := NewBuilder(ctx, i.store, BuilderOptions{
FromImage: imageID,
PullPolicy: define.PullNever,
ContainerSuffix: "tmp",
}); err2 == nil {
mountPoint, err = b.Mount(i.mountLabel)
cleanup = func() {
cid := b.ContainerID
if err := b.Delete(); err != nil {
logrus.Debugf("unmounting image %q as container %q: %v", imageID, cid, err)
}
}
}
}
if err != nil {
return nil, fmt.Errorf("mounting image %q to examine its contents: %w", imageID, err)
}
defer cleanup()
globs := make([]string, 0, len(exclusions))
for _, exclusion := range exclusions {
globs = append(globs, exclusion.Path)
@ -835,7 +852,7 @@ func (i containerImageRef) filterExclusionsByImage(exclusions []copier.EnsurePar
return paths, nil
}
func (i *containerImageRef) NewImageSource(_ context.Context, _ *types.SystemContext) (src types.ImageSource, err error) {
func (i *containerImageRef) NewImageSource(ctx context.Context, _ *types.SystemContext) (src types.ImageSource, err error) {
// These maps will let us check if a layer ID is part of one group or another.
parentLayerIDs := make(map[string]bool)
apiLayerIDs := make(map[string]bool)
@ -1038,7 +1055,7 @@ func (i *containerImageRef) NewImageSource(_ context.Context, _ *types.SystemCon
// And we _might_ need to filter out directories that modified
// by creating and removing mount targets, _if_ they were the
// same in the base image for this stage.
layerPullUps, err := i.filterExclusionsByImage(i.layerPullUps, i.fromImageID)
layerPullUps, err := i.filterExclusionsByImage(ctx, i.layerPullUps, i.fromImageID)
if err != nil {
return nil, fmt.Errorf("checking which exclusions are in base image %q: %w", i.fromImageID, err)
}

View File

@ -8853,3 +8853,24 @@ _EOF
run_buildah build --layers ${contextdir}
run_buildah build ${contextdir}
}
@test "bud with a previously-used graphroot with base image in it used as imagestore" {
# there are subtle differences between "this was always the imagestore" and
# "this was a graphroot, but i'm using it as an imagestore now"
case "${STORAGE_DRIVER}" in
overlay) ;;
*) skip "imagestore flag is compatible with overlay, but not ${STORAGE_DRIVER}" ;;
esac
_prefetch quay.io/libpod/alpine:latest
local contextdir=${TEST_SCRATCH_DIR}/context
mkdir -p ${contextdir}
cat > ${contextdir}/Dockerfile << _EOF
FROM quay.io/libpod/alpine:latest
RUN mkdir hello
_EOF
run_buildah --root=${TEST_SCRATCH_DIR}/newroot --storage-opt=imagestore=${TEST_SCRATCH_DIR}/root build --pull=never --no-cache --layers=true ${contextdir}
run_buildah --root=${TEST_SCRATCH_DIR}/newroot --storage-opt=imagestore=${TEST_SCRATCH_DIR}/root build --pull=never ${contextdir}
run_buildah --root=${TEST_SCRATCH_DIR}/newroot --storage-opt=imagestore=${TEST_SCRATCH_DIR}/root build --pull=never --squash ${contextdir}
}