2015-04-27 07:53:12 -07:00
|
|
|
// +build linux
|
|
|
|
|
2013-11-07 04:33:31 -08:00
|
|
|
/*
|
|
|
|
|
|
|
|
aufs driver directory structure
|
|
|
|
|
2015-04-27 07:53:12 -07:00
|
|
|
.
|
|
|
|
├── layers // Metadata of layers
|
|
|
|
│ ├── 1
|
|
|
|
│ ├── 2
|
|
|
|
│ └── 3
|
|
|
|
├── diff // Content of the layer
|
|
|
|
│ ├── 1 // Contains layers that need to be mounted for the id
|
|
|
|
│ ├── 2
|
|
|
|
│ └── 3
|
|
|
|
└── mnt // Mount points for the rw layers to be mounted
|
|
|
|
├── 1
|
|
|
|
├── 2
|
|
|
|
└── 3
|
2013-11-07 04:33:31 -08:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-10-31 18:07:54 -07:00
|
|
|
package aufs
|
|
|
|
|
|
|
|
import (
|
2013-11-07 17:57:14 -08:00
|
|
|
"bufio"
|
2013-10-31 18:07:54 -07:00
|
|
|
"fmt"
|
2015-03-26 06:02:21 +00:00
|
|
|
"io/ioutil"
|
2013-10-31 18:07:54 -07:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
2013-11-07 17:57:14 -08:00
|
|
|
"strings"
|
2014-01-14 12:23:20 +01:00
|
|
|
"sync"
|
2014-05-29 22:55:59 +03:00
|
|
|
"syscall"
|
|
|
|
|
2015-03-26 23:22:04 +01:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
2014-09-29 23:23:36 -07:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2014-10-29 21:06:51 +02:00
|
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
2015-02-27 19:50:55 +01:00
|
|
|
"github.com/docker/docker/pkg/directory"
|
2014-07-24 22:19:50 +00:00
|
|
|
mountpk "github.com/docker/docker/pkg/mount"
|
2015-03-24 12:25:26 +01:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-07-16 16:00:55 -07:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2013-10-31 18:07:54 -07:00
|
|
|
)
|
|
|
|
|
2014-02-18 11:58:38 +01:00
|
|
|
var (
|
2015-07-22 02:15:14 +00:00
|
|
|
// ErrAufsNotSupported is returned if aufs is not supported by the host.
|
2014-02-18 11:58:38 +01:00
|
|
|
ErrAufsNotSupported = fmt.Errorf("AUFS was not found in /proc/filesystems")
|
2014-06-02 19:26:41 -06:00
|
|
|
incompatibleFsMagic = []graphdriver.FsMagic{
|
|
|
|
graphdriver.FsMagicBtrfs,
|
|
|
|
graphdriver.FsMagicAufs,
|
|
|
|
}
|
2015-01-15 16:40:39 -05:00
|
|
|
backingFs = "<unknown>"
|
2015-03-26 06:02:21 +00:00
|
|
|
|
|
|
|
enableDirpermLock sync.Once
|
|
|
|
enableDirperm bool
|
2014-02-18 11:58:38 +01:00
|
|
|
)
|
|
|
|
|
2013-11-04 15:22:34 -08:00
|
|
|
func init() {
|
|
|
|
graphdriver.Register("aufs", Init)
|
|
|
|
}
|
|
|
|
|
2015-07-22 02:15:14 +00:00
|
|
|
// Driver contains information about the filesystem mounted.
|
|
|
|
// root of the filesystem
|
|
|
|
// sync.Mutex to protect against concurrent modifications
|
|
|
|
// active maps mount id to the count
|
2013-11-19 03:27:59 -08:00
|
|
|
type Driver struct {
|
2014-01-14 12:23:20 +01:00
|
|
|
root string
|
|
|
|
sync.Mutex // Protects concurrent modification to active
|
|
|
|
active map[string]int
|
2013-10-31 18:07:54 -07:00
|
|
|
}
|
|
|
|
|
2015-07-22 02:15:14 +00:00
|
|
|
// Init returns a new AUFS driver.
|
2013-11-04 01:54:51 +00:00
|
|
|
// An error is returned if AUFS is not supported.
|
2014-06-05 10:34:20 +02:00
|
|
|
func Init(root string, options []string) (graphdriver.Driver, error) {
|
2015-01-15 16:40:39 -05:00
|
|
|
|
2013-11-04 15:22:34 -08:00
|
|
|
// Try to load the aufs kernel module
|
2013-11-07 17:57:14 -08:00
|
|
|
if err := supportsAufs(); err != nil {
|
2014-03-27 17:41:06 +01:00
|
|
|
return nil, graphdriver.ErrNotSupported
|
2013-11-04 15:22:34 -08:00
|
|
|
}
|
2014-05-29 22:55:59 +03:00
|
|
|
|
2015-01-15 16:40:39 -05:00
|
|
|
fsMagic, err := graphdriver.GetFSMagic(root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
|
|
|
|
backingFs = fsName
|
2014-05-29 22:55:59 +03:00
|
|
|
}
|
|
|
|
|
2014-06-02 19:26:41 -06:00
|
|
|
for _, magic := range incompatibleFsMagic {
|
2015-01-15 16:40:39 -05:00
|
|
|
if fsMagic == magic {
|
2014-05-29 22:55:59 +03:00
|
|
|
return nil, graphdriver.ErrIncompatibleFS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-07 04:33:31 -08:00
|
|
|
paths := []string{
|
|
|
|
"mnt",
|
|
|
|
"diff",
|
|
|
|
"layers",
|
|
|
|
}
|
|
|
|
|
2014-01-14 12:23:20 +01:00
|
|
|
a := &Driver{
|
|
|
|
root: root,
|
|
|
|
active: make(map[string]int),
|
|
|
|
}
|
|
|
|
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[v2: a separate aufs commit is merged into this one]
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-29 16:49:05 -07:00
|
|
|
// Create the root aufs driver dir
|
2013-11-08 11:36:58 -08:00
|
|
|
if err := os.MkdirAll(root, 0755); err != nil {
|
2013-11-07 04:33:31 -08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-10-30 17:04:56 -04:00
|
|
|
if err := mountpk.MakePrivate(root); err != nil {
|
2014-06-05 12:50:53 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[v2: a separate aufs commit is merged into this one]
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-29 16:49:05 -07:00
|
|
|
// Populate the dir structure
|
2013-11-07 04:33:31 -08:00
|
|
|
for _, p := range paths {
|
2013-11-08 11:36:58 -08:00
|
|
|
if err := os.MkdirAll(path.Join(root, p), 0755); err != nil {
|
2013-11-07 04:33:31 -08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-01-14 12:23:20 +01:00
|
|
|
return a, nil
|
2013-10-31 18:07:54 -07:00
|
|
|
}
|
|
|
|
|
2013-11-07 17:57:14 -08:00
|
|
|
// Return a nil error if the kernel supports aufs
|
|
|
|
// We cannot modprobe because inside dind modprobe fails
|
|
|
|
// to run
|
|
|
|
func supportsAufs() error {
|
2013-11-14 09:42:12 -08:00
|
|
|
// We can try to modprobe aufs first before looking at
|
|
|
|
// proc/filesystems for when aufs is supported
|
|
|
|
exec.Command("modprobe", "aufs").Run()
|
|
|
|
|
2013-11-07 17:57:14 -08:00
|
|
|
f, err := os.Open("/proc/filesystems")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
s := bufio.NewScanner(f)
|
|
|
|
for s.Scan() {
|
|
|
|
if strings.Contains(s.Text(), "aufs") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 11:58:38 +01:00
|
|
|
return ErrAufsNotSupported
|
2013-11-07 17:57:14 -08:00
|
|
|
}
|
|
|
|
|
2014-12-12 10:46:09 -08:00
|
|
|
func (a *Driver) rootPath() string {
|
2013-11-08 11:36:58 -08:00
|
|
|
return a.root
|
2013-11-07 04:33:31 -08:00
|
|
|
}
|
|
|
|
|
2014-12-12 10:46:09 -08:00
|
|
|
func (*Driver) String() string {
|
2013-11-07 04:33:31 -08:00
|
|
|
return "aufs"
|
|
|
|
}
|
|
|
|
|
2015-07-22 02:15:14 +00:00
|
|
|
// Status returns current information about the filesystem such as root directory, number of directories mounted, etc.
|
2014-12-12 10:46:09 -08:00
|
|
|
func (a *Driver) Status() [][2]string {
|
2013-11-20 13:53:54 -08:00
|
|
|
ids, _ := loadIds(path.Join(a.rootPath(), "layers"))
|
|
|
|
return [][2]string{
|
|
|
|
{"Root Dir", a.rootPath()},
|
2015-01-15 16:40:39 -05:00
|
|
|
{"Backing Filesystem", backingFs},
|
2013-11-20 13:53:54 -08:00
|
|
|
{"Dirs", fmt.Sprintf("%d", len(ids))},
|
2015-03-26 17:58:49 +00:00
|
|
|
{"Dirperm1 Supported", fmt.Sprintf("%v", useDirperm())},
|
2013-11-20 13:53:54 -08:00
|
|
|
}
|
2013-11-15 11:04:02 +01:00
|
|
|
}
|
|
|
|
|
2015-07-22 02:15:14 +00:00
|
|
|
// GetMetadata not implemented
|
2015-06-15 14:05:10 -04:00
|
|
|
func (a *Driver) GetMetadata(id string) (map[string]string, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2013-11-18 17:20:03 -08:00
|
|
|
// Exists returns true if the given id is registered with
|
|
|
|
// this driver
|
2014-12-12 10:46:09 -08:00
|
|
|
func (a *Driver) Exists(id string) bool {
|
2013-11-18 23:28:45 -08:00
|
|
|
if _, err := os.Lstat(path.Join(a.rootPath(), "layers", id)); err != nil {
|
2013-11-15 17:16:30 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-07-22 02:15:14 +00:00
|
|
|
// Create three folders for each id
|
2013-11-07 04:33:31 -08:00
|
|
|
// mnt, layers, and diff
|
2014-04-17 23:47:27 +00:00
|
|
|
func (a *Driver) Create(id, parent string) error {
|
2013-11-07 04:33:31 -08:00
|
|
|
if err := a.createDirsFor(id); err != nil {
|
2013-11-04 20:51:12 -08:00
|
|
|
return err
|
|
|
|
}
|
2013-11-07 04:33:31 -08:00
|
|
|
// Write the layers metadata
|
|
|
|
f, err := os.Create(path.Join(a.rootPath(), "layers", id))
|
|
|
|
if err != nil {
|
2013-11-04 20:51:12 -08:00
|
|
|
return err
|
|
|
|
}
|
2013-11-07 04:33:31 -08:00
|
|
|
defer f.Close()
|
2013-11-04 20:51:12 -08:00
|
|
|
|
2013-11-07 04:33:31 -08:00
|
|
|
if parent != "" {
|
|
|
|
ids, err := getParentIds(a.rootPath(), parent)
|
|
|
|
if err != nil {
|
2013-11-04 20:51:12 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-11-08 11:10:33 -08:00
|
|
|
if _, err := fmt.Fprintln(f, parent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-07 04:33:31 -08:00
|
|
|
for _, i := range ids {
|
2013-11-08 11:10:33 -08:00
|
|
|
if _, err := fmt.Fprintln(f, i); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-07 04:33:31 -08:00
|
|
|
}
|
2013-11-04 20:51:12 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-19 03:27:59 -08:00
|
|
|
func (a *Driver) createDirsFor(id string) error {
|
2013-11-07 04:33:31 -08:00
|
|
|
paths := []string{
|
|
|
|
"mnt",
|
|
|
|
"diff",
|
|
|
|
}
|
2013-11-04 20:51:12 -08:00
|
|
|
|
2013-11-07 04:33:31 -08:00
|
|
|
for _, p := range paths {
|
2013-11-11 14:30:38 -08:00
|
|
|
if err := os.MkdirAll(path.Join(a.rootPath(), p, id), 0755); err != nil {
|
2013-11-07 04:33:31 -08:00
|
|
|
return err
|
|
|
|
}
|
2013-11-04 20:51:12 -08:00
|
|
|
}
|
2013-11-07 04:33:31 -08:00
|
|
|
return nil
|
|
|
|
}
|
2013-11-04 20:51:12 -08:00
|
|
|
|
2015-07-22 02:15:14 +00:00
|
|
|
// Remove will unmount and remove the given id.
|
2013-11-19 03:27:59 -08:00
|
|
|
func (a *Driver) Remove(id string) error {
|
2014-01-14 12:23:20 +01:00
|
|
|
// Protect the a.active from concurrent access
|
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
|
|
|
|
if a.active[id] != 0 {
|
2015-03-26 23:22:04 +01:00
|
|
|
logrus.Errorf("Removing active id %s", id)
|
2014-01-14 12:23:20 +01:00
|
|
|
}
|
|
|
|
|
2013-11-07 04:33:31 -08:00
|
|
|
// Make sure the dir is umounted first
|
2013-11-08 10:17:51 -08:00
|
|
|
if err := a.unmount(id); err != nil {
|
2013-11-04 20:51:12 -08:00
|
|
|
return err
|
|
|
|
}
|
2013-11-07 04:33:31 -08:00
|
|
|
tmpDirs := []string{
|
|
|
|
"mnt",
|
|
|
|
"diff",
|
|
|
|
}
|
|
|
|
|
2014-01-27 18:07:30 +00:00
|
|
|
// Atomically remove each directory in turn by first moving it out of the
|
|
|
|
// way (so that docker doesn't find it anymore) before doing removal of
|
|
|
|
// the whole tree.
|
2013-11-07 04:33:31 -08:00
|
|
|
for _, p := range tmpDirs {
|
2014-01-27 18:07:30 +00:00
|
|
|
|
2013-11-07 04:33:31 -08:00
|
|
|
realPath := path.Join(a.rootPath(), p, id)
|
2014-01-27 18:07:30 +00:00
|
|
|
tmpPath := path.Join(a.rootPath(), p, fmt.Sprintf("%s-removing", id))
|
|
|
|
if err := os.Rename(realPath, tmpPath); err != nil && !os.IsNotExist(err) {
|
2013-11-07 04:33:31 -08:00
|
|
|
return err
|
|
|
|
}
|
2014-01-27 18:07:30 +00:00
|
|
|
defer os.RemoveAll(tmpPath)
|
2013-11-07 04:33:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the layers file for the id
|
2013-11-19 17:08:21 -08:00
|
|
|
if err := os.Remove(path.Join(a.rootPath(), "layers", id)); err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2013-11-04 20:51:12 -08:00
|
|
|
}
|
|
|
|
|
2015-07-22 02:15:14 +00:00
|
|
|
// Get returns the rootfs path for the id.
|
2013-11-07 04:33:31 -08:00
|
|
|
// This will mount the dir at it's given path
|
2014-04-17 23:47:27 +00:00
|
|
|
func (a *Driver) Get(id, mountLabel string) (string, error) {
|
2013-11-07 04:33:31 -08:00
|
|
|
ids, err := getParentIds(a.rootPath(), id)
|
2013-10-31 18:07:54 -07:00
|
|
|
if err != nil {
|
2013-11-07 04:33:31 -08:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ids = []string{}
|
2013-10-31 18:07:54 -07:00
|
|
|
}
|
|
|
|
|
2014-01-14 12:23:20 +01:00
|
|
|
// Protect the a.active from concurrent access
|
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
|
|
|
|
count := a.active[id]
|
|
|
|
|
2013-11-07 04:33:31 -08:00
|
|
|
// If a dir does not have a parent ( no layers )do not try to mount
|
|
|
|
// just return the diff path to the data
|
|
|
|
out := path.Join(a.rootPath(), "diff", id)
|
|
|
|
if len(ids) > 0 {
|
|
|
|
out = path.Join(a.rootPath(), "mnt", id)
|
2014-01-14 12:23:20 +01:00
|
|
|
|
|
|
|
if count == 0 {
|
2014-04-17 23:47:27 +00:00
|
|
|
if err := a.mount(id, mountLabel); err != nil {
|
2014-01-14 12:23:20 +01:00
|
|
|
return "", err
|
|
|
|
}
|
2013-11-07 04:33:31 -08:00
|
|
|
}
|
|
|
|
}
|
2014-01-14 12:23:20 +01:00
|
|
|
|
|
|
|
a.active[id] = count + 1
|
|
|
|
|
2013-11-07 04:33:31 -08:00
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2015-07-22 02:15:14 +00:00
|
|
|
// Put unmounts and updates list of active mounts.
|
2015-01-09 17:14:52 -05:00
|
|
|
func (a *Driver) Put(id string) error {
|
2014-01-14 12:23:20 +01:00
|
|
|
// Protect the a.active from concurrent access
|
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
|
|
|
|
if count := a.active[id]; count > 1 {
|
|
|
|
a.active[id] = count - 1
|
|
|
|
} else {
|
|
|
|
ids, _ := getParentIds(a.rootPath(), id)
|
|
|
|
// We only mounted if there are any parents
|
|
|
|
if ids != nil && len(ids) > 0 {
|
|
|
|
a.unmount(id)
|
|
|
|
}
|
|
|
|
delete(a.active, id)
|
|
|
|
}
|
2015-01-09 17:14:52 -05:00
|
|
|
return nil
|
2013-12-05 22:18:02 +01:00
|
|
|
}
|
|
|
|
|
2014-09-10 20:30:52 -07:00
|
|
|
// Diff produces an archive of the changes between the specified
|
|
|
|
// layer and its parent layer which may be "".
|
|
|
|
func (a *Driver) Diff(id, parent string) (archive.Archive, error) {
|
|
|
|
// AUFS doesn't need the parent layer to produce a diff.
|
2014-02-13 16:05:36 -08:00
|
|
|
return archive.TarWithOptions(path.Join(a.rootPath(), "diff", id), &archive.TarOptions{
|
2014-10-23 14:30:11 -07:00
|
|
|
Compression: archive.Uncompressed,
|
|
|
|
ExcludePatterns: []string{".wh..wh.*"},
|
2013-11-11 17:17:38 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-04 09:52:54 +08:00
|
|
|
func (a *Driver) applyDiff(id string, diff archive.Reader) error {
|
2015-07-27 13:23:52 -04:00
|
|
|
return chrootarchive.UntarUncompressed(diff, path.Join(a.rootPath(), "diff", id), nil)
|
2013-11-07 04:33:31 -08:00
|
|
|
}
|
2013-10-31 18:07:54 -07:00
|
|
|
|
2014-09-10 20:30:52 -07:00
|
|
|
// DiffSize calculates the changes between the specified id
|
|
|
|
// and its parent and returns the size in bytes of the changes
|
|
|
|
// relative to its base filesystem directory.
|
2014-12-17 18:26:03 -08:00
|
|
|
func (a *Driver) DiffSize(id, parent string) (size int64, err error) {
|
2014-09-10 20:30:52 -07:00
|
|
|
// AUFS doesn't need the parent layer to calculate the diff size.
|
2015-02-27 19:50:55 +01:00
|
|
|
return directory.Size(path.Join(a.rootPath(), "diff", id))
|
2013-11-07 04:33:31 -08:00
|
|
|
}
|
|
|
|
|
2014-09-10 20:30:52 -07:00
|
|
|
// ApplyDiff extracts the changeset from the given diff into the
|
|
|
|
// layer with the specified id and parent, returning the size of the
|
|
|
|
// new layer in bytes.
|
2015-08-04 09:52:54 +08:00
|
|
|
func (a *Driver) ApplyDiff(id, parent string, diff archive.Reader) (size int64, err error) {
|
2014-09-10 20:30:52 -07:00
|
|
|
// AUFS doesn't need the parent id to apply the diff.
|
|
|
|
if err = a.applyDiff(id, diff); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.DiffSize(id, parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Changes produces a list of changes between the specified layer
|
|
|
|
// and its parent layer. If parent is "", then all changes will be ADD changes.
|
|
|
|
func (a *Driver) Changes(id, parent string) ([]archive.Change, error) {
|
|
|
|
// AUFS doesn't have snapshots, so we need to get changes from all parent
|
|
|
|
// layers.
|
2013-11-08 11:10:33 -08:00
|
|
|
layers, err := a.getParentLayerPaths(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return archive.Changes(layers, path.Join(a.rootPath(), "diff", id))
|
|
|
|
}
|
|
|
|
|
2013-11-19 03:27:59 -08:00
|
|
|
func (a *Driver) getParentLayerPaths(id string) ([]string, error) {
|
2013-11-08 11:10:33 -08:00
|
|
|
parentIds, err := getParentIds(a.rootPath(), id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
layers := make([]string, len(parentIds))
|
|
|
|
|
|
|
|
// Get the diff paths for all the parent ids
|
|
|
|
for i, p := range parentIds {
|
|
|
|
layers[i] = path.Join(a.rootPath(), "diff", p)
|
|
|
|
}
|
|
|
|
return layers, nil
|
2013-11-07 04:33:31 -08:00
|
|
|
}
|
|
|
|
|
2014-04-17 23:47:27 +00:00
|
|
|
func (a *Driver) mount(id, mountLabel string) error {
|
2013-11-07 04:33:31 -08:00
|
|
|
// If the id is mounted or we get an error return
|
|
|
|
if mounted, err := a.mounted(id); err != nil || mounted {
|
2013-10-31 18:07:54 -07:00
|
|
|
return err
|
|
|
|
}
|
2013-11-07 04:33:31 -08:00
|
|
|
|
|
|
|
var (
|
|
|
|
target = path.Join(a.rootPath(), "mnt", id)
|
|
|
|
rw = path.Join(a.rootPath(), "diff", id)
|
|
|
|
)
|
|
|
|
|
2013-11-08 11:10:33 -08:00
|
|
|
layers, err := a.getParentLayerPaths(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-11-07 04:33:31 -08:00
|
|
|
}
|
|
|
|
|
2014-04-17 23:47:27 +00:00
|
|
|
if err := a.aufsMount(layers, rw, target, mountLabel); err != nil {
|
2015-03-12 06:15:32 -07:00
|
|
|
return fmt.Errorf("error creating aufs mount to %s: %v", target, err)
|
2013-10-31 18:07:54 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-19 03:27:59 -08:00
|
|
|
func (a *Driver) unmount(id string) error {
|
2013-11-07 04:33:31 -08:00
|
|
|
if mounted, err := a.mounted(id); err != nil || !mounted {
|
2013-10-31 18:07:54 -07:00
|
|
|
return err
|
|
|
|
}
|
2013-11-07 04:33:31 -08:00
|
|
|
target := path.Join(a.rootPath(), "mnt", id)
|
2013-10-31 18:07:54 -07:00
|
|
|
return Unmount(target)
|
|
|
|
}
|
|
|
|
|
2013-11-19 03:27:59 -08:00
|
|
|
func (a *Driver) mounted(id string) (bool, error) {
|
2013-11-07 04:33:31 -08:00
|
|
|
target := path.Join(a.rootPath(), "mnt", id)
|
2013-12-18 16:42:49 -08:00
|
|
|
return mountpk.Mounted(target)
|
2013-11-04 20:51:12 -08:00
|
|
|
}
|
|
|
|
|
2015-07-22 02:15:14 +00:00
|
|
|
// Cleanup aufs and unmount all mountpoints
|
2013-11-19 03:27:59 -08:00
|
|
|
func (a *Driver) Cleanup() error {
|
2013-11-07 04:33:31 -08:00
|
|
|
ids, err := loadIds(path.Join(a.rootPath(), "layers"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-05 12:50:53 -07:00
|
|
|
|
2013-11-07 04:33:31 -08:00
|
|
|
for _, id := range ids {
|
|
|
|
if err := a.unmount(id); err != nil {
|
2015-03-26 23:22:04 +01:00
|
|
|
logrus.Errorf("Unmounting %s: %s", stringid.TruncateID(id), err)
|
2013-11-04 20:51:12 -08:00
|
|
|
}
|
|
|
|
}
|
2014-06-05 12:50:53 -07:00
|
|
|
|
|
|
|
return mountpk.Unmount(a.root)
|
2013-10-31 18:07:54 -07:00
|
|
|
}
|
|
|
|
|
2014-04-17 23:47:27 +00:00
|
|
|
func (a *Driver) aufsMount(ro []string, rw, target, mountLabel string) (err error) {
|
2013-11-26 10:50:53 -08:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
Unmount(target)
|
|
|
|
}
|
|
|
|
}()
|
2013-10-31 18:07:54 -07:00
|
|
|
|
2014-11-13 19:57:28 +02:00
|
|
|
// Mount options are clipped to page size(4096 bytes). If there are more
|
|
|
|
// layers then these are remounted individually using append.
|
|
|
|
|
2015-03-26 06:02:21 +00:00
|
|
|
offset := 54
|
|
|
|
if useDirperm() {
|
|
|
|
offset += len("dirperm1")
|
|
|
|
}
|
|
|
|
b := make([]byte, syscall.Getpagesize()-len(mountLabel)-offset) // room for xino & mountLabel
|
2014-11-13 19:57:28 +02:00
|
|
|
bp := copy(b, fmt.Sprintf("br:%s=rw", rw))
|
|
|
|
|
|
|
|
firstMount := true
|
|
|
|
i := 0
|
|
|
|
|
|
|
|
for {
|
|
|
|
for ; i < len(ro); i++ {
|
|
|
|
layer := fmt.Sprintf(":%s=ro+wh", ro[i])
|
|
|
|
|
|
|
|
if firstMount {
|
|
|
|
if bp+len(layer) > len(b) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
bp += copy(b[bp:], layer)
|
|
|
|
} else {
|
|
|
|
data := label.FormatMountLabel(fmt.Sprintf("append%s", layer), mountLabel)
|
2015-07-22 02:15:14 +00:00
|
|
|
if err = mount("none", target, "aufs", syscall.MS_REMOUNT, data); err != nil {
|
2014-11-13 19:57:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2013-10-31 18:07:54 -07:00
|
|
|
}
|
2013-11-26 10:50:53 -08:00
|
|
|
|
2014-11-13 19:57:28 +02:00
|
|
|
if firstMount {
|
2015-03-26 06:02:21 +00:00
|
|
|
opts := "dio,xino=/dev/shm/aufs.xino"
|
|
|
|
if useDirperm() {
|
|
|
|
opts += ",dirperm1"
|
|
|
|
}
|
|
|
|
data := label.FormatMountLabel(fmt.Sprintf("%s,%s", string(b[:bp]), opts), mountLabel)
|
2014-11-13 19:57:28 +02:00
|
|
|
if err = mount("none", target, "aufs", 0, data); err != nil {
|
2013-11-26 10:50:53 -08:00
|
|
|
return
|
|
|
|
}
|
2014-11-13 19:57:28 +02:00
|
|
|
firstMount = false
|
2013-10-31 18:07:54 -07:00
|
|
|
}
|
2013-11-26 10:50:53 -08:00
|
|
|
|
2014-11-13 19:57:28 +02:00
|
|
|
if i == len(ro) {
|
|
|
|
break
|
|
|
|
}
|
2013-11-26 10:50:53 -08:00
|
|
|
}
|
2014-11-13 19:57:28 +02:00
|
|
|
|
|
|
|
return
|
2013-10-31 18:07:54 -07:00
|
|
|
}
|
2015-03-26 06:02:21 +00:00
|
|
|
|
|
|
|
// useDirperm checks dirperm1 mount option can be used with the current
|
|
|
|
// version of aufs.
|
|
|
|
func useDirperm() bool {
|
|
|
|
enableDirpermLock.Do(func() {
|
|
|
|
base, err := ioutil.TempDir("", "docker-aufs-base")
|
|
|
|
if err != nil {
|
2015-03-30 11:46:44 -07:00
|
|
|
logrus.Errorf("error checking dirperm1: %v", err)
|
2015-03-26 06:02:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(base)
|
|
|
|
|
|
|
|
union, err := ioutil.TempDir("", "docker-aufs-union")
|
|
|
|
if err != nil {
|
2015-03-30 11:46:44 -07:00
|
|
|
logrus.Errorf("error checking dirperm1: %v", err)
|
2015-03-26 06:02:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(union)
|
|
|
|
|
|
|
|
opts := fmt.Sprintf("br:%s,dirperm1,xino=/dev/shm/aufs.xino", base)
|
|
|
|
if err := mount("none", union, "aufs", 0, opts); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
enableDirperm = true
|
|
|
|
if err := Unmount(union); err != nil {
|
2015-03-30 11:46:44 -07:00
|
|
|
logrus.Errorf("error checking dirperm1: failed to unmount %v", err)
|
2015-03-26 06:02:21 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return enableDirperm
|
|
|
|
}
|