overlay.go 15 KB

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