github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/daemon/graphdriver/fuse-overlayfs/fuseoverlayfs.go (about) 1 //go:build linux 2 // +build linux 3 4 package fuseoverlayfs // import "github.com/docker/docker/daemon/graphdriver/fuse-overlayfs" 5 6 import ( 7 "bytes" 8 "context" 9 "fmt" 10 "io" 11 "os" 12 "os/exec" 13 "path" 14 "path/filepath" 15 "strings" 16 17 "github.com/containerd/containerd/sys" 18 "github.com/docker/docker/daemon/graphdriver" 19 "github.com/docker/docker/daemon/graphdriver/overlayutils" 20 "github.com/docker/docker/pkg/archive" 21 "github.com/docker/docker/pkg/chrootarchive" 22 "github.com/docker/docker/pkg/containerfs" 23 "github.com/docker/docker/pkg/directory" 24 "github.com/docker/docker/pkg/idtools" 25 "github.com/docker/docker/pkg/parsers/kernel" 26 "github.com/docker/docker/pkg/system" 27 "github.com/moby/locker" 28 "github.com/moby/sys/mount" 29 "github.com/opencontainers/selinux/go-selinux/label" 30 "github.com/pkg/errors" 31 "github.com/sirupsen/logrus" 32 "golang.org/x/sys/unix" 33 ) 34 35 var ( 36 // untar defines the untar method 37 untar = chrootarchive.UntarUncompressed 38 ) 39 40 const ( 41 driverName = "fuse-overlayfs" 42 binary = "fuse-overlayfs" 43 linkDir = "l" 44 diffDirName = "diff" 45 workDirName = "work" 46 mergedDirName = "merged" 47 lowerFile = "lower" 48 maxDepth = 128 49 50 // idLength represents the number of random characters 51 // which can be used to create the unique link identifier 52 // for every layer. If this value is too long then the 53 // page size limit for the mount command may be exceeded. 54 // The idLength should be selected such that following equation 55 // is true (512 is a buffer for label metadata). 56 // ((idLength + len(linkDir) + 1) * maxDepth) <= (pageSize - 512) 57 idLength = 26 58 ) 59 60 // Driver contains information about the home directory and the list of active 61 // mounts that are created using this driver. 62 type Driver struct { 63 home string 64 uidMaps []idtools.IDMap 65 gidMaps []idtools.IDMap 66 ctr *graphdriver.RefCounter 67 naiveDiff graphdriver.DiffDriver 68 locker *locker.Locker 69 } 70 71 var ( 72 logger = logrus.WithField("storage-driver", driverName) 73 ) 74 75 func init() { 76 graphdriver.Register(driverName, Init) 77 } 78 79 // Init returns the naive diff driver for fuse-overlayfs. 80 // If fuse-overlayfs is not supported on the host, the error 81 // graphdriver.ErrNotSupported is returned. 82 func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) { 83 if _, err := exec.LookPath(binary); err != nil { 84 logger.Error(err) 85 return nil, graphdriver.ErrNotSupported 86 } 87 if !kernel.CheckKernelVersion(4, 18, 0) { 88 return nil, graphdriver.ErrNotSupported 89 } 90 91 remappedRoot := idtools.NewIDMappingsFromMaps(uidMaps, gidMaps) 92 currentID := idtools.CurrentIdentity() 93 dirID := idtools.Identity{ 94 UID: currentID.UID, 95 GID: remappedRoot.RootPair().GID, 96 } 97 98 if err := idtools.MkdirAllAndChown(home, 0710, dirID); err != nil { 99 return nil, err 100 } 101 if err := idtools.MkdirAllAndChown(path.Join(home, linkDir), 0700, currentID); err != nil { 102 return nil, err 103 } 104 105 d := &Driver{ 106 home: home, 107 uidMaps: uidMaps, 108 gidMaps: gidMaps, 109 ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicFUSE)), 110 locker: locker.New(), 111 } 112 113 d.naiveDiff = graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps) 114 115 return d, nil 116 } 117 118 func (d *Driver) String() string { 119 return driverName 120 } 121 122 // Status returns current driver information in a two dimensional string array. 123 func (d *Driver) Status() [][2]string { 124 return [][2]string{} 125 } 126 127 // GetMetadata returns metadata about the overlay driver such as the LowerDir, 128 // UpperDir, WorkDir, and MergeDir used to store data. 129 func (d *Driver) GetMetadata(id string) (map[string]string, error) { 130 dir := d.dir(id) 131 if _, err := os.Stat(dir); err != nil { 132 return nil, err 133 } 134 135 metadata := map[string]string{ 136 "WorkDir": path.Join(dir, workDirName), 137 "MergedDir": path.Join(dir, mergedDirName), 138 "UpperDir": path.Join(dir, diffDirName), 139 } 140 141 lowerDirs, err := d.getLowerDirs(id) 142 if err != nil { 143 return nil, err 144 } 145 if len(lowerDirs) > 0 { 146 metadata["LowerDir"] = strings.Join(lowerDirs, ":") 147 } 148 149 return metadata, nil 150 } 151 152 // Cleanup any state created by overlay which should be cleaned when daemon 153 // is being shutdown. For now, we just have to unmount the bind mounted 154 // we had created. 155 func (d *Driver) Cleanup() error { 156 return mount.RecursiveUnmount(d.home) 157 } 158 159 // CreateReadWrite creates a layer that is writable for use as a container 160 // file system. 161 func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error { 162 if opts != nil && len(opts.StorageOpt) != 0 { 163 return fmt.Errorf("--storage-opt is not supported") 164 } 165 return d.create(id, parent, opts) 166 } 167 168 // Create is used to create the upper, lower, and merge directories required for overlay fs for a given id. 169 // The parent filesystem is used to configure these directories for the overlay. 170 func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) (retErr error) { 171 if opts != nil && len(opts.StorageOpt) != 0 { 172 return fmt.Errorf("--storage-opt is not supported") 173 } 174 return d.create(id, parent, opts) 175 } 176 177 func (d *Driver) create(id, parent string, opts *graphdriver.CreateOpts) (retErr error) { 178 dir := d.dir(id) 179 180 rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps) 181 if err != nil { 182 return err 183 } 184 root := idtools.Identity{UID: rootUID, GID: rootGID} 185 186 dirID := idtools.Identity{ 187 UID: rootUID, 188 GID: rootGID, 189 } 190 191 if err := idtools.MkdirAllAndChown(path.Dir(dir), 0710, dirID); err != nil { 192 return err 193 } 194 if err := idtools.MkdirAndChown(dir, 0710, dirID); err != nil { 195 return err 196 } 197 198 defer func() { 199 // Clean up on failure 200 if retErr != nil { 201 os.RemoveAll(dir) 202 } 203 }() 204 205 if opts != nil && len(opts.StorageOpt) > 0 { 206 return fmt.Errorf("--storage-opt is not supported") 207 } 208 209 if err := idtools.MkdirAndChown(path.Join(dir, diffDirName), 0755, root); err != nil { 210 return err 211 } 212 213 lid := overlayutils.GenerateID(idLength, logger) 214 if err := os.Symlink(path.Join("..", id, diffDirName), path.Join(d.home, linkDir, lid)); err != nil { 215 return err 216 } 217 218 // Write link id to link file 219 if err := os.WriteFile(path.Join(dir, "link"), []byte(lid), 0644); err != nil { 220 return err 221 } 222 223 // if no parent directory, done 224 if parent == "" { 225 return nil 226 } 227 228 if err := idtools.MkdirAndChown(path.Join(dir, workDirName), 0710, dirID); err != nil { 229 return err 230 } 231 232 if err := os.WriteFile(path.Join(d.dir(parent), "committed"), []byte{}, 0600); err != nil { 233 return err 234 } 235 236 lower, err := d.getLower(parent) 237 if err != nil { 238 return err 239 } 240 if lower != "" { 241 if err := os.WriteFile(path.Join(dir, lowerFile), []byte(lower), 0666); err != nil { 242 return err 243 } 244 } 245 246 return nil 247 } 248 249 func (d *Driver) getLower(parent string) (string, error) { 250 parentDir := d.dir(parent) 251 252 // Ensure parent exists 253 if _, err := os.Lstat(parentDir); err != nil { 254 return "", err 255 } 256 257 // Read Parent link fileA 258 parentLink, err := os.ReadFile(path.Join(parentDir, "link")) 259 if err != nil { 260 return "", err 261 } 262 lowers := []string{path.Join(linkDir, string(parentLink))} 263 264 parentLower, err := os.ReadFile(path.Join(parentDir, lowerFile)) 265 if err == nil { 266 parentLowers := strings.Split(string(parentLower), ":") 267 lowers = append(lowers, parentLowers...) 268 } 269 if len(lowers) > maxDepth { 270 return "", errors.New("max depth exceeded") 271 } 272 return strings.Join(lowers, ":"), nil 273 } 274 275 func (d *Driver) dir(id string) string { 276 return path.Join(d.home, id) 277 } 278 279 func (d *Driver) getLowerDirs(id string) ([]string, error) { 280 var lowersArray []string 281 lowers, err := os.ReadFile(path.Join(d.dir(id), lowerFile)) 282 if err == nil { 283 for _, s := range strings.Split(string(lowers), ":") { 284 lp, err := os.Readlink(path.Join(d.home, s)) 285 if err != nil { 286 return nil, err 287 } 288 lowersArray = append(lowersArray, path.Clean(path.Join(d.home, linkDir, lp))) 289 } 290 } else if !os.IsNotExist(err) { 291 return nil, err 292 } 293 return lowersArray, nil 294 } 295 296 // Remove cleans the directories that are created for this id. 297 func (d *Driver) Remove(id string) error { 298 if id == "" { 299 return fmt.Errorf("refusing to remove the directories: id is empty") 300 } 301 d.locker.Lock(id) 302 defer d.locker.Unlock(id) 303 dir := d.dir(id) 304 lid, err := os.ReadFile(path.Join(dir, "link")) 305 if err == nil { 306 if len(lid) == 0 { 307 logger.Errorf("refusing to remove empty link for layer %v", id) 308 } else if err := os.RemoveAll(path.Join(d.home, linkDir, string(lid))); err != nil { 309 logger.Debugf("Failed to remove link: %v", err) 310 } 311 } 312 313 if err := system.EnsureRemoveAll(dir); err != nil && !os.IsNotExist(err) { 314 return err 315 } 316 return nil 317 } 318 319 // Get creates and mounts the required file system for the given id and returns the mount path. 320 func (d *Driver) Get(id, mountLabel string) (_ containerfs.ContainerFS, retErr error) { 321 d.locker.Lock(id) 322 defer d.locker.Unlock(id) 323 dir := d.dir(id) 324 if _, err := os.Stat(dir); err != nil { 325 return nil, err 326 } 327 328 diffDir := path.Join(dir, diffDirName) 329 lowers, err := os.ReadFile(path.Join(dir, lowerFile)) 330 if err != nil { 331 // If no lower, just return diff directory 332 if os.IsNotExist(err) { 333 return containerfs.NewLocalContainerFS(diffDir), nil 334 } 335 return nil, err 336 } 337 338 mergedDir := path.Join(dir, mergedDirName) 339 if count := d.ctr.Increment(mergedDir); count > 1 { 340 return containerfs.NewLocalContainerFS(mergedDir), nil 341 } 342 defer func() { 343 if retErr != nil { 344 if c := d.ctr.Decrement(mergedDir); c <= 0 { 345 if unmounted := fusermountU(mergedDir); !unmounted { 346 if mntErr := unix.Unmount(mergedDir, 0); mntErr != nil { 347 logger.Errorf("error unmounting %v: %v", mergedDir, mntErr) 348 } 349 } 350 // Cleanup the created merged directory; see the comment in Put's rmdir 351 if rmErr := unix.Rmdir(mergedDir); rmErr != nil && !os.IsNotExist(rmErr) { 352 logger.Debugf("Failed to remove %s: %v: %v", id, rmErr, err) 353 } 354 } 355 } 356 }() 357 358 workDir := path.Join(dir, workDirName) 359 splitLowers := strings.Split(string(lowers), ":") 360 absLowers := make([]string, len(splitLowers)) 361 for i, s := range splitLowers { 362 absLowers[i] = path.Join(d.home, s) 363 } 364 var readonly bool 365 if _, err := os.Stat(path.Join(dir, "committed")); err == nil { 366 readonly = true 367 } else if !os.IsNotExist(err) { 368 return nil, err 369 } 370 371 var opts string 372 if readonly { 373 opts = "lowerdir=" + diffDir + ":" + strings.Join(absLowers, ":") 374 } else { 375 opts = "lowerdir=" + strings.Join(absLowers, ":") + ",upperdir=" + diffDir + ",workdir=" + workDir 376 } 377 378 mountData := label.FormatMountLabel(opts, mountLabel) 379 mountTarget := mergedDir 380 381 rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps) 382 if err != nil { 383 return nil, err 384 } 385 if err := idtools.MkdirAndChown(mergedDir, 0700, idtools.Identity{UID: rootUID, GID: rootGID}); err != nil { 386 return nil, err 387 } 388 389 mountProgram := exec.Command(binary, "-o", mountData, mountTarget) 390 mountProgram.Dir = d.home 391 var b bytes.Buffer 392 mountProgram.Stderr = &b 393 if err = mountProgram.Run(); err != nil { 394 output := b.String() 395 if output == "" { 396 output = "<stderr empty>" 397 } 398 return nil, errors.Wrapf(err, "using mount program %s: %s", binary, output) 399 } 400 401 return containerfs.NewLocalContainerFS(mergedDir), nil 402 } 403 404 // Put unmounts the mount path created for the give id. 405 // It also removes the 'merged' directory to force the kernel to unmount the 406 // overlay mount in other namespaces. 407 func (d *Driver) Put(id string) error { 408 d.locker.Lock(id) 409 defer d.locker.Unlock(id) 410 dir := d.dir(id) 411 _, err := os.ReadFile(path.Join(dir, lowerFile)) 412 if err != nil { 413 // If no lower, no mount happened and just return directly 414 if os.IsNotExist(err) { 415 return nil 416 } 417 return err 418 } 419 420 mountpoint := path.Join(dir, mergedDirName) 421 if count := d.ctr.Decrement(mountpoint); count > 0 { 422 return nil 423 } 424 if unmounted := fusermountU(mountpoint); !unmounted { 425 if err := unix.Unmount(mountpoint, unix.MNT_DETACH); err != nil { 426 logger.Debugf("Failed to unmount %s overlay: %s - %v", id, mountpoint, err) 427 } 428 } 429 // Remove the mountpoint here. Removing the mountpoint (in newer kernels) 430 // will cause all other instances of this mount in other mount namespaces 431 // to be unmounted. This is necessary to avoid cases where an overlay mount 432 // that is present in another namespace will cause subsequent mounts 433 // operations to fail with ebusy. We ignore any errors here because this may 434 // fail on older kernels which don't have 435 // torvalds/linux@8ed936b5671bfb33d89bc60bdcc7cf0470ba52fe applied. 436 if err := unix.Rmdir(mountpoint); err != nil && !os.IsNotExist(err) { 437 logger.Debugf("Failed to remove %s overlay: %v", id, err) 438 } 439 return nil 440 } 441 442 // Exists checks to see if the id is already mounted. 443 func (d *Driver) Exists(id string) bool { 444 _, err := os.Stat(d.dir(id)) 445 return err == nil 446 } 447 448 // isParent determines whether the given parent is the direct parent of the 449 // given layer id 450 func (d *Driver) isParent(id, parent string) bool { 451 lowers, err := d.getLowerDirs(id) 452 if err != nil { 453 return false 454 } 455 if parent == "" && len(lowers) > 0 { 456 return false 457 } 458 459 parentDir := d.dir(parent) 460 var ld string 461 if len(lowers) > 0 { 462 ld = filepath.Dir(lowers[0]) 463 } 464 if ld == "" && parent == "" { 465 return true 466 } 467 return ld == parentDir 468 } 469 470 // ApplyDiff applies the new layer into a root 471 func (d *Driver) ApplyDiff(id string, parent string, diff io.Reader) (size int64, err error) { 472 if !d.isParent(id, parent) { 473 return d.naiveDiff.ApplyDiff(id, parent, diff) 474 } 475 476 applyDir := d.getDiffPath(id) 477 478 logger.Debugf("Applying tar in %s", applyDir) 479 // Overlay doesn't need the parent id to apply the diff 480 if err := untar(diff, applyDir, &archive.TarOptions{ 481 UIDMaps: d.uidMaps, 482 GIDMaps: d.gidMaps, 483 // Use AUFS whiteout format: https://github.com/containers/storage/blob/39a8d5ed9843844eafb5d2ba6e6a7510e0126f40/drivers/overlay/overlay.go#L1084-L1089 484 WhiteoutFormat: archive.AUFSWhiteoutFormat, 485 InUserNS: sys.RunningInUserNS(), 486 }); err != nil { 487 return 0, err 488 } 489 490 return directory.Size(context.TODO(), applyDir) 491 } 492 493 func (d *Driver) getDiffPath(id string) string { 494 dir := d.dir(id) 495 496 return path.Join(dir, diffDirName) 497 } 498 499 // DiffSize calculates the changes between the specified id 500 // and its parent and returns the size in bytes of the changes 501 // relative to its base filesystem directory. 502 func (d *Driver) DiffSize(id, parent string) (size int64, err error) { 503 return d.naiveDiff.DiffSize(id, parent) 504 } 505 506 // Diff produces an archive of the changes between the specified 507 // layer and its parent layer which may be "". 508 func (d *Driver) Diff(id, parent string) (io.ReadCloser, error) { 509 return d.naiveDiff.Diff(id, parent) 510 } 511 512 // Changes produces a list of changes between the specified layer and its 513 // parent layer. If parent is "", then all changes will be ADD changes. 514 func (d *Driver) Changes(id, parent string) ([]archive.Change, error) { 515 return d.naiveDiff.Changes(id, parent) 516 } 517 518 // fusermountU is from https://github.com/containers/storage/blob/39a8d5ed9843844eafb5d2ba6e6a7510e0126f40/drivers/overlay/overlay.go#L1016-L1040 519 func fusermountU(mountpoint string) (unmounted bool) { 520 // Attempt to unmount the FUSE mount using either fusermount or fusermount3. 521 // If they fail, fallback to unix.Unmount 522 for _, v := range []string{"fusermount3", "fusermount"} { 523 err := exec.Command(v, "-u", mountpoint).Run() 524 if err != nil && !os.IsNotExist(err) { 525 logrus.Debugf("Error unmounting %s with %s - %v", mountpoint, v, err) 526 } 527 if err == nil { 528 unmounted = true 529 break 530 } 531 } 532 // If fusermount|fusermount3 failed to unmount the FUSE file system, make sure all 533 // pending changes are propagated to the file system 534 if !unmounted { 535 fd, err := unix.Open(mountpoint, unix.O_DIRECTORY, 0) 536 if err == nil { 537 if err := unix.Syncfs(fd); err != nil { 538 logrus.Debugf("Error Syncfs(%s) - %v", mountpoint, err) 539 } 540 unix.Close(fd) 541 } 542 } 543 return 544 }