aufs.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. aufs driver directory structure
  3. .
  4. ├── layers // Metadata of layers
  5. │   ├── 1
  6. │   ├── 2
  7. │   └── 3
  8. ├── diffs // Content of the layer
  9. │   ├── 1 // Contains layers that need to be mounted for the id
  10. │   ├── 2
  11. │   └── 3
  12. └── mnt // Mount points for the rw layers to be mounted
  13. ├── 1
  14. ├── 2
  15. └── 3
  16. */
  17. package aufs
  18. import (
  19. "bufio"
  20. "fmt"
  21. "github.com/dotcloud/docker/archive"
  22. "github.com/dotcloud/docker/daemon/graphdriver"
  23. "github.com/dotcloud/docker/pkg/label"
  24. mountpk "github.com/dotcloud/docker/pkg/mount"
  25. "github.com/dotcloud/docker/utils"
  26. "os"
  27. "os/exec"
  28. "path"
  29. "strings"
  30. "sync"
  31. )
  32. var (
  33. ErrAufsNotSupported = fmt.Errorf("AUFS was not found in /proc/filesystems")
  34. )
  35. func init() {
  36. graphdriver.Register("aufs", Init)
  37. }
  38. type Driver struct {
  39. root string
  40. sync.Mutex // Protects concurrent modification to active
  41. active map[string]int
  42. }
  43. // New returns a new AUFS driver.
  44. // An error is returned if AUFS is not supported.
  45. func Init(root string) (graphdriver.Driver, error) {
  46. // Try to load the aufs kernel module
  47. if err := supportsAufs(); err != nil {
  48. return nil, graphdriver.ErrNotSupported
  49. }
  50. paths := []string{
  51. "mnt",
  52. "diff",
  53. "layers",
  54. }
  55. a := &Driver{
  56. root: root,
  57. active: make(map[string]int),
  58. }
  59. // Create the root aufs driver dir and return
  60. // if it already exists
  61. // If not populate the dir structure
  62. if err := os.MkdirAll(root, 0755); err != nil {
  63. if os.IsExist(err) {
  64. return a, nil
  65. }
  66. return nil, err
  67. }
  68. for _, p := range paths {
  69. if err := os.MkdirAll(path.Join(root, p), 0755); err != nil {
  70. return nil, err
  71. }
  72. }
  73. return a, nil
  74. }
  75. // Return a nil error if the kernel supports aufs
  76. // We cannot modprobe because inside dind modprobe fails
  77. // to run
  78. func supportsAufs() error {
  79. // We can try to modprobe aufs first before looking at
  80. // proc/filesystems for when aufs is supported
  81. exec.Command("modprobe", "aufs").Run()
  82. f, err := os.Open("/proc/filesystems")
  83. if err != nil {
  84. return err
  85. }
  86. defer f.Close()
  87. s := bufio.NewScanner(f)
  88. for s.Scan() {
  89. if strings.Contains(s.Text(), "aufs") {
  90. return nil
  91. }
  92. }
  93. return ErrAufsNotSupported
  94. }
  95. func (a Driver) rootPath() string {
  96. return a.root
  97. }
  98. func (Driver) String() string {
  99. return "aufs"
  100. }
  101. func (a Driver) Status() [][2]string {
  102. ids, _ := loadIds(path.Join(a.rootPath(), "layers"))
  103. return [][2]string{
  104. {"Root Dir", a.rootPath()},
  105. {"Dirs", fmt.Sprintf("%d", len(ids))},
  106. }
  107. }
  108. // Exists returns true if the given id is registered with
  109. // this driver
  110. func (a Driver) Exists(id string) bool {
  111. if _, err := os.Lstat(path.Join(a.rootPath(), "layers", id)); err != nil {
  112. return false
  113. }
  114. return true
  115. }
  116. // Three folders are created for each id
  117. // mnt, layers, and diff
  118. func (a *Driver) Create(id, parent string) error {
  119. if err := a.createDirsFor(id); err != nil {
  120. return err
  121. }
  122. // Write the layers metadata
  123. f, err := os.Create(path.Join(a.rootPath(), "layers", id))
  124. if err != nil {
  125. return err
  126. }
  127. defer f.Close()
  128. if parent != "" {
  129. ids, err := getParentIds(a.rootPath(), parent)
  130. if err != nil {
  131. return err
  132. }
  133. if _, err := fmt.Fprintln(f, parent); err != nil {
  134. return err
  135. }
  136. for _, i := range ids {
  137. if _, err := fmt.Fprintln(f, i); err != nil {
  138. return err
  139. }
  140. }
  141. }
  142. return nil
  143. }
  144. func (a *Driver) createDirsFor(id string) error {
  145. paths := []string{
  146. "mnt",
  147. "diff",
  148. }
  149. for _, p := range paths {
  150. if err := os.MkdirAll(path.Join(a.rootPath(), p, id), 0755); err != nil {
  151. return err
  152. }
  153. }
  154. return nil
  155. }
  156. // Unmount and remove the dir information
  157. func (a *Driver) Remove(id string) error {
  158. // Protect the a.active from concurrent access
  159. a.Lock()
  160. defer a.Unlock()
  161. if a.active[id] != 0 {
  162. utils.Errorf("Warning: removing active id %s\n", id)
  163. }
  164. // Make sure the dir is umounted first
  165. if err := a.unmount(id); err != nil {
  166. return err
  167. }
  168. tmpDirs := []string{
  169. "mnt",
  170. "diff",
  171. }
  172. // Atomically remove each directory in turn by first moving it out of the
  173. // way (so that docker doesn't find it anymore) before doing removal of
  174. // the whole tree.
  175. for _, p := range tmpDirs {
  176. realPath := path.Join(a.rootPath(), p, id)
  177. tmpPath := path.Join(a.rootPath(), p, fmt.Sprintf("%s-removing", id))
  178. if err := os.Rename(realPath, tmpPath); err != nil && !os.IsNotExist(err) {
  179. return err
  180. }
  181. defer os.RemoveAll(tmpPath)
  182. }
  183. // Remove the layers file for the id
  184. if err := os.Remove(path.Join(a.rootPath(), "layers", id)); err != nil && !os.IsNotExist(err) {
  185. return err
  186. }
  187. return nil
  188. }
  189. // Return the rootfs path for the id
  190. // This will mount the dir at it's given path
  191. func (a *Driver) Get(id, mountLabel string) (string, error) {
  192. ids, err := getParentIds(a.rootPath(), id)
  193. if err != nil {
  194. if !os.IsNotExist(err) {
  195. return "", err
  196. }
  197. ids = []string{}
  198. }
  199. // Protect the a.active from concurrent access
  200. a.Lock()
  201. defer a.Unlock()
  202. count := a.active[id]
  203. // If a dir does not have a parent ( no layers )do not try to mount
  204. // just return the diff path to the data
  205. out := path.Join(a.rootPath(), "diff", id)
  206. if len(ids) > 0 {
  207. out = path.Join(a.rootPath(), "mnt", id)
  208. if count == 0 {
  209. if err := a.mount(id, mountLabel); err != nil {
  210. return "", err
  211. }
  212. }
  213. }
  214. a.active[id] = count + 1
  215. return out, nil
  216. }
  217. func (a *Driver) Put(id string) {
  218. // Protect the a.active from concurrent access
  219. a.Lock()
  220. defer a.Unlock()
  221. if count := a.active[id]; count > 1 {
  222. a.active[id] = count - 1
  223. } else {
  224. ids, _ := getParentIds(a.rootPath(), id)
  225. // We only mounted if there are any parents
  226. if ids != nil && len(ids) > 0 {
  227. a.unmount(id)
  228. }
  229. delete(a.active, id)
  230. }
  231. }
  232. // Returns an archive of the contents for the id
  233. func (a *Driver) Diff(id string) (archive.Archive, error) {
  234. return archive.TarFilter(path.Join(a.rootPath(), "diff", id), &archive.TarOptions{
  235. Compression: archive.Uncompressed,
  236. })
  237. }
  238. func (a *Driver) ApplyDiff(id string, diff archive.ArchiveReader) error {
  239. return archive.Untar(diff, path.Join(a.rootPath(), "diff", id), nil)
  240. }
  241. // Returns the size of the contents for the id
  242. func (a *Driver) DiffSize(id string) (int64, error) {
  243. return utils.TreeSize(path.Join(a.rootPath(), "diff", id))
  244. }
  245. func (a *Driver) Changes(id string) ([]archive.Change, error) {
  246. layers, err := a.getParentLayerPaths(id)
  247. if err != nil {
  248. return nil, err
  249. }
  250. return archive.Changes(layers, path.Join(a.rootPath(), "diff", id))
  251. }
  252. func (a *Driver) getParentLayerPaths(id string) ([]string, error) {
  253. parentIds, err := getParentIds(a.rootPath(), id)
  254. if err != nil {
  255. return nil, err
  256. }
  257. if len(parentIds) == 0 {
  258. return nil, fmt.Errorf("Dir %s does not have any parent layers", id)
  259. }
  260. layers := make([]string, len(parentIds))
  261. // Get the diff paths for all the parent ids
  262. for i, p := range parentIds {
  263. layers[i] = path.Join(a.rootPath(), "diff", p)
  264. }
  265. return layers, nil
  266. }
  267. func (a *Driver) mount(id, mountLabel string) error {
  268. // If the id is mounted or we get an error return
  269. if mounted, err := a.mounted(id); err != nil || mounted {
  270. return err
  271. }
  272. var (
  273. target = path.Join(a.rootPath(), "mnt", id)
  274. rw = path.Join(a.rootPath(), "diff", id)
  275. )
  276. layers, err := a.getParentLayerPaths(id)
  277. if err != nil {
  278. return err
  279. }
  280. if err := a.aufsMount(layers, rw, target, mountLabel); err != nil {
  281. return err
  282. }
  283. return nil
  284. }
  285. func (a *Driver) unmount(id string) error {
  286. if mounted, err := a.mounted(id); err != nil || !mounted {
  287. return err
  288. }
  289. target := path.Join(a.rootPath(), "mnt", id)
  290. return Unmount(target)
  291. }
  292. func (a *Driver) mounted(id string) (bool, error) {
  293. target := path.Join(a.rootPath(), "mnt", id)
  294. return mountpk.Mounted(target)
  295. }
  296. // During cleanup aufs needs to unmount all mountpoints
  297. func (a *Driver) Cleanup() error {
  298. ids, err := loadIds(path.Join(a.rootPath(), "layers"))
  299. if err != nil {
  300. return err
  301. }
  302. for _, id := range ids {
  303. if err := a.unmount(id); err != nil {
  304. utils.Errorf("Unmounting %s: %s", utils.TruncateID(id), err)
  305. }
  306. }
  307. return nil
  308. }
  309. func (a *Driver) aufsMount(ro []string, rw, target, mountLabel string) (err error) {
  310. defer func() {
  311. if err != nil {
  312. Unmount(target)
  313. }
  314. }()
  315. if err = a.tryMount(ro, rw, target, mountLabel); err != nil {
  316. if err = a.mountRw(rw, target, mountLabel); err != nil {
  317. return
  318. }
  319. for _, layer := range ro {
  320. data := label.FormatMountLabel(fmt.Sprintf("append:%s=ro+wh", layer), mountLabel)
  321. if err = mount("none", target, "aufs", MsRemount, data); err != nil {
  322. return
  323. }
  324. }
  325. }
  326. return
  327. }
  328. // Try to mount using the aufs fast path, if this fails then
  329. // append ro layers.
  330. func (a *Driver) tryMount(ro []string, rw, target, mountLabel string) (err error) {
  331. var (
  332. rwBranch = fmt.Sprintf("%s=rw", rw)
  333. roBranches = fmt.Sprintf("%s=ro+wh:", strings.Join(ro, "=ro+wh:"))
  334. data = label.FormatMountLabel(fmt.Sprintf("br:%v:%v,xino=/dev/shm/aufs.xino", rwBranch, roBranches), mountLabel)
  335. )
  336. return mount("none", target, "aufs", 0, data)
  337. }
  338. func (a *Driver) mountRw(rw, target, mountLabel string) error {
  339. data := label.FormatMountLabel(fmt.Sprintf("br:%s,xino=/dev/shm/aufs.xino", rw), mountLabel)
  340. return mount("none", target, "aufs", 0, data)
  341. }
  342. func rollbackMount(target string, err error) {
  343. if err != nil {
  344. Unmount(target)
  345. }
  346. }