overlay.go 23 KB

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