aufs.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. // +build linux
  2. /*
  3. aufs driver directory structure
  4. .
  5. ├── layers // Metadata of layers
  6. │ ├── 1
  7. │ ├── 2
  8. │ └── 3
  9. ├── diff // Content of the layer
  10. │ ├── 1 // Contains layers that need to be mounted for the id
  11. │ ├── 2
  12. │ └── 3
  13. └── mnt // Mount points for the rw layers to be mounted
  14. ├── 1
  15. ├── 2
  16. └── 3
  17. */
  18. package aufs
  19. import (
  20. "bufio"
  21. "fmt"
  22. "io"
  23. "io/ioutil"
  24. "os"
  25. "os/exec"
  26. "path"
  27. "path/filepath"
  28. "strings"
  29. "sync"
  30. "syscall"
  31. "time"
  32. "github.com/Sirupsen/logrus"
  33. "github.com/vbatts/tar-split/tar/storage"
  34. "github.com/docker/docker/daemon/graphdriver"
  35. "github.com/docker/docker/pkg/archive"
  36. "github.com/docker/docker/pkg/chrootarchive"
  37. "github.com/docker/docker/pkg/directory"
  38. "github.com/docker/docker/pkg/idtools"
  39. "github.com/docker/docker/pkg/locker"
  40. mountpk "github.com/docker/docker/pkg/mount"
  41. "github.com/docker/docker/pkg/system"
  42. rsystem "github.com/opencontainers/runc/libcontainer/system"
  43. "github.com/opencontainers/selinux/go-selinux/label"
  44. )
  45. var (
  46. // ErrAufsNotSupported is returned if aufs is not supported by the host.
  47. ErrAufsNotSupported = fmt.Errorf("AUFS was not found in /proc/filesystems")
  48. // ErrAufsNested means aufs cannot be used bc we are in a user namespace
  49. ErrAufsNested = fmt.Errorf("AUFS cannot be used in non-init user namespace")
  50. backingFs = "<unknown>"
  51. enableDirpermLock sync.Once
  52. enableDirperm bool
  53. )
  54. func init() {
  55. graphdriver.Register("aufs", Init)
  56. }
  57. // Driver contains information about the filesystem mounted.
  58. type Driver struct {
  59. sync.Mutex
  60. root string
  61. uidMaps []idtools.IDMap
  62. gidMaps []idtools.IDMap
  63. ctr *graphdriver.RefCounter
  64. pathCacheLock sync.Mutex
  65. pathCache map[string]string
  66. naiveDiff graphdriver.DiffDriver
  67. locker *locker.Locker
  68. }
  69. // Init returns a new AUFS driver.
  70. // An error is returned if AUFS is not supported.
  71. func Init(root string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
  72. // Try to load the aufs kernel module
  73. if err := supportsAufs(); err != nil {
  74. return nil, graphdriver.ErrNotSupported
  75. }
  76. fsMagic, err := graphdriver.GetFSMagic(root)
  77. if err != nil {
  78. return nil, err
  79. }
  80. if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
  81. backingFs = fsName
  82. }
  83. switch fsMagic {
  84. case graphdriver.FsMagicAufs, graphdriver.FsMagicBtrfs, graphdriver.FsMagicEcryptfs:
  85. logrus.Errorf("AUFS is not supported over %s", backingFs)
  86. return nil, graphdriver.ErrIncompatibleFS
  87. }
  88. paths := []string{
  89. "mnt",
  90. "diff",
  91. "layers",
  92. }
  93. a := &Driver{
  94. root: root,
  95. uidMaps: uidMaps,
  96. gidMaps: gidMaps,
  97. pathCache: make(map[string]string),
  98. ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicAufs)),
  99. locker: locker.New(),
  100. }
  101. rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
  102. if err != nil {
  103. return nil, err
  104. }
  105. // Create the root aufs driver dir and return
  106. // if it already exists
  107. // If not populate the dir structure
  108. if err := idtools.MkdirAllAs(root, 0700, rootUID, rootGID); err != nil {
  109. if os.IsExist(err) {
  110. return a, nil
  111. }
  112. return nil, err
  113. }
  114. if err := mountpk.MakePrivate(root); err != nil {
  115. return nil, err
  116. }
  117. // Populate the dir structure
  118. for _, p := range paths {
  119. if err := idtools.MkdirAllAs(path.Join(root, p), 0700, rootUID, rootGID); err != nil {
  120. return nil, err
  121. }
  122. }
  123. a.naiveDiff = graphdriver.NewNaiveDiffDriver(a, uidMaps, gidMaps)
  124. return a, nil
  125. }
  126. // Return a nil error if the kernel supports aufs
  127. // We cannot modprobe because inside dind modprobe fails
  128. // to run
  129. func supportsAufs() error {
  130. // We can try to modprobe aufs first before looking at
  131. // proc/filesystems for when aufs is supported
  132. exec.Command("modprobe", "aufs").Run()
  133. if rsystem.RunningInUserNS() {
  134. return ErrAufsNested
  135. }
  136. f, err := os.Open("/proc/filesystems")
  137. if err != nil {
  138. return err
  139. }
  140. defer f.Close()
  141. s := bufio.NewScanner(f)
  142. for s.Scan() {
  143. if strings.Contains(s.Text(), "aufs") {
  144. return nil
  145. }
  146. }
  147. return ErrAufsNotSupported
  148. }
  149. func (a *Driver) rootPath() string {
  150. return a.root
  151. }
  152. func (*Driver) String() string {
  153. return "aufs"
  154. }
  155. // Status returns current information about the filesystem such as root directory, number of directories mounted, etc.
  156. func (a *Driver) Status() [][2]string {
  157. ids, _ := loadIds(path.Join(a.rootPath(), "layers"))
  158. return [][2]string{
  159. {"Root Dir", a.rootPath()},
  160. {"Backing Filesystem", backingFs},
  161. {"Dirs", fmt.Sprintf("%d", len(ids))},
  162. {"Dirperm1 Supported", fmt.Sprintf("%v", useDirperm())},
  163. }
  164. }
  165. // GetMetadata not implemented
  166. func (a *Driver) GetMetadata(id string) (map[string]string, error) {
  167. return nil, nil
  168. }
  169. // Exists returns true if the given id is registered with
  170. // this driver
  171. func (a *Driver) Exists(id string) bool {
  172. if _, err := os.Lstat(path.Join(a.rootPath(), "layers", id)); err != nil {
  173. return false
  174. }
  175. return true
  176. }
  177. // CreateReadWrite creates a layer that is writable for use as a container
  178. // file system.
  179. func (a *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
  180. return a.Create(id, parent, opts)
  181. }
  182. // Create three folders for each id
  183. // mnt, layers, and diff
  184. func (a *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
  185. if opts != nil && len(opts.StorageOpt) != 0 {
  186. return fmt.Errorf("--storage-opt is not supported for aufs")
  187. }
  188. if err := a.createDirsFor(id); err != nil {
  189. return err
  190. }
  191. // Write the layers metadata
  192. f, err := os.Create(path.Join(a.rootPath(), "layers", id))
  193. if err != nil {
  194. return err
  195. }
  196. defer f.Close()
  197. if parent != "" {
  198. ids, err := getParentIDs(a.rootPath(), parent)
  199. if err != nil {
  200. return err
  201. }
  202. if _, err := fmt.Fprintln(f, parent); err != nil {
  203. return err
  204. }
  205. for _, i := range ids {
  206. if _, err := fmt.Fprintln(f, i); err != nil {
  207. return err
  208. }
  209. }
  210. }
  211. return nil
  212. }
  213. // createDirsFor creates two directories for the given id.
  214. // mnt and diff
  215. func (a *Driver) createDirsFor(id string) error {
  216. paths := []string{
  217. "mnt",
  218. "diff",
  219. }
  220. rootUID, rootGID, err := idtools.GetRootUIDGID(a.uidMaps, a.gidMaps)
  221. if err != nil {
  222. return err
  223. }
  224. // Directory permission is 0755.
  225. // The path of directories are <aufs_root_path>/mnt/<image_id>
  226. // and <aufs_root_path>/diff/<image_id>
  227. for _, p := range paths {
  228. if err := idtools.MkdirAllAs(path.Join(a.rootPath(), p, id), 0755, rootUID, rootGID); err != nil {
  229. return err
  230. }
  231. }
  232. return nil
  233. }
  234. // Remove will unmount and remove the given id.
  235. func (a *Driver) Remove(id string) error {
  236. a.locker.Lock(id)
  237. defer a.locker.Unlock(id)
  238. a.pathCacheLock.Lock()
  239. mountpoint, exists := a.pathCache[id]
  240. a.pathCacheLock.Unlock()
  241. if !exists {
  242. mountpoint = a.getMountpoint(id)
  243. }
  244. var retries int
  245. for {
  246. mounted, err := a.mounted(mountpoint)
  247. if err != nil {
  248. return err
  249. }
  250. if !mounted {
  251. break
  252. }
  253. if err := a.unmount(mountpoint); err != nil {
  254. if err != syscall.EBUSY {
  255. return fmt.Errorf("aufs: unmount error: %s: %v", mountpoint, err)
  256. }
  257. if retries >= 5 {
  258. return fmt.Errorf("aufs: unmount error after retries: %s: %v", mountpoint, err)
  259. }
  260. // If unmount returns EBUSY, it could be a transient error. Sleep and retry.
  261. retries++
  262. logrus.Warnf("unmount failed due to EBUSY: retry count: %d", retries)
  263. time.Sleep(100 * time.Millisecond)
  264. continue
  265. }
  266. break
  267. }
  268. // Atomically remove each directory in turn by first moving it out of the
  269. // way (so that docker doesn't find it anymore) before doing removal of
  270. // the whole tree.
  271. tmpMntPath := path.Join(a.mntPath(), fmt.Sprintf("%s-removing", id))
  272. if err := os.Rename(mountpoint, tmpMntPath); err != nil && !os.IsNotExist(err) {
  273. if err == syscall.EBUSY {
  274. logrus.Warn("os.Rename err due to EBUSY")
  275. }
  276. return err
  277. }
  278. defer system.EnsureRemoveAll(tmpMntPath)
  279. tmpDiffpath := path.Join(a.diffPath(), fmt.Sprintf("%s-removing", id))
  280. if err := os.Rename(a.getDiffPath(id), tmpDiffpath); err != nil && !os.IsNotExist(err) {
  281. return err
  282. }
  283. defer system.EnsureRemoveAll(tmpDiffpath)
  284. // Remove the layers file for the id
  285. if err := os.Remove(path.Join(a.rootPath(), "layers", id)); err != nil && !os.IsNotExist(err) {
  286. return err
  287. }
  288. a.pathCacheLock.Lock()
  289. delete(a.pathCache, id)
  290. a.pathCacheLock.Unlock()
  291. return nil
  292. }
  293. // Get returns the rootfs path for the id.
  294. // This will mount the dir at its given path
  295. func (a *Driver) Get(id, mountLabel string) (string, error) {
  296. a.locker.Lock(id)
  297. defer a.locker.Unlock(id)
  298. parents, err := a.getParentLayerPaths(id)
  299. if err != nil && !os.IsNotExist(err) {
  300. return "", err
  301. }
  302. a.pathCacheLock.Lock()
  303. m, exists := a.pathCache[id]
  304. a.pathCacheLock.Unlock()
  305. if !exists {
  306. m = a.getDiffPath(id)
  307. if len(parents) > 0 {
  308. m = a.getMountpoint(id)
  309. }
  310. }
  311. if count := a.ctr.Increment(m); count > 1 {
  312. return m, nil
  313. }
  314. // If a dir does not have a parent ( no layers )do not try to mount
  315. // just return the diff path to the data
  316. if len(parents) > 0 {
  317. if err := a.mount(id, m, mountLabel, parents); err != nil {
  318. return "", err
  319. }
  320. }
  321. a.pathCacheLock.Lock()
  322. a.pathCache[id] = m
  323. a.pathCacheLock.Unlock()
  324. return m, nil
  325. }
  326. // Put unmounts and updates list of active mounts.
  327. func (a *Driver) Put(id string) error {
  328. a.locker.Lock(id)
  329. defer a.locker.Unlock(id)
  330. a.pathCacheLock.Lock()
  331. m, exists := a.pathCache[id]
  332. if !exists {
  333. m = a.getMountpoint(id)
  334. a.pathCache[id] = m
  335. }
  336. a.pathCacheLock.Unlock()
  337. if count := a.ctr.Decrement(m); count > 0 {
  338. return nil
  339. }
  340. err := a.unmount(m)
  341. if err != nil {
  342. logrus.Debugf("Failed to unmount %s aufs: %v", id, err)
  343. }
  344. return err
  345. }
  346. // isParent returns if the passed in parent is the direct parent of the passed in layer
  347. func (a *Driver) isParent(id, parent string) bool {
  348. parents, _ := getParentIDs(a.rootPath(), id)
  349. if parent == "" && len(parents) > 0 {
  350. return false
  351. }
  352. return !(len(parents) > 0 && parent != parents[0])
  353. }
  354. // Diff produces an archive of the changes between the specified
  355. // layer and its parent layer which may be "".
  356. func (a *Driver) Diff(id, parent string) (io.ReadCloser, error) {
  357. if !a.isParent(id, parent) {
  358. return a.naiveDiff.Diff(id, parent)
  359. }
  360. // AUFS doesn't need the parent layer to produce a diff.
  361. return archive.TarWithOptions(path.Join(a.rootPath(), "diff", id), &archive.TarOptions{
  362. Compression: archive.Uncompressed,
  363. ExcludePatterns: []string{archive.WhiteoutMetaPrefix + "*", "!" + archive.WhiteoutOpaqueDir},
  364. UIDMaps: a.uidMaps,
  365. GIDMaps: a.gidMaps,
  366. })
  367. }
  368. type fileGetNilCloser struct {
  369. storage.FileGetter
  370. }
  371. func (f fileGetNilCloser) Close() error {
  372. return nil
  373. }
  374. // DiffGetter returns a FileGetCloser that can read files from the directory that
  375. // contains files for the layer differences. Used for direct access for tar-split.
  376. func (a *Driver) DiffGetter(id string) (graphdriver.FileGetCloser, error) {
  377. p := path.Join(a.rootPath(), "diff", id)
  378. return fileGetNilCloser{storage.NewPathFileGetter(p)}, nil
  379. }
  380. func (a *Driver) applyDiff(id string, diff io.Reader) error {
  381. return chrootarchive.UntarUncompressed(diff, path.Join(a.rootPath(), "diff", id), &archive.TarOptions{
  382. UIDMaps: a.uidMaps,
  383. GIDMaps: a.gidMaps,
  384. })
  385. }
  386. // DiffSize calculates the changes between the specified id
  387. // and its parent and returns the size in bytes of the changes
  388. // relative to its base filesystem directory.
  389. func (a *Driver) DiffSize(id, parent string) (size int64, err error) {
  390. if !a.isParent(id, parent) {
  391. return a.naiveDiff.DiffSize(id, parent)
  392. }
  393. // AUFS doesn't need the parent layer to calculate the diff size.
  394. return directory.Size(path.Join(a.rootPath(), "diff", id))
  395. }
  396. // ApplyDiff extracts the changeset from the given diff into the
  397. // layer with the specified id and parent, returning the size of the
  398. // new layer in bytes.
  399. func (a *Driver) ApplyDiff(id, parent string, diff io.Reader) (size int64, err error) {
  400. if !a.isParent(id, parent) {
  401. return a.naiveDiff.ApplyDiff(id, parent, diff)
  402. }
  403. // AUFS doesn't need the parent id to apply the diff if it is the direct parent.
  404. if err = a.applyDiff(id, diff); err != nil {
  405. return
  406. }
  407. return a.DiffSize(id, parent)
  408. }
  409. // Changes produces a list of changes between the specified layer
  410. // and its parent layer. If parent is "", then all changes will be ADD changes.
  411. func (a *Driver) Changes(id, parent string) ([]archive.Change, error) {
  412. if !a.isParent(id, parent) {
  413. return a.naiveDiff.Changes(id, parent)
  414. }
  415. // AUFS doesn't have snapshots, so we need to get changes from all parent
  416. // layers.
  417. layers, err := a.getParentLayerPaths(id)
  418. if err != nil {
  419. return nil, err
  420. }
  421. return archive.Changes(layers, path.Join(a.rootPath(), "diff", id))
  422. }
  423. func (a *Driver) getParentLayerPaths(id string) ([]string, error) {
  424. parentIds, err := getParentIDs(a.rootPath(), id)
  425. if err != nil {
  426. return nil, err
  427. }
  428. layers := make([]string, len(parentIds))
  429. // Get the diff paths for all the parent ids
  430. for i, p := range parentIds {
  431. layers[i] = path.Join(a.rootPath(), "diff", p)
  432. }
  433. return layers, nil
  434. }
  435. func (a *Driver) mount(id string, target string, mountLabel string, layers []string) error {
  436. a.Lock()
  437. defer a.Unlock()
  438. // If the id is mounted or we get an error return
  439. if mounted, err := a.mounted(target); err != nil || mounted {
  440. return err
  441. }
  442. rw := a.getDiffPath(id)
  443. if err := a.aufsMount(layers, rw, target, mountLabel); err != nil {
  444. return fmt.Errorf("error creating aufs mount to %s: %v", target, err)
  445. }
  446. return nil
  447. }
  448. func (a *Driver) unmount(mountPath string) error {
  449. a.Lock()
  450. defer a.Unlock()
  451. if mounted, err := a.mounted(mountPath); err != nil || !mounted {
  452. return err
  453. }
  454. if err := Unmount(mountPath); err != nil {
  455. return err
  456. }
  457. return nil
  458. }
  459. func (a *Driver) mounted(mountpoint string) (bool, error) {
  460. return graphdriver.Mounted(graphdriver.FsMagicAufs, mountpoint)
  461. }
  462. // Cleanup aufs and unmount all mountpoints
  463. func (a *Driver) Cleanup() error {
  464. var dirs []string
  465. if err := filepath.Walk(a.mntPath(), func(path string, info os.FileInfo, err error) error {
  466. if err != nil {
  467. return err
  468. }
  469. if !info.IsDir() {
  470. return nil
  471. }
  472. dirs = append(dirs, path)
  473. return nil
  474. }); err != nil {
  475. return err
  476. }
  477. for _, m := range dirs {
  478. if err := a.unmount(m); err != nil {
  479. logrus.Debugf("aufs error unmounting %s: %s", m, err)
  480. }
  481. }
  482. return mountpk.Unmount(a.root)
  483. }
  484. func (a *Driver) aufsMount(ro []string, rw, target, mountLabel string) (err error) {
  485. defer func() {
  486. if err != nil {
  487. Unmount(target)
  488. }
  489. }()
  490. // Mount options are clipped to page size(4096 bytes). If there are more
  491. // layers then these are remounted individually using append.
  492. offset := 54
  493. if useDirperm() {
  494. offset += len("dirperm1")
  495. }
  496. b := make([]byte, syscall.Getpagesize()-len(mountLabel)-offset) // room for xino & mountLabel
  497. bp := copy(b, fmt.Sprintf("br:%s=rw", rw))
  498. index := 0
  499. for ; index < len(ro); index++ {
  500. layer := fmt.Sprintf(":%s=ro+wh", ro[index])
  501. if bp+len(layer) > len(b) {
  502. break
  503. }
  504. bp += copy(b[bp:], layer)
  505. }
  506. opts := "dio,xino=/dev/shm/aufs.xino"
  507. if useDirperm() {
  508. opts += ",dirperm1"
  509. }
  510. data := label.FormatMountLabel(fmt.Sprintf("%s,%s", string(b[:bp]), opts), mountLabel)
  511. if err = mount("none", target, "aufs", 0, data); err != nil {
  512. return
  513. }
  514. for ; index < len(ro); index++ {
  515. layer := fmt.Sprintf(":%s=ro+wh", ro[index])
  516. data := label.FormatMountLabel(fmt.Sprintf("append%s", layer), mountLabel)
  517. if err = mount("none", target, "aufs", syscall.MS_REMOUNT, data); err != nil {
  518. return
  519. }
  520. }
  521. return
  522. }
  523. // useDirperm checks dirperm1 mount option can be used with the current
  524. // version of aufs.
  525. func useDirperm() bool {
  526. enableDirpermLock.Do(func() {
  527. base, err := ioutil.TempDir("", "docker-aufs-base")
  528. if err != nil {
  529. logrus.Errorf("error checking dirperm1: %v", err)
  530. return
  531. }
  532. defer os.RemoveAll(base)
  533. union, err := ioutil.TempDir("", "docker-aufs-union")
  534. if err != nil {
  535. logrus.Errorf("error checking dirperm1: %v", err)
  536. return
  537. }
  538. defer os.RemoveAll(union)
  539. opts := fmt.Sprintf("br:%s,dirperm1,xino=/dev/shm/aufs.xino", base)
  540. if err := mount("none", union, "aufs", 0, opts); err != nil {
  541. return
  542. }
  543. enableDirperm = true
  544. if err := Unmount(union); err != nil {
  545. logrus.Errorf("error checking dirperm1: failed to unmount %v", err)
  546. }
  547. })
  548. return enableDirperm
  549. }