1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
package snapshot
import (
"context"
"os"
"strings"
"sync"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/pkg/userns"
"github.com/containerd/containerd/snapshots"
"github.com/docker/docker/pkg/idtools"
"github.com/moby/buildkit/executor"
"github.com/pkg/errors"
)
type Mountable = executor.MountableRef
// Snapshotter defines interface that any snapshot implementation should satisfy
type Snapshotter interface {
Name() string
Mounts(ctx context.Context, key string) (Mountable, error)
Prepare(ctx context.Context, key, parent string, opts ...snapshots.Opt) error
View(ctx context.Context, key, parent string, opts ...snapshots.Opt) (Mountable, error)
Stat(ctx context.Context, key string) (snapshots.Info, error)
Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (snapshots.Info, error)
Usage(ctx context.Context, key string) (snapshots.Usage, error)
Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error
Remove(ctx context.Context, key string) error
Walk(ctx context.Context, fn snapshots.WalkFunc, filters ...string) error
Close() error
IdentityMapping() *idtools.IdentityMapping
}
func FromContainerdSnapshotter(name string, s snapshots.Snapshotter, idmap *idtools.IdentityMapping) Snapshotter {
return &fromContainerd{name: name, Snapshotter: s, idmap: idmap}
}
type fromContainerd struct {
name string
snapshots.Snapshotter
idmap *idtools.IdentityMapping
}
func (s *fromContainerd) Name() string {
return s.name
}
func (s *fromContainerd) Mounts(ctx context.Context, key string) (Mountable, error) {
mounts, err := s.Snapshotter.Mounts(ctx, key)
if err != nil {
return nil, err
}
return &staticMountable{mounts: mounts, idmap: s.idmap, id: key}, nil
}
func (s *fromContainerd) Prepare(ctx context.Context, key, parent string, opts ...snapshots.Opt) error {
_, err := s.Snapshotter.Prepare(ctx, key, parent, opts...)
return err
}
func (s *fromContainerd) View(ctx context.Context, key, parent string, opts ...snapshots.Opt) (Mountable, error) {
mounts, err := s.Snapshotter.View(ctx, key, parent, opts...)
if err != nil {
return nil, err
}
return &staticMountable{mounts: mounts, idmap: s.idmap, id: key}, nil
}
func (s *fromContainerd) IdentityMapping() *idtools.IdentityMapping {
return s.idmap
}
func (s *fromContainerd) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error {
info, err := s.Stat(ctx, key)
if err != nil {
return errors.Wrap(err, "failed to stat active key during commit")
}
opts = append(opts, snapshots.WithLabels(snapshots.FilterInheritedLabels(info.Labels)))
return s.Snapshotter.Commit(ctx, name, key, opts...)
}
// NewContainerdSnapshotter converts snapshotter to containerd snapshotter
func NewContainerdSnapshotter(s Snapshotter) (snapshots.Snapshotter, func() error) {
cs := &containerdSnapshotter{Snapshotter: s}
return cs, cs.release
}
type containerdSnapshotter struct {
mu sync.Mutex
releasers []func() error
Snapshotter
}
func (cs *containerdSnapshotter) release() error {
cs.mu.Lock()
defer cs.mu.Unlock()
var err error
for _, f := range cs.releasers {
if err1 := f(); err1 != nil && err == nil {
err = err1
}
}
return err
}
func (cs *containerdSnapshotter) returnMounts(mf Mountable) ([]mount.Mount, error) {
mounts, release, err := mf.Mount()
if err != nil {
return nil, err
}
cs.mu.Lock()
cs.releasers = append(cs.releasers, release)
cs.mu.Unlock()
redirectDirOption := getRedirectDirOption()
if redirectDirOption != "" {
mounts = setRedirectDir(mounts, redirectDirOption)
}
return mounts, nil
}
func (cs *containerdSnapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, error) {
mf, err := cs.Snapshotter.Mounts(ctx, key)
if err != nil {
return nil, err
}
return cs.returnMounts(mf)
}
func (cs *containerdSnapshotter) Prepare(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
if err := cs.Snapshotter.Prepare(ctx, key, parent, opts...); err != nil {
return nil, err
}
return cs.Mounts(ctx, key)
}
func (cs *containerdSnapshotter) View(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
mf, err := cs.Snapshotter.View(ctx, key, parent, opts...)
if err != nil {
return nil, err
}
return cs.returnMounts(mf)
}
var redirectDirOption string
var redirectDirOptionOnce sync.Once
func getRedirectDirOption() string {
redirectDirOptionOnce.Do(func() {
if _, err := os.Stat("/sys/module/overlay/parameters/redirect_dir"); err != nil {
redirectDirOption = "" // redirect_dir unsupported
return
}
if userns.RunningInUserNS() {
// userxattr (kernel >= 5.11) disables redirect_dir and doesn't allow specifying "off".
redirectDirOption = ""
return
}
redirectDirOption = "off" // disable redirect_dir to avoid broken diff
})
return redirectDirOption
}
func setRedirectDir(mounts []mount.Mount, redirectDirOption string) (ret []mount.Mount) {
if redirectDirOption == "" {
return mounts
}
for _, m := range mounts {
// Replace redirect_dir options, but only for overlay.
// redirect_dir is not supported by fuse-overlayfs.
if m.Type == "overlay" {
var opts []string
for _, o := range m.Options {
if strings.HasPrefix(o, "redirect_dir=") {
continue
}
opts = append(opts, o)
}
m.Options = append(opts, "redirect_dir="+redirectDirOption)
}
ret = append(ret, m)
}
return ret
}
|