2017-05-27 06:12:13 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2019-09-18 00:18:32 +00:00
|
|
|
"fmt"
|
2019-07-29 18:00:10 +00:00
|
|
|
"strings"
|
2017-05-27 06:12:13 +00:00
|
|
|
"sync"
|
2019-07-22 21:43:16 +00:00
|
|
|
"time"
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2019-09-18 00:18:32 +00:00
|
|
|
"github.com/containerd/containerd/errdefs"
|
2019-07-22 21:43:16 +00:00
|
|
|
"github.com/containerd/containerd/leases"
|
2017-05-27 06:12:13 +00:00
|
|
|
"github.com/containerd/containerd/mount"
|
2019-09-18 00:18:32 +00:00
|
|
|
"github.com/containerd/containerd/snapshots"
|
2019-03-20 21:54:20 +00:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2017-07-03 23:08:20 +00:00
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
2017-06-30 22:54:51 +00:00
|
|
|
"github.com/moby/buildkit/identity"
|
2020-10-27 06:13:39 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2018-04-16 22:23:10 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
|
|
|
"github.com/moby/buildkit/util/compression"
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/util/flightcontrol"
|
2019-10-03 21:11:54 +00:00
|
|
|
"github.com/moby/buildkit/util/leaseutil"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/moby/buildkit/util/winlayers"
|
2020-05-13 15:37:27 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2019-09-18 00:18:32 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-05-27 06:12:13 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-09-28 05:38:54 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-05-28 20:46:33 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-05-27 06:12:13 +00:00
|
|
|
)
|
|
|
|
|
2017-11-06 06:30:16 +00:00
|
|
|
// Ref is a reference to cacheable objects.
|
|
|
|
type Ref interface {
|
2017-05-27 06:12:13 +00:00
|
|
|
Mountable
|
|
|
|
ID() string
|
2017-06-13 23:01:47 +00:00
|
|
|
Release(context.Context) error
|
2017-05-31 23:45:04 +00:00
|
|
|
Size(ctx context.Context) (int64, error)
|
2017-11-06 06:30:16 +00:00
|
|
|
Metadata() *metadata.StorageItem
|
2019-03-20 21:54:20 +00:00
|
|
|
IdentityMapping() *idtools.IdentityMapping
|
2017-11-06 06:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ImmutableRef interface {
|
|
|
|
Ref
|
2017-07-10 20:03:38 +00:00
|
|
|
Parent() ImmutableRef
|
2018-07-11 22:02:58 +00:00
|
|
|
Finalize(ctx context.Context, commit bool) error // Make sure reference is flushed to driver
|
2018-05-10 04:52:05 +00:00
|
|
|
Clone() ImmutableRef
|
2019-07-22 21:43:16 +00:00
|
|
|
|
|
|
|
Info() RefInfo
|
2020-10-27 06:13:39 +00:00
|
|
|
Extract(ctx context.Context, s session.Group) error // +progress
|
|
|
|
GetRemote(ctx context.Context, createIfNeeded bool, compressionType compression.Type, s session.Group) (*solver.Remote, error)
|
2019-07-22 21:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RefInfo struct {
|
|
|
|
SnapshotID string
|
|
|
|
ChainID digest.Digest
|
|
|
|
BlobChainID digest.Digest
|
|
|
|
DiffID digest.Digest
|
|
|
|
Blob digest.Digest
|
2019-09-18 00:18:32 +00:00
|
|
|
MediaType string
|
2019-07-22 21:43:16 +00:00
|
|
|
Extracted bool
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MutableRef interface {
|
2017-11-06 06:30:16 +00:00
|
|
|
Ref
|
2017-07-14 18:59:31 +00:00
|
|
|
Commit(context.Context) (ImmutableRef, error)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Mountable interface {
|
2020-10-27 06:13:39 +00:00
|
|
|
Mount(ctx context.Context, readonly bool, s session.Group) (snapshot.Mountable, error)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 20:35:41 +00:00
|
|
|
type ref interface {
|
|
|
|
updateLastUsed() bool
|
|
|
|
}
|
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
type cacheRecord struct {
|
2017-12-26 19:42:14 +00:00
|
|
|
cm *cacheManager
|
|
|
|
mu *sync.Mutex // the mutex is shared by records sharing data
|
|
|
|
|
|
|
|
mutable bool
|
2018-09-14 20:35:41 +00:00
|
|
|
refs map[ref]struct{}
|
2019-05-25 01:37:24 +00:00
|
|
|
parent *immutableRef
|
2017-12-26 19:42:14 +00:00
|
|
|
md *metadata.StorageItem
|
|
|
|
|
|
|
|
// dead means record is marked as deleted
|
|
|
|
dead bool
|
|
|
|
|
2017-05-30 21:17:04 +00:00
|
|
|
view string
|
2018-04-16 22:23:10 +00:00
|
|
|
viewMount snapshot.Mountable
|
2017-05-31 23:45:04 +00:00
|
|
|
|
|
|
|
sizeG flightcontrol.Group
|
2017-07-14 18:59:31 +00:00
|
|
|
|
|
|
|
// these are filled if multiple refs point to same data
|
|
|
|
equalMutable *mutableRef
|
|
|
|
equalImmutable *immutableRef
|
2019-09-20 21:49:29 +00:00
|
|
|
|
|
|
|
parentChainCache []digest.Digest
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 19:42:14 +00:00
|
|
|
// hold ref lock before calling
|
2020-05-28 20:46:33 +00:00
|
|
|
func (cr *cacheRecord) ref(triggerLastUsed bool, descHandlers DescHandlers) *immutableRef {
|
|
|
|
ref := &immutableRef{
|
|
|
|
cacheRecord: cr,
|
|
|
|
triggerLastUsed: triggerLastUsed,
|
|
|
|
descHandlers: descHandlers,
|
|
|
|
}
|
2017-06-01 22:24:23 +00:00
|
|
|
cr.refs[ref] = struct{}{}
|
|
|
|
return ref
|
|
|
|
}
|
|
|
|
|
2017-12-26 19:42:14 +00:00
|
|
|
// hold ref lock before calling
|
2020-05-28 20:46:33 +00:00
|
|
|
func (cr *cacheRecord) mref(triggerLastUsed bool, descHandlers DescHandlers) *mutableRef {
|
|
|
|
ref := &mutableRef{
|
|
|
|
cacheRecord: cr,
|
|
|
|
triggerLastUsed: triggerLastUsed,
|
|
|
|
descHandlers: descHandlers,
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
cr.refs[ref] = struct{}{}
|
|
|
|
return ref
|
|
|
|
}
|
|
|
|
|
2019-09-20 21:49:29 +00:00
|
|
|
func (cr *cacheRecord) parentChain() []digest.Digest {
|
|
|
|
if cr.parentChainCache != nil {
|
|
|
|
return cr.parentChainCache
|
|
|
|
}
|
|
|
|
blob := getBlob(cr.md)
|
|
|
|
if blob == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var parent []digest.Digest
|
|
|
|
if cr.parent != nil {
|
|
|
|
parent = cr.parent.parentChain()
|
|
|
|
}
|
|
|
|
pcc := make([]digest.Digest, len(parent)+1)
|
|
|
|
copy(pcc, parent)
|
|
|
|
pcc[len(parent)] = digest.Digest(blob)
|
|
|
|
cr.parentChainCache = pcc
|
|
|
|
return pcc
|
|
|
|
}
|
|
|
|
|
2017-12-26 19:42:14 +00:00
|
|
|
// hold ref lock before calling
|
|
|
|
func (cr *cacheRecord) isDead() bool {
|
|
|
|
return cr.dead || (cr.equalImmutable != nil && cr.equalImmutable.dead) || (cr.equalMutable != nil && cr.equalMutable.dead)
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
func (cr *cacheRecord) isLazy(ctx context.Context) (bool, error) {
|
|
|
|
if !getBlobOnly(cr.md) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-11-12 23:36:16 +00:00
|
|
|
dgst := getBlob(cr.md)
|
|
|
|
// special case for moby where there is no compressed blob (empty digest)
|
|
|
|
if dgst == "" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
_, err := cr.cm.ContentStore.Info(ctx, digest.Digest(dgst))
|
2020-05-28 20:46:33 +00:00
|
|
|
if errors.Is(err, errdefs.ErrNotFound) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2019-03-20 21:54:20 +00:00
|
|
|
func (cr *cacheRecord) IdentityMapping() *idtools.IdentityMapping {
|
|
|
|
return cr.cm.IdentityMapping()
|
|
|
|
}
|
|
|
|
|
2017-05-31 23:45:04 +00:00
|
|
|
func (cr *cacheRecord) Size(ctx context.Context) (int64, error) {
|
|
|
|
// this expects that usage() is implemented lazily
|
2017-07-03 23:08:20 +00:00
|
|
|
s, err := cr.sizeG.Do(ctx, cr.ID(), func(ctx context.Context) (interface{}, error) {
|
2017-05-31 23:45:04 +00:00
|
|
|
cr.mu.Lock()
|
2017-07-03 23:08:20 +00:00
|
|
|
s := getSize(cr.md)
|
2017-05-31 23:45:04 +00:00
|
|
|
if s != sizeUnknown {
|
2017-07-14 18:59:31 +00:00
|
|
|
cr.mu.Unlock()
|
2017-05-31 23:45:04 +00:00
|
|
|
return s, nil
|
|
|
|
}
|
2019-07-22 21:43:16 +00:00
|
|
|
driverID := getSnapshotID(cr.md)
|
2017-07-14 18:59:31 +00:00
|
|
|
if cr.equalMutable != nil {
|
2019-07-22 21:43:16 +00:00
|
|
|
driverID = getSnapshotID(cr.equalMutable.md)
|
2017-07-14 18:59:31 +00:00
|
|
|
}
|
|
|
|
cr.mu.Unlock()
|
2019-09-18 00:18:32 +00:00
|
|
|
var usage snapshots.Usage
|
|
|
|
if !getBlobOnly(cr.md) {
|
|
|
|
var err error
|
|
|
|
usage, err = cr.cm.ManagerOpt.Snapshotter.Usage(ctx, driverID)
|
|
|
|
if err != nil {
|
|
|
|
cr.mu.Lock()
|
|
|
|
isDead := cr.isDead()
|
|
|
|
cr.mu.Unlock()
|
|
|
|
if isDead {
|
|
|
|
return int64(0), nil
|
|
|
|
}
|
2020-04-19 05:17:47 +00:00
|
|
|
if !errors.Is(err, errdefs.ErrNotFound) {
|
2019-09-18 00:18:32 +00:00
|
|
|
return s, errors.Wrapf(err, "failed to get usage for %s", cr.ID())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dgst := getBlob(cr.md); dgst != "" {
|
|
|
|
info, err := cr.cm.ContentStore.Info(ctx, digest.Digest(dgst))
|
|
|
|
if err == nil {
|
|
|
|
usage.Size += info.Size
|
2017-12-27 01:22:50 +00:00
|
|
|
}
|
2017-05-31 23:45:04 +00:00
|
|
|
}
|
|
|
|
cr.mu.Lock()
|
2017-07-03 23:08:20 +00:00
|
|
|
setSize(cr.md, usage.Size)
|
|
|
|
if err := cr.md.Commit(); err != nil {
|
2018-04-30 17:38:47 +00:00
|
|
|
cr.mu.Unlock()
|
2017-07-03 23:08:20 +00:00
|
|
|
return s, err
|
|
|
|
}
|
2017-05-31 23:45:04 +00:00
|
|
|
cr.mu.Unlock()
|
|
|
|
return usage.Size, nil
|
|
|
|
})
|
2020-05-27 21:58:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return s.(int64), nil
|
2017-05-31 23:45:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
func (cr *cacheRecord) parentRef(hidden bool, descHandlers DescHandlers) *immutableRef {
|
2019-05-25 01:37:24 +00:00
|
|
|
p := cr.parent
|
|
|
|
if p == nil {
|
2017-07-10 20:03:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-12-26 19:42:14 +00:00
|
|
|
p.mu.Lock()
|
2017-12-28 23:03:49 +00:00
|
|
|
defer p.mu.Unlock()
|
2020-05-28 20:46:33 +00:00
|
|
|
return p.ref(hidden, descHandlers)
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
// must be called holding cacheRecord mu
|
|
|
|
func (cr *cacheRecord) mount(ctx context.Context, readonly bool) (snapshot.Mountable, error) {
|
2017-06-01 22:24:23 +00:00
|
|
|
if cr.mutable {
|
2019-07-22 21:43:16 +00:00
|
|
|
m, err := cr.cm.Snapshotter.Mounts(ctx, getSnapshotID(cr.md))
|
2017-05-27 06:12:13 +00:00
|
|
|
if err != nil {
|
2017-07-03 23:08:20 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to mount %s", cr.ID())
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
2017-07-18 06:08:22 +00:00
|
|
|
if readonly {
|
|
|
|
m = setReadonly(m)
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
return m, nil
|
2017-06-13 08:32:30 +00:00
|
|
|
}
|
2017-07-18 06:08:22 +00:00
|
|
|
|
|
|
|
if cr.equalMutable != nil && readonly {
|
2019-07-22 21:43:16 +00:00
|
|
|
m, err := cr.cm.Snapshotter.Mounts(ctx, getSnapshotID(cr.equalMutable.md))
|
2017-07-18 06:08:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to mount %s", cr.equalMutable.ID())
|
|
|
|
}
|
|
|
|
return setReadonly(m), nil
|
|
|
|
}
|
|
|
|
|
2018-07-11 22:02:58 +00:00
|
|
|
if err := cr.finalize(ctx, true); err != nil {
|
2017-07-14 18:59:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-13 08:32:30 +00:00
|
|
|
if cr.viewMount == nil { // TODO: handle this better
|
2019-09-19 20:08:36 +00:00
|
|
|
view := identity.NewID()
|
|
|
|
l, err := cr.cm.LeaseManager.Create(ctx, func(l *leases.Lease) error {
|
|
|
|
l.ID = view
|
|
|
|
l.Labels = map[string]string{
|
2019-10-03 21:11:54 +00:00
|
|
|
"containerd.io/gc.flat": time.Now().UTC().Format(time.RFC3339Nano),
|
2019-09-19 20:08:36 +00:00
|
|
|
}
|
|
|
|
return nil
|
2019-10-03 21:11:54 +00:00
|
|
|
}, leaseutil.MakeTemporary)
|
2019-09-19 20:08:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx = leases.WithLease(ctx, l.ID)
|
|
|
|
m, err := cr.cm.Snapshotter.View(ctx, view, getSnapshotID(cr.md))
|
2017-06-13 08:32:30 +00:00
|
|
|
if err != nil {
|
2019-09-19 20:08:36 +00:00
|
|
|
cr.cm.LeaseManager.Delete(context.TODO(), leases.Lease{ID: l.ID})
|
2017-07-03 23:08:20 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to mount %s", cr.ID())
|
2017-05-30 21:17:04 +00:00
|
|
|
}
|
2019-09-19 20:08:36 +00:00
|
|
|
cr.view = view
|
2017-06-13 08:32:30 +00:00
|
|
|
cr.viewMount = m
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
2017-06-13 08:32:30 +00:00
|
|
|
return cr.viewMount, nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-27 01:22:50 +00:00
|
|
|
// call when holding the manager lock
|
2017-07-14 18:59:31 +00:00
|
|
|
func (cr *cacheRecord) remove(ctx context.Context, removeSnapshot bool) error {
|
2017-07-19 23:39:32 +00:00
|
|
|
delete(cr.cm.records, cr.ID())
|
2017-12-27 01:22:50 +00:00
|
|
|
if cr.parent != nil {
|
2019-11-12 19:28:39 +00:00
|
|
|
cr.parent.mu.Lock()
|
|
|
|
err := cr.parent.release(ctx)
|
|
|
|
cr.parent.mu.Unlock()
|
|
|
|
if err != nil {
|
2017-12-27 01:22:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
}
|
|
|
|
if removeSnapshot {
|
2019-09-19 21:55:10 +00:00
|
|
|
if err := cr.cm.LeaseManager.Delete(context.TODO(), leases.Lease{ID: cr.ID()}); err != nil {
|
2019-06-01 23:34:02 +00:00
|
|
|
return errors.Wrapf(err, "failed to remove %s", cr.ID())
|
2017-07-14 18:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-27 01:22:50 +00:00
|
|
|
if err := cr.cm.md.Clear(cr.ID()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-01 22:24:23 +00:00
|
|
|
func (cr *cacheRecord) ID() string {
|
2017-07-03 23:08:20 +00:00
|
|
|
return cr.md.ID()
|
2017-06-01 22:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type immutableRef struct {
|
|
|
|
*cacheRecord
|
2018-09-14 20:35:41 +00:00
|
|
|
triggerLastUsed bool
|
2020-05-28 20:46:33 +00:00
|
|
|
descHandlers DescHandlers
|
2017-06-01 22:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type mutableRef struct {
|
|
|
|
*cacheRecord
|
2018-09-14 20:35:41 +00:00
|
|
|
triggerLastUsed bool
|
2020-05-28 20:46:33 +00:00
|
|
|
descHandlers DescHandlers
|
2017-06-01 22:24:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 04:52:05 +00:00
|
|
|
func (sr *immutableRef) Clone() ImmutableRef {
|
|
|
|
sr.mu.Lock()
|
2020-05-28 20:46:33 +00:00
|
|
|
ref := sr.ref(false, sr.descHandlers)
|
2018-05-10 04:52:05 +00:00
|
|
|
sr.mu.Unlock()
|
|
|
|
return ref
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
func (sr *immutableRef) Parent() ImmutableRef {
|
|
|
|
if p := sr.parentRef(true, sr.descHandlers); p != nil { // avoid returning typed nil pointer
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-22 21:43:16 +00:00
|
|
|
func (sr *immutableRef) Info() RefInfo {
|
|
|
|
return RefInfo{
|
|
|
|
ChainID: digest.Digest(getChainID(sr.md)),
|
|
|
|
DiffID: digest.Digest(getDiffID(sr.md)),
|
|
|
|
Blob: digest.Digest(getBlob(sr.md)),
|
2019-09-18 00:18:32 +00:00
|
|
|
MediaType: getMediaType(sr.md),
|
2019-07-22 21:43:16 +00:00
|
|
|
BlobChainID: digest.Digest(getBlobChainID(sr.md)),
|
|
|
|
SnapshotID: getSnapshotID(sr.md),
|
|
|
|
Extracted: !getBlobOnly(sr.md),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
func (sr *immutableRef) ociDesc() (ocispec.Descriptor, error) {
|
|
|
|
desc := ocispec.Descriptor{
|
|
|
|
Digest: digest.Digest(getBlob(sr.md)),
|
|
|
|
Size: getBlobSize(sr.md),
|
|
|
|
MediaType: getMediaType(sr.md),
|
|
|
|
Annotations: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
diffID := getDiffID(sr.md)
|
|
|
|
if diffID != "" {
|
|
|
|
desc.Annotations["containerd.io/uncompressed"] = diffID
|
|
|
|
}
|
|
|
|
|
|
|
|
createdAt := GetCreatedAt(sr.md)
|
|
|
|
if !createdAt.IsZero() {
|
|
|
|
createdAt, err := createdAt.MarshalText()
|
|
|
|
if err != nil {
|
|
|
|
return ocispec.Descriptor{}, err
|
|
|
|
}
|
|
|
|
desc.Annotations["buildkit/createdat"] = string(createdAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return desc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// order is from parent->child, sr will be at end of slice
|
|
|
|
func (sr *immutableRef) parentRefChain() []*immutableRef {
|
|
|
|
var count int
|
|
|
|
for ref := sr; ref != nil; ref = ref.parent {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
refs := make([]*immutableRef, count)
|
|
|
|
for i, ref := count-1, sr; ref != nil; i, ref = i-1, ref.parent {
|
|
|
|
refs[i] = ref
|
|
|
|
}
|
|
|
|
return refs
|
|
|
|
}
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
func (sr *immutableRef) Mount(ctx context.Context, readonly bool, s session.Group) (snapshot.Mountable, error) {
|
2020-10-31 03:55:33 +00:00
|
|
|
if err := sr.Extract(ctx, s); err != nil {
|
|
|
|
return nil, err
|
2020-05-28 20:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sr.mu.Lock()
|
|
|
|
defer sr.mu.Unlock()
|
|
|
|
return sr.mount(ctx, readonly)
|
|
|
|
}
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
func (sr *immutableRef) Extract(ctx context.Context, s session.Group) (rerr error) {
|
2020-10-31 03:55:33 +00:00
|
|
|
if !getBlobOnly(sr.md) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
ctx, done, err := leaseutil.WithLease(ctx, sr.cm.LeaseManager, leaseutil.MakeTemporary)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer done(ctx)
|
|
|
|
|
|
|
|
if GetLayerType(sr) == "windows" {
|
|
|
|
ctx = winlayers.UseWindowsLayerMode(ctx)
|
|
|
|
}
|
|
|
|
|
2020-08-17 09:34:33 +00:00
|
|
|
if _, err := sr.prepareRemoteSnapshots(ctx, sr.descHandlers); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
return sr.extract(ctx, sr.descHandlers, s)
|
2020-05-28 20:46:33 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 09:34:33 +00:00
|
|
|
func (sr *immutableRef) prepareRemoteSnapshots(ctx context.Context, dhs DescHandlers) (bool, error) {
|
|
|
|
ok, err := sr.sizeG.Do(ctx, sr.ID()+"-prepare-remote-snapshot", func(ctx context.Context) (_ interface{}, rerr error) {
|
|
|
|
snapshotID := getSnapshotID(sr.md)
|
|
|
|
if _, err := sr.cm.Snapshotter.Stat(ctx, snapshotID); err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
desc, err := sr.ociDesc()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
dh := dhs[desc.Digest]
|
|
|
|
if dh == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
parentID := ""
|
|
|
|
if sr.parent != nil {
|
|
|
|
if ok, err := sr.parent.prepareRemoteSnapshots(ctx, dhs); !ok {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
parentID = getSnapshotID(sr.parent.md)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hint labels to the snapshotter
|
|
|
|
labels := dh.SnapshotLabels
|
|
|
|
if labels == nil {
|
|
|
|
labels = make(map[string]string)
|
|
|
|
}
|
|
|
|
labels["containerd.io/snapshot.ref"] = snapshotID
|
|
|
|
opt := snapshots.WithLabels(labels)
|
|
|
|
|
|
|
|
// Try to preapre the remote snapshot
|
|
|
|
key := fmt.Sprintf("tmp-%s %s", identity.NewID(), sr.Info().ChainID)
|
|
|
|
if err = sr.cm.Snapshotter.Prepare(ctx, key, parentID, opt); err != nil {
|
|
|
|
if errdefs.IsAlreadyExists(err) {
|
|
|
|
// Check if the targeting snapshot ID has been prepared as a remote
|
|
|
|
// snapshot in the snapshotter.
|
|
|
|
if _, err := sr.cm.Snapshotter.Stat(ctx, snapshotID); err == nil {
|
|
|
|
// We can use this remote snapshot without unlazying.
|
|
|
|
// Try the next layer as well.
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This layer cannot be prepared without unlazying.
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
return ok.(bool), err
|
|
|
|
}
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
func (sr *immutableRef) extract(ctx context.Context, dhs DescHandlers, s session.Group) error {
|
2020-05-28 20:46:33 +00:00
|
|
|
_, err := sr.sizeG.Do(ctx, sr.ID()+"-extract", func(ctx context.Context) (_ interface{}, rerr error) {
|
2019-09-18 00:18:32 +00:00
|
|
|
snapshotID := getSnapshotID(sr.md)
|
|
|
|
if _, err := sr.cm.Snapshotter.Stat(ctx, snapshotID); err == nil {
|
2020-08-17 09:34:33 +00:00
|
|
|
return nil, nil
|
2019-09-18 00:18:32 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 23:36:16 +00:00
|
|
|
if sr.cm.Applier == nil {
|
|
|
|
return nil, errors.New("extract requires an applier")
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
eg, egctx := errgroup.WithContext(ctx)
|
|
|
|
|
2019-09-18 00:18:32 +00:00
|
|
|
parentID := ""
|
|
|
|
if sr.parent != nil {
|
2020-05-28 20:46:33 +00:00
|
|
|
eg.Go(func() error {
|
2020-10-27 06:13:39 +00:00
|
|
|
if err := sr.parent.extract(egctx, dhs, s); err != nil {
|
2020-05-28 20:46:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
parentID = getSnapshotID(sr.parent.md)
|
|
|
|
return nil
|
|
|
|
})
|
2019-09-18 00:18:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
desc, err := sr.ociDesc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dh := dhs[desc.Digest]
|
|
|
|
|
|
|
|
eg.Go(func() error {
|
|
|
|
// unlazies if needed, otherwise a no-op
|
|
|
|
return lazyRefProvider{
|
2020-10-27 06:13:39 +00:00
|
|
|
ref: sr,
|
|
|
|
desc: desc,
|
|
|
|
dh: dh,
|
|
|
|
session: s,
|
2020-05-28 20:46:33 +00:00
|
|
|
}.Unlazy(egctx)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if dh != nil && dh.Progress != nil {
|
|
|
|
_, stopProgress := dh.Progress.Start(ctx)
|
|
|
|
defer stopProgress(rerr)
|
|
|
|
statusDone := dh.Progress.Status("extracting "+desc.Digest.String(), "extracting")
|
|
|
|
defer statusDone()
|
|
|
|
}
|
|
|
|
|
|
|
|
key := fmt.Sprintf("extract-%s %s", identity.NewID(), sr.Info().ChainID)
|
|
|
|
|
|
|
|
err = sr.cm.Snapshotter.Prepare(ctx, key, parentID)
|
2019-09-18 00:18:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mountable, err := sr.cm.Snapshotter.Mounts(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mounts, unmount, err := mountable.Mount()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-28 20:46:33 +00:00
|
|
|
_, err = sr.cm.Applier.Apply(ctx, desc, mounts)
|
2019-09-18 00:18:32 +00:00
|
|
|
if err != nil {
|
|
|
|
unmount()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := unmount(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := sr.cm.Snapshotter.Commit(ctx, getSnapshotID(sr.md), key); err != nil {
|
2020-04-19 05:17:47 +00:00
|
|
|
if !errors.Is(err, errdefs.ErrAlreadyExists) {
|
2019-09-18 00:18:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
queueBlobOnly(sr.md, false)
|
2020-05-28 20:46:33 +00:00
|
|
|
setSize(sr.md, sizeUnknown)
|
2019-09-18 00:18:32 +00:00
|
|
|
if err := sr.md.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
func (sr *immutableRef) Release(ctx context.Context) error {
|
2017-05-27 06:12:13 +00:00
|
|
|
sr.cm.mu.Lock()
|
|
|
|
defer sr.cm.mu.Unlock()
|
|
|
|
|
|
|
|
sr.mu.Lock()
|
|
|
|
defer sr.mu.Unlock()
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
return sr.release(ctx)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 20:35:41 +00:00
|
|
|
func (sr *immutableRef) updateLastUsed() bool {
|
|
|
|
return sr.triggerLastUsed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sr *immutableRef) updateLastUsedNow() bool {
|
|
|
|
if !sr.triggerLastUsed {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for r := range sr.refs {
|
|
|
|
if r.updateLastUsed() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
func (sr *immutableRef) release(ctx context.Context) error {
|
2017-05-27 06:12:13 +00:00
|
|
|
delete(sr.refs, sr)
|
|
|
|
|
2018-09-14 20:35:41 +00:00
|
|
|
if sr.updateLastUsedNow() {
|
2018-05-11 05:33:51 +00:00
|
|
|
updateLastUsed(sr.md)
|
2018-09-14 20:35:41 +00:00
|
|
|
if sr.equalMutable != nil {
|
|
|
|
sr.equalMutable.triggerLastUsed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sr.refs) == 0 {
|
2017-12-11 23:35:42 +00:00
|
|
|
if sr.viewMount != nil { // TODO: release viewMount earlier if possible
|
2019-09-19 20:08:36 +00:00
|
|
|
if err := sr.cm.LeaseManager.Delete(ctx, leases.Lease{ID: sr.view}); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to remove view lease %s", sr.view)
|
2017-12-11 23:35:42 +00:00
|
|
|
}
|
|
|
|
sr.view = ""
|
|
|
|
sr.viewMount = nil
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
if sr.equalMutable != nil {
|
|
|
|
sr.equalMutable.release(ctx)
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-11 22:02:58 +00:00
|
|
|
func (sr *immutableRef) Finalize(ctx context.Context, b bool) error {
|
2017-05-27 06:12:13 +00:00
|
|
|
sr.mu.Lock()
|
|
|
|
defer sr.mu.Unlock()
|
|
|
|
|
2018-07-11 22:02:58 +00:00
|
|
|
return sr.finalize(ctx, b)
|
2017-07-14 18:59:31 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:35 +00:00
|
|
|
func (cr *cacheRecord) Metadata() *metadata.StorageItem {
|
|
|
|
return cr.md
|
2017-07-20 22:55:24 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 22:02:58 +00:00
|
|
|
func (cr *cacheRecord) finalize(ctx context.Context, commit bool) error {
|
2017-11-17 02:09:35 +00:00
|
|
|
mutable := cr.equalMutable
|
2017-07-14 18:59:31 +00:00
|
|
|
if mutable == nil {
|
|
|
|
return nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
2018-07-11 22:02:58 +00:00
|
|
|
if !commit {
|
|
|
|
if HasCachePolicyRetain(mutable) {
|
|
|
|
CachePolicyRetain(mutable)
|
|
|
|
return mutable.Metadata().Commit()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-22 21:43:16 +00:00
|
|
|
|
2019-09-30 23:38:02 +00:00
|
|
|
_, err := cr.cm.ManagerOpt.LeaseManager.Create(ctx, func(l *leases.Lease) error {
|
2019-07-22 21:43:16 +00:00
|
|
|
l.ID = cr.ID()
|
|
|
|
l.Labels = map[string]string{
|
|
|
|
"containerd.io/gc.flat": time.Now().UTC().Format(time.RFC3339Nano),
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-04-19 05:17:47 +00:00
|
|
|
if !errors.Is(err, errdefs.ErrAlreadyExists) { // migrator adds leases for everything
|
2019-09-30 23:38:02 +00:00
|
|
|
return errors.Wrap(err, "failed to create lease")
|
|
|
|
}
|
2019-07-22 21:43:16 +00:00
|
|
|
}
|
|
|
|
|
2019-09-30 23:38:02 +00:00
|
|
|
if err := cr.cm.ManagerOpt.LeaseManager.AddResource(ctx, leases.Lease{ID: cr.ID()}, leases.Resource{
|
2019-07-22 21:43:16 +00:00
|
|
|
ID: cr.ID(),
|
|
|
|
Type: "snapshots/" + cr.cm.ManagerOpt.Snapshotter.Name(),
|
|
|
|
}); err != nil {
|
2019-09-24 01:02:17 +00:00
|
|
|
cr.cm.LeaseManager.Delete(context.TODO(), leases.Lease{ID: cr.ID()})
|
2019-07-22 21:43:16 +00:00
|
|
|
return errors.Wrapf(err, "failed to add snapshot %s to lease", cr.ID())
|
|
|
|
}
|
|
|
|
|
2019-09-19 21:55:10 +00:00
|
|
|
err = cr.cm.Snapshotter.Commit(ctx, cr.ID(), mutable.ID())
|
|
|
|
if err != nil {
|
|
|
|
cr.cm.LeaseManager.Delete(context.TODO(), leases.Lease{ID: cr.ID()})
|
|
|
|
return errors.Wrapf(err, "failed to commit %s", mutable.ID())
|
|
|
|
}
|
|
|
|
mutable.dead = true
|
|
|
|
go func() {
|
|
|
|
cr.cm.mu.Lock()
|
|
|
|
defer cr.cm.mu.Unlock()
|
|
|
|
if err := mutable.remove(context.TODO(), true); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-11-17 02:09:35 +00:00
|
|
|
cr.equalMutable = nil
|
|
|
|
clearEqualMutable(cr.md)
|
|
|
|
return cr.md.Commit()
|
2017-07-14 18:59:31 +00:00
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2018-09-14 20:35:41 +00:00
|
|
|
func (sr *mutableRef) updateLastUsed() bool {
|
|
|
|
return sr.triggerLastUsed
|
|
|
|
}
|
|
|
|
|
2019-05-25 01:37:24 +00:00
|
|
|
func (sr *mutableRef) commit(ctx context.Context) (*immutableRef, error) {
|
2017-07-14 18:59:31 +00:00
|
|
|
if !sr.mutable || len(sr.refs) == 0 {
|
2019-02-27 22:40:45 +00:00
|
|
|
return nil, errors.Wrapf(errInvalid, "invalid mutable ref %p", sr)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
id := identity.NewID()
|
|
|
|
md, _ := sr.cm.md.Get(id)
|
|
|
|
rec := &cacheRecord{
|
2017-12-26 19:42:14 +00:00
|
|
|
mu: sr.mu,
|
2017-07-14 18:59:31 +00:00
|
|
|
cm: sr.cm,
|
2020-05-28 20:46:33 +00:00
|
|
|
parent: sr.parentRef(false, sr.descHandlers),
|
2017-07-14 18:59:31 +00:00
|
|
|
equalMutable: sr,
|
2018-09-14 20:35:41 +00:00
|
|
|
refs: make(map[ref]struct{}),
|
2017-08-10 01:20:33 +00:00
|
|
|
md: md,
|
2017-07-14 18:59:31 +00:00
|
|
|
}
|
2017-06-01 22:24:23 +00:00
|
|
|
|
2017-12-08 18:53:17 +00:00
|
|
|
if descr := GetDescription(sr.md); descr != "" {
|
2017-08-10 01:20:33 +00:00
|
|
|
if err := queueDescription(md, descr); err != nil {
|
2017-07-25 19:11:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 21:43:16 +00:00
|
|
|
parentID := ""
|
|
|
|
if rec.parent != nil {
|
|
|
|
parentID = rec.parent.ID()
|
|
|
|
}
|
|
|
|
if err := initializeMetadata(rec, parentID); err != nil {
|
2017-07-25 19:11:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
sr.cm.records[id] = rec
|
2017-06-01 22:24:23 +00:00
|
|
|
|
2017-07-03 23:08:20 +00:00
|
|
|
if err := sr.md.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2019-07-22 21:43:16 +00:00
|
|
|
queueCommitted(md)
|
2017-08-10 01:20:33 +00:00
|
|
|
setSize(md, sizeUnknown)
|
|
|
|
setEqualMutable(md, sr.ID())
|
2017-07-14 18:59:31 +00:00
|
|
|
if err := md.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
ref := rec.ref(true, sr.descHandlers)
|
2017-07-14 18:59:31 +00:00
|
|
|
sr.equalImmutable = ref
|
|
|
|
return ref, nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
func (sr *mutableRef) Mount(ctx context.Context, readonly bool, s session.Group) (snapshot.Mountable, error) {
|
2020-05-28 20:46:33 +00:00
|
|
|
sr.mu.Lock()
|
|
|
|
defer sr.mu.Unlock()
|
|
|
|
|
|
|
|
return sr.mount(ctx, readonly)
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
func (sr *mutableRef) Commit(ctx context.Context) (ImmutableRef, error) {
|
2017-05-27 06:12:13 +00:00
|
|
|
sr.cm.mu.Lock()
|
|
|
|
defer sr.cm.mu.Unlock()
|
|
|
|
|
|
|
|
sr.mu.Lock()
|
2017-07-14 18:59:31 +00:00
|
|
|
defer sr.mu.Unlock()
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
return sr.commit(ctx)
|
|
|
|
}
|
2017-07-03 23:08:20 +00:00
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
func (sr *mutableRef) Release(ctx context.Context) error {
|
|
|
|
sr.cm.mu.Lock()
|
|
|
|
defer sr.cm.mu.Unlock()
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
sr.mu.Lock()
|
|
|
|
defer sr.mu.Unlock()
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
return sr.release(ctx)
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
func (sr *mutableRef) release(ctx context.Context) error {
|
|
|
|
delete(sr.refs, sr)
|
2017-07-20 22:55:24 +00:00
|
|
|
if getCachePolicy(sr.md) != cachePolicyRetain {
|
2017-07-19 23:39:32 +00:00
|
|
|
if sr.equalImmutable != nil {
|
2017-07-20 22:55:24 +00:00
|
|
|
if getCachePolicy(sr.equalImmutable.md) == cachePolicyRetain {
|
2018-09-14 20:35:41 +00:00
|
|
|
if sr.updateLastUsed() {
|
|
|
|
updateLastUsed(sr.md)
|
|
|
|
sr.triggerLastUsed = false
|
|
|
|
}
|
2017-07-20 22:55:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-07-19 23:39:32 +00:00
|
|
|
if err := sr.equalImmutable.remove(ctx, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sr.remove(ctx, true)
|
2020-07-18 16:11:39 +00:00
|
|
|
}
|
|
|
|
if sr.updateLastUsed() {
|
|
|
|
updateLastUsed(sr.md)
|
|
|
|
sr.triggerLastUsed = false
|
2017-07-19 23:39:32 +00:00
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
return nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
2017-07-18 06:08:22 +00:00
|
|
|
|
2018-04-16 22:23:10 +00:00
|
|
|
func setReadonly(mounts snapshot.Mountable) snapshot.Mountable {
|
|
|
|
return &readOnlyMounter{mounts}
|
|
|
|
}
|
|
|
|
|
|
|
|
type readOnlyMounter struct {
|
|
|
|
snapshot.Mountable
|
|
|
|
}
|
|
|
|
|
2019-08-15 22:42:03 +00:00
|
|
|
func (m *readOnlyMounter) Mount() ([]mount.Mount, func() error, error) {
|
|
|
|
mounts, release, err := m.Mountable.Mount()
|
2018-04-16 22:23:10 +00:00
|
|
|
if err != nil {
|
2019-08-15 22:42:03 +00:00
|
|
|
return nil, nil, err
|
2018-04-16 22:23:10 +00:00
|
|
|
}
|
2017-07-18 06:08:22 +00:00
|
|
|
for i, m := range mounts {
|
2019-07-29 18:00:10 +00:00
|
|
|
if m.Type == "overlay" {
|
|
|
|
mounts[i].Options = readonlyOverlay(m.Options)
|
|
|
|
continue
|
|
|
|
}
|
2017-07-18 06:08:22 +00:00
|
|
|
opts := make([]string, 0, len(m.Options))
|
|
|
|
for _, opt := range m.Options {
|
|
|
|
if opt != "rw" {
|
|
|
|
opts = append(opts, opt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
opts = append(opts, "ro")
|
|
|
|
mounts[i].Options = opts
|
|
|
|
}
|
2019-08-15 22:42:03 +00:00
|
|
|
return mounts, release, nil
|
2017-07-18 06:08:22 +00:00
|
|
|
}
|
2019-07-29 18:00:10 +00:00
|
|
|
|
|
|
|
func readonlyOverlay(opt []string) []string {
|
|
|
|
out := make([]string, 0, len(opt))
|
|
|
|
upper := ""
|
|
|
|
for _, o := range opt {
|
|
|
|
if strings.HasPrefix(o, "upperdir=") {
|
|
|
|
upper = strings.TrimPrefix(o, "upperdir=")
|
|
|
|
} else if !strings.HasPrefix(o, "workdir=") {
|
|
|
|
out = append(out, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if upper != "" {
|
|
|
|
for i, o := range out {
|
|
|
|
if strings.HasPrefix(o, "lowerdir=") {
|
|
|
|
out[i] = "lowerdir=" + upper + ":" + strings.TrimPrefix(o, "lowerdir=")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|