overlay.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // +build linux
  2. package overlay2
  3. import (
  4. "bufio"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "os/exec"
  10. "path"
  11. "strings"
  12. "syscall"
  13. "github.com/Sirupsen/logrus"
  14. "github.com/docker/docker/daemon/graphdriver"
  15. "github.com/docker/docker/pkg/archive"
  16. "github.com/docker/docker/pkg/chrootarchive"
  17. "github.com/docker/docker/pkg/directory"
  18. "github.com/docker/docker/pkg/idtools"
  19. "github.com/docker/docker/pkg/mount"
  20. "github.com/docker/docker/pkg/parsers/kernel"
  21. "github.com/opencontainers/runc/libcontainer/label"
  22. )
  23. var (
  24. // untar defines the untar method
  25. untar = chrootarchive.UntarUncompressed
  26. )
  27. // This backend uses the overlay union filesystem for containers
  28. // with diff directories for each layer.
  29. // This version of the overlay driver requires at least kernel
  30. // 4.0.0 in order to support mounting multiple diff directories.
  31. // Each container/image has at least a "diff" directory and "link" file.
  32. // If there is also a "lower" file when there are diff layers
  33. // below as well as "merged" and "work" directories. The "diff" directory
  34. // has the upper layer of the overlay and is used to capture any
  35. // changes to the layer. The "lower" file contains all the lower layer
  36. // mounts separated by ":" and ordered from uppermost to lowermost
  37. // layers. The overlay itself is mounted in the "merged" directory,
  38. // and the "work" dir is needed for overlay to work.
  39. // The "link" file for each layer contains a unique string for the layer.
  40. // Under the "l" directory at the root there will be a symbolic link
  41. // with that unique string pointing the "diff" directory for the layer.
  42. // The symbolic links are used to reference lower layers in the "lower"
  43. // file and on mount. The links are used to shorten the total length
  44. // of a layer reference without requiring changes to the layer identifier
  45. // or root directory. Mounts are always done relative to root and
  46. // referencing the symbolic links in order to ensure the number of
  47. // lower directories can fit in a single page for making the mount
  48. // syscall. A hard upper limit of 128 lower layers is enforced to ensure
  49. // that mounts do not fail due to length.
  50. const (
  51. driverName = "overlay2"
  52. linkDir = "l"
  53. lowerFile = "lower"
  54. maxDepth = 128
  55. // idLength represents the number of random characters
  56. // which can be used to create the unique link identifer
  57. // for every layer. If this value is too long then the
  58. // page size limit for the mount command may be exceeded.
  59. // The idLength should be selected such that following equation
  60. // is true (512 is a buffer for label metadata).
  61. // ((idLength + len(linkDir) + 1) * maxDepth) <= (pageSize - 512)
  62. idLength = 26
  63. )
  64. // Driver contains information about the home directory and the list of active mounts that are created using this driver.
  65. type Driver struct {
  66. home string
  67. uidMaps []idtools.IDMap
  68. gidMaps []idtools.IDMap
  69. ctr *graphdriver.RefCounter
  70. }
  71. var backingFs = "<unknown>"
  72. func init() {
  73. graphdriver.Register(driverName, Init)
  74. }
  75. // Init returns the a native diff driver for overlay filesystem.
  76. // If overlay filesystem is not supported on the host, graphdriver.ErrNotSupported is returned as error.
  77. // If a overlay filesystem is not supported over a existing filesystem then error graphdriver.ErrIncompatibleFS is returned.
  78. func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
  79. if err := supportsOverlay(); err != nil {
  80. return nil, graphdriver.ErrNotSupported
  81. }
  82. // require kernel 4.0.0 to ensure multiple lower dirs are supported
  83. v, err := kernel.GetKernelVersion()
  84. if err != nil {
  85. return nil, err
  86. }
  87. if kernel.CompareKernelVersion(*v, kernel.VersionInfo{Kernel: 4, Major: 0, Minor: 0}) < 0 {
  88. return nil, graphdriver.ErrNotSupported
  89. }
  90. fsMagic, err := graphdriver.GetFSMagic(home)
  91. if err != nil {
  92. return nil, err
  93. }
  94. if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
  95. backingFs = fsName
  96. }
  97. // check if they are running over btrfs, aufs, zfs, overlay, or ecryptfs
  98. switch fsMagic {
  99. case graphdriver.FsMagicBtrfs, graphdriver.FsMagicAufs, graphdriver.FsMagicZfs, graphdriver.FsMagicOverlay, graphdriver.FsMagicEcryptfs:
  100. logrus.Errorf("'overlay2' is not supported over %s", backingFs)
  101. return nil, graphdriver.ErrIncompatibleFS
  102. }
  103. rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
  104. if err != nil {
  105. return nil, err
  106. }
  107. // Create the driver home dir
  108. if err := idtools.MkdirAllAs(path.Join(home, linkDir), 0700, rootUID, rootGID); err != nil && !os.IsExist(err) {
  109. return nil, err
  110. }
  111. if err := mount.MakePrivate(home); err != nil {
  112. return nil, err
  113. }
  114. d := &Driver{
  115. home: home,
  116. uidMaps: uidMaps,
  117. gidMaps: gidMaps,
  118. ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicOverlay)),
  119. }
  120. return d, nil
  121. }
  122. func supportsOverlay() error {
  123. // We can try to modprobe overlay first before looking at
  124. // proc/filesystems for when overlay is supported
  125. exec.Command("modprobe", "overlay").Run()
  126. f, err := os.Open("/proc/filesystems")
  127. if err != nil {
  128. return err
  129. }
  130. defer f.Close()
  131. s := bufio.NewScanner(f)
  132. for s.Scan() {
  133. if s.Text() == "nodev\toverlay" {
  134. return nil
  135. }
  136. }
  137. logrus.Error("'overlay' not found as a supported filesystem on this host. Please ensure kernel is new enough and has overlay support loaded.")
  138. return graphdriver.ErrNotSupported
  139. }
  140. func (d *Driver) String() string {
  141. return driverName
  142. }
  143. // Status returns current driver information in a two dimensional string array.
  144. // Output contains "Backing Filesystem" used in this implementation.
  145. func (d *Driver) Status() [][2]string {
  146. return [][2]string{
  147. {"Backing Filesystem", backingFs},
  148. }
  149. }
  150. // GetMetadata returns meta data about the overlay driver such as
  151. // LowerDir, UpperDir, WorkDir and MergeDir used to store data.
  152. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  153. dir := d.dir(id)
  154. if _, err := os.Stat(dir); err != nil {
  155. return nil, err
  156. }
  157. metadata := map[string]string{
  158. "WorkDir": path.Join(dir, "work"),
  159. "MergedDir": path.Join(dir, "merged"),
  160. "UpperDir": path.Join(dir, "diff"),
  161. }
  162. lowerDirs, err := d.getLowerDirs(id)
  163. if err != nil {
  164. return nil, err
  165. }
  166. if len(lowerDirs) > 0 {
  167. metadata["LowerDir"] = strings.Join(lowerDirs, ":")
  168. }
  169. return metadata, nil
  170. }
  171. // Cleanup any state created by overlay which should be cleaned when daemon
  172. // is being shutdown. For now, we just have to unmount the bind mounted
  173. // we had created.
  174. func (d *Driver) Cleanup() error {
  175. return mount.Unmount(d.home)
  176. }
  177. // CreateReadWrite creates a layer that is writable for use as a container
  178. // file system.
  179. func (d *Driver) CreateReadWrite(id, parent, mountLabel string, storageOpt map[string]string) error {
  180. return d.Create(id, parent, mountLabel, storageOpt)
  181. }
  182. // Create is used to create the upper, lower, and merge directories required for overlay fs for a given id.
  183. // The parent filesystem is used to configure these directories for the overlay.
  184. func (d *Driver) Create(id, parent, mountLabel string, storageOpt map[string]string) (retErr error) {
  185. if len(storageOpt) != 0 {
  186. return fmt.Errorf("--storage-opt is not supported for overlay")
  187. }
  188. dir := d.dir(id)
  189. rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  190. if err != nil {
  191. return err
  192. }
  193. if err := idtools.MkdirAllAs(path.Dir(dir), 0700, rootUID, rootGID); err != nil {
  194. return err
  195. }
  196. if err := idtools.MkdirAs(dir, 0700, rootUID, rootGID); err != nil {
  197. return err
  198. }
  199. defer func() {
  200. // Clean up on failure
  201. if retErr != nil {
  202. os.RemoveAll(dir)
  203. }
  204. }()
  205. if err := idtools.MkdirAs(path.Join(dir, "diff"), 0755, rootUID, rootGID); err != nil {
  206. return err
  207. }
  208. lid := generateID(idLength)
  209. if err := os.Symlink(path.Join("..", id, "diff"), path.Join(d.home, linkDir, lid)); err != nil {
  210. return err
  211. }
  212. // Write link id to link file
  213. if err := ioutil.WriteFile(path.Join(dir, "link"), []byte(lid), 0644); err != nil {
  214. return err
  215. }
  216. // if no parent directory, done
  217. if parent == "" {
  218. return nil
  219. }
  220. if err := idtools.MkdirAs(path.Join(dir, "work"), 0700, rootUID, rootGID); err != nil {
  221. return err
  222. }
  223. if err := idtools.MkdirAs(path.Join(dir, "merged"), 0700, rootUID, rootGID); err != nil {
  224. return err
  225. }
  226. lower, err := d.getLower(parent)
  227. if err != nil {
  228. return err
  229. }
  230. if lower != "" {
  231. if err := ioutil.WriteFile(path.Join(dir, lowerFile), []byte(lower), 0666); err != nil {
  232. return err
  233. }
  234. }
  235. return nil
  236. }
  237. func (d *Driver) getLower(parent string) (string, error) {
  238. parentDir := d.dir(parent)
  239. // Ensure parent exists
  240. if _, err := os.Lstat(parentDir); err != nil {
  241. return "", err
  242. }
  243. // Read Parent link fileA
  244. parentLink, err := ioutil.ReadFile(path.Join(parentDir, "link"))
  245. if err != nil {
  246. return "", err
  247. }
  248. lowers := []string{path.Join(linkDir, string(parentLink))}
  249. parentLower, err := ioutil.ReadFile(path.Join(parentDir, lowerFile))
  250. if err == nil {
  251. parentLowers := strings.Split(string(parentLower), ":")
  252. lowers = append(lowers, parentLowers...)
  253. }
  254. if len(lowers) > maxDepth {
  255. return "", errors.New("max depth exceeded")
  256. }
  257. return strings.Join(lowers, ":"), nil
  258. }
  259. func (d *Driver) dir(id string) string {
  260. return path.Join(d.home, id)
  261. }
  262. func (d *Driver) getLowerDirs(id string) ([]string, error) {
  263. var lowersArray []string
  264. lowers, err := ioutil.ReadFile(path.Join(d.dir(id), lowerFile))
  265. if err == nil {
  266. for _, s := range strings.Split(string(lowers), ":") {
  267. lp, err := os.Readlink(path.Join(d.home, s))
  268. if err != nil {
  269. return nil, err
  270. }
  271. lowersArray = append(lowersArray, path.Clean(path.Join(d.home, "link", lp)))
  272. }
  273. } else if !os.IsNotExist(err) {
  274. return nil, err
  275. }
  276. return lowersArray, nil
  277. }
  278. // Remove cleans the directories that are created for this id.
  279. func (d *Driver) Remove(id string) error {
  280. dir := d.dir(id)
  281. lid, err := ioutil.ReadFile(path.Join(dir, "link"))
  282. if err == nil {
  283. if err := os.RemoveAll(path.Join(d.home, linkDir, string(lid))); err != nil {
  284. logrus.Debugf("Failed to remove link: %v", err)
  285. }
  286. }
  287. if err := os.RemoveAll(dir); err != nil && !os.IsNotExist(err) {
  288. return err
  289. }
  290. return nil
  291. }
  292. // Get creates and mounts the required file system for the given id and returns the mount path.
  293. func (d *Driver) Get(id string, mountLabel string) (s string, err error) {
  294. dir := d.dir(id)
  295. if _, err := os.Stat(dir); err != nil {
  296. return "", err
  297. }
  298. diffDir := path.Join(dir, "diff")
  299. lowers, err := ioutil.ReadFile(path.Join(dir, lowerFile))
  300. if err != nil {
  301. // If no lower, just return diff directory
  302. if os.IsNotExist(err) {
  303. return diffDir, nil
  304. }
  305. return "", err
  306. }
  307. mergedDir := path.Join(dir, "merged")
  308. if count := d.ctr.Increment(mergedDir); count > 1 {
  309. return mergedDir, nil
  310. }
  311. defer func() {
  312. if err != nil {
  313. if c := d.ctr.Decrement(mergedDir); c <= 0 {
  314. syscall.Unmount(mergedDir, 0)
  315. }
  316. }
  317. }()
  318. workDir := path.Join(dir, "work")
  319. opts := fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", string(lowers), path.Join(id, "diff"), path.Join(id, "work"))
  320. mountLabel = label.FormatMountLabel(opts, mountLabel)
  321. if len(mountLabel) > syscall.Getpagesize() {
  322. return "", fmt.Errorf("cannot mount layer, mount label too large %d", len(mountLabel))
  323. }
  324. if err := mountFrom(d.home, "overlay", path.Join(id, "merged"), "overlay", mountLabel); err != nil {
  325. return "", fmt.Errorf("error creating overlay mount to %s: %v", mergedDir, err)
  326. }
  327. // chown "workdir/work" to the remapped root UID/GID. Overlay fs inside a
  328. // user namespace requires this to move a directory from lower to upper.
  329. rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  330. if err != nil {
  331. return "", err
  332. }
  333. if err := os.Chown(path.Join(workDir, "work"), rootUID, rootGID); err != nil {
  334. return "", err
  335. }
  336. return mergedDir, nil
  337. }
  338. // Put unmounts the mount path created for the give id.
  339. func (d *Driver) Put(id string) error {
  340. mountpoint := path.Join(d.dir(id), "merged")
  341. if count := d.ctr.Decrement(mountpoint); count > 0 {
  342. return nil
  343. }
  344. if err := syscall.Unmount(mountpoint, 0); err != nil {
  345. logrus.Debugf("Failed to unmount %s overlay: %v", id, err)
  346. }
  347. return nil
  348. }
  349. // Exists checks to see if the id is already mounted.
  350. func (d *Driver) Exists(id string) bool {
  351. _, err := os.Stat(d.dir(id))
  352. return err == nil
  353. }
  354. // ApplyDiff applies the new layer into a root
  355. func (d *Driver) ApplyDiff(id string, parent string, diff archive.Reader) (size int64, err error) {
  356. applyDir := d.getDiffPath(id)
  357. logrus.Debugf("Applying tar in %s", applyDir)
  358. // Overlay doesn't need the parent id to apply the diff
  359. if err := untar(diff, applyDir, &archive.TarOptions{
  360. UIDMaps: d.uidMaps,
  361. GIDMaps: d.gidMaps,
  362. WhiteoutFormat: archive.OverlayWhiteoutFormat,
  363. }); err != nil {
  364. return 0, err
  365. }
  366. return d.DiffSize(id, parent)
  367. }
  368. func (d *Driver) getDiffPath(id string) string {
  369. dir := d.dir(id)
  370. return path.Join(dir, "diff")
  371. }
  372. // DiffSize calculates the changes between the specified id
  373. // and its parent and returns the size in bytes of the changes
  374. // relative to its base filesystem directory.
  375. func (d *Driver) DiffSize(id, parent string) (size int64, err error) {
  376. return directory.Size(d.getDiffPath(id))
  377. }
  378. // Diff produces an archive of the changes between the specified
  379. // layer and its parent layer which may be "".
  380. func (d *Driver) Diff(id, parent string) (archive.Archive, error) {
  381. diffPath := d.getDiffPath(id)
  382. logrus.Debugf("Tar with options on %s", diffPath)
  383. return archive.TarWithOptions(diffPath, &archive.TarOptions{
  384. Compression: archive.Uncompressed,
  385. UIDMaps: d.uidMaps,
  386. GIDMaps: d.gidMaps,
  387. WhiteoutFormat: archive.OverlayWhiteoutFormat,
  388. })
  389. }
  390. // Changes produces a list of changes between the specified layer
  391. // and its parent layer. If parent is "", then all changes will be ADD changes.
  392. func (d *Driver) Changes(id, parent string) ([]archive.Change, error) {
  393. // Overlay doesn't have snapshots, so we need to get changes from all parent
  394. // layers.
  395. diffPath := d.getDiffPath(id)
  396. layers, err := d.getLowerDirs(id)
  397. if err != nil {
  398. return nil, err
  399. }
  400. return archive.OverlayChanges(layers, diffPath)
  401. }