overlay.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // +build linux
  2. package overlay // import "github.com/docker/docker/daemon/graphdriver/overlay"
  3. import (
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "path"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "github.com/docker/docker/daemon/graphdriver"
  13. "github.com/docker/docker/daemon/graphdriver/copy"
  14. "github.com/docker/docker/daemon/graphdriver/overlayutils"
  15. "github.com/docker/docker/pkg/archive"
  16. "github.com/docker/docker/pkg/containerfs"
  17. "github.com/docker/docker/pkg/fsutils"
  18. "github.com/docker/docker/pkg/idtools"
  19. "github.com/docker/docker/pkg/parsers"
  20. "github.com/docker/docker/pkg/system"
  21. "github.com/moby/locker"
  22. "github.com/moby/sys/mount"
  23. "github.com/opencontainers/selinux/go-selinux/label"
  24. "github.com/sirupsen/logrus"
  25. "golang.org/x/sys/unix"
  26. )
  27. // This is a small wrapper over the NaiveDiffWriter that lets us have a custom
  28. // implementation of ApplyDiff()
  29. var (
  30. // ErrApplyDiffFallback is returned to indicate that a normal ApplyDiff is applied as a fallback from Naive diff writer.
  31. ErrApplyDiffFallback = fmt.Errorf("Fall back to normal ApplyDiff")
  32. backingFs = "<unknown>"
  33. )
  34. // ApplyDiffProtoDriver wraps the ProtoDriver by extending the interface with ApplyDiff method.
  35. type ApplyDiffProtoDriver interface {
  36. graphdriver.ProtoDriver
  37. // ApplyDiff writes the diff to the archive for the given id and parent id.
  38. // It returns the size in bytes written if successful, an error ErrApplyDiffFallback is returned otherwise.
  39. ApplyDiff(id, parent string, diff io.Reader) (size int64, err error)
  40. }
  41. type naiveDiffDriverWithApply struct {
  42. graphdriver.Driver
  43. applyDiff ApplyDiffProtoDriver
  44. }
  45. // NaiveDiffDriverWithApply returns a NaiveDiff driver with custom ApplyDiff.
  46. func NaiveDiffDriverWithApply(driver ApplyDiffProtoDriver, uidMaps, gidMaps []idtools.IDMap) graphdriver.Driver {
  47. return &naiveDiffDriverWithApply{
  48. Driver: graphdriver.NewNaiveDiffDriver(driver, uidMaps, gidMaps),
  49. applyDiff: driver,
  50. }
  51. }
  52. // ApplyDiff creates a diff layer with either the NaiveDiffDriver or with a fallback.
  53. func (d *naiveDiffDriverWithApply) ApplyDiff(id, parent string, diff io.Reader) (int64, error) {
  54. b, err := d.applyDiff.ApplyDiff(id, parent, diff)
  55. if err == ErrApplyDiffFallback {
  56. return d.Driver.ApplyDiff(id, parent, diff)
  57. }
  58. return b, err
  59. }
  60. // This backend uses the overlay union filesystem for containers
  61. // plus hard link file sharing for images.
  62. // Each container/image can have a "root" subdirectory which is a plain
  63. // filesystem hierarchy, or they can use overlay.
  64. // If they use overlay there is a "upper" directory and a "lower-id"
  65. // file, as well as "merged" and "work" directories. The "upper"
  66. // directory has the upper layer of the overlay, and "lower-id" contains
  67. // the id of the parent whose "root" directory shall be used as the lower
  68. // layer in the overlay. The overlay itself is mounted in the "merged"
  69. // directory, and the "work" dir is needed for overlay to work.
  70. // When an overlay layer is created there are two cases, either the
  71. // parent has a "root" dir, then we start out with an empty "upper"
  72. // directory overlaid on the parents root. This is typically the
  73. // case with the init layer of a container which is based on an image.
  74. // If there is no "root" in the parent, we inherit the lower-id from
  75. // the parent and start by making a copy in the parent's "upper" dir.
  76. // This is typically the case for a container layer which copies
  77. // its parent -init upper layer.
  78. // Additionally we also have a custom implementation of ApplyLayer
  79. // which makes a recursive copy of the parent "root" layer using
  80. // hardlinks to share file data, and then applies the layer on top
  81. // of that. This means all child images share file (but not directory)
  82. // data with the parent.
  83. type overlayOptions struct{}
  84. // Driver contains information about the home directory and the list of active mounts that are created using this driver.
  85. type Driver struct {
  86. home string
  87. uidMaps []idtools.IDMap
  88. gidMaps []idtools.IDMap
  89. ctr *graphdriver.RefCounter
  90. supportsDType bool
  91. locker *locker.Locker
  92. }
  93. func init() {
  94. graphdriver.Register("overlay", Init)
  95. }
  96. // Init returns the NaiveDiffDriver, a native diff driver for overlay filesystem.
  97. // If overlay filesystem is not supported on the host, the error
  98. // graphdriver.ErrNotSupported is returned.
  99. // If an overlay filesystem is not supported over an existing filesystem then
  100. // error graphdriver.ErrIncompatibleFS is returned.
  101. func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
  102. _, err := parseOptions(options)
  103. if err != nil {
  104. return nil, err
  105. }
  106. // Perform feature detection on /var/lib/docker/overlay if it's an existing directory.
  107. // This covers situations where /var/lib/docker/overlay is a mount, and on a different
  108. // filesystem than /var/lib/docker.
  109. // If the path does not exist, fall back to using /var/lib/docker for feature detection.
  110. testdir := home
  111. if _, err := os.Stat(testdir); os.IsNotExist(err) {
  112. testdir = filepath.Dir(testdir)
  113. }
  114. if err := overlayutils.SupportsOverlay(testdir, false); err != nil {
  115. logrus.WithField("storage-driver", "overlay").Error(err)
  116. return nil, graphdriver.ErrNotSupported
  117. }
  118. fsMagic, err := graphdriver.GetFSMagic(testdir)
  119. if err != nil {
  120. return nil, err
  121. }
  122. if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
  123. backingFs = fsName
  124. }
  125. supportsDType, err := fsutils.SupportsDType(testdir)
  126. if err != nil {
  127. return nil, err
  128. }
  129. if !supportsDType {
  130. if !graphdriver.IsInitialized(home) {
  131. return nil, overlayutils.ErrDTypeNotSupported("overlay", backingFs)
  132. }
  133. // allow running without d_type only for existing setups (#27443)
  134. logrus.WithField("storage-driver", "overlay").Warn(overlayutils.ErrDTypeNotSupported("overlay", backingFs))
  135. }
  136. currentID := idtools.CurrentIdentity()
  137. _, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
  138. if err != nil {
  139. return nil, err
  140. }
  141. dirID := idtools.Identity{
  142. UID: currentID.UID,
  143. GID: rootGID,
  144. }
  145. // Create the driver home dir
  146. if err := idtools.MkdirAllAndChown(home, 0710, dirID); err != nil {
  147. return nil, err
  148. }
  149. d := &Driver{
  150. home: home,
  151. uidMaps: uidMaps,
  152. gidMaps: gidMaps,
  153. ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicOverlay)),
  154. supportsDType: supportsDType,
  155. locker: locker.New(),
  156. }
  157. return NaiveDiffDriverWithApply(d, uidMaps, gidMaps), nil
  158. }
  159. func parseOptions(options []string) (*overlayOptions, error) {
  160. o := &overlayOptions{}
  161. for _, option := range options {
  162. key, _, err := parsers.ParseKeyValueOpt(option)
  163. if err != nil {
  164. return nil, err
  165. }
  166. key = strings.ToLower(key)
  167. switch key {
  168. default:
  169. return nil, fmt.Errorf("overlay: unknown option %s", key)
  170. }
  171. }
  172. return o, nil
  173. }
  174. func (d *Driver) String() string {
  175. return "overlay"
  176. }
  177. // Status returns current driver information in a two dimensional string array.
  178. // Output contains "Backing Filesystem" used in this implementation.
  179. func (d *Driver) Status() [][2]string {
  180. return [][2]string{
  181. {"Backing Filesystem", backingFs},
  182. {"Supports d_type", strconv.FormatBool(d.supportsDType)},
  183. }
  184. }
  185. // GetMetadata returns metadata about the overlay driver such as root,
  186. // LowerDir, UpperDir, WorkDir and MergeDir used to store data.
  187. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  188. dir := d.dir(id)
  189. if _, err := os.Stat(dir); err != nil {
  190. return nil, err
  191. }
  192. metadata := make(map[string]string)
  193. // If id has a root, it is an image
  194. rootDir := path.Join(dir, "root")
  195. if _, err := os.Stat(rootDir); err == nil {
  196. metadata["RootDir"] = rootDir
  197. return metadata, nil
  198. }
  199. lowerID, err := ioutil.ReadFile(path.Join(dir, "lower-id"))
  200. if err != nil {
  201. return nil, err
  202. }
  203. metadata["LowerDir"] = path.Join(d.dir(string(lowerID)), "root")
  204. metadata["UpperDir"] = path.Join(dir, "upper")
  205. metadata["WorkDir"] = path.Join(dir, "work")
  206. metadata["MergedDir"] = path.Join(dir, "merged")
  207. return metadata, nil
  208. }
  209. // Cleanup any state created by overlay which should be cleaned when daemon
  210. // is being shutdown. For now, we just have to unmount the bind mounted
  211. // we had created.
  212. func (d *Driver) Cleanup() error {
  213. return mount.RecursiveUnmount(d.home)
  214. }
  215. // CreateReadWrite creates a layer that is writable for use as a container
  216. // file system.
  217. func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
  218. return d.Create(id, parent, opts)
  219. }
  220. // Create is used to create the upper, lower, and merge directories required for overlay fs for a given id.
  221. // The parent filesystem is used to configure these directories for the overlay.
  222. func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) (retErr error) {
  223. if opts != nil && len(opts.StorageOpt) != 0 {
  224. return fmt.Errorf("--storage-opt is not supported for overlay")
  225. }
  226. dir := d.dir(id)
  227. rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  228. if err != nil {
  229. return err
  230. }
  231. root := idtools.Identity{UID: rootUID, GID: rootGID}
  232. currentID := idtools.CurrentIdentity()
  233. dirID := idtools.Identity{
  234. UID: currentID.UID,
  235. GID: rootGID,
  236. }
  237. if err := idtools.MkdirAndChown(dir, 0710, dirID); err != nil {
  238. return err
  239. }
  240. defer func() {
  241. // Clean up on failure
  242. if retErr != nil {
  243. os.RemoveAll(dir)
  244. }
  245. }()
  246. // Toplevel images are just a "root" dir
  247. if parent == "" {
  248. // This must be 0755 otherwise unprivileged users will in the container will not be able to read / in the container
  249. return idtools.MkdirAndChown(path.Join(dir, "root"), 0755, root)
  250. }
  251. parentDir := d.dir(parent)
  252. // Ensure parent exists
  253. if _, err := os.Lstat(parentDir); err != nil {
  254. return err
  255. }
  256. // If parent has a root, just do an overlay to it
  257. parentRoot := path.Join(parentDir, "root")
  258. if s, err := os.Lstat(parentRoot); err == nil {
  259. if err := idtools.MkdirAndChown(path.Join(dir, "upper"), s.Mode(), root); err != nil {
  260. return err
  261. }
  262. if err := idtools.MkdirAndChown(path.Join(dir, "work"), 0700, root); err != nil {
  263. return err
  264. }
  265. return ioutil.WriteFile(path.Join(dir, "lower-id"), []byte(parent), 0600)
  266. }
  267. // Otherwise, copy the upper and the lower-id from the parent
  268. lowerID, err := ioutil.ReadFile(path.Join(parentDir, "lower-id"))
  269. if err != nil {
  270. return err
  271. }
  272. if err := ioutil.WriteFile(path.Join(dir, "lower-id"), lowerID, 0600); err != nil {
  273. return err
  274. }
  275. parentUpperDir := path.Join(parentDir, "upper")
  276. s, err := os.Lstat(parentUpperDir)
  277. if err != nil {
  278. return err
  279. }
  280. upperDir := path.Join(dir, "upper")
  281. if err := idtools.MkdirAndChown(upperDir, s.Mode(), root); err != nil {
  282. return err
  283. }
  284. if err := idtools.MkdirAndChown(path.Join(dir, "work"), 0700, root); err != nil {
  285. return err
  286. }
  287. return copy.DirCopy(parentUpperDir, upperDir, copy.Content, true)
  288. }
  289. func (d *Driver) dir(id string) string {
  290. return path.Join(d.home, id)
  291. }
  292. // Remove cleans the directories that are created for this id.
  293. func (d *Driver) Remove(id string) error {
  294. if id == "" {
  295. return fmt.Errorf("refusing to remove the directories: id is empty")
  296. }
  297. d.locker.Lock(id)
  298. defer d.locker.Unlock(id)
  299. return system.EnsureRemoveAll(d.dir(id))
  300. }
  301. // Get creates and mounts the required file system for the given id and returns the mount path.
  302. func (d *Driver) Get(id, mountLabel string) (_ containerfs.ContainerFS, err error) {
  303. d.locker.Lock(id)
  304. defer d.locker.Unlock(id)
  305. dir := d.dir(id)
  306. if _, err := os.Stat(dir); err != nil {
  307. return nil, err
  308. }
  309. // If id has a root, just return it
  310. rootDir := path.Join(dir, "root")
  311. if _, err := os.Stat(rootDir); err == nil {
  312. return containerfs.NewLocalContainerFS(rootDir), nil
  313. }
  314. mergedDir := path.Join(dir, "merged")
  315. if count := d.ctr.Increment(mergedDir); count > 1 {
  316. return containerfs.NewLocalContainerFS(mergedDir), nil
  317. }
  318. defer func() {
  319. if err != nil {
  320. if c := d.ctr.Decrement(mergedDir); c <= 0 {
  321. if mntErr := unix.Unmount(mergedDir, 0); mntErr != nil {
  322. logrus.WithField("storage-driver", "overlay").Debugf("Failed to unmount %s: %v: %v", id, mntErr, err)
  323. }
  324. // Cleanup the created merged directory; see the comment in Put's rmdir
  325. if rmErr := unix.Rmdir(mergedDir); rmErr != nil && !os.IsNotExist(rmErr) {
  326. logrus.WithField("storage-driver", "overlay").Warnf("Failed to remove %s: %v: %v", id, rmErr, err)
  327. }
  328. }
  329. }
  330. }()
  331. lowerID, err := ioutil.ReadFile(path.Join(dir, "lower-id"))
  332. if err != nil {
  333. return nil, err
  334. }
  335. rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  336. if err != nil {
  337. return nil, err
  338. }
  339. if err := idtools.MkdirAndChown(mergedDir, 0700, idtools.Identity{UID: rootUID, GID: rootGID}); err != nil {
  340. return nil, err
  341. }
  342. var (
  343. lowerDir = path.Join(d.dir(string(lowerID)), "root")
  344. upperDir = path.Join(dir, "upper")
  345. workDir = path.Join(dir, "work")
  346. opts = fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", lowerDir, upperDir, workDir)
  347. )
  348. if err := unix.Mount("overlay", mergedDir, "overlay", 0, label.FormatMountLabel(opts, mountLabel)); err != nil {
  349. return nil, fmt.Errorf("error creating overlay mount to %s: %v", mergedDir, err)
  350. }
  351. // chown "workdir/work" to the remapped root UID/GID. Overlay fs inside a
  352. // user namespace requires this to move a directory from lower to upper.
  353. if err := os.Chown(path.Join(workDir, "work"), rootUID, rootGID); err != nil {
  354. return nil, err
  355. }
  356. return containerfs.NewLocalContainerFS(mergedDir), nil
  357. }
  358. // Put unmounts the mount path created for the give id.
  359. // It also removes the 'merged' directory to force the kernel to unmount the
  360. // overlay mount in other namespaces.
  361. func (d *Driver) Put(id string) error {
  362. d.locker.Lock(id)
  363. defer d.locker.Unlock(id)
  364. // If id has a root, just return
  365. if _, err := os.Stat(path.Join(d.dir(id), "root")); err == nil {
  366. return nil
  367. }
  368. mountpoint := path.Join(d.dir(id), "merged")
  369. logger := logrus.WithField("storage-driver", "overlay")
  370. if count := d.ctr.Decrement(mountpoint); count > 0 {
  371. return nil
  372. }
  373. if err := unix.Unmount(mountpoint, unix.MNT_DETACH); err != nil {
  374. logger.Debugf("Failed to unmount %s overlay: %v", id, err)
  375. }
  376. // Remove the mountpoint here. Removing the mountpoint (in newer kernels)
  377. // will cause all other instances of this mount in other mount namespaces
  378. // to be unmounted. This is necessary to avoid cases where an overlay mount
  379. // that is present in another namespace will cause subsequent mounts
  380. // operations to fail with ebusy. We ignore any errors here because this may
  381. // fail on older kernels which don't have
  382. // torvalds/linux@8ed936b5671bfb33d89bc60bdcc7cf0470ba52fe applied.
  383. if err := unix.Rmdir(mountpoint); err != nil {
  384. logger.Debugf("Failed to remove %s overlay: %v", id, err)
  385. }
  386. return nil
  387. }
  388. // ApplyDiff applies the new layer on top of the root, if parent does not exist with will return an ErrApplyDiffFallback error.
  389. func (d *Driver) ApplyDiff(id string, parent string, diff io.Reader) (size int64, err error) {
  390. dir := d.dir(id)
  391. if parent == "" {
  392. return 0, ErrApplyDiffFallback
  393. }
  394. parentRootDir := path.Join(d.dir(parent), "root")
  395. if _, err := os.Stat(parentRootDir); err != nil {
  396. return 0, ErrApplyDiffFallback
  397. }
  398. // We now know there is a parent, and it has a "root" directory containing
  399. // the full root filesystem. We can just hardlink it and apply the
  400. // layer. This relies on two things:
  401. // 1) ApplyDiff is only run once on a clean (no writes to upper layer) container
  402. // 2) ApplyDiff doesn't do any in-place writes to files (would break hardlinks)
  403. // These are all currently true and are not expected to break
  404. tmpRootDir, err := ioutil.TempDir(dir, "tmproot")
  405. if err != nil {
  406. return 0, err
  407. }
  408. defer func() {
  409. if err != nil {
  410. os.RemoveAll(tmpRootDir)
  411. } else {
  412. os.RemoveAll(path.Join(dir, "upper"))
  413. os.RemoveAll(path.Join(dir, "work"))
  414. os.RemoveAll(path.Join(dir, "merged"))
  415. os.RemoveAll(path.Join(dir, "lower-id"))
  416. }
  417. }()
  418. if err = copy.DirCopy(parentRootDir, tmpRootDir, copy.Hardlink, true); err != nil {
  419. return 0, err
  420. }
  421. options := &archive.TarOptions{UIDMaps: d.uidMaps, GIDMaps: d.gidMaps}
  422. if size, err = graphdriver.ApplyUncompressedLayer(tmpRootDir, diff, options); err != nil {
  423. return 0, err
  424. }
  425. rootDir := path.Join(dir, "root")
  426. if err := os.Rename(tmpRootDir, rootDir); err != nil {
  427. return 0, err
  428. }
  429. return
  430. }
  431. // Exists checks to see if the id is already mounted.
  432. func (d *Driver) Exists(id string) bool {
  433. _, err := os.Stat(d.dir(id))
  434. return err == nil
  435. }