overlay.go 13 KB

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