2017-01-13 23:31:21 +00:00
|
|
|
package overlay
|
2016-12-09 19:27:30 +00:00
|
|
|
|
|
|
|
import (
|
2017-02-24 23:23:23 +00:00
|
|
|
"context"
|
2016-12-09 19:27:30 +00:00
|
|
|
"fmt"
|
2017-02-03 02:55:53 +00:00
|
|
|
"io/ioutil"
|
2016-12-09 19:27:30 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2017-04-03 20:14:15 +00:00
|
|
|
"github.com/containerd/containerd"
|
|
|
|
"github.com/containerd/containerd/log"
|
|
|
|
"github.com/containerd/containerd/plugin"
|
|
|
|
"github.com/containerd/containerd/snapshot"
|
|
|
|
"github.com/containerd/containerd/snapshot/storage"
|
2017-02-07 23:51:37 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-12-09 19:27:30 +00:00
|
|
|
)
|
|
|
|
|
2017-03-07 22:55:36 +00:00
|
|
|
func init() {
|
|
|
|
plugin.Register("snapshot-overlay", &plugin.Registration{
|
|
|
|
Type: plugin.SnapshotPlugin,
|
|
|
|
Init: func(ic *plugin.InitContext) (interface{}, error) {
|
2017-03-25 00:16:41 +00:00
|
|
|
return NewSnapshotter(filepath.Join(ic.Root, "snapshot", "overlay"))
|
2017-03-07 22:55:36 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:21:03 +00:00
|
|
|
type snapshotter struct {
|
2017-03-16 06:25:53 +00:00
|
|
|
root string
|
2017-03-25 00:16:41 +00:00
|
|
|
ms *storage.MetaStore
|
2017-02-03 22:20:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 06:25:53 +00:00
|
|
|
type activeSnapshot struct {
|
|
|
|
id string
|
|
|
|
name string
|
|
|
|
parentID interface{}
|
|
|
|
readonly bool
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:21:03 +00:00
|
|
|
// NewSnapshotter returns a Snapshotter which uses overlayfs. The overlayfs
|
|
|
|
// diffs are stored under the provided root. A metadata file is stored under
|
|
|
|
// the root.
|
2017-03-25 00:16:41 +00:00
|
|
|
func NewSnapshotter(root string) (snapshot.Snapshotter, error) {
|
2016-12-09 19:27:30 +00:00
|
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-25 00:16:41 +00:00
|
|
|
ms, err := storage.NewMetaStore(filepath.Join(root, "metadata.db"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-05 04:52:46 +00:00
|
|
|
if err := os.MkdirAll(filepath.Join(root, "snapshots"), 0700); err != nil {
|
2017-03-16 06:25:53 +00:00
|
|
|
return nil, err
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
2017-03-16 06:25:53 +00:00
|
|
|
|
2017-03-25 00:21:03 +00:00
|
|
|
return &snapshotter{
|
2017-03-16 06:25:53 +00:00
|
|
|
root: root,
|
|
|
|
ms: ms,
|
2016-12-09 19:27:30 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
// Stat returns the info for an active or committed snapshot by name or
|
|
|
|
// key.
|
|
|
|
//
|
|
|
|
// Should be used for parent resolution, existence checks and to discern
|
|
|
|
// the kind of snapshot.
|
2017-03-25 00:21:03 +00:00
|
|
|
func (o *snapshotter) Stat(ctx context.Context, key string) (snapshot.Info, error) {
|
2017-03-16 06:25:53 +00:00
|
|
|
ctx, t, err := o.ms.TransactionContext(ctx, false)
|
2017-02-07 23:51:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return snapshot.Info{}, err
|
|
|
|
}
|
2017-03-16 06:25:53 +00:00
|
|
|
defer t.Rollback()
|
2017-03-25 00:16:41 +00:00
|
|
|
return storage.GetInfo(ctx, key)
|
2017-02-07 23:51:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 00:21:03 +00:00
|
|
|
func (o *snapshotter) Prepare(ctx context.Context, key, parent string) ([]containerd.Mount, error) {
|
2017-03-16 06:25:53 +00:00
|
|
|
return o.createActive(ctx, key, parent, false)
|
2017-02-03 02:55:53 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 00:21:03 +00:00
|
|
|
func (o *snapshotter) View(ctx context.Context, key, parent string) ([]containerd.Mount, error) {
|
2017-03-16 06:25:53 +00:00
|
|
|
return o.createActive(ctx, key, parent, true)
|
2017-02-03 02:55:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mounts returns the mounts for the transaction identified by key. Can be
|
|
|
|
// called on an read-write or readonly transaction.
|
|
|
|
//
|
|
|
|
// This can be used to recover mounts after calling View or Prepare.
|
2017-03-25 00:21:03 +00:00
|
|
|
func (o *snapshotter) Mounts(ctx context.Context, key string) ([]containerd.Mount, error) {
|
2017-03-16 06:25:53 +00:00
|
|
|
ctx, t, err := o.ms.TransactionContext(ctx, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-25 00:16:41 +00:00
|
|
|
active, err := storage.GetActive(ctx, key)
|
2017-03-16 06:25:53 +00:00
|
|
|
t.Rollback()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to get active mount")
|
|
|
|
}
|
|
|
|
return o.mounts(active), nil
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 00:21:03 +00:00
|
|
|
func (o *snapshotter) Commit(ctx context.Context, name, key string) error {
|
2017-03-16 06:25:53 +00:00
|
|
|
ctx, t, err := o.ms.TransactionContext(ctx, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-25 00:16:41 +00:00
|
|
|
if _, err := storage.CommitActive(ctx, key, name); err != nil {
|
2017-03-16 06:25:53 +00:00
|
|
|
if rerr := t.Rollback(); rerr != nil {
|
|
|
|
log.G(ctx).WithError(rerr).Warn("Failure rolling back transaction")
|
|
|
|
}
|
|
|
|
return errors.Wrap(err, "failed to commit snapshot")
|
|
|
|
}
|
|
|
|
return t.Commit()
|
2017-02-03 02:55:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove abandons the transaction identified by key. All resources
|
|
|
|
// associated with the key will be removed.
|
2017-03-25 00:21:03 +00:00
|
|
|
func (o *snapshotter) Remove(ctx context.Context, key string) (err error) {
|
2017-03-16 06:25:53 +00:00
|
|
|
ctx, t, err := o.ms.TransactionContext(ctx, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil && t != nil {
|
|
|
|
if rerr := t.Rollback(); rerr != nil {
|
|
|
|
log.G(ctx).WithError(rerr).Warn("Failure rolling back transaction")
|
|
|
|
}
|
2017-02-07 23:51:37 +00:00
|
|
|
}
|
2017-03-16 06:25:53 +00:00
|
|
|
}()
|
2017-02-03 02:55:53 +00:00
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
id, _, err := storage.Remove(ctx, key)
|
2017-03-16 06:25:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to remove")
|
|
|
|
}
|
2017-02-03 02:55:53 +00:00
|
|
|
|
2017-03-16 06:25:53 +00:00
|
|
|
path := filepath.Join(o.root, "snapshots", id)
|
|
|
|
renamed := filepath.Join(o.root, "snapshots", "rm-"+id)
|
|
|
|
if err := os.Rename(path, renamed); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to rename")
|
|
|
|
}
|
2017-02-03 02:55:53 +00:00
|
|
|
|
2017-03-16 06:25:53 +00:00
|
|
|
err = t.Commit()
|
|
|
|
t = nil
|
|
|
|
if err != nil {
|
|
|
|
if err1 := os.Rename(renamed, path); err1 != nil {
|
|
|
|
// May cause inconsistent data on disk
|
|
|
|
log.G(ctx).WithError(err1).WithField("path", renamed).Errorf("Failed to rename after failed commit")
|
2017-02-07 23:51:37 +00:00
|
|
|
}
|
2017-03-16 06:25:53 +00:00
|
|
|
return errors.Wrap(err, "failed to commit")
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(renamed); err != nil {
|
|
|
|
// Must be cleaned up, any "rm-*" could be removed if no active transactions
|
|
|
|
log.G(ctx).WithError(err).WithField("path", renamed).Warnf("Failed to remove root filesystem")
|
|
|
|
}
|
2017-02-03 02:55:53 +00:00
|
|
|
|
2017-03-16 06:25:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-02-03 02:55:53 +00:00
|
|
|
|
2017-03-16 06:25:53 +00:00
|
|
|
// Walk the committed snapshots.
|
2017-03-25 00:21:03 +00:00
|
|
|
func (o *snapshotter) Walk(ctx context.Context, fn func(context.Context, snapshot.Info) error) error {
|
2017-03-16 06:25:53 +00:00
|
|
|
ctx, t, err := o.ms.TransactionContext(ctx, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer t.Rollback()
|
2017-03-25 00:16:41 +00:00
|
|
|
return storage.WalkInfo(ctx, fn)
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 00:21:03 +00:00
|
|
|
func (o *snapshotter) createActive(ctx context.Context, key, parent string, readonly bool) ([]containerd.Mount, error) {
|
2016-12-09 19:27:30 +00:00
|
|
|
var (
|
2017-03-16 06:25:53 +00:00
|
|
|
path string
|
|
|
|
snapshotDir = filepath.Join(o.root, "snapshots")
|
2016-12-09 19:27:30 +00:00
|
|
|
)
|
2017-03-16 06:25:53 +00:00
|
|
|
|
|
|
|
td, err := ioutil.TempDir(snapshotDir, "new-")
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create temp dir")
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
2017-03-16 06:25:53 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if td != "" {
|
|
|
|
if err1 := os.RemoveAll(td); err1 != nil {
|
|
|
|
err = errors.Wrapf(err, "remove failed: %v", err1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if path != "" {
|
|
|
|
if err1 := os.RemoveAll(path); err1 != nil {
|
|
|
|
err = errors.Wrapf(err, "failed to remove path: %v", err1)
|
|
|
|
}
|
2017-02-18 00:34:21 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-16 06:25:53 +00:00
|
|
|
}()
|
2017-02-07 23:51:37 +00:00
|
|
|
|
2017-03-24 19:47:52 +00:00
|
|
|
if err = os.MkdirAll(filepath.Join(td, "fs"), 0711); err != nil {
|
2017-02-07 23:51:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-16 06:25:53 +00:00
|
|
|
if !readonly {
|
|
|
|
if err = os.MkdirAll(filepath.Join(td, "work"), 0700); err != nil {
|
|
|
|
return nil, err
|
2017-02-18 02:43:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 06:25:53 +00:00
|
|
|
ctx, t, err := o.ms.TransactionContext(ctx, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
2017-02-03 02:55:53 +00:00
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
active, err := storage.CreateActive(ctx, key, parent, readonly)
|
2017-03-16 06:25:53 +00:00
|
|
|
if err != nil {
|
|
|
|
if rerr := t.Rollback(); rerr != nil {
|
2017-03-16 17:05:46 +00:00
|
|
|
log.G(ctx).WithError(rerr).Warn("Failure rolling back transaction")
|
2017-03-16 06:25:53 +00:00
|
|
|
}
|
|
|
|
return nil, errors.Wrap(err, "failed to create active")
|
2017-02-03 02:55:53 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 06:25:53 +00:00
|
|
|
path = filepath.Join(snapshotDir, active.ID)
|
2017-03-25 00:16:41 +00:00
|
|
|
if err = os.Rename(td, path); err != nil {
|
2017-03-16 06:25:53 +00:00
|
|
|
if rerr := t.Rollback(); rerr != nil {
|
2017-03-16 17:05:46 +00:00
|
|
|
log.G(ctx).WithError(rerr).Warn("Failure rolling back transaction")
|
2017-03-16 06:25:53 +00:00
|
|
|
}
|
|
|
|
return nil, errors.Wrap(err, "failed to rename")
|
2017-02-07 23:51:37 +00:00
|
|
|
}
|
2017-03-16 06:25:53 +00:00
|
|
|
td = ""
|
2017-02-07 23:51:37 +00:00
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
if err = t.Commit(); err != nil {
|
2017-03-16 06:25:53 +00:00
|
|
|
return nil, errors.Wrap(err, "commit failed")
|
2017-02-07 23:51:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 06:25:53 +00:00
|
|
|
return o.mounts(active), nil
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 00:21:03 +00:00
|
|
|
func (o *snapshotter) mounts(active storage.Active) []containerd.Mount {
|
2017-03-16 06:25:53 +00:00
|
|
|
if len(active.ParentIDs) == 0 {
|
2016-12-09 19:27:30 +00:00
|
|
|
// if we only have one layer/no parents then just return a bind mount as overlay
|
2017-02-27 21:39:52 +00:00
|
|
|
// will not work
|
|
|
|
roFlag := "rw"
|
2017-03-16 06:25:53 +00:00
|
|
|
if active.Readonly {
|
2017-02-27 21:39:52 +00:00
|
|
|
roFlag = "ro"
|
|
|
|
}
|
|
|
|
|
2016-12-09 19:27:30 +00:00
|
|
|
return []containerd.Mount{
|
|
|
|
{
|
2017-03-16 06:25:53 +00:00
|
|
|
Source: o.upperPath(active.ID),
|
2016-12-09 19:27:30 +00:00
|
|
|
Type: "bind",
|
|
|
|
Options: []string{
|
2017-02-27 21:39:52 +00:00
|
|
|
roFlag,
|
2016-12-09 19:27:30 +00:00
|
|
|
"rbind",
|
|
|
|
},
|
|
|
|
},
|
2017-03-16 06:25:53 +00:00
|
|
|
}
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
2017-02-18 00:34:21 +00:00
|
|
|
var options []string
|
|
|
|
|
2017-03-16 06:25:53 +00:00
|
|
|
if !active.Readonly {
|
2017-02-18 00:34:21 +00:00
|
|
|
options = append(options,
|
2017-03-16 06:25:53 +00:00
|
|
|
fmt.Sprintf("workdir=%s", o.workPath(active.ID)),
|
|
|
|
fmt.Sprintf("upperdir=%s", o.upperPath(active.ID)),
|
2017-02-18 00:34:21 +00:00
|
|
|
)
|
2017-03-16 06:25:53 +00:00
|
|
|
} else if len(active.ParentIDs) == 1 {
|
2017-02-27 21:21:20 +00:00
|
|
|
return []containerd.Mount{
|
|
|
|
{
|
2017-03-16 06:25:53 +00:00
|
|
|
Source: o.upperPath(active.ParentIDs[0]),
|
2017-02-27 21:21:20 +00:00
|
|
|
Type: "bind",
|
|
|
|
Options: []string{
|
|
|
|
"ro",
|
|
|
|
"rbind",
|
|
|
|
},
|
|
|
|
},
|
2017-03-16 06:25:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parentPaths := make([]string, len(active.ParentIDs))
|
|
|
|
for i := range active.ParentIDs {
|
|
|
|
parentPaths[i] = o.upperPath(active.ParentIDs[i])
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
2017-02-18 00:34:21 +00:00
|
|
|
|
2017-03-16 06:25:53 +00:00
|
|
|
options = append(options, fmt.Sprintf("lowerdir=%s", strings.Join(parentPaths, ":")))
|
2016-12-09 19:27:30 +00:00
|
|
|
return []containerd.Mount{
|
|
|
|
{
|
|
|
|
Type: "overlay",
|
|
|
|
Source: "overlay",
|
|
|
|
Options: options,
|
|
|
|
},
|
2016-12-12 21:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:21:03 +00:00
|
|
|
func (o *snapshotter) upperPath(id string) string {
|
2017-03-16 06:25:53 +00:00
|
|
|
return filepath.Join(o.root, "snapshots", id, "fs")
|
2016-12-12 21:35:02 +00:00
|
|
|
}
|
2017-02-03 02:55:53 +00:00
|
|
|
|
2017-03-25 00:21:03 +00:00
|
|
|
func (o *snapshotter) workPath(id string) string {
|
2017-03-16 06:25:53 +00:00
|
|
|
return filepath.Join(o.root, "snapshots", id, "work")
|
2017-02-03 02:55:53 +00:00
|
|
|
}
|