Skip to content

Commit

Permalink
Fix some bugs with OCI image handling (#6896)
Browse files Browse the repository at this point in the history
  • Loading branch information
bduffany authored Jun 24, 2024
1 parent fd465db commit 4c07f97
Showing 1 changed file with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ type ociContainer struct {
workDir string

imageRef string
layerDigests []ctr.Hash
overlayfsMounted bool
}

Expand Down Expand Up @@ -249,19 +248,9 @@ func (c *ociContainer) PullImage(ctx context.Context, creds oci.Credentials) err
if c.imageRef == TestBusyboxImageRef {
return nil
}
layers, err := c.imageStore.Pull(ctx, c.imageRef, creds)
if err != nil {
if _, err := c.imageStore.Pull(ctx, c.imageRef, creds); err != nil {
return status.WrapError(err, "pull OCI image")
}
var layerDigests []ctr.Hash
for _, layer := range layers {
d, err := layer.Digest()
if err != nil {
return status.UnavailableErrorf("get layer digest: %s", err)
}
layerDigests = append(layerDigests, d)
}
c.layerDigests = layerDigests
return nil
}

Expand Down Expand Up @@ -382,8 +371,27 @@ func (c *ociContainer) createRootfs(ctx context.Context) error {

// Create an overlayfs with the pulled image layers.
var lowerDirs []string
for _, d := range c.layerDigests {
lowerDirs = append(lowerDirs, layerPath(c.layersRoot, d))
layers, ok := c.imageStore.CachedLayers(c.imageRef)
if !ok {
return fmt.Errorf("bad state: attempted to create rootfs before pulling image")
}
for _, layer := range layers {
d, err := layer.Digest()
if err != nil {
return fmt.Errorf("get layer digest: %w", err)
}
path := layerPath(c.layersRoot, d)
// Skip empty dirs - these can cause conflicts since they will always
// have the same digest, and also just add more overhead.
// TODO: precompute this
children, err := os.ReadDir(path)
if err != nil {
return fmt.Errorf("read layer dir: %w", err)
}
if len(children) == 0 {
continue
}
lowerDirs = append(lowerDirs, path)
}
// Create workdir and upperdir.
workdir := filepath.Join(c.overlayTmpPath(), "work")
Expand All @@ -403,6 +411,7 @@ func (c *ociContainer) createRootfs(ctx context.Context) error {
options := fmt.Sprintf(
"lowerdir=%s,upperdir=%s,workdir=%s,userxattr,volatile",
strings.Join(lowerDirs, ":"), upperdir, workdir)
log.CtxDebugf(ctx, "Mounting overlayfs to %q, options=%q", c.rootfsPath(), options)
if err := syscall.Mount("none", c.rootfsPath(), "overlay", 0, options); err != nil {
return fmt.Errorf("mount overlayfs: %w", err)
}
Expand Down

0 comments on commit 4c07f97

Please sign in to comment.