aufs.go 17 KB

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