Adds a new overlay driver which uses multiple lower directories to create the union fs.
Additionally it uses symlinks and relative mount paths to allow a depth of 128 and stay within the mount page size limit.
Diffs and done directly over a single directory allowing diffs to be done efficiently and without the need fo the naive diff driver.
Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,91 @@ |
| 0 |
+// +build linux |
|
| 1 |
+ |
|
| 2 |
+package overlay2 |
|
| 3 |
+ |
|
| 4 |
+import ( |
|
| 5 |
+ "bytes" |
|
| 6 |
+ "encoding/json" |
|
| 7 |
+ "flag" |
|
| 8 |
+ "fmt" |
|
| 9 |
+ "os" |
|
| 10 |
+ "runtime" |
|
| 11 |
+ "syscall" |
|
| 12 |
+ |
|
| 13 |
+ "github.com/docker/docker/pkg/reexec" |
|
| 14 |
+) |
|
| 15 |
+ |
|
| 16 |
+func init() {
|
|
| 17 |
+ reexec.Register("docker-mountfrom", mountFromMain)
|
|
| 18 |
+} |
|
| 19 |
+ |
|
| 20 |
+func fatal(err error) {
|
|
| 21 |
+ fmt.Fprint(os.Stderr, err) |
|
| 22 |
+ os.Exit(1) |
|
| 23 |
+} |
|
| 24 |
+ |
|
| 25 |
+type mountOptions struct {
|
|
| 26 |
+ Device string |
|
| 27 |
+ Target string |
|
| 28 |
+ Type string |
|
| 29 |
+ Label string |
|
| 30 |
+ Flag uint32 |
|
| 31 |
+} |
|
| 32 |
+ |
|
| 33 |
+func mountFrom(dir, device, target, mType, label string) error {
|
|
| 34 |
+ |
|
| 35 |
+ r, w, err := os.Pipe() |
|
| 36 |
+ if err != nil {
|
|
| 37 |
+ return fmt.Errorf("mountfrom pipe failure: %v", err)
|
|
| 38 |
+ } |
|
| 39 |
+ |
|
| 40 |
+ options := &mountOptions{
|
|
| 41 |
+ Device: device, |
|
| 42 |
+ Target: target, |
|
| 43 |
+ Type: mType, |
|
| 44 |
+ Flag: 0, |
|
| 45 |
+ Label: label, |
|
| 46 |
+ } |
|
| 47 |
+ |
|
| 48 |
+ cmd := reexec.Command("docker-mountfrom", dir)
|
|
| 49 |
+ cmd.Stdin = r |
|
| 50 |
+ |
|
| 51 |
+ output := bytes.NewBuffer(nil) |
|
| 52 |
+ cmd.Stdout = output |
|
| 53 |
+ cmd.Stderr = output |
|
| 54 |
+ |
|
| 55 |
+ if err := cmd.Start(); err != nil {
|
|
| 56 |
+ return fmt.Errorf("mountfrom error on re-exec cmd: %v", err)
|
|
| 57 |
+ } |
|
| 58 |
+ //write the options to the pipe for the untar exec to read |
|
| 59 |
+ if err := json.NewEncoder(w).Encode(options); err != nil {
|
|
| 60 |
+ return fmt.Errorf("mountfrom json encode to pipe failed: %v", err)
|
|
| 61 |
+ } |
|
| 62 |
+ w.Close() |
|
| 63 |
+ |
|
| 64 |
+ if err := cmd.Wait(); err != nil {
|
|
| 65 |
+ return fmt.Errorf("mountfrom re-exec error: %v: output: %s", err, output)
|
|
| 66 |
+ } |
|
| 67 |
+ return nil |
|
| 68 |
+} |
|
| 69 |
+ |
|
| 70 |
+// mountfromMain is the entry-point for docker-mountfrom on re-exec. |
|
| 71 |
+func mountFromMain() {
|
|
| 72 |
+ runtime.LockOSThread() |
|
| 73 |
+ flag.Parse() |
|
| 74 |
+ |
|
| 75 |
+ var options *mountOptions |
|
| 76 |
+ |
|
| 77 |
+ if err := json.NewDecoder(os.Stdin).Decode(&options); err != nil {
|
|
| 78 |
+ fatal(err) |
|
| 79 |
+ } |
|
| 80 |
+ |
|
| 81 |
+ if err := os.Chdir(flag.Arg(0)); err != nil {
|
|
| 82 |
+ fatal(err) |
|
| 83 |
+ } |
|
| 84 |
+ |
|
| 85 |
+ if err := syscall.Mount(options.Device, options.Target, options.Type, uintptr(options.Flag), options.Label); err != nil {
|
|
| 86 |
+ fatal(err) |
|
| 87 |
+ } |
|
| 88 |
+ |
|
| 89 |
+ os.Exit(0) |
|
| 90 |
+} |
| 0 | 91 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,476 @@ |
| 0 |
+// +build linux |
|
| 1 |
+ |
|
| 2 |
+package overlay2 |
|
| 3 |
+ |
|
| 4 |
+import ( |
|
| 5 |
+ "bufio" |
|
| 6 |
+ "errors" |
|
| 7 |
+ "fmt" |
|
| 8 |
+ "io/ioutil" |
|
| 9 |
+ "os" |
|
| 10 |
+ "os/exec" |
|
| 11 |
+ "path" |
|
| 12 |
+ "strings" |
|
| 13 |
+ "syscall" |
|
| 14 |
+ |
|
| 15 |
+ "github.com/Sirupsen/logrus" |
|
| 16 |
+ |
|
| 17 |
+ "github.com/docker/docker/daemon/graphdriver" |
|
| 18 |
+ "github.com/docker/docker/pkg/archive" |
|
| 19 |
+ "github.com/docker/docker/pkg/chrootarchive" |
|
| 20 |
+ "github.com/docker/docker/pkg/directory" |
|
| 21 |
+ "github.com/docker/docker/pkg/idtools" |
|
| 22 |
+ "github.com/docker/docker/pkg/mount" |
|
| 23 |
+ "github.com/docker/docker/pkg/parsers/kernel" |
|
| 24 |
+ |
|
| 25 |
+ "github.com/opencontainers/runc/libcontainer/label" |
|
| 26 |
+) |
|
| 27 |
+ |
|
| 28 |
+var ( |
|
| 29 |
+ // untar defines the untar method |
|
| 30 |
+ untar = chrootarchive.UntarUncompressed |
|
| 31 |
+) |
|
| 32 |
+ |
|
| 33 |
+// This backend uses the overlay union filesystem for containers |
|
| 34 |
+// with diff directories for each layer. |
|
| 35 |
+ |
|
| 36 |
+// This version of the overlay driver requires at least kernel |
|
| 37 |
+// 4.0.0 in order to support mounting multiple diff directories. |
|
| 38 |
+ |
|
| 39 |
+// Each container/image has at least a "diff" directory and "link" file. |
|
| 40 |
+// If there is also a "lower" file when there are diff layers |
|
| 41 |
+// below as well as "merged" and "work" directories. The "diff" directory |
|
| 42 |
+// has the upper layer of the overlay and is used to capture any |
|
| 43 |
+// changes to the layer. The "lower" file contains all the lower layer |
|
| 44 |
+// mounts separated by ":" and ordered from uppermost to lowermost |
|
| 45 |
+// layers. The overlay itself is mounted in the "merged" directory, |
|
| 46 |
+// and the "work" dir is needed for overlay to work. |
|
| 47 |
+ |
|
| 48 |
+// The "link" file for each layer contains a unique string for the layer. |
|
| 49 |
+// Under the "l" directory at the root there will be a symbolic link |
|
| 50 |
+// with that unique string pointing the "diff" directory for the layer. |
|
| 51 |
+// The symbolic links are used to reference lower layers in the "lower" |
|
| 52 |
+// file and on mount. The links are used to shorten the total length |
|
| 53 |
+// of a layer reference without requiring changes to the layer identifier |
|
| 54 |
+// or root directory. Mounts are always done relative to root and |
|
| 55 |
+// referencing the symbolic links in order to ensure the number of |
|
| 56 |
+// lower directories can fit in a single page for making the mount |
|
| 57 |
+// syscall. A hard upper limit of 128 lower layers is enforced to ensure |
|
| 58 |
+// that mounts do not fail due to length. |
|
| 59 |
+ |
|
| 60 |
+const ( |
|
| 61 |
+ driverName = "overlay2" |
|
| 62 |
+ linkDir = "l" |
|
| 63 |
+ lowerFile = "lower" |
|
| 64 |
+ maxDepth = 128 |
|
| 65 |
+ |
|
| 66 |
+ // idLength represents the number of random characters |
|
| 67 |
+ // which can be used to create the unique link identifer |
|
| 68 |
+ // for every layer. If this value is too long then the |
|
| 69 |
+ // page size limit for the mount command may be exceeded. |
|
| 70 |
+ // The idLength should be selected such that following equation |
|
| 71 |
+ // is true (512 is a buffer for label metadata). |
|
| 72 |
+ // ((idLength + len(linkDir) + 1) * maxDepth) <= (pageSize - 512) |
|
| 73 |
+ idLength = 26 |
|
| 74 |
+) |
|
| 75 |
+ |
|
| 76 |
+// Driver contains information about the home directory and the list of active mounts that are created using this driver. |
|
| 77 |
+type Driver struct {
|
|
| 78 |
+ home string |
|
| 79 |
+ uidMaps []idtools.IDMap |
|
| 80 |
+ gidMaps []idtools.IDMap |
|
| 81 |
+ ctr *graphdriver.RefCounter |
|
| 82 |
+} |
|
| 83 |
+ |
|
| 84 |
+var backingFs = "<unknown>" |
|
| 85 |
+ |
|
| 86 |
+func init() {
|
|
| 87 |
+ graphdriver.Register(driverName, Init) |
|
| 88 |
+} |
|
| 89 |
+ |
|
| 90 |
+// Init returns the a native diff driver for overlay filesystem. |
|
| 91 |
+// If overlay filesystem is not supported on the host, graphdriver.ErrNotSupported is returned as error. |
|
| 92 |
+// If a overlay filesystem is not supported over a existing filesystem then error graphdriver.ErrIncompatibleFS is returned. |
|
| 93 |
+func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
|
|
| 94 |
+ |
|
| 95 |
+ if err := supportsOverlay(); err != nil {
|
|
| 96 |
+ return nil, graphdriver.ErrNotSupported |
|
| 97 |
+ } |
|
| 98 |
+ |
|
| 99 |
+ // require kernel 4.0.0 to ensure multiple lower dirs are supported |
|
| 100 |
+ v, err := kernel.GetKernelVersion() |
|
| 101 |
+ if err != nil {
|
|
| 102 |
+ return nil, err |
|
| 103 |
+ } |
|
| 104 |
+ if kernel.CompareKernelVersion(*v, kernel.VersionInfo{Kernel: 4, Major: 0, Minor: 0}) < 0 {
|
|
| 105 |
+ return nil, graphdriver.ErrNotSupported |
|
| 106 |
+ } |
|
| 107 |
+ |
|
| 108 |
+ fsMagic, err := graphdriver.GetFSMagic(home) |
|
| 109 |
+ if err != nil {
|
|
| 110 |
+ return nil, err |
|
| 111 |
+ } |
|
| 112 |
+ if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
|
|
| 113 |
+ backingFs = fsName |
|
| 114 |
+ } |
|
| 115 |
+ |
|
| 116 |
+ // check if they are running over btrfs, aufs, zfs or overlay |
|
| 117 |
+ switch fsMagic {
|
|
| 118 |
+ case graphdriver.FsMagicBtrfs: |
|
| 119 |
+ logrus.Error("'overlay' is not supported over btrfs.")
|
|
| 120 |
+ return nil, graphdriver.ErrIncompatibleFS |
|
| 121 |
+ case graphdriver.FsMagicAufs: |
|
| 122 |
+ logrus.Error("'overlay' is not supported over aufs.")
|
|
| 123 |
+ return nil, graphdriver.ErrIncompatibleFS |
|
| 124 |
+ case graphdriver.FsMagicZfs: |
|
| 125 |
+ logrus.Error("'overlay' is not supported over zfs.")
|
|
| 126 |
+ return nil, graphdriver.ErrIncompatibleFS |
|
| 127 |
+ case graphdriver.FsMagicOverlay: |
|
| 128 |
+ logrus.Error("'overlay' is not supported over overlay.")
|
|
| 129 |
+ return nil, graphdriver.ErrIncompatibleFS |
|
| 130 |
+ } |
|
| 131 |
+ |
|
| 132 |
+ rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps) |
|
| 133 |
+ if err != nil {
|
|
| 134 |
+ return nil, err |
|
| 135 |
+ } |
|
| 136 |
+ // Create the driver home dir |
|
| 137 |
+ if err := idtools.MkdirAllAs(path.Join(home, linkDir), 0700, rootUID, rootGID); err != nil && !os.IsExist(err) {
|
|
| 138 |
+ return nil, err |
|
| 139 |
+ } |
|
| 140 |
+ |
|
| 141 |
+ if err := mount.MakePrivate(home); err != nil {
|
|
| 142 |
+ return nil, err |
|
| 143 |
+ } |
|
| 144 |
+ |
|
| 145 |
+ d := &Driver{
|
|
| 146 |
+ home: home, |
|
| 147 |
+ uidMaps: uidMaps, |
|
| 148 |
+ gidMaps: gidMaps, |
|
| 149 |
+ ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicOverlay)), |
|
| 150 |
+ } |
|
| 151 |
+ |
|
| 152 |
+ return d, nil |
|
| 153 |
+} |
|
| 154 |
+ |
|
| 155 |
+func supportsOverlay() error {
|
|
| 156 |
+ // We can try to modprobe overlay first before looking at |
|
| 157 |
+ // proc/filesystems for when overlay is supported |
|
| 158 |
+ exec.Command("modprobe", "overlay").Run()
|
|
| 159 |
+ |
|
| 160 |
+ f, err := os.Open("/proc/filesystems")
|
|
| 161 |
+ if err != nil {
|
|
| 162 |
+ return err |
|
| 163 |
+ } |
|
| 164 |
+ defer f.Close() |
|
| 165 |
+ |
|
| 166 |
+ s := bufio.NewScanner(f) |
|
| 167 |
+ for s.Scan() {
|
|
| 168 |
+ if s.Text() == "nodev\toverlay" {
|
|
| 169 |
+ return nil |
|
| 170 |
+ } |
|
| 171 |
+ } |
|
| 172 |
+ logrus.Error("'overlay' not found as a supported filesystem on this host. Please ensure kernel is new enough and has overlay support loaded.")
|
|
| 173 |
+ return graphdriver.ErrNotSupported |
|
| 174 |
+} |
|
| 175 |
+ |
|
| 176 |
+func (d *Driver) String() string {
|
|
| 177 |
+ return driverName |
|
| 178 |
+} |
|
| 179 |
+ |
|
| 180 |
+// Status returns current driver information in a two dimensional string array. |
|
| 181 |
+// Output contains "Backing Filesystem" used in this implementation. |
|
| 182 |
+func (d *Driver) Status() [][2]string {
|
|
| 183 |
+ return [][2]string{
|
|
| 184 |
+ {"Backing Filesystem", backingFs},
|
|
| 185 |
+ } |
|
| 186 |
+} |
|
| 187 |
+ |
|
| 188 |
+// GetMetadata returns meta data about the overlay driver such as |
|
| 189 |
+// LowerDir, UpperDir, WorkDir and MergeDir used to store data. |
|
| 190 |
+func (d *Driver) GetMetadata(id string) (map[string]string, error) {
|
|
| 191 |
+ dir := d.dir(id) |
|
| 192 |
+ if _, err := os.Stat(dir); err != nil {
|
|
| 193 |
+ return nil, err |
|
| 194 |
+ } |
|
| 195 |
+ |
|
| 196 |
+ metadata := map[string]string{
|
|
| 197 |
+ "WorkDir": path.Join(dir, "work"), |
|
| 198 |
+ "MergedDir": path.Join(dir, "merged"), |
|
| 199 |
+ "UpperDir": path.Join(dir, "diff"), |
|
| 200 |
+ } |
|
| 201 |
+ |
|
| 202 |
+ lowerDirs, err := d.getLowerDirs(id) |
|
| 203 |
+ if err != nil {
|
|
| 204 |
+ return nil, err |
|
| 205 |
+ } |
|
| 206 |
+ if len(lowerDirs) > 0 {
|
|
| 207 |
+ metadata["LowerDir"] = strings.Join(lowerDirs, ":") |
|
| 208 |
+ } |
|
| 209 |
+ |
|
| 210 |
+ return metadata, nil |
|
| 211 |
+} |
|
| 212 |
+ |
|
| 213 |
+// Cleanup any state created by overlay which should be cleaned when daemon |
|
| 214 |
+// is being shutdown. For now, we just have to unmount the bind mounted |
|
| 215 |
+// we had created. |
|
| 216 |
+func (d *Driver) Cleanup() error {
|
|
| 217 |
+ return mount.Unmount(d.home) |
|
| 218 |
+} |
|
| 219 |
+ |
|
| 220 |
+// CreateReadWrite creates a layer that is writable for use as a container |
|
| 221 |
+// file system. |
|
| 222 |
+func (d *Driver) CreateReadWrite(id, parent, mountLabel string, storageOpt map[string]string) error {
|
|
| 223 |
+ return d.Create(id, parent, mountLabel, storageOpt) |
|
| 224 |
+} |
|
| 225 |
+ |
|
| 226 |
+// Create is used to create the upper, lower, and merge directories required for overlay fs for a given id. |
|
| 227 |
+// The parent filesystem is used to configure these directories for the overlay. |
|
| 228 |
+func (d *Driver) Create(id, parent, mountLabel string, storageOpt map[string]string) (retErr error) {
|
|
| 229 |
+ |
|
| 230 |
+ if len(storageOpt) != 0 {
|
|
| 231 |
+ return fmt.Errorf("--storage-opt is not supported for overlay")
|
|
| 232 |
+ } |
|
| 233 |
+ |
|
| 234 |
+ dir := d.dir(id) |
|
| 235 |
+ |
|
| 236 |
+ rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps) |
|
| 237 |
+ if err != nil {
|
|
| 238 |
+ return err |
|
| 239 |
+ } |
|
| 240 |
+ if err := idtools.MkdirAllAs(path.Dir(dir), 0700, rootUID, rootGID); err != nil {
|
|
| 241 |
+ return err |
|
| 242 |
+ } |
|
| 243 |
+ if err := idtools.MkdirAs(dir, 0700, rootUID, rootGID); err != nil {
|
|
| 244 |
+ return err |
|
| 245 |
+ } |
|
| 246 |
+ |
|
| 247 |
+ defer func() {
|
|
| 248 |
+ // Clean up on failure |
|
| 249 |
+ if retErr != nil {
|
|
| 250 |
+ os.RemoveAll(dir) |
|
| 251 |
+ } |
|
| 252 |
+ }() |
|
| 253 |
+ |
|
| 254 |
+ if err := idtools.MkdirAs(path.Join(dir, "diff"), 0755, rootUID, rootGID); err != nil {
|
|
| 255 |
+ return err |
|
| 256 |
+ } |
|
| 257 |
+ |
|
| 258 |
+ lid := generateID(idLength) |
|
| 259 |
+ if err := os.Symlink(path.Join("..", id, "diff"), path.Join(d.home, linkDir, lid)); err != nil {
|
|
| 260 |
+ return err |
|
| 261 |
+ } |
|
| 262 |
+ |
|
| 263 |
+ // Write link id to link file |
|
| 264 |
+ if err := ioutil.WriteFile(path.Join(dir, "link"), []byte(lid), 0644); err != nil {
|
|
| 265 |
+ return err |
|
| 266 |
+ } |
|
| 267 |
+ |
|
| 268 |
+ // if no parent directory, done |
|
| 269 |
+ if parent == "" {
|
|
| 270 |
+ return nil |
|
| 271 |
+ } |
|
| 272 |
+ |
|
| 273 |
+ if err := idtools.MkdirAs(path.Join(dir, "work"), 0700, rootUID, rootGID); err != nil {
|
|
| 274 |
+ return err |
|
| 275 |
+ } |
|
| 276 |
+ if err := idtools.MkdirAs(path.Join(dir, "merged"), 0700, rootUID, rootGID); err != nil {
|
|
| 277 |
+ return err |
|
| 278 |
+ } |
|
| 279 |
+ |
|
| 280 |
+ lower, err := d.getLower(parent) |
|
| 281 |
+ if err != nil {
|
|
| 282 |
+ return err |
|
| 283 |
+ } |
|
| 284 |
+ if lower != "" {
|
|
| 285 |
+ if err := ioutil.WriteFile(path.Join(dir, lowerFile), []byte(lower), 0666); err != nil {
|
|
| 286 |
+ return err |
|
| 287 |
+ } |
|
| 288 |
+ } |
|
| 289 |
+ |
|
| 290 |
+ return nil |
|
| 291 |
+} |
|
| 292 |
+ |
|
| 293 |
+func (d *Driver) getLower(parent string) (string, error) {
|
|
| 294 |
+ parentDir := d.dir(parent) |
|
| 295 |
+ |
|
| 296 |
+ // Ensure parent exists |
|
| 297 |
+ if _, err := os.Lstat(parentDir); err != nil {
|
|
| 298 |
+ return "", err |
|
| 299 |
+ } |
|
| 300 |
+ |
|
| 301 |
+ // Read Parent link fileA |
|
| 302 |
+ parentLink, err := ioutil.ReadFile(path.Join(parentDir, "link")) |
|
| 303 |
+ if err != nil {
|
|
| 304 |
+ return "", err |
|
| 305 |
+ } |
|
| 306 |
+ lowers := []string{path.Join(linkDir, string(parentLink))}
|
|
| 307 |
+ |
|
| 308 |
+ parentLower, err := ioutil.ReadFile(path.Join(parentDir, lowerFile)) |
|
| 309 |
+ if err == nil {
|
|
| 310 |
+ parentLowers := strings.Split(string(parentLower), ":") |
|
| 311 |
+ lowers = append(lowers, parentLowers...) |
|
| 312 |
+ } |
|
| 313 |
+ if len(lowers) > maxDepth {
|
|
| 314 |
+ return "", errors.New("max depth exceeded")
|
|
| 315 |
+ } |
|
| 316 |
+ return strings.Join(lowers, ":"), nil |
|
| 317 |
+} |
|
| 318 |
+ |
|
| 319 |
+func (d *Driver) dir(id string) string {
|
|
| 320 |
+ return path.Join(d.home, id) |
|
| 321 |
+} |
|
| 322 |
+ |
|
| 323 |
+func (d *Driver) getLowerDirs(id string) ([]string, error) {
|
|
| 324 |
+ var lowersArray []string |
|
| 325 |
+ lowers, err := ioutil.ReadFile(path.Join(d.dir(id), lowerFile)) |
|
| 326 |
+ if err == nil {
|
|
| 327 |
+ for _, s := range strings.Split(string(lowers), ":") {
|
|
| 328 |
+ lp, err := os.Readlink(path.Join(d.home, s)) |
|
| 329 |
+ if err != nil {
|
|
| 330 |
+ return nil, err |
|
| 331 |
+ } |
|
| 332 |
+ lowersArray = append(lowersArray, path.Clean(path.Join(d.home, "link", lp))) |
|
| 333 |
+ } |
|
| 334 |
+ } else if !os.IsNotExist(err) {
|
|
| 335 |
+ return nil, err |
|
| 336 |
+ } |
|
| 337 |
+ return lowersArray, nil |
|
| 338 |
+} |
|
| 339 |
+ |
|
| 340 |
+// Remove cleans the directories that are created for this id. |
|
| 341 |
+func (d *Driver) Remove(id string) error {
|
|
| 342 |
+ if err := os.RemoveAll(d.dir(id)); err != nil && !os.IsNotExist(err) {
|
|
| 343 |
+ return err |
|
| 344 |
+ } |
|
| 345 |
+ return nil |
|
| 346 |
+} |
|
| 347 |
+ |
|
| 348 |
+// Get creates and mounts the required file system for the given id and returns the mount path. |
|
| 349 |
+func (d *Driver) Get(id string, mountLabel string) (s string, err error) {
|
|
| 350 |
+ dir := d.dir(id) |
|
| 351 |
+ if _, err := os.Stat(dir); err != nil {
|
|
| 352 |
+ return "", err |
|
| 353 |
+ } |
|
| 354 |
+ |
|
| 355 |
+ diffDir := path.Join(dir, "diff") |
|
| 356 |
+ lowers, err := ioutil.ReadFile(path.Join(dir, lowerFile)) |
|
| 357 |
+ if err != nil {
|
|
| 358 |
+ // If no lower, just return diff directory |
|
| 359 |
+ if os.IsNotExist(err) {
|
|
| 360 |
+ return diffDir, nil |
|
| 361 |
+ } |
|
| 362 |
+ return "", err |
|
| 363 |
+ } |
|
| 364 |
+ |
|
| 365 |
+ mergedDir := path.Join(dir, "merged") |
|
| 366 |
+ if count := d.ctr.Increment(mergedDir); count > 1 {
|
|
| 367 |
+ return mergedDir, nil |
|
| 368 |
+ } |
|
| 369 |
+ defer func() {
|
|
| 370 |
+ if err != nil {
|
|
| 371 |
+ if c := d.ctr.Decrement(mergedDir); c <= 0 {
|
|
| 372 |
+ syscall.Unmount(mergedDir, 0) |
|
| 373 |
+ } |
|
| 374 |
+ } |
|
| 375 |
+ }() |
|
| 376 |
+ |
|
| 377 |
+ workDir := path.Join(dir, "work") |
|
| 378 |
+ opts := fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", string(lowers), path.Join(id, "diff"), path.Join(id, "work"))
|
|
| 379 |
+ mountLabel = label.FormatMountLabel(opts, mountLabel) |
|
| 380 |
+ if len(mountLabel) > syscall.Getpagesize() {
|
|
| 381 |
+ return "", fmt.Errorf("cannot mount layer, mount label too large %d", len(mountLabel))
|
|
| 382 |
+ } |
|
| 383 |
+ |
|
| 384 |
+ if err := mountFrom(d.home, "overlay", path.Join(id, "merged"), "overlay", mountLabel); err != nil {
|
|
| 385 |
+ return "", fmt.Errorf("error creating overlay mount to %s: %v", mergedDir, err)
|
|
| 386 |
+ } |
|
| 387 |
+ |
|
| 388 |
+ // chown "workdir/work" to the remapped root UID/GID. Overlay fs inside a |
|
| 389 |
+ // user namespace requires this to move a directory from lower to upper. |
|
| 390 |
+ rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps) |
|
| 391 |
+ if err != nil {
|
|
| 392 |
+ return "", err |
|
| 393 |
+ } |
|
| 394 |
+ |
|
| 395 |
+ if err := os.Chown(path.Join(workDir, "work"), rootUID, rootGID); err != nil {
|
|
| 396 |
+ return "", err |
|
| 397 |
+ } |
|
| 398 |
+ |
|
| 399 |
+ return mergedDir, nil |
|
| 400 |
+} |
|
| 401 |
+ |
|
| 402 |
+// Put unmounts the mount path created for the give id. |
|
| 403 |
+func (d *Driver) Put(id string) error {
|
|
| 404 |
+ mountpoint := path.Join(d.dir(id), "merged") |
|
| 405 |
+ if count := d.ctr.Decrement(mountpoint); count > 0 {
|
|
| 406 |
+ return nil |
|
| 407 |
+ } |
|
| 408 |
+ if err := syscall.Unmount(mountpoint, 0); err != nil {
|
|
| 409 |
+ logrus.Debugf("Failed to unmount %s overlay: %v", id, err)
|
|
| 410 |
+ } |
|
| 411 |
+ return nil |
|
| 412 |
+} |
|
| 413 |
+ |
|
| 414 |
+// Exists checks to see if the id is already mounted. |
|
| 415 |
+func (d *Driver) Exists(id string) bool {
|
|
| 416 |
+ _, err := os.Stat(d.dir(id)) |
|
| 417 |
+ return err == nil |
|
| 418 |
+} |
|
| 419 |
+ |
|
| 420 |
+// ApplyDiff applies the new layer into a root |
|
| 421 |
+func (d *Driver) ApplyDiff(id string, parent string, diff archive.Reader) (size int64, err error) {
|
|
| 422 |
+ applyDir := d.getDiffPath(id) |
|
| 423 |
+ |
|
| 424 |
+ logrus.Debugf("Applying tar in %s", applyDir)
|
|
| 425 |
+ // Overlay doesn't need the parent id to apply the diff |
|
| 426 |
+ if err := untar(diff, applyDir, &archive.TarOptions{
|
|
| 427 |
+ UIDMaps: d.uidMaps, |
|
| 428 |
+ GIDMaps: d.gidMaps, |
|
| 429 |
+ WhiteoutFormat: archive.OverlayWhiteoutFormat, |
|
| 430 |
+ }); err != nil {
|
|
| 431 |
+ return 0, err |
|
| 432 |
+ } |
|
| 433 |
+ |
|
| 434 |
+ return d.DiffSize(id, parent) |
|
| 435 |
+} |
|
| 436 |
+ |
|
| 437 |
+func (d *Driver) getDiffPath(id string) string {
|
|
| 438 |
+ dir := d.dir(id) |
|
| 439 |
+ |
|
| 440 |
+ return path.Join(dir, "diff") |
|
| 441 |
+} |
|
| 442 |
+ |
|
| 443 |
+// DiffSize calculates the changes between the specified id |
|
| 444 |
+// and its parent and returns the size in bytes of the changes |
|
| 445 |
+// relative to its base filesystem directory. |
|
| 446 |
+func (d *Driver) DiffSize(id, parent string) (size int64, err error) {
|
|
| 447 |
+ return directory.Size(d.getDiffPath(id)) |
|
| 448 |
+} |
|
| 449 |
+ |
|
| 450 |
+// Diff produces an archive of the changes between the specified |
|
| 451 |
+// layer and its parent layer which may be "". |
|
| 452 |
+func (d *Driver) Diff(id, parent string) (archive.Archive, error) {
|
|
| 453 |
+ diffPath := d.getDiffPath(id) |
|
| 454 |
+ logrus.Debugf("Tar with options on %s", diffPath)
|
|
| 455 |
+ return archive.TarWithOptions(diffPath, &archive.TarOptions{
|
|
| 456 |
+ Compression: archive.Uncompressed, |
|
| 457 |
+ UIDMaps: d.uidMaps, |
|
| 458 |
+ GIDMaps: d.gidMaps, |
|
| 459 |
+ WhiteoutFormat: archive.OverlayWhiteoutFormat, |
|
| 460 |
+ }) |
|
| 461 |
+} |
|
| 462 |
+ |
|
| 463 |
+// Changes produces a list of changes between the specified layer |
|
| 464 |
+// and its parent layer. If parent is "", then all changes will be ADD changes. |
|
| 465 |
+func (d *Driver) Changes(id, parent string) ([]archive.Change, error) {
|
|
| 466 |
+ // Overlay doesn't have snapshots, so we need to get changes from all parent |
|
| 467 |
+ // layers. |
|
| 468 |
+ diffPath := d.getDiffPath(id) |
|
| 469 |
+ layers, err := d.getLowerDirs(id) |
|
| 470 |
+ if err != nil {
|
|
| 471 |
+ return nil, err |
|
| 472 |
+ } |
|
| 473 |
+ |
|
| 474 |
+ return archive.OverlayChanges(layers, diffPath) |
|
| 475 |
+} |
| 0 | 476 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,106 @@ |
| 0 |
+// +build linux |
|
| 1 |
+ |
|
| 2 |
+package overlay2 |
|
| 3 |
+ |
|
| 4 |
+import ( |
|
| 5 |
+ "os" |
|
| 6 |
+ "syscall" |
|
| 7 |
+ "testing" |
|
| 8 |
+ |
|
| 9 |
+ "github.com/docker/docker/daemon/graphdriver" |
|
| 10 |
+ "github.com/docker/docker/daemon/graphdriver/graphtest" |
|
| 11 |
+ "github.com/docker/docker/pkg/archive" |
|
| 12 |
+ "github.com/docker/docker/pkg/reexec" |
|
| 13 |
+) |
|
| 14 |
+ |
|
| 15 |
+func init() {
|
|
| 16 |
+ // Do not sure chroot to speed run time and allow archive |
|
| 17 |
+ // errors or hangs to be debugged directly from the test process. |
|
| 18 |
+ untar = archive.UntarUncompressed |
|
| 19 |
+ graphdriver.ApplyUncompressedLayer = archive.ApplyUncompressedLayer |
|
| 20 |
+ |
|
| 21 |
+ reexec.Init() |
|
| 22 |
+} |
|
| 23 |
+ |
|
| 24 |
+func cdMountFrom(dir, device, target, mType, label string) error {
|
|
| 25 |
+ wd, err := os.Getwd() |
|
| 26 |
+ if err != nil {
|
|
| 27 |
+ return err |
|
| 28 |
+ } |
|
| 29 |
+ os.Chdir(dir) |
|
| 30 |
+ defer os.Chdir(wd) |
|
| 31 |
+ |
|
| 32 |
+ return syscall.Mount(device, target, mType, 0, label) |
|
| 33 |
+} |
|
| 34 |
+ |
|
| 35 |
+// This avoids creating a new driver for each test if all tests are run |
|
| 36 |
+// Make sure to put new tests between TestOverlaySetup and TestOverlayTeardown |
|
| 37 |
+func TestOverlaySetup(t *testing.T) {
|
|
| 38 |
+ graphtest.GetDriver(t, driverName) |
|
| 39 |
+} |
|
| 40 |
+ |
|
| 41 |
+func TestOverlayCreateEmpty(t *testing.T) {
|
|
| 42 |
+ graphtest.DriverTestCreateEmpty(t, driverName) |
|
| 43 |
+} |
|
| 44 |
+ |
|
| 45 |
+func TestOverlayCreateBase(t *testing.T) {
|
|
| 46 |
+ graphtest.DriverTestCreateBase(t, driverName) |
|
| 47 |
+} |
|
| 48 |
+ |
|
| 49 |
+func TestOverlayCreateSnap(t *testing.T) {
|
|
| 50 |
+ graphtest.DriverTestCreateSnap(t, driverName) |
|
| 51 |
+} |
|
| 52 |
+ |
|
| 53 |
+func TestOverlay128LayerRead(t *testing.T) {
|
|
| 54 |
+ graphtest.DriverTestDeepLayerRead(t, 128, driverName) |
|
| 55 |
+} |
|
| 56 |
+ |
|
| 57 |
+func TestOverlayDiffApply10Files(t *testing.T) {
|
|
| 58 |
+ graphtest.DriverTestDiffApply(t, 10, driverName) |
|
| 59 |
+} |
|
| 60 |
+ |
|
| 61 |
+func TestOverlayChanges(t *testing.T) {
|
|
| 62 |
+ graphtest.DriverTestChanges(t, driverName) |
|
| 63 |
+} |
|
| 64 |
+ |
|
| 65 |
+func TestOverlayTeardown(t *testing.T) {
|
|
| 66 |
+ graphtest.PutDriver(t) |
|
| 67 |
+} |
|
| 68 |
+ |
|
| 69 |
+// Benchmarks should always setup new driver |
|
| 70 |
+ |
|
| 71 |
+func BenchmarkExists(b *testing.B) {
|
|
| 72 |
+ graphtest.DriverBenchExists(b, driverName) |
|
| 73 |
+} |
|
| 74 |
+ |
|
| 75 |
+func BenchmarkGetEmpty(b *testing.B) {
|
|
| 76 |
+ graphtest.DriverBenchGetEmpty(b, driverName) |
|
| 77 |
+} |
|
| 78 |
+ |
|
| 79 |
+func BenchmarkDiffBase(b *testing.B) {
|
|
| 80 |
+ graphtest.DriverBenchDiffBase(b, driverName) |
|
| 81 |
+} |
|
| 82 |
+ |
|
| 83 |
+func BenchmarkDiffSmallUpper(b *testing.B) {
|
|
| 84 |
+ graphtest.DriverBenchDiffN(b, 10, 10, driverName) |
|
| 85 |
+} |
|
| 86 |
+ |
|
| 87 |
+func BenchmarkDiff10KFileUpper(b *testing.B) {
|
|
| 88 |
+ graphtest.DriverBenchDiffN(b, 10, 10000, driverName) |
|
| 89 |
+} |
|
| 90 |
+ |
|
| 91 |
+func BenchmarkDiff10KFilesBottom(b *testing.B) {
|
|
| 92 |
+ graphtest.DriverBenchDiffN(b, 10000, 10, driverName) |
|
| 93 |
+} |
|
| 94 |
+ |
|
| 95 |
+func BenchmarkDiffApply100(b *testing.B) {
|
|
| 96 |
+ graphtest.DriverBenchDiffApplyN(b, 100, driverName) |
|
| 97 |
+} |
|
| 98 |
+ |
|
| 99 |
+func BenchmarkDiff20Layers(b *testing.B) {
|
|
| 100 |
+ graphtest.DriverBenchDeepLayerDiff(b, 20, driverName) |
|
| 101 |
+} |
|
| 102 |
+ |
|
| 103 |
+func BenchmarkRead20Layers(b *testing.B) {
|
|
| 104 |
+ graphtest.DriverBenchDeepLayerRead(b, 20, driverName) |
|
| 105 |
+} |
| 0 | 3 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,80 @@ |
| 0 |
+// +build linux |
|
| 1 |
+ |
|
| 2 |
+package overlay2 |
|
| 3 |
+ |
|
| 4 |
+import ( |
|
| 5 |
+ "crypto/rand" |
|
| 6 |
+ "encoding/base32" |
|
| 7 |
+ "fmt" |
|
| 8 |
+ "io" |
|
| 9 |
+ "os" |
|
| 10 |
+ "syscall" |
|
| 11 |
+ "time" |
|
| 12 |
+ |
|
| 13 |
+ "github.com/Sirupsen/logrus" |
|
| 14 |
+) |
|
| 15 |
+ |
|
| 16 |
+// generateID creates a new random string identifier with the given length |
|
| 17 |
+func generateID(l int) string {
|
|
| 18 |
+ const ( |
|
| 19 |
+ // ensures we backoff for less than 450ms total. Use the following to |
|
| 20 |
+ // select new value, in units of 10ms: |
|
| 21 |
+ // n*(n+1)/2 = d -> n^2 + n - 2d -> n = (sqrt(8d + 1) - 1)/2 |
|
| 22 |
+ maxretries = 9 |
|
| 23 |
+ backoff = time.Millisecond * 10 |
|
| 24 |
+ ) |
|
| 25 |
+ |
|
| 26 |
+ var ( |
|
| 27 |
+ totalBackoff time.Duration |
|
| 28 |
+ count int |
|
| 29 |
+ retries int |
|
| 30 |
+ size = (l*5 + 7) / 8 |
|
| 31 |
+ u = make([]byte, size) |
|
| 32 |
+ ) |
|
| 33 |
+ // TODO: Include time component, counter component, random component |
|
| 34 |
+ |
|
| 35 |
+ for {
|
|
| 36 |
+ // This should never block but the read may fail. Because of this, |
|
| 37 |
+ // we just try to read the random number generator until we get |
|
| 38 |
+ // something. This is a very rare condition but may happen. |
|
| 39 |
+ b := time.Duration(retries) * backoff |
|
| 40 |
+ time.Sleep(b) |
|
| 41 |
+ totalBackoff += b |
|
| 42 |
+ |
|
| 43 |
+ n, err := io.ReadFull(rand.Reader, u[count:]) |
|
| 44 |
+ if err != nil {
|
|
| 45 |
+ if retryOnError(err) && retries < maxretries {
|
|
| 46 |
+ count += n |
|
| 47 |
+ retries++ |
|
| 48 |
+ logrus.Errorf("error generating version 4 uuid, retrying: %v", err)
|
|
| 49 |
+ continue |
|
| 50 |
+ } |
|
| 51 |
+ |
|
| 52 |
+ // Any other errors represent a system problem. What did someone |
|
| 53 |
+ // do to /dev/urandom? |
|
| 54 |
+ panic(fmt.Errorf("error reading random number generator, retried for %v: %v", totalBackoff.String(), err))
|
|
| 55 |
+ } |
|
| 56 |
+ |
|
| 57 |
+ break |
|
| 58 |
+ } |
|
| 59 |
+ |
|
| 60 |
+ s := base32.StdEncoding.EncodeToString(u) |
|
| 61 |
+ |
|
| 62 |
+ return s[:l] |
|
| 63 |
+} |
|
| 64 |
+ |
|
| 65 |
+// retryOnError tries to detect whether or not retrying would be fruitful. |
|
| 66 |
+func retryOnError(err error) bool {
|
|
| 67 |
+ switch err := err.(type) {
|
|
| 68 |
+ case *os.PathError: |
|
| 69 |
+ return retryOnError(err.Err) // unpack the target error |
|
| 70 |
+ case syscall.Errno: |
|
| 71 |
+ if err == syscall.EPERM {
|
|
| 72 |
+ // EPERM represents an entropy pool exhaustion, a condition under |
|
| 73 |
+ // which we backoff and retry. |
|
| 74 |
+ return true |
|
| 75 |
+ } |
|
| 76 |
+ } |
|
| 77 |
+ |
|
| 78 |
+ return false |
|
| 79 |
+} |