snapshots: separate implementations into packages

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day 2017-01-13 15:31:21 -08:00
parent c7f77f475a
commit e3f83fd53d
8 changed files with 22 additions and 16 deletions

View file

@ -0,0 +1,184 @@
package overlay
import (
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"github.com/docker/containerd"
)
func NewOverlayfs(root string) (*Overlayfs, error) {
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
}
}
return &Overlayfs{
root: root,
cache: newCache(),
}, nil
}
type Overlayfs struct {
root string
cache *cache
}
func (o *Overlayfs) Prepare(key string, parentName string) ([]containerd.Mount, error) {
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
}
}
return active.mounts(o.cache)
}
func (o *Overlayfs) Commit(name, key string) error {
active := o.getActive(key)
return active.commit(name)
}
func (o *Overlayfs) newActiveDir(key string) (*activeDir, error) {
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
}
func (o *Overlayfs) getActive(key string) *activeDir {
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))
}
func (a *activeDir) mounts(c *cache) ([]containerd.Mount, error) {
var (
parents []string
err error
current = a.path
)
for {
if current, err = c.get(current); err != nil {
if os.IsNotExist(err) {
break
}
return nil, err
}
parents = append(parents, filepath.Join(current, "fs"))
}
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
}
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
}

View file

@ -0,0 +1,180 @@
package overlay
import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
"testing"
"github.com/docker/containerd"
)
func TestOverlayfs(t *testing.T) {
root, err := ioutil.TempDir("", "overlay")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(root)
o, err := NewOverlayfs(root)
if err != nil {
t.Error(err)
return
}
mounts, err := o.Prepare("/tmp/test", "")
if err != nil {
t.Error(err)
return
}
if len(mounts) != 1 {
t.Errorf("should only have 1 mount but received %d", len(mounts))
}
m := mounts[0]
if m.Type != "bind" {
t.Errorf("mount type should be bind but received %q", m.Type)
}
expected := filepath.Join(root, "active", hash("/tmp/test"), "fs")
if m.Source != expected {
t.Errorf("expected source %q but received %q", expected, m.Source)
}
if m.Options[0] != "rw" {
t.Errorf("expected mount option rw but received %q", m.Options[0])
}
if m.Options[1] != "rbind" {
t.Errorf("expected mount option rbind but received %q", m.Options[1])
}
}
func TestOverlayfsCommit(t *testing.T) {
root, err := ioutil.TempDir("", "overlay")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(root)
o, err := NewOverlayfs(root)
if err != nil {
t.Error(err)
return
}
key := "/tmp/test"
mounts, err := o.Prepare(key, "")
if err != nil {
t.Error(err)
return
}
m := mounts[0]
if err := ioutil.WriteFile(filepath.Join(m.Source, "foo"), []byte("hi"), 0660); err != nil {
t.Error(err)
return
}
if err := o.Commit("base", key); err != nil {
t.Error(err)
return
}
}
func TestOverlayfsOverlayMount(t *testing.T) {
root, err := ioutil.TempDir("", "overlay")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(root)
o, err := NewOverlayfs(root)
if err != nil {
t.Error(err)
return
}
key := "/tmp/test"
mounts, err := o.Prepare(key, "")
if err != nil {
t.Error(err)
return
}
if err := o.Commit("base", key); err != nil {
t.Error(err)
return
}
if mounts, err = o.Prepare("/tmp/layer2", "base"); err != nil {
t.Error(err)
return
}
if len(mounts) != 1 {
t.Errorf("should only have 1 mount but received %d", len(mounts))
}
m := mounts[0]
if m.Type != "overlay" {
t.Errorf("mount type should be overlay but received %q", m.Type)
}
if m.Source != "overlay" {
t.Errorf("expected source %q but received %q", "overlay", m.Source)
}
var (
hash = hash("/tmp/layer2")
work = "workdir=" + filepath.Join(root, "active", hash, "work")
upper = "upperdir=" + filepath.Join(root, "active", hash, "fs")
lower = "lowerdir=" + filepath.Join(root, "snapshots", "base", "fs")
)
for i, v := range []string{
work,
upper,
lower,
} {
if m.Options[i] != v {
t.Errorf("expected %q but received %q", v, m.Options[i])
}
}
}
func TestOverlayfsOverlayRead(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("not running as root")
}
root, err := ioutil.TempDir("", "overlay")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(root)
o, err := NewOverlayfs(root)
if err != nil {
t.Error(err)
return
}
key := "/tmp/test"
mounts, err := o.Prepare(key, "")
if err != nil {
t.Error(err)
return
}
m := mounts[0]
if err := ioutil.WriteFile(filepath.Join(m.Source, "foo"), []byte("hi"), 0660); err != nil {
t.Error(err)
return
}
if err := o.Commit("base", key); err != nil {
t.Error(err)
return
}
if mounts, err = o.Prepare("/tmp/layer2", "base"); err != nil {
t.Error(err)
return
}
dest := filepath.Join(root, "dest")
if err := os.Mkdir(dest, 0700); err != nil {
t.Error(err)
return
}
if err := containerd.MountFS(mounts, dest); err != nil {
t.Error(err)
return
}
defer syscall.Unmount(dest, 0)
data, err := ioutil.ReadFile(filepath.Join(dest, "foo"))
if err != nil {
t.Error(err)
return
}
if e := string(data); e != "hi" {
t.Errorf("expected file contents hi but got %q", e)
return
}
}