aufs.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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/graphdriver"
  23. mountpk "github.com/dotcloud/docker/mount"
  24. "github.com/dotcloud/docker/utils"
  25. "os"
  26. "os/exec"
  27. "path"
  28. "strings"
  29. )
  30. func init() {
  31. graphdriver.Register("aufs", Init)
  32. }
  33. type Driver struct {
  34. root string
  35. }
  36. // New returns a new AUFS driver.
  37. // An error is returned if AUFS is not supported.
  38. func Init(root string) (graphdriver.Driver, error) {
  39. // Try to load the aufs kernel module
  40. if err := supportsAufs(); err != nil {
  41. return nil, err
  42. }
  43. paths := []string{
  44. "mnt",
  45. "diff",
  46. "layers",
  47. }
  48. // Create the root aufs driver dir and return
  49. // if it already exists
  50. // If not populate the dir structure
  51. if err := os.MkdirAll(root, 0755); err != nil {
  52. if os.IsExist(err) {
  53. return &Driver{root}, nil
  54. }
  55. return nil, err
  56. }
  57. for _, p := range paths {
  58. if err := os.MkdirAll(path.Join(root, p), 0755); err != nil {
  59. return nil, err
  60. }
  61. }
  62. return &Driver{root}, nil
  63. }
  64. // Return a nil error if the kernel supports aufs
  65. // We cannot modprobe because inside dind modprobe fails
  66. // to run
  67. func supportsAufs() error {
  68. // We can try to modprobe aufs first before looking at
  69. // proc/filesystems for when aufs is supported
  70. exec.Command("modprobe", "aufs").Run()
  71. f, err := os.Open("/proc/filesystems")
  72. if err != nil {
  73. return err
  74. }
  75. defer f.Close()
  76. s := bufio.NewScanner(f)
  77. for s.Scan() {
  78. if strings.Contains(s.Text(), "aufs") {
  79. return nil
  80. }
  81. }
  82. return fmt.Errorf("AUFS was not found in /proc/filesystems")
  83. }
  84. func (a Driver) rootPath() string {
  85. return a.root
  86. }
  87. func (Driver) String() string {
  88. return "aufs"
  89. }
  90. func (a Driver) Status() [][2]string {
  91. ids, _ := loadIds(path.Join(a.rootPath(), "layers"))
  92. return [][2]string{
  93. {"Root Dir", a.rootPath()},
  94. {"Dirs", fmt.Sprintf("%d", len(ids))},
  95. }
  96. }
  97. // Exists returns true if the given id is registered with
  98. // this driver
  99. func (a Driver) Exists(id string) bool {
  100. if _, err := os.Lstat(path.Join(a.rootPath(), "layers", id)); err != nil {
  101. return false
  102. }
  103. return true
  104. }
  105. // Three folders are created for each id
  106. // mnt, layers, and diff
  107. func (a *Driver) Create(id, parent string) error {
  108. if err := a.createDirsFor(id); err != nil {
  109. return err
  110. }
  111. // Write the layers metadata
  112. f, err := os.Create(path.Join(a.rootPath(), "layers", id))
  113. if err != nil {
  114. return err
  115. }
  116. defer f.Close()
  117. if parent != "" {
  118. ids, err := getParentIds(a.rootPath(), parent)
  119. if err != nil {
  120. return err
  121. }
  122. if _, err := fmt.Fprintln(f, parent); err != nil {
  123. return err
  124. }
  125. for _, i := range ids {
  126. if _, err := fmt.Fprintln(f, i); err != nil {
  127. return err
  128. }
  129. }
  130. }
  131. return nil
  132. }
  133. func (a *Driver) createDirsFor(id string) error {
  134. paths := []string{
  135. "mnt",
  136. "diff",
  137. }
  138. for _, p := range paths {
  139. if err := os.MkdirAll(path.Join(a.rootPath(), p, id), 0755); err != nil {
  140. return err
  141. }
  142. }
  143. return nil
  144. }
  145. // Unmount and remove the dir information
  146. func (a *Driver) Remove(id string) error {
  147. // Make sure the dir is umounted first
  148. if err := a.unmount(id); err != nil {
  149. return err
  150. }
  151. tmpDirs := []string{
  152. "mnt",
  153. "diff",
  154. }
  155. // Remove the dirs atomically
  156. for _, p := range tmpDirs {
  157. // We need to use a temp dir in the same dir as the driver so Rename
  158. // does not fall back to the slow copy if /tmp and the driver dir
  159. // are on different devices
  160. tmp := path.Join(a.rootPath(), "tmp", p, id)
  161. if err := os.MkdirAll(tmp, 0755); err != nil {
  162. return err
  163. }
  164. realPath := path.Join(a.rootPath(), p, id)
  165. if err := os.Rename(realPath, tmp); err != nil && !os.IsNotExist(err) {
  166. return err
  167. }
  168. defer os.RemoveAll(tmp)
  169. }
  170. // Remove the layers file for the id
  171. if err := os.Remove(path.Join(a.rootPath(), "layers", id)); err != nil && !os.IsNotExist(err) {
  172. return err
  173. }
  174. return nil
  175. }
  176. // Return the rootfs path for the id
  177. // This will mount the dir at it's given path
  178. func (a *Driver) Get(id string) (string, error) {
  179. ids, err := getParentIds(a.rootPath(), id)
  180. if err != nil {
  181. if !os.IsNotExist(err) {
  182. return "", err
  183. }
  184. ids = []string{}
  185. }
  186. // If a dir does not have a parent ( no layers )do not try to mount
  187. // just return the diff path to the data
  188. out := path.Join(a.rootPath(), "diff", id)
  189. if len(ids) > 0 {
  190. out = path.Join(a.rootPath(), "mnt", id)
  191. if err := a.mount(id); err != nil {
  192. return "", err
  193. }
  194. }
  195. return out, nil
  196. }
  197. // Returns an archive of the contents for the id
  198. func (a *Driver) Diff(id string) (archive.Archive, error) {
  199. return archive.TarFilter(path.Join(a.rootPath(), "diff", id), &archive.TarOptions{
  200. Recursive: true,
  201. Compression: archive.Uncompressed,
  202. })
  203. }
  204. func (a *Driver) ApplyDiff(id string, diff archive.Archive) error {
  205. return archive.Untar(diff, path.Join(a.rootPath(), "diff", id), nil)
  206. }
  207. // Returns the size of the contents for the id
  208. func (a *Driver) DiffSize(id string) (int64, error) {
  209. return utils.TreeSize(path.Join(a.rootPath(), "diff", id))
  210. }
  211. func (a *Driver) Changes(id string) ([]archive.Change, error) {
  212. layers, err := a.getParentLayerPaths(id)
  213. if err != nil {
  214. return nil, err
  215. }
  216. return archive.Changes(layers, path.Join(a.rootPath(), "diff", id))
  217. }
  218. func (a *Driver) getParentLayerPaths(id string) ([]string, error) {
  219. parentIds, err := getParentIds(a.rootPath(), id)
  220. if err != nil {
  221. return nil, err
  222. }
  223. if len(parentIds) == 0 {
  224. return nil, fmt.Errorf("Dir %s does not have any parent layers", id)
  225. }
  226. layers := make([]string, len(parentIds))
  227. // Get the diff paths for all the parent ids
  228. for i, p := range parentIds {
  229. layers[i] = path.Join(a.rootPath(), "diff", p)
  230. }
  231. return layers, nil
  232. }
  233. func (a *Driver) mount(id string) error {
  234. // If the id is mounted or we get an error return
  235. if mounted, err := a.mounted(id); err != nil || mounted {
  236. return err
  237. }
  238. var (
  239. target = path.Join(a.rootPath(), "mnt", id)
  240. rw = path.Join(a.rootPath(), "diff", id)
  241. )
  242. layers, err := a.getParentLayerPaths(id)
  243. if err != nil {
  244. return err
  245. }
  246. if err := a.aufsMount(layers, rw, target); err != nil {
  247. return err
  248. }
  249. return nil
  250. }
  251. func (a *Driver) unmount(id string) error {
  252. if mounted, err := a.mounted(id); err != nil || !mounted {
  253. return err
  254. }
  255. target := path.Join(a.rootPath(), "mnt", id)
  256. return Unmount(target)
  257. }
  258. func (a *Driver) mounted(id string) (bool, error) {
  259. target := path.Join(a.rootPath(), "mnt", id)
  260. return mountpk.Mounted(target)
  261. }
  262. // During cleanup aufs needs to unmount all mountpoints
  263. func (a *Driver) Cleanup() error {
  264. ids, err := loadIds(path.Join(a.rootPath(), "layers"))
  265. if err != nil {
  266. return err
  267. }
  268. for _, id := range ids {
  269. if err := a.unmount(id); err != nil {
  270. utils.Errorf("Unmounting %s: %s", utils.TruncateID(id), err)
  271. }
  272. }
  273. return nil
  274. }
  275. func (a *Driver) aufsMount(ro []string, rw, target string) (err error) {
  276. defer func() {
  277. if err != nil {
  278. Unmount(target)
  279. }
  280. }()
  281. if err = a.tryMount(ro, rw, target); err != nil {
  282. if err = a.mountRw(rw, target); err != nil {
  283. return
  284. }
  285. for _, layer := range ro {
  286. branch := fmt.Sprintf("append:%s=ro+wh", layer)
  287. if err = mount("none", target, "aufs", MsRemount, branch); err != nil {
  288. return
  289. }
  290. }
  291. }
  292. return
  293. }
  294. // Try to mount using the aufs fast path, if this fails then
  295. // append ro layers.
  296. func (a *Driver) tryMount(ro []string, rw, target string) (err error) {
  297. var (
  298. rwBranch = fmt.Sprintf("%s=rw", rw)
  299. roBranches = fmt.Sprintf("%s=ro+wh:", strings.Join(ro, "=ro+wh:"))
  300. )
  301. return mount("none", target, "aufs", 0, fmt.Sprintf("br:%v:%v,xino=/dev/shm/aufs.xino", rwBranch, roBranches))
  302. }
  303. func (a *Driver) mountRw(rw, target string) error {
  304. return mount("none", target, "aufs", 0, fmt.Sprintf("br:%s,xino=/dev/shm/aufs.xino", rw))
  305. }
  306. func rollbackMount(target string, err error) {
  307. if err != nil {
  308. Unmount(target)
  309. }
  310. }