From e905172d17b74146447fcf81b7bd44d6e05030fb Mon Sep 17 00:00:00 2001 From: Ramkumar Chinchani <45800463+rchincha@users.noreply.github.com> Date: Wed, 8 Nov 2023 14:07:59 -0800 Subject: [PATCH] fix: handle ancient empty docker layers (#522) (#538) Earlier versions of docker images had empty layers of 1024 zero-valued octets. https://github.com/moby/moby/issues/20917#issuecomment-191901912 (cherry picked from commit f0f964216cb7dc1c5ec529ec49824e9a1879ef45) Signed-off-by: Ramkumar Chinchani --- pkg/log/log.go | 4 ++++ pkg/overlay/metadata.go | 11 ++++++++++- pkg/stacker/base.go | 6 +++++- pkg/types/layer_type.go | 4 +++- test/empty-layers.bats | 25 +++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/pkg/log/log.go b/pkg/log/log.go index 553383f0..c8a8c280 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -46,6 +46,10 @@ func Infof(msg string, v ...interface{}) { addStackerLogSentinel(log.NewEntry(log.Log.(*log.Logger))).Infof(msg, v...) } +func Warnf(msg string, v ...interface{}) { + addStackerLogSentinel(log.NewEntry(log.Log.(*log.Logger))).Warnf(msg, v...) +} + func Errorf(msg string, v ...interface{}) { addStackerLogSentinel(log.NewEntry(log.Log.(*log.Logger))).Errorf(msg, v...) } diff --git a/pkg/overlay/metadata.go b/pkg/overlay/metadata.go index 2d5cbff7..d2bbcf0e 100644 --- a/pkg/overlay/metadata.go +++ b/pkg/overlay/metadata.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io/fs" "os" "path" @@ -118,7 +119,15 @@ func (ovl overlayMetadata) lxcRootfsString(config types.StackerConfig, tag strin for _, layer := range manifest.Layers { contents := overlayPath(config, layer.Digest, "overlay") if _, err := os.Stat(contents); err != nil { - return "", errors.Wrapf(err, "%s does not exist", contents) + if errors.Is(err, fs.ErrNotExist) { + // some docker layers may be empty tars, so ignore these + // https://github.com/moby/moby/issues/20917#issuecomment-191901912 + log.Warnf("%s skipping empty tar layer", layer.Digest) + + continue + } + + return "", errors.Wrapf(err, "%s unable to stat", contents) } lowerdirs = append(lowerdirs, contents) } diff --git a/pkg/stacker/base.go b/pkg/stacker/base.go index 20c5e63b..c85330ce 100644 --- a/pkg/stacker/base.go +++ b/pkg/stacker/base.go @@ -87,7 +87,11 @@ func SetupRootfs(o BaseLayerOpts) error { case types.OCILayer: fallthrough case types.DockerLayer: - return setupContainersImageRootfs(o) + err := setupContainersImageRootfs(o) + if err != nil && errors.Is(err, types.ErrEmptyLayers) { + return o.Storage.SetupEmptyRootfs(o.Name) + } + return err default: return errors.Errorf("unknown layer type: %v", o.Layer.From.Type) } diff --git a/pkg/types/layer_type.go b/pkg/types/layer_type.go index 6d1a1353..69e20a5b 100644 --- a/pkg/types/layer_type.go +++ b/pkg/types/layer_type.go @@ -10,6 +10,8 @@ import ( "stackerbuild.io/stacker/pkg/squashfs" ) +var ErrEmptyLayers = errors.New("empty layers") + type LayerType struct { Type string Verity squashfs.VerityMetadata @@ -53,7 +55,7 @@ func NewLayerType(lt string, verity squashfs.VerityMetadata) (LayerType, error) func NewLayerTypeManifest(manifest ispec.Manifest) (LayerType, error) { if len(manifest.Layers) == 0 { - return LayerType{}, errors.Errorf("no existing layers to determine layer type") + return NewLayerType("tar", squashfs.VerityMetadataMissing) } switch manifest.Layers[0].MediaType { diff --git a/test/empty-layers.bats b/test/empty-layers.bats index 940136fe..5e839af9 100644 --- a/test/empty-layers.bats +++ b/test/empty-layers.bats @@ -61,3 +61,28 @@ EOF [ "$layers0" = "$layers1" ] } + +@test "an image with empty layers" { + umoci init --layout oci + umoci new --image oci:emptylayer + chmod -R a+rw oci + + cat > stacker.yaml < stacker.yaml <