aufs.go 15 KB

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