2017-01-13 23:31:21 +00:00
|
|
|
package btrfs
|
2016-12-13 05:53:48 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-13 23:31:21 +00:00
|
|
|
"crypto/sha256"
|
2016-12-13 05:53:48 +00:00
|
|
|
"fmt"
|
2017-02-03 22:20:05 +00:00
|
|
|
"io/ioutil"
|
2016-12-13 05:53:48 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-02-08 01:21:19 +00:00
|
|
|
"strings"
|
2016-12-13 05:53:48 +00:00
|
|
|
|
|
|
|
"github.com/docker/containerd"
|
2017-02-08 01:21:19 +00:00
|
|
|
"github.com/docker/containerd/snapshot"
|
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-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-02-08 01:21:19 +00:00
|
|
|
func NewSnapshotter(device, root string) (*Snapshotter, error) {
|
|
|
|
var (
|
|
|
|
active = filepath.Join(root, "active")
|
|
|
|
snapshots = filepath.Join(root, "snapshots")
|
|
|
|
parents = filepath.Join(root, "parents")
|
|
|
|
index = filepath.Join(root, "index")
|
|
|
|
names = filepath.Join(root, "names")
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, path := range []string{
|
|
|
|
active,
|
|
|
|
snapshots,
|
|
|
|
parents,
|
|
|
|
index,
|
|
|
|
names,
|
|
|
|
} {
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Snapshotter{device: device, root: root}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (b *Snapshotter) Stat(key string) (snapshot.Info, error) {
|
|
|
|
// resolve the snapshot out of the index.
|
|
|
|
target, err := os.Readlink(filepath.Join(b.root, "index", hash(key)))
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return snapshot.Info{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return snapshot.Info{}, errors.Errorf("snapshot %v not found", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.stat(target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Snapshotter) stat(target string) (snapshot.Info, error) {
|
|
|
|
var (
|
|
|
|
parents = filepath.Join(b.root, "parents")
|
|
|
|
names = filepath.Join(b.root, "names")
|
|
|
|
namep = filepath.Join(names, filepath.Base(target))
|
|
|
|
parentlink = filepath.Join(parents, filepath.Base(target))
|
|
|
|
)
|
|
|
|
|
|
|
|
// grab information about the subvolume
|
|
|
|
info, err := btrfs.SubvolInfo(target)
|
|
|
|
if err != nil {
|
|
|
|
return snapshot.Info{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the name out of the names!
|
|
|
|
nameraw, err := ioutil.ReadFile(namep)
|
|
|
|
if err != nil {
|
|
|
|
return snapshot.Info{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve the parents path.
|
|
|
|
parentp, err := os.Readlink(parentlink)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return snapshot.Info{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// no parent!
|
|
|
|
}
|
|
|
|
|
|
|
|
var parent string
|
|
|
|
if parentp != "" {
|
|
|
|
// okay, grab the basename of the parent and look up its name!
|
|
|
|
parentnamep := filepath.Join(names, filepath.Base(parentp))
|
|
|
|
|
|
|
|
p, err := ioutil.ReadFile(parentnamep)
|
|
|
|
if err != nil {
|
|
|
|
return snapshot.Info{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
parent = string(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
kind := snapshot.KindCommitted
|
|
|
|
if strings.HasPrefix(target, filepath.Join(b.root, "active")) {
|
|
|
|
kind = snapshot.KindActive
|
|
|
|
}
|
|
|
|
|
|
|
|
return snapshot.Info{
|
|
|
|
Name: string(nameraw),
|
|
|
|
Parent: parent,
|
|
|
|
Readonly: info.Readonly,
|
|
|
|
Kind: kind,
|
|
|
|
}, nil
|
|
|
|
|
2016-12-13 05:53:48 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
func (b *Snapshotter) Prepare(key, parent string) ([]containerd.Mount, error) {
|
2017-02-03 22:20:05 +00:00
|
|
|
return b.makeActive(key, parent, false)
|
|
|
|
}
|
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
func (b *Snapshotter) View(key, parent string) ([]containerd.Mount, error) {
|
2017-02-03 22:20:05 +00:00
|
|
|
return b.makeActive(key, parent, true)
|
|
|
|
}
|
2016-12-13 05:53:48 +00:00
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
func (b *Snapshotter) makeActive(key, parent string, readonly bool) ([]containerd.Mount, error) {
|
2017-02-03 22:20:05 +00:00
|
|
|
var (
|
|
|
|
active = filepath.Join(b.root, "active")
|
|
|
|
snapshots = filepath.Join(b.root, "snapshots")
|
2017-02-08 01:21:19 +00:00
|
|
|
parents = filepath.Join(b.root, "parents")
|
|
|
|
index = filepath.Join(b.root, "index")
|
2017-02-03 22:20:05 +00:00
|
|
|
names = filepath.Join(b.root, "names")
|
2017-02-07 01:09:11 +00:00
|
|
|
keyh = hash(key)
|
|
|
|
parenth = hash(parent)
|
2017-02-08 01:21:19 +00:00
|
|
|
target = filepath.Join(active, keyh)
|
2017-02-07 01:09:11 +00:00
|
|
|
namep = filepath.Join(names, keyh)
|
2017-02-08 01:21:19 +00:00
|
|
|
indexlink = filepath.Join(index, keyh)
|
2017-02-07 01:09:11 +00:00
|
|
|
parentlink = filepath.Join(parents, keyh)
|
2017-02-08 01:21:19 +00:00
|
|
|
parentp = filepath.Join(snapshots, parenth) // parent must be restricted to snaps
|
2017-02-03 22:20:05 +00:00
|
|
|
)
|
|
|
|
|
2016-12-13 05:53:48 +00:00
|
|
|
if parent == "" {
|
|
|
|
// create new subvolume
|
|
|
|
// btrfs subvolume create /dir
|
2017-02-08 01:21:19 +00:00
|
|
|
if err := btrfs.SubvolCreate(target); err != nil {
|
2016-12-13 05:53:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// btrfs subvolume snapshot /parent /subvol
|
2017-02-08 01:21:19 +00:00
|
|
|
if err := btrfs.SubvolSnapshot(target, parentp, readonly); err != nil {
|
2017-02-03 22:20:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Symlink(parentp, parentlink); err != nil {
|
2016-12-13 05:53:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
// write in the name
|
2017-02-03 22:20:05 +00:00
|
|
|
if err := ioutil.WriteFile(namep, []byte(key), 0644); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
if err := os.Symlink(target, indexlink); err != nil {
|
|
|
|
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-02-08 01:21:19 +00:00
|
|
|
func (b *Snapshotter) Commit(name, key string) error {
|
2017-02-03 22:20:05 +00:00
|
|
|
var (
|
|
|
|
active = filepath.Join(b.root, "active")
|
|
|
|
snapshots = filepath.Join(b.root, "snapshots")
|
2017-02-08 01:21:19 +00:00
|
|
|
index = filepath.Join(b.root, "index")
|
2017-02-03 22:20:05 +00:00
|
|
|
parents = filepath.Join(b.root, "parents")
|
2017-02-08 01:21:19 +00:00
|
|
|
names = filepath.Join(b.root, "names")
|
2017-02-07 01:09:11 +00:00
|
|
|
keyh = hash(key)
|
|
|
|
nameh = hash(name)
|
|
|
|
dir = filepath.Join(active, keyh)
|
|
|
|
target = filepath.Join(snapshots, nameh)
|
|
|
|
keynamep = filepath.Join(names, keyh)
|
|
|
|
namep = filepath.Join(names, nameh)
|
|
|
|
keyparentlink = filepath.Join(parents, keyh)
|
|
|
|
parentlink = filepath.Join(parents, nameh)
|
2017-02-08 01:21:19 +00:00
|
|
|
keyindexlink = filepath.Join(index, keyh)
|
|
|
|
indexlink = filepath.Join(index, nameh)
|
2017-02-03 22:20:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
info, err := btrfs.SubvolInfo(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Readonly {
|
|
|
|
return fmt.Errorf("may not commit view snapshot %q", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// look up the parent information to make sure we have the right parent link.
|
|
|
|
parentp, err := os.Readlink(keyparentlink)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have no parent!
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(snapshots, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := btrfs.SubvolSnapshot(target, dir, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-13 05:53:48 +00:00
|
|
|
|
2017-02-03 22:20:05 +00:00
|
|
|
// remove the key name path as we no longer need it.
|
|
|
|
if err := os.Remove(keynamep); err != nil {
|
2016-12-13 05:53:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
if err := os.Remove(keyindexlink); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-03 22:20:05 +00:00
|
|
|
if err := ioutil.WriteFile(namep, []byte(name), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if parentp != "" {
|
|
|
|
// move over the parent link into the commit name.
|
|
|
|
if err := os.Rename(keyparentlink, parentlink); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(stevvooe): For this to not break horribly, we should really
|
|
|
|
// start taking a full lock. We are going to move all this metadata
|
|
|
|
// into common storage, so let's not fret over it for now.
|
|
|
|
}
|
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
if err := os.Symlink(target, indexlink); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-03 22:20:05 +00:00
|
|
|
if err := btrfs.SubvolDelete(dir); err != nil {
|
|
|
|
return errors.Wrapf(err, "delete subvol failed on %v", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08 01:21:19 +00:00
|
|
|
func (b *Snapshotter) Mounts(key string) ([]containerd.Mount, error) {
|
2017-02-03 22:20:05 +00:00
|
|
|
dir := filepath.Join(b.root, "active", hash(key))
|
|
|
|
return b.mounts(dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove abandons the transaction identified by key. All resources
|
|
|
|
// associated with the key will be removed.
|
2017-02-08 01:21:19 +00:00
|
|
|
func (b *Snapshotter) Remove(key string) error {
|
2017-02-03 22:20:05 +00:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
// Walk the committed snapshots.
|
|
|
|
func (b *Snapshotter) Walk(fn func(snapshot.Info) error) error {
|
|
|
|
// TODO(stevvooe): Copy-pasted almost verbatim from overlay. Really need to
|
|
|
|
// unify the metadata for snapshot implementations.
|
|
|
|
root := filepath.Join(b.root, "index")
|
|
|
|
return filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-02-03 22:20:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
if path == root {
|
|
|
|
return nil
|
2017-02-03 22:20:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
if fi.Mode()&os.ModeSymlink == 0 {
|
|
|
|
// only follow links
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
2017-02-03 22:20:05 +00:00
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
target, err := os.Readlink(path)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-02-03 22:20:05 +00:00
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
si, err := b.stat(target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-03 22:20:05 +00:00
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
if err := fn(si); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-03 22:20:05 +00:00
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
return nil
|
|
|
|
})
|
2016-12-13 05:53:48 +00:00
|
|
|
}
|
2017-01-13 23:31:21 +00:00
|
|
|
|
|
|
|
func hash(k string) string {
|
|
|
|
return fmt.Sprintf("%x", sha256.Sum224([]byte(k)))
|
|
|
|
}
|