Skip to content

Commit

Permalink
fix: mount previously built squashfs layers if needed (#502)
Browse files Browse the repository at this point in the history
When we run a stackerfile a second time, previously built tar layers
are assumed to already exist.  But if we are using squashfs layers,
then those layers are missing on a second run.  Make sure to mount
them when we find a built layer.

Closes #454

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Signed-off-by: Ramkumar Chinchani <rchincha@cisco.com>
Co-authored-by: Serge Hallyn <serge.hallyn@ubuntu.com>
  • Loading branch information
rchincha and hallyn authored Sep 11, 2023
1 parent 0fe7ea8 commit a1af45a
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pkg/overlay/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
"path"
"syscall"

ispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
"stackerbuild.io/stacker/pkg/mount"
"stackerbuild.io/stacker/pkg/squashfs"
"stackerbuild.io/stacker/pkg/types"
)

Expand Down Expand Up @@ -141,6 +144,14 @@ func (o *overlay) SetupEmptyRootfs(name string) error {
return ovl.write(o.config, name)
}

func hasDirEntries(dir string) bool {
ents, err := os.ReadDir(dir)
if err != nil {
return false
}
return len(ents) != 0
}

func (o *overlay) snapshot(source string, target string) error {
err := o.Create(target)
if err != nil {
Expand All @@ -153,6 +164,31 @@ func (o *overlay) snapshot(source string, target string) error {
return err
}

var manifest ispec.Manifest
for _, m := range ovl.Manifests {
manifest = m
}
cacheDir := path.Join(o.config.StackerDir, "layer-bases", "oci")
for _, layer := range manifest.Layers {
if !squashfs.IsSquashfsMediaType(layer.MediaType) {
continue
}
digest := layer.Digest
contents := overlayPath(o.config.RootFSDir, digest, "overlay")
mounted, err := mount.IsMountpoint(contents)
if err == nil && mounted {
// We have already mounted this atom
continue
}
if hasDirEntries(contents) {
// We have done an unsquashfs of this atom
continue
}
if err := unpackOne(cacheDir, contents, digest, true); err != nil {
return errors.Wrapf(err, "Failed mounting %#v", layer)
}
}

ovl.BuiltLayers = append(ovl.BuiltLayers, source)

return ovl.write(o.config, target)
Expand Down

0 comments on commit a1af45a

Please sign in to comment.