2017-01-13 23:31:21 +00:00
|
|
|
package overlay
|
2016-12-09 19:27:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"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-01-17 08:42:27 +00:00
|
|
|
func NewOverlay(root string) (*Overlay, error) {
|
2016-12-09 19:27:30 +00:00
|
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, p := range []string{
|
|
|
|
"snapshots",
|
|
|
|
"active",
|
|
|
|
} {
|
|
|
|
if err := os.MkdirAll(filepath.Join(root, p), 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-01-17 08:42:27 +00:00
|
|
|
return &Overlay{
|
2016-12-12 21:35:02 +00:00
|
|
|
root: root,
|
|
|
|
cache: newCache(),
|
2016-12-09 19:27:30 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
type Overlay struct {
|
2016-12-12 21:35:02 +00:00
|
|
|
root string
|
|
|
|
cache *cache
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
func (o *Overlay) Prepare(key string, parentName string) ([]containerd.Mount, error) {
|
2016-12-09 19:27:30 +00:00
|
|
|
if err := validKey(key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
active, err := o.newActiveDir(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if parentName != "" {
|
|
|
|
if err := active.setParent(parentName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-12-12 21:35:02 +00:00
|
|
|
return active.mounts(o.cache)
|
2016-12-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
func (o *Overlay) Commit(name, key string) error {
|
2016-12-09 19:27:30 +00:00
|
|
|
active := o.getActive(key)
|
|
|
|
return active.commit(name)
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
func (o *Overlay) newActiveDir(key string) (*activeDir, error) {
|
2016-12-09 19:27:30 +00:00
|
|
|
var (
|
|
|
|
hash = hash(key)
|
|
|
|
path = filepath.Join(o.root, "active", hash)
|
|
|
|
)
|
|
|
|
a := &activeDir{
|
|
|
|
path: path,
|
|
|
|
snapshotsDir: filepath.Join(o.root, "snapshots"),
|
|
|
|
}
|
|
|
|
for _, p := range []string{
|
|
|
|
"work",
|
|
|
|
"fs",
|
|
|
|
} {
|
|
|
|
if err := os.MkdirAll(filepath.Join(path, p), 0700); err != nil {
|
|
|
|
a.delete()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
func (o *Overlay) getActive(key string) *activeDir {
|
2016-12-09 19:27:30 +00:00
|
|
|
return &activeDir{
|
|
|
|
path: filepath.Join(o.root, "active", hash(key)),
|
|
|
|
snapshotsDir: filepath.Join(o.root, "snapshots"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validKey(key string) error {
|
|
|
|
_, err := filepath.Abs(key)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func hash(k string) string {
|
|
|
|
h := md5.New()
|
|
|
|
h.Write([]byte(k))
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
type activeDir struct {
|
|
|
|
snapshotsDir string
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *activeDir) delete() error {
|
|
|
|
return os.RemoveAll(a.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *activeDir) setParent(name string) error {
|
|
|
|
return os.Symlink(filepath.Join(a.snapshotsDir, name), filepath.Join(a.path, "parent"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *activeDir) commit(name string) error {
|
|
|
|
if err := os.RemoveAll(filepath.Join(a.path, "work")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Rename(a.path, filepath.Join(a.snapshotsDir, name))
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-12-12 21:35:02 +00:00
|
|
|
if current, err = c.get(current); err != nil {
|
2016-12-09 19:27:30 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
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{
|
|
|
|
parents: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type cache struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
parents map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) get(path string) (string, error) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
parentRoot, ok := c.parents[path]
|
|
|
|
if !ok {
|
|
|
|
link, err := os.Readlink(filepath.Join(path, "parent"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
c.parents[path], parentRoot = link, link
|
|
|
|
}
|
|
|
|
return parentRoot, nil
|
|
|
|
}
|