2017-01-13 23:31:21 +00:00
|
|
|
package overlay
|
2016-12-09 19:27:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-02-03 02:55:53 +00:00
|
|
|
"io/ioutil"
|
2016-12-09 19:27:30 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2016-12-12 21:35:02 +00:00
|
|
|
"sync"
|
2016-12-09 19:27:30 +00:00
|
|
|
|
|
|
|
"github.com/docker/containerd"
|
2017-02-07 23:51:37 +00:00
|
|
|
"github.com/docker/containerd/snapshot"
|
2017-02-03 02:55:53 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2017-02-07 23:51:37 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-12-09 19:27:30 +00:00
|
|
|
)
|
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
type Snapshotter struct {
|
2017-02-03 22:20:05 +00:00
|
|
|
root string
|
2017-02-07 23:51:37 +00:00
|
|
|
links *cache
|
2017-02-03 22:20:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
func NewSnapshotter(root string) (*Snapshotter, error) {
|
2016-12-09 19:27:30 +00:00
|
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, p := range []string{
|
2017-02-07 23:51:37 +00:00
|
|
|
"committed", // committed snapshots
|
|
|
|
"active", // active snapshots
|
|
|
|
"index", // snapshots by hashed name
|
2016-12-09 19:27:30 +00:00
|
|
|
} {
|
|
|
|
if err := os.MkdirAll(filepath.Join(root, p), 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-02-07 23:51:37 +00:00
|
|
|
return &Snapshotter{
|
2016-12-12 21:35:02 +00:00
|
|
|
root: root,
|
2017-02-07 23:51:37 +00:00
|
|
|
links: newCache(),
|
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.
|
|
|
|
func (o *Snapshotter) Stat(key string) (snapshot.Info, error) {
|
|
|
|
path, err := o.links.get(filepath.Join(o.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(stevvooe): We don't confirm the name to avoid the lookup cost.
|
|
|
|
return o.stat(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Snapshotter) stat(path string) (snapshot.Info, error) {
|
|
|
|
ppath, err := o.links.get(filepath.Join(path, "parent"))
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return snapshot.Info{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// no parent
|
|
|
|
}
|
|
|
|
|
|
|
|
kp, err := ioutil.ReadFile(filepath.Join(path, "name"))
|
|
|
|
if err != nil {
|
|
|
|
return snapshot.Info{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var parent string
|
|
|
|
if ppath != "" {
|
|
|
|
p, err := ioutil.ReadFile(filepath.Join(ppath, "name"))
|
|
|
|
if err != nil {
|
|
|
|
return snapshot.Info{}, err
|
|
|
|
}
|
|
|
|
parent = string(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
ro := true
|
|
|
|
kind := snapshot.KindCommitted
|
|
|
|
if strings.HasPrefix(path, filepath.Join(o.root, "active")) {
|
|
|
|
// TODO(stevvooe): Maybe there is a better way?
|
|
|
|
kind = snapshot.KindActive
|
|
|
|
|
|
|
|
// TODO(stevvooe): We haven't introduced this to overlay yet.
|
|
|
|
// We'll add it when we add tests for it.
|
|
|
|
ro = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return snapshot.Info{
|
|
|
|
Name: string(kp),
|
|
|
|
Parent: parent,
|
|
|
|
Kind: kind,
|
|
|
|
Readonly: ro,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Snapshotter) Prepare(key, parent string) ([]containerd.Mount, error) {
|
2016-12-09 19:27:30 +00:00
|
|
|
active, err := o.newActiveDir(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-03 02:55:53 +00:00
|
|
|
if parent != "" {
|
|
|
|
if err := active.setParent(parent); err != nil {
|
2016-12-09 19:27:30 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-02-07 23:51:37 +00:00
|
|
|
return active.mounts(o.links)
|
2017-02-03 02:55:53 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
func (o *Snapshotter) View(key, parent string) ([]containerd.Mount, error) {
|
2017-02-03 02:55:53 +00:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-07 23:51:37 +00:00
|
|
|
func (o *Snapshotter) Mounts(key string) ([]containerd.Mount, error) {
|
2017-02-03 02:55:53 +00:00
|
|
|
active := o.getActive(key)
|
2017-02-07 23:51:37 +00:00
|
|
|
return active.mounts(o.links)
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
func (o *Snapshotter) Commit(name, key string) error {
|
2016-12-09 19:27:30 +00:00
|
|
|
active := o.getActive(key)
|
2017-02-07 23:51:37 +00:00
|
|
|
return active.commit(name, o.links)
|
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-02-07 23:51:37 +00:00
|
|
|
func (o *Snapshotter) Remove(key string) error {
|
2017-02-03 02:55:53 +00:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
// Walk the committed snapshots.
|
|
|
|
func (o *Snapshotter) Walk(fn func(snapshot.Info) error) error {
|
|
|
|
root := filepath.Join(o.root, "index")
|
|
|
|
return filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-02-03 02:55:53 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
if path == root {
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-03 02:55:53 +00:00
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
if fi.Mode()&os.ModeSymlink == 0 {
|
|
|
|
// only follow links
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
2017-02-03 02:55:53 +00:00
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
target, err := o.links.get(path)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-02-03 02:55:53 +00:00
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
si, err := o.stat(target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-03 02:55:53 +00:00
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
if err := fn(si); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-03 02:55:53 +00:00
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
return nil
|
|
|
|
})
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
func (o *Snapshotter) newActiveDir(key string) (*activeDir, error) {
|
2016-12-09 19:27:30 +00:00
|
|
|
var (
|
2017-02-07 23:51:37 +00:00
|
|
|
path = filepath.Join(o.root, "active", hash(key))
|
|
|
|
name = filepath.Join(path, "name")
|
|
|
|
indexlink = filepath.Join(o.root, "index", hash(key))
|
2016-12-09 19:27:30 +00:00
|
|
|
)
|
|
|
|
a := &activeDir{
|
|
|
|
path: path,
|
2017-02-07 23:51:37 +00:00
|
|
|
committedDir: filepath.Join(o.root, "committed"),
|
|
|
|
indexlink: indexlink,
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
for _, p := range []string{
|
|
|
|
"work",
|
|
|
|
"fs",
|
|
|
|
} {
|
|
|
|
if err := os.MkdirAll(filepath.Join(path, p), 0700); err != nil {
|
|
|
|
a.delete()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-02-07 23:51:37 +00:00
|
|
|
|
|
|
|
if err := ioutil.WriteFile(name, []byte(key), 0644); err != nil {
|
|
|
|
a.delete()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// link from namespace
|
|
|
|
if err := os.Symlink(path, indexlink); err != nil {
|
|
|
|
a.delete()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-09 19:27:30 +00:00
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
func (o *Snapshotter) getActive(key string) *activeDir {
|
2016-12-09 19:27:30 +00:00
|
|
|
return &activeDir{
|
|
|
|
path: filepath.Join(o.root, "active", hash(key)),
|
2017-02-07 23:51:37 +00:00
|
|
|
committedDir: filepath.Join(o.root, "committed"),
|
|
|
|
indexlink: filepath.Join(o.root, "index", hash(key)),
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hash(k string) string {
|
2017-02-03 02:55:53 +00:00
|
|
|
return digest.FromString(k).Hex()
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type activeDir struct {
|
2017-02-07 23:51:37 +00:00
|
|
|
committedDir string
|
2016-12-09 19:27:30 +00:00
|
|
|
path string
|
2017-02-07 23:51:37 +00:00
|
|
|
indexlink string
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *activeDir) delete() error {
|
|
|
|
return os.RemoveAll(a.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *activeDir) setParent(name string) error {
|
2017-02-07 23:51:37 +00:00
|
|
|
return os.Symlink(filepath.Join(a.committedDir, hash(name)), filepath.Join(a.path, "parent"))
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 02:55:53 +00:00
|
|
|
func (a *activeDir) commit(name string, c *cache) error {
|
|
|
|
// TODO(stevvooe): This doesn't quite meet the current model. The new model
|
|
|
|
// is to copy all of this out and let the transaction continue. We don't
|
|
|
|
// really have tests for it yet, but this will be the spot to fix it.
|
|
|
|
//
|
|
|
|
// Nothing should be removed until remove is called on the active
|
|
|
|
// transaction.
|
2016-12-09 19:27:30 +00:00
|
|
|
if err := os.RemoveAll(filepath.Join(a.path, "work")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-03 02:55:53 +00:00
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(a.path, "name"), []byte(name), 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.invalidate(a.path) // clears parent cache, since we end up moving.
|
2017-02-07 23:51:37 +00:00
|
|
|
c.invalidate(filepath.Join(a.path, "parent"))
|
|
|
|
c.invalidate(a.indexlink)
|
|
|
|
|
|
|
|
committed := filepath.Join(a.committedDir, hash(name))
|
|
|
|
if err := os.Rename(a.path, committed); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Remove(a.indexlink); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
indexlink := filepath.Join(filepath.Dir(a.indexlink), hash(name))
|
|
|
|
return os.Symlink(committed, indexlink)
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 21:35:02 +00:00
|
|
|
func (a *activeDir) mounts(c *cache) ([]containerd.Mount, error) {
|
2016-12-09 19:27:30 +00:00
|
|
|
var (
|
2016-12-12 21:35:02 +00:00
|
|
|
parents []string
|
|
|
|
err error
|
|
|
|
current = a.path
|
2016-12-09 19:27:30 +00:00
|
|
|
)
|
|
|
|
for {
|
2017-02-07 23:51:37 +00:00
|
|
|
if current, err = c.get(filepath.Join(current, "parent")); err != nil {
|
2016-12-09 19:27:30 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
break
|
|
|
|
}
|
2017-02-07 23:51:37 +00:00
|
|
|
|
2016-12-09 19:27:30 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-12 21:35:02 +00:00
|
|
|
parents = append(parents, filepath.Join(current, "fs"))
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
if len(parents) == 0 {
|
|
|
|
// if we only have one layer/no parents then just return a bind mount as overlay
|
|
|
|
// will not work
|
|
|
|
return []containerd.Mount{
|
|
|
|
{
|
|
|
|
Source: filepath.Join(a.path, "fs"),
|
|
|
|
Type: "bind",
|
|
|
|
Options: []string{
|
|
|
|
"rw",
|
|
|
|
"rbind",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
options := []string{
|
|
|
|
fmt.Sprintf("workdir=%s", filepath.Join(a.path, "work")),
|
|
|
|
fmt.Sprintf("upperdir=%s", filepath.Join(a.path, "fs")),
|
|
|
|
fmt.Sprintf("lowerdir=%s", strings.Join(parents, ":")),
|
|
|
|
}
|
|
|
|
return []containerd.Mount{
|
|
|
|
{
|
|
|
|
Type: "overlay",
|
|
|
|
Source: "overlay",
|
|
|
|
Options: options,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2016-12-12 21:35:02 +00:00
|
|
|
|
|
|
|
func newCache() *cache {
|
|
|
|
return &cache{
|
2017-02-07 23:51:37 +00:00
|
|
|
links: make(map[string]string),
|
2016-12-12 21:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type cache struct {
|
2017-02-07 23:51:37 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
links map[string]string
|
2016-12-12 21:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) get(path string) (string, error) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2017-02-07 23:51:37 +00:00
|
|
|
target, ok := c.links[path]
|
2016-12-12 21:35:02 +00:00
|
|
|
if !ok {
|
2017-02-07 23:51:37 +00:00
|
|
|
link, err := os.Readlink(path)
|
2016-12-12 21:35:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-02-07 23:51:37 +00:00
|
|
|
c.links[path], target = link, link
|
2016-12-12 21:35:02 +00:00
|
|
|
}
|
2017-02-07 23:51:37 +00:00
|
|
|
return target, nil
|
2016-12-12 21:35:02 +00:00
|
|
|
}
|
2017-02-03 02:55:53 +00:00
|
|
|
|
|
|
|
func (c *cache) invalidate(path string) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
2017-02-07 23:51:37 +00:00
|
|
|
delete(c.links, path)
|
2017-02-03 02:55:53 +00:00
|
|
|
}
|