Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mount previously built squashfs layers if needed #502

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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