2017-01-13 23:31:21 +00:00
|
|
|
package btrfs
|
2016-12-13 05:53:48 +00:00
|
|
|
|
|
|
|
import (
|
2017-02-24 23:23:23 +00:00
|
|
|
"context"
|
2016-12-13 05:53:48 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-03-22 17:58:45 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-12-13 05:53:48 +00:00
|
|
|
"github.com/docker/containerd"
|
2017-03-16 17:05:46 +00:00
|
|
|
"github.com/docker/containerd/log"
|
2017-03-07 22:55:36 +00:00
|
|
|
"github.com/docker/containerd/plugin"
|
2017-02-08 01:21:19 +00:00
|
|
|
"github.com/docker/containerd/snapshot"
|
2017-03-16 17:05:46 +00:00
|
|
|
"github.com/docker/containerd/snapshot/storage"
|
|
|
|
"github.com/docker/containerd/snapshot/storage/boltdb"
|
2017-02-03 22:20:05 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-12-13 05:53:48 +00:00
|
|
|
"github.com/stevvooe/go-btrfs"
|
|
|
|
)
|
|
|
|
|
2017-03-07 22:55:36 +00:00
|
|
|
type btrfsConfig struct {
|
|
|
|
Device string `toml:"device"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
plugin.Register("snapshot-btrfs", &plugin.Registration{
|
|
|
|
Type: plugin.SnapshotPlugin,
|
|
|
|
Config: &btrfsConfig{},
|
|
|
|
Init: func(ic *plugin.InitContext) (interface{}, error) {
|
2017-03-16 17:05:46 +00:00
|
|
|
root := filepath.Join(ic.Root, "snapshot", "btrfs")
|
2017-03-07 22:55:36 +00:00
|
|
|
conf := ic.Config.(*btrfsConfig)
|
|
|
|
if conf.Device == "" {
|
2017-03-16 17:05:46 +00:00
|
|
|
// TODO: check device for root
|
2017-03-07 22:55:36 +00:00
|
|
|
return nil, errors.Errorf("btrfs requires \"device\" configuration")
|
|
|
|
}
|
2017-03-16 17:05:46 +00:00
|
|
|
|
|
|
|
ms, err := boltdb.NewMetaStore(ic.Context, filepath.Join(root, "metadata.db"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewSnapshotter(conf.Device, root, ms)
|
2017-03-07 22:55:36 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
type Snapshotter struct {
|
2016-12-13 05:53:48 +00:00
|
|
|
device string // maybe we can resolve it with path?
|
|
|
|
root string // root provides paths for internal storage.
|
2017-03-16 17:05:46 +00:00
|
|
|
ms storage.MetaStore
|
2016-12-13 05:53:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
func NewSnapshotter(device, root string, ms storage.MetaStore) (snapshot.Snapshotter, error) {
|
2017-02-08 01:21:19 +00:00
|
|
|
var (
|
|
|
|
active = filepath.Join(root, "active")
|
|
|
|
snapshots = filepath.Join(root, "snapshots")
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, path := range []string{
|
|
|
|
active,
|
|
|
|
snapshots,
|
|
|
|
} {
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
return &Snapshotter{
|
|
|
|
device: device,
|
|
|
|
root: root,
|
|
|
|
ms: ms,
|
|
|
|
}, nil
|
2017-02-08 01:21:19 +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-02-24 23:23:23 +00:00
|
|
|
func (b *Snapshotter) Stat(ctx context.Context, key string) (snapshot.Info, error) {
|
2017-03-16 17:05:46 +00:00
|
|
|
ctx, t, err := b.ms.TransactionContext(ctx, false)
|
2017-02-08 01:21:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return snapshot.Info{}, err
|
|
|
|
}
|
2017-03-16 17:05:46 +00:00
|
|
|
defer t.Rollback()
|
|
|
|
return b.ms.Stat(ctx, key)
|
|
|
|
}
|
2017-02-08 01:21:19 +00:00
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
// Walk the committed snapshots.
|
|
|
|
func (b *Snapshotter) Walk(ctx context.Context, fn func(context.Context, snapshot.Info) error) error {
|
|
|
|
ctx, t, err := b.ms.TransactionContext(ctx, false)
|
2017-02-08 01:21:19 +00:00
|
|
|
if err != nil {
|
2017-03-16 17:05:46 +00:00
|
|
|
return err
|
2017-02-08 01:21:19 +00:00
|
|
|
}
|
2017-03-16 17:05:46 +00:00
|
|
|
defer t.Rollback()
|
|
|
|
return b.ms.Walk(ctx, fn)
|
2016-12-13 05:53:48 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 23:23:23 +00:00
|
|
|
func (b *Snapshotter) Prepare(ctx context.Context, key, parent string) ([]containerd.Mount, error) {
|
2017-03-16 17:05:46 +00:00
|
|
|
return b.makeActive(ctx, key, parent, false)
|
2017-02-03 22:20:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 23:23:23 +00:00
|
|
|
func (b *Snapshotter) View(ctx context.Context, key, parent string) ([]containerd.Mount, error) {
|
2017-03-16 17:05:46 +00:00
|
|
|
return b.makeActive(ctx, key, parent, true)
|
2017-02-03 22:20:05 +00:00
|
|
|
}
|
2016-12-13 05:53:48 +00:00
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
func (b *Snapshotter) makeActive(ctx context.Context, key, parent string, readonly bool) ([]containerd.Mount, error) {
|
|
|
|
ctx, t, err := b.ms.TransactionContext(ctx, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil && t != nil {
|
|
|
|
if rerr := t.Rollback(); rerr != nil {
|
|
|
|
log.G(ctx).WithError(rerr).Warn("Failure rolling back transaction")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
a, err := b.ms.CreateActive(ctx, key, parent, readonly)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-03 22:20:05 +00:00
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
target := filepath.Join(b.root, "active", a.ID)
|
|
|
|
|
|
|
|
if len(a.ParentIDs) == 0 {
|
2016-12-13 05:53:48 +00:00
|
|
|
// create new subvolume
|
|
|
|
// btrfs subvolume create /dir
|
2017-03-16 17:05:46 +00:00
|
|
|
if err = btrfs.SubvolCreate(target); err != nil {
|
2016-12-13 05:53:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-16 17:05:46 +00:00
|
|
|
parentp := filepath.Join(b.root, "snapshots", a.ParentIDs[0])
|
2016-12-13 05:53:48 +00:00
|
|
|
// btrfs subvolume snapshot /parent /subvol
|
2017-03-16 17:05:46 +00:00
|
|
|
if err = btrfs.SubvolSnapshot(target, parentp, a.Readonly); err != nil {
|
2016-12-13 05:53:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-03-16 17:05:46 +00:00
|
|
|
err = t.Commit()
|
|
|
|
t = nil
|
|
|
|
if err != nil {
|
2017-03-22 17:58:45 +00:00
|
|
|
if derr := btrfs.SubvolDelete(target); derr != nil {
|
|
|
|
log.G(ctx).WithError(derr).WithField("subvolume", target).Error("Failed to delete subvolume")
|
|
|
|
}
|
2017-02-08 01:21:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.mounts(target)
|
2017-02-03 22:20:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
func (b *Snapshotter) mounts(dir string) ([]containerd.Mount, error) {
|
2017-02-03 22:20:05 +00:00
|
|
|
var options []string
|
|
|
|
|
2016-12-13 05:53:48 +00:00
|
|
|
// get the subvolume id back out for the mount
|
|
|
|
info, err := btrfs.SubvolInfo(dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-03 22:20:05 +00:00
|
|
|
options = append(options, fmt.Sprintf("subvolid=%d", info.ID))
|
|
|
|
|
|
|
|
if info.Readonly {
|
|
|
|
options = append(options, "ro")
|
|
|
|
}
|
|
|
|
|
2016-12-13 05:53:48 +00:00
|
|
|
return []containerd.Mount{
|
|
|
|
{
|
|
|
|
Type: "btrfs",
|
2017-01-17 08:42:27 +00:00
|
|
|
Source: b.device, // device?
|
2016-12-13 05:53:48 +00:00
|
|
|
// NOTE(stevvooe): While it would be nice to use to uuids for
|
|
|
|
// mounts, they don't work reliably if the uuids are missing.
|
2017-02-03 22:20:05 +00:00
|
|
|
Options: options,
|
2016-12-13 05:53:48 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
func (b *Snapshotter) Commit(ctx context.Context, name, key string) (err error) {
|
|
|
|
ctx, t, err := b.ms.TransactionContext(ctx, true)
|
2017-02-03 22:20:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-16 17:05:46 +00:00
|
|
|
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-03 22:20:05 +00:00
|
|
|
}
|
2017-03-16 17:05:46 +00:00
|
|
|
}()
|
2017-02-03 22:20:05 +00:00
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
id, err := b.ms.Commit(ctx, key, name)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to commit")
|
2017-02-03 22:20:05 +00:00
|
|
|
}
|
2016-12-13 05:53:48 +00:00
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
source := filepath.Join(b.root, "active", id)
|
|
|
|
target := filepath.Join(b.root, "snapshots", id)
|
2016-12-13 05:53:48 +00:00
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
if err := btrfs.SubvolSnapshot(target, source, true); err != nil {
|
2017-02-08 01:21:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
err = t.Commit()
|
|
|
|
t = nil
|
|
|
|
if err != nil {
|
|
|
|
if derr := btrfs.SubvolDelete(target); derr != nil {
|
2017-03-22 17:58:45 +00:00
|
|
|
log.G(ctx).WithError(derr).WithField("subvolume", target).Error("Failed to delete subvolume")
|
2017-02-03 22:20:05 +00:00
|
|
|
}
|
2017-02-08 01:21:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
if derr := btrfs.SubvolDelete(source); derr != nil {
|
2017-03-22 17:58:45 +00:00
|
|
|
// Log as warning, only needed for cleanup, will not cause name collision
|
|
|
|
log.G(ctx).WithError(derr).WithField("subvolume", source).Warn("Failed to delete subvolume")
|
2017-02-03 22:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-02-24 23:23:23 +00:00
|
|
|
func (b *Snapshotter) Mounts(ctx context.Context, key string) ([]containerd.Mount, error) {
|
2017-03-16 17:05:46 +00:00
|
|
|
ctx, t, err := b.ms.TransactionContext(ctx, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
a, err := b.ms.GetActive(ctx, key)
|
|
|
|
t.Rollback()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to get active snapshot")
|
|
|
|
}
|
|
|
|
dir := filepath.Join(b.root, "active", a.ID)
|
2017-02-03 22:20:05 +00:00
|
|
|
return b.mounts(dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove abandons the transaction identified by key. All resources
|
|
|
|
// associated with the key will be removed.
|
2017-03-16 17:05:46 +00:00
|
|
|
func (b *Snapshotter) Remove(ctx context.Context, key string) (err error) {
|
|
|
|
var (
|
|
|
|
source, removed string
|
|
|
|
readonly bool
|
|
|
|
)
|
2017-02-03 22:20:05 +00:00
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
ctx, t, err := b.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-03 22:20:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
if removed != "" {
|
|
|
|
if derr := btrfs.SubvolDelete(removed); derr != nil {
|
2017-03-22 17:58:45 +00:00
|
|
|
log.G(ctx).WithError(derr).WithField("subvolume", removed).Warn("Failed to delete subvolume")
|
2017-03-16 17:05:46 +00:00
|
|
|
}
|
2017-02-03 22:20:05 +00:00
|
|
|
}
|
2017-03-16 17:05:46 +00:00
|
|
|
}()
|
2017-02-03 22:20:05 +00:00
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
id, k, err := b.ms.Remove(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to remove snapshot")
|
|
|
|
}
|
2017-02-03 22:20:05 +00:00
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
if k == snapshot.KindActive {
|
|
|
|
source = filepath.Join(b.root, "active", id)
|
2017-02-03 22:20:05 +00:00
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
info, err := btrfs.SubvolInfo(source)
|
2017-02-08 01:21:19 +00:00
|
|
|
if err != nil {
|
2017-03-16 17:05:46 +00:00
|
|
|
source = ""
|
2017-02-08 01:21:19 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-02-03 22:20:05 +00:00
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
readonly = info.Readonly
|
|
|
|
removed = filepath.Join(b.root, "active", "rm-"+id)
|
|
|
|
} else {
|
|
|
|
source = filepath.Join(b.root, "snapshots", id)
|
|
|
|
removed = filepath.Join(b.root, "snapshots", "rm-"+id)
|
|
|
|
readonly = true
|
|
|
|
}
|
2017-02-03 22:20:05 +00:00
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
if err := btrfs.SubvolSnapshot(removed, source, readonly); err != nil {
|
|
|
|
removed = ""
|
|
|
|
return err
|
|
|
|
}
|
2017-01-13 23:31:21 +00:00
|
|
|
|
2017-03-16 17:05:46 +00:00
|
|
|
if err := btrfs.SubvolDelete(source); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to remove snapshot %v", source)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.Commit()
|
|
|
|
t = nil
|
|
|
|
if err != nil {
|
|
|
|
// Attempt to restore source
|
|
|
|
if err1 := btrfs.SubvolSnapshot(source, removed, readonly); err1 != nil {
|
2017-03-22 17:58:45 +00:00
|
|
|
log.G(ctx).WithFields(logrus.Fields{
|
|
|
|
logrus.ErrorKey: err1,
|
|
|
|
"subvolume": source,
|
|
|
|
"renamed": removed,
|
|
|
|
}).Error("Failed to restore subvolume from renamed")
|
2017-03-16 17:05:46 +00:00
|
|
|
// Keep removed to allow for manual restore
|
|
|
|
removed = ""
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-01-13 23:31:21 +00:00
|
|
|
}
|