overlay.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. // +build linux
  2. package overlay2 // import "github.com/docker/docker/daemon/graphdriver/overlay2"
  3. import (
  4. "bufio"
  5. "context"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "os"
  11. "os/exec"
  12. "path"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. "sync"
  17. "github.com/docker/docker/daemon/graphdriver"
  18. "github.com/docker/docker/daemon/graphdriver/overlayutils"
  19. "github.com/docker/docker/daemon/graphdriver/quota"
  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/fsutils"
  25. "github.com/docker/docker/pkg/idtools"
  26. "github.com/docker/docker/pkg/locker"
  27. "github.com/docker/docker/pkg/mount"
  28. "github.com/docker/docker/pkg/parsers"
  29. "github.com/docker/docker/pkg/parsers/kernel"
  30. "github.com/docker/docker/pkg/system"
  31. "github.com/docker/go-units"
  32. rsystem "github.com/opencontainers/runc/libcontainer/system"
  33. "github.com/opencontainers/selinux/go-selinux/label"
  34. "github.com/sirupsen/logrus"
  35. "golang.org/x/sys/unix"
  36. )
  37. var (
  38. // untar defines the untar method
  39. untar = chrootarchive.UntarUncompressed
  40. )
  41. // This backend uses the overlay union filesystem for containers
  42. // with diff directories for each layer.
  43. // This version of the overlay driver requires at least kernel
  44. // 4.0.0 in order to support mounting multiple diff directories.
  45. // Each container/image has at least a "diff" directory and "link" file.
  46. // If there is also a "lower" file when there are diff layers
  47. // below as well as "merged" and "work" directories. The "diff" directory
  48. // has the upper layer of the overlay and is used to capture any
  49. // changes to the layer. The "lower" file contains all the lower layer
  50. // mounts separated by ":" and ordered from uppermost to lowermost
  51. // layers. The overlay itself is mounted in the "merged" directory,
  52. // and the "work" dir is needed for overlay to work.
  53. // The "link" file for each layer contains a unique string for the layer.
  54. // Under the "l" directory at the root there will be a symbolic link
  55. // with that unique string pointing the "diff" directory for the layer.
  56. // The symbolic links are used to reference lower layers in the "lower"
  57. // file and on mount. The links are used to shorten the total length
  58. // of a layer reference without requiring changes to the layer identifier
  59. // or root directory. Mounts are always done relative to root and
  60. // referencing the symbolic links in order to ensure the number of
  61. // lower directories can fit in a single page for making the mount
  62. // syscall. A hard upper limit of 128 lower layers is enforced to ensure
  63. // that mounts do not fail due to length.
  64. const (
  65. driverName = "overlay2"
  66. linkDir = "l"
  67. lowerFile = "lower"
  68. maxDepth = 128
  69. // idLength represents the number of random characters
  70. // which can be used to create the unique link identifier
  71. // for every layer. If this value is too long then the
  72. // page size limit for the mount command may be exceeded.
  73. // The idLength should be selected such that following equation
  74. // is true (512 is a buffer for label metadata).
  75. // ((idLength + len(linkDir) + 1) * maxDepth) <= (pageSize - 512)
  76. idLength = 26
  77. )
  78. type overlayOptions struct {
  79. overrideKernelCheck bool
  80. quota quota.Quota
  81. }
  82. // Driver contains information about the home directory and the list of active
  83. // mounts that are created using this driver.
  84. type Driver struct {
  85. home string
  86. uidMaps []idtools.IDMap
  87. gidMaps []idtools.IDMap
  88. ctr *graphdriver.RefCounter
  89. quotaCtl *quota.Control
  90. options overlayOptions
  91. naiveDiff graphdriver.DiffDriver
  92. supportsDType bool
  93. locker *locker.Locker
  94. }
  95. var (
  96. logger = logrus.WithField("storage-driver", "overlay2")
  97. backingFs = "<unknown>"
  98. projectQuotaSupported = false
  99. useNaiveDiffLock sync.Once
  100. useNaiveDiffOnly bool
  101. )
  102. func init() {
  103. graphdriver.Register(driverName, Init)
  104. }
  105. // Init returns the native diff driver for overlay filesystem.
  106. // If overlay filesystem is not supported on the host, the error
  107. // graphdriver.ErrNotSupported is returned.
  108. // If an overlay filesystem is not supported over an existing filesystem then
  109. // the error graphdriver.ErrIncompatibleFS is returned.
  110. func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
  111. opts, err := parseOptions(options)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if err := supportsOverlay(); err != nil {
  116. return nil, graphdriver.ErrNotSupported
  117. }
  118. // require kernel 4.0.0 to ensure multiple lower dirs are supported
  119. v, err := kernel.GetKernelVersion()
  120. if err != nil {
  121. return nil, err
  122. }
  123. // Perform feature detection on /var/lib/docker/overlay2 if it's an existing directory.
  124. // This covers situations where /var/lib/docker/overlay2 is a mount, and on a different
  125. // filesystem than /var/lib/docker.
  126. // If the path does not exist, fall back to using /var/lib/docker for feature detection.
  127. testdir := home
  128. if _, err := os.Stat(testdir); os.IsNotExist(err) {
  129. testdir = filepath.Dir(testdir)
  130. }
  131. fsMagic, err := graphdriver.GetFSMagic(testdir)
  132. if err != nil {
  133. return nil, err
  134. }
  135. if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
  136. backingFs = fsName
  137. }
  138. switch fsMagic {
  139. case graphdriver.FsMagicAufs, graphdriver.FsMagicEcryptfs, graphdriver.FsMagicNfsFs, graphdriver.FsMagicOverlay, graphdriver.FsMagicZfs:
  140. logger.Errorf("'overlay2' is not supported over %s", backingFs)
  141. return nil, graphdriver.ErrIncompatibleFS
  142. case graphdriver.FsMagicBtrfs:
  143. // Support for OverlayFS on BTRFS was added in kernel 4.7
  144. // See https://btrfs.wiki.kernel.org/index.php/Changelog
  145. if kernel.CompareKernelVersion(*v, kernel.VersionInfo{Kernel: 4, Major: 7, Minor: 0}) < 0 {
  146. if !opts.overrideKernelCheck {
  147. logger.Errorf("'overlay2' requires kernel 4.7 to use on %s", backingFs)
  148. return nil, graphdriver.ErrIncompatibleFS
  149. }
  150. logger.Warn("Using pre-4.7.0 kernel for overlay2 on btrfs, may require kernel update")
  151. }
  152. }
  153. if kernel.CompareKernelVersion(*v, kernel.VersionInfo{Kernel: 4, Major: 0, Minor: 0}) < 0 {
  154. if opts.overrideKernelCheck {
  155. logger.Warn("Using pre-4.0.0 kernel for overlay2, mount failures may require kernel update")
  156. } else {
  157. if err := supportsMultipleLowerDir(testdir); err != nil {
  158. logger.Debugf("Multiple lower dirs not supported: %v", err)
  159. return nil, graphdriver.ErrNotSupported
  160. }
  161. }
  162. }
  163. supportsDType, err := fsutils.SupportsDType(testdir)
  164. if err != nil {
  165. return nil, err
  166. }
  167. if !supportsDType {
  168. if !graphdriver.IsInitialized(home) {
  169. return nil, overlayutils.ErrDTypeNotSupported("overlay2", backingFs)
  170. }
  171. // allow running without d_type only for existing setups (#27443)
  172. logger.Warn(overlayutils.ErrDTypeNotSupported("overlay2", backingFs))
  173. }
  174. rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
  175. if err != nil {
  176. return nil, err
  177. }
  178. // Create the driver home dir
  179. if err := idtools.MkdirAllAndChown(path.Join(home, linkDir), 0700, idtools.Identity{UID: rootUID, GID: rootGID}); err != nil {
  180. return nil, err
  181. }
  182. d := &Driver{
  183. home: home,
  184. uidMaps: uidMaps,
  185. gidMaps: gidMaps,
  186. ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicOverlay)),
  187. supportsDType: supportsDType,
  188. locker: locker.New(),
  189. options: *opts,
  190. }
  191. d.naiveDiff = graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps)
  192. if backingFs == "xfs" {
  193. // Try to enable project quota support over xfs.
  194. if d.quotaCtl, err = quota.NewControl(home); err == nil {
  195. projectQuotaSupported = true
  196. } else if opts.quota.Size > 0 {
  197. return nil, fmt.Errorf("Storage option overlay2.size not supported. Filesystem does not support Project Quota: %v", err)
  198. }
  199. } else if opts.quota.Size > 0 {
  200. // if xfs is not the backing fs then error out if the storage-opt overlay2.size is used.
  201. return nil, fmt.Errorf("Storage Option overlay2.size only supported for backingFS XFS. Found %v", backingFs)
  202. }
  203. logger.Debugf("backingFs=%s, projectQuotaSupported=%v", backingFs, projectQuotaSupported)
  204. return d, nil
  205. }
  206. func parseOptions(options []string) (*overlayOptions, error) {
  207. o := &overlayOptions{}
  208. for _, option := range options {
  209. key, val, err := parsers.ParseKeyValueOpt(option)
  210. if err != nil {
  211. return nil, err
  212. }
  213. key = strings.ToLower(key)
  214. switch key {
  215. case "overlay2.override_kernel_check":
  216. o.overrideKernelCheck, err = strconv.ParseBool(val)
  217. if err != nil {
  218. return nil, err
  219. }
  220. case "overlay2.size":
  221. size, err := units.RAMInBytes(val)
  222. if err != nil {
  223. return nil, err
  224. }
  225. o.quota.Size = uint64(size)
  226. default:
  227. return nil, fmt.Errorf("overlay2: unknown option %s", key)
  228. }
  229. }
  230. return o, nil
  231. }
  232. func supportsOverlay() error {
  233. // We can try to modprobe overlay first before looking at
  234. // proc/filesystems for when overlay is supported
  235. exec.Command("modprobe", "overlay").Run()
  236. f, err := os.Open("/proc/filesystems")
  237. if err != nil {
  238. return err
  239. }
  240. defer f.Close()
  241. s := bufio.NewScanner(f)
  242. for s.Scan() {
  243. if s.Text() == "nodev\toverlay" {
  244. return nil
  245. }
  246. }
  247. logger.Error("'overlay' not found as a supported filesystem on this host. Please ensure kernel is new enough and has overlay support loaded.")
  248. return graphdriver.ErrNotSupported
  249. }
  250. func useNaiveDiff(home string) bool {
  251. useNaiveDiffLock.Do(func() {
  252. if err := doesSupportNativeDiff(home); err != nil {
  253. logger.Warnf("Not using native diff for overlay2, this may cause degraded performance for building images: %v", err)
  254. useNaiveDiffOnly = true
  255. }
  256. })
  257. return useNaiveDiffOnly
  258. }
  259. func (d *Driver) String() string {
  260. return driverName
  261. }
  262. // Status returns current driver information in a two dimensional string array.
  263. // Output contains "Backing Filesystem" used in this implementation.
  264. func (d *Driver) Status() [][2]string {
  265. return [][2]string{
  266. {"Backing Filesystem", backingFs},
  267. {"Supports d_type", strconv.FormatBool(d.supportsDType)},
  268. {"Native Overlay Diff", strconv.FormatBool(!useNaiveDiff(d.home))},
  269. }
  270. }
  271. // GetMetadata returns metadata about the overlay driver such as the LowerDir,
  272. // UpperDir, WorkDir, and MergeDir used to store data.
  273. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  274. dir := d.dir(id)
  275. if _, err := os.Stat(dir); err != nil {
  276. return nil, err
  277. }
  278. metadata := map[string]string{
  279. "WorkDir": path.Join(dir, "work"),
  280. "MergedDir": path.Join(dir, "merged"),
  281. "UpperDir": path.Join(dir, "diff"),
  282. }
  283. lowerDirs, err := d.getLowerDirs(id)
  284. if err != nil {
  285. return nil, err
  286. }
  287. if len(lowerDirs) > 0 {
  288. metadata["LowerDir"] = strings.Join(lowerDirs, ":")
  289. }
  290. return metadata, nil
  291. }
  292. // Cleanup any state created by overlay which should be cleaned when daemon
  293. // is being shutdown. For now, we just have to unmount the bind mounted
  294. // we had created.
  295. func (d *Driver) Cleanup() error {
  296. return mount.RecursiveUnmount(d.home)
  297. }
  298. // CreateReadWrite creates a layer that is writable for use as a container
  299. // file system.
  300. func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
  301. if opts != nil && len(opts.StorageOpt) != 0 && !projectQuotaSupported {
  302. return fmt.Errorf("--storage-opt is supported only for overlay over xfs with 'pquota' mount option")
  303. }
  304. if opts == nil {
  305. opts = &graphdriver.CreateOpts{
  306. StorageOpt: map[string]string{},
  307. }
  308. }
  309. if _, ok := opts.StorageOpt["size"]; !ok {
  310. if opts.StorageOpt == nil {
  311. opts.StorageOpt = map[string]string{}
  312. }
  313. opts.StorageOpt["size"] = strconv.FormatUint(d.options.quota.Size, 10)
  314. }
  315. return d.create(id, parent, opts)
  316. }
  317. // Create is used to create the upper, lower, and merge directories required for overlay fs for a given id.
  318. // The parent filesystem is used to configure these directories for the overlay.
  319. func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) (retErr error) {
  320. if opts != nil && len(opts.StorageOpt) != 0 {
  321. if _, ok := opts.StorageOpt["size"]; ok {
  322. return fmt.Errorf("--storage-opt size is only supported for ReadWrite Layers")
  323. }
  324. }
  325. return d.create(id, parent, opts)
  326. }
  327. func (d *Driver) create(id, parent string, opts *graphdriver.CreateOpts) (retErr error) {
  328. dir := d.dir(id)
  329. rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  330. if err != nil {
  331. return err
  332. }
  333. root := idtools.Identity{UID: rootUID, GID: rootGID}
  334. if err := idtools.MkdirAllAndChown(path.Dir(dir), 0700, root); err != nil {
  335. return err
  336. }
  337. if err := idtools.MkdirAndChown(dir, 0700, root); err != nil {
  338. return err
  339. }
  340. defer func() {
  341. // Clean up on failure
  342. if retErr != nil {
  343. os.RemoveAll(dir)
  344. }
  345. }()
  346. if opts != nil && len(opts.StorageOpt) > 0 {
  347. driver := &Driver{}
  348. if err := d.parseStorageOpt(opts.StorageOpt, driver); err != nil {
  349. return err
  350. }
  351. if driver.options.quota.Size > 0 {
  352. // Set container disk quota limit
  353. if err := d.quotaCtl.SetQuota(dir, driver.options.quota); err != nil {
  354. return err
  355. }
  356. }
  357. }
  358. if err := idtools.MkdirAndChown(path.Join(dir, "diff"), 0755, root); err != nil {
  359. return err
  360. }
  361. lid := generateID(idLength)
  362. if err := os.Symlink(path.Join("..", id, "diff"), path.Join(d.home, linkDir, lid)); err != nil {
  363. return err
  364. }
  365. // Write link id to link file
  366. if err := ioutil.WriteFile(path.Join(dir, "link"), []byte(lid), 0644); err != nil {
  367. return err
  368. }
  369. // if no parent directory, done
  370. if parent == "" {
  371. return nil
  372. }
  373. if err := idtools.MkdirAndChown(path.Join(dir, "work"), 0700, root); err != nil {
  374. return err
  375. }
  376. lower, err := d.getLower(parent)
  377. if err != nil {
  378. return err
  379. }
  380. if lower != "" {
  381. if err := ioutil.WriteFile(path.Join(dir, lowerFile), []byte(lower), 0666); err != nil {
  382. return err
  383. }
  384. }
  385. return nil
  386. }
  387. // Parse overlay storage options
  388. func (d *Driver) parseStorageOpt(storageOpt map[string]string, driver *Driver) error {
  389. // Read size to set the disk project quota per container
  390. for key, val := range storageOpt {
  391. key := strings.ToLower(key)
  392. switch key {
  393. case "size":
  394. size, err := units.RAMInBytes(val)
  395. if err != nil {
  396. return err
  397. }
  398. driver.options.quota.Size = uint64(size)
  399. default:
  400. return fmt.Errorf("Unknown option %s", key)
  401. }
  402. }
  403. return nil
  404. }
  405. func (d *Driver) getLower(parent string) (string, error) {
  406. parentDir := d.dir(parent)
  407. // Ensure parent exists
  408. if _, err := os.Lstat(parentDir); err != nil {
  409. return "", err
  410. }
  411. // Read Parent link fileA
  412. parentLink, err := ioutil.ReadFile(path.Join(parentDir, "link"))
  413. if err != nil {
  414. return "", err
  415. }
  416. lowers := []string{path.Join(linkDir, string(parentLink))}
  417. parentLower, err := ioutil.ReadFile(path.Join(parentDir, lowerFile))
  418. if err == nil {
  419. parentLowers := strings.Split(string(parentLower), ":")
  420. lowers = append(lowers, parentLowers...)
  421. }
  422. if len(lowers) > maxDepth {
  423. return "", errors.New("max depth exceeded")
  424. }
  425. return strings.Join(lowers, ":"), nil
  426. }
  427. func (d *Driver) dir(id string) string {
  428. return path.Join(d.home, id)
  429. }
  430. func (d *Driver) getLowerDirs(id string) ([]string, error) {
  431. var lowersArray []string
  432. lowers, err := ioutil.ReadFile(path.Join(d.dir(id), lowerFile))
  433. if err == nil {
  434. for _, s := range strings.Split(string(lowers), ":") {
  435. lp, err := os.Readlink(path.Join(d.home, s))
  436. if err != nil {
  437. return nil, err
  438. }
  439. lowersArray = append(lowersArray, path.Clean(path.Join(d.home, linkDir, lp)))
  440. }
  441. } else if !os.IsNotExist(err) {
  442. return nil, err
  443. }
  444. return lowersArray, nil
  445. }
  446. // Remove cleans the directories that are created for this id.
  447. func (d *Driver) Remove(id string) error {
  448. if id == "" {
  449. return fmt.Errorf("refusing to remove the directories: id is empty")
  450. }
  451. d.locker.Lock(id)
  452. defer d.locker.Unlock(id)
  453. dir := d.dir(id)
  454. lid, err := ioutil.ReadFile(path.Join(dir, "link"))
  455. if err == nil {
  456. if len(lid) == 0 {
  457. logger.Errorf("refusing to remove empty link for layer %v", id)
  458. } else if err := os.RemoveAll(path.Join(d.home, linkDir, string(lid))); err != nil {
  459. logger.Debugf("Failed to remove link: %v", err)
  460. }
  461. }
  462. if err := system.EnsureRemoveAll(dir); err != nil && !os.IsNotExist(err) {
  463. return err
  464. }
  465. return nil
  466. }
  467. // Get creates and mounts the required file system for the given id and returns the mount path.
  468. func (d *Driver) Get(id, mountLabel string) (_ containerfs.ContainerFS, retErr error) {
  469. d.locker.Lock(id)
  470. defer d.locker.Unlock(id)
  471. dir := d.dir(id)
  472. if _, err := os.Stat(dir); err != nil {
  473. return nil, err
  474. }
  475. diffDir := path.Join(dir, "diff")
  476. lowers, err := ioutil.ReadFile(path.Join(dir, lowerFile))
  477. if err != nil {
  478. // If no lower, just return diff directory
  479. if os.IsNotExist(err) {
  480. return containerfs.NewLocalContainerFS(diffDir), nil
  481. }
  482. return nil, err
  483. }
  484. mergedDir := path.Join(dir, "merged")
  485. if count := d.ctr.Increment(mergedDir); count > 1 {
  486. return containerfs.NewLocalContainerFS(mergedDir), nil
  487. }
  488. defer func() {
  489. if retErr != nil {
  490. if c := d.ctr.Decrement(mergedDir); c <= 0 {
  491. if mntErr := unix.Unmount(mergedDir, 0); mntErr != nil {
  492. logger.Errorf("error unmounting %v: %v", mergedDir, mntErr)
  493. }
  494. // Cleanup the created merged directory; see the comment in Put's rmdir
  495. if rmErr := unix.Rmdir(mergedDir); rmErr != nil && !os.IsNotExist(rmErr) {
  496. logger.Debugf("Failed to remove %s: %v: %v", id, rmErr, err)
  497. }
  498. }
  499. }
  500. }()
  501. workDir := path.Join(dir, "work")
  502. splitLowers := strings.Split(string(lowers), ":")
  503. absLowers := make([]string, len(splitLowers))
  504. for i, s := range splitLowers {
  505. absLowers[i] = path.Join(d.home, s)
  506. }
  507. opts := fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", strings.Join(absLowers, ":"), path.Join(dir, "diff"), path.Join(dir, "work"))
  508. mountData := label.FormatMountLabel(opts, mountLabel)
  509. mount := unix.Mount
  510. mountTarget := mergedDir
  511. rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  512. if err != nil {
  513. return nil, err
  514. }
  515. if err := idtools.MkdirAndChown(mergedDir, 0700, idtools.Identity{UID: rootUID, GID: rootGID}); err != nil {
  516. return nil, err
  517. }
  518. pageSize := unix.Getpagesize()
  519. // Go can return a larger page size than supported by the system
  520. // as of go 1.7. This will be fixed in 1.8 and this block can be
  521. // removed when building with 1.8.
  522. // See https://github.com/golang/go/commit/1b9499b06989d2831e5b156161d6c07642926ee1
  523. // See https://github.com/docker/docker/issues/27384
  524. if pageSize > 4096 {
  525. pageSize = 4096
  526. }
  527. // Use relative paths and mountFrom when the mount data has exceeded
  528. // the page size. The mount syscall fails if the mount data cannot
  529. // fit within a page and relative links make the mount data much
  530. // smaller at the expense of requiring a fork exec to chroot.
  531. if len(mountData) > pageSize {
  532. opts = fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", string(lowers), path.Join(id, "diff"), path.Join(id, "work"))
  533. mountData = label.FormatMountLabel(opts, mountLabel)
  534. if len(mountData) > pageSize {
  535. return nil, fmt.Errorf("cannot mount layer, mount label too large %d", len(mountData))
  536. }
  537. mount = func(source string, target string, mType string, flags uintptr, label string) error {
  538. return mountFrom(d.home, source, target, mType, flags, label)
  539. }
  540. mountTarget = path.Join(id, "merged")
  541. }
  542. if err := mount("overlay", mountTarget, "overlay", 0, mountData); err != nil {
  543. return nil, fmt.Errorf("error creating overlay mount to %s: %v", mergedDir, err)
  544. }
  545. // chown "workdir/work" to the remapped root UID/GID. Overlay fs inside a
  546. // user namespace requires this to move a directory from lower to upper.
  547. if err := os.Chown(path.Join(workDir, "work"), rootUID, rootGID); err != nil {
  548. return nil, err
  549. }
  550. return containerfs.NewLocalContainerFS(mergedDir), nil
  551. }
  552. // Put unmounts the mount path created for the give id.
  553. // It also removes the 'merged' directory to force the kernel to unmount the
  554. // overlay mount in other namespaces.
  555. func (d *Driver) Put(id string) error {
  556. d.locker.Lock(id)
  557. defer d.locker.Unlock(id)
  558. dir := d.dir(id)
  559. _, err := ioutil.ReadFile(path.Join(dir, lowerFile))
  560. if err != nil {
  561. // If no lower, no mount happened and just return directly
  562. if os.IsNotExist(err) {
  563. return nil
  564. }
  565. return err
  566. }
  567. mountpoint := path.Join(dir, "merged")
  568. if count := d.ctr.Decrement(mountpoint); count > 0 {
  569. return nil
  570. }
  571. if err := unix.Unmount(mountpoint, unix.MNT_DETACH); err != nil {
  572. logger.Debugf("Failed to unmount %s overlay: %s - %v", id, mountpoint, err)
  573. }
  574. // Remove the mountpoint here. Removing the mountpoint (in newer kernels)
  575. // will cause all other instances of this mount in other mount namespaces
  576. // to be unmounted. This is necessary to avoid cases where an overlay mount
  577. // that is present in another namespace will cause subsequent mounts
  578. // operations to fail with ebusy. We ignore any errors here because this may
  579. // fail on older kernels which don't have
  580. // torvalds/linux@8ed936b5671bfb33d89bc60bdcc7cf0470ba52fe applied.
  581. if err := unix.Rmdir(mountpoint); err != nil && !os.IsNotExist(err) {
  582. logger.Debugf("Failed to remove %s overlay: %v", id, err)
  583. }
  584. return nil
  585. }
  586. // Exists checks to see if the id is already mounted.
  587. func (d *Driver) Exists(id string) bool {
  588. _, err := os.Stat(d.dir(id))
  589. return err == nil
  590. }
  591. // isParent determines whether the given parent is the direct parent of the
  592. // given layer id
  593. func (d *Driver) isParent(id, parent string) bool {
  594. lowers, err := d.getLowerDirs(id)
  595. if err != nil {
  596. return false
  597. }
  598. if parent == "" && len(lowers) > 0 {
  599. return false
  600. }
  601. parentDir := d.dir(parent)
  602. var ld string
  603. if len(lowers) > 0 {
  604. ld = filepath.Dir(lowers[0])
  605. }
  606. if ld == "" && parent == "" {
  607. return true
  608. }
  609. return ld == parentDir
  610. }
  611. // ApplyDiff applies the new layer into a root
  612. func (d *Driver) ApplyDiff(id string, parent string, diff io.Reader) (size int64, err error) {
  613. if !d.isParent(id, parent) {
  614. return d.naiveDiff.ApplyDiff(id, parent, diff)
  615. }
  616. applyDir := d.getDiffPath(id)
  617. logger.Debugf("Applying tar in %s", applyDir)
  618. // Overlay doesn't need the parent id to apply the diff
  619. if err := untar(diff, applyDir, &archive.TarOptions{
  620. UIDMaps: d.uidMaps,
  621. GIDMaps: d.gidMaps,
  622. WhiteoutFormat: archive.OverlayWhiteoutFormat,
  623. InUserNS: rsystem.RunningInUserNS(),
  624. }); err != nil {
  625. return 0, err
  626. }
  627. return directory.Size(context.TODO(), applyDir)
  628. }
  629. func (d *Driver) getDiffPath(id string) string {
  630. dir := d.dir(id)
  631. return path.Join(dir, "diff")
  632. }
  633. // DiffSize calculates the changes between the specified id
  634. // and its parent and returns the size in bytes of the changes
  635. // relative to its base filesystem directory.
  636. func (d *Driver) DiffSize(id, parent string) (size int64, err error) {
  637. if useNaiveDiff(d.home) || !d.isParent(id, parent) {
  638. return d.naiveDiff.DiffSize(id, parent)
  639. }
  640. return directory.Size(context.TODO(), d.getDiffPath(id))
  641. }
  642. // Diff produces an archive of the changes between the specified
  643. // layer and its parent layer which may be "".
  644. func (d *Driver) Diff(id, parent string) (io.ReadCloser, error) {
  645. if useNaiveDiff(d.home) || !d.isParent(id, parent) {
  646. return d.naiveDiff.Diff(id, parent)
  647. }
  648. diffPath := d.getDiffPath(id)
  649. logger.Debugf("Tar with options on %s", diffPath)
  650. return archive.TarWithOptions(diffPath, &archive.TarOptions{
  651. Compression: archive.Uncompressed,
  652. UIDMaps: d.uidMaps,
  653. GIDMaps: d.gidMaps,
  654. WhiteoutFormat: archive.OverlayWhiteoutFormat,
  655. })
  656. }
  657. // Changes produces a list of changes between the specified layer and its
  658. // parent layer. If parent is "", then all changes will be ADD changes.
  659. func (d *Driver) Changes(id, parent string) ([]archive.Change, error) {
  660. return d.naiveDiff.Changes(id, parent)
  661. }