overlay.go 14 KB

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