migrate.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package aufs
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path"
  8. )
  9. type metadata struct {
  10. ID string `json:"id"`
  11. ParentID string `json:"parent,omitempty"`
  12. Image string `json:"Image,omitempty"`
  13. parent *metadata
  14. }
  15. func pathExists(pth string) bool {
  16. if _, err := os.Stat(pth); err != nil {
  17. return false
  18. }
  19. return true
  20. }
  21. // Migrate existing images and containers from docker < 0.7.x
  22. //
  23. // The format pre 0.7 is for docker to store the metadata and filesystem
  24. // content in the same directory. For the migration to work we need to move Image layer
  25. // data from /var/lib/docker/graph/<id>/layers to the diff of the registered id.
  26. //
  27. // Next we need to migrate the container's rw layer to diff of the driver. After the
  28. // contents are migrated we need to register the image and container ids with the
  29. // driver.
  30. //
  31. // For the migration we try to move the folder containing the layer files, if that
  32. // fails because the data is currently mounted we will fallback to creating a
  33. // symlink.
  34. func (a *Driver) Migrate(pth string, setupInit func(p string) error) error {
  35. if pathExists(path.Join(pth, "graph")) {
  36. if err := a.migrateRepositories(pth); err != nil {
  37. return err
  38. }
  39. if err := a.migrateImages(path.Join(pth, "graph")); err != nil {
  40. return err
  41. }
  42. return a.migrateContainers(path.Join(pth, "containers"), setupInit)
  43. }
  44. return nil
  45. }
  46. func (a *Driver) migrateRepositories(pth string) error {
  47. name := path.Join(pth, "repositories")
  48. if err := os.Rename(name, name+"-aufs"); err != nil && !os.IsNotExist(err) {
  49. return err
  50. }
  51. return nil
  52. }
  53. func (a *Driver) migrateContainers(pth string, setupInit func(p string) error) error {
  54. fis, err := ioutil.ReadDir(pth)
  55. if err != nil {
  56. return err
  57. }
  58. for _, fi := range fis {
  59. if id := fi.Name(); fi.IsDir() && pathExists(path.Join(pth, id, "rw")) {
  60. if err := tryRelocate(path.Join(pth, id, "rw"), path.Join(a.rootPath(), "diff", id)); err != nil {
  61. return err
  62. }
  63. if !a.Exists(id) {
  64. metadata, err := loadMetadata(path.Join(pth, id, "config.json"))
  65. if err != nil {
  66. return err
  67. }
  68. initID := fmt.Sprintf("%s-init", id)
  69. if err := a.Create(initID, metadata.Image); err != nil {
  70. return err
  71. }
  72. initPath, err := a.Get(initID, "")
  73. if err != nil {
  74. return err
  75. }
  76. // setup init layer
  77. if err := setupInit(initPath); err != nil {
  78. return err
  79. }
  80. if err := a.Create(id, initID); err != nil {
  81. return err
  82. }
  83. }
  84. }
  85. }
  86. return nil
  87. }
  88. func (a *Driver) migrateImages(pth string) error {
  89. fis, err := ioutil.ReadDir(pth)
  90. if err != nil {
  91. return err
  92. }
  93. var (
  94. m = make(map[string]*metadata)
  95. current *metadata
  96. exists bool
  97. )
  98. for _, fi := range fis {
  99. if id := fi.Name(); fi.IsDir() && pathExists(path.Join(pth, id, "layer")) {
  100. if current, exists = m[id]; !exists {
  101. current, err = loadMetadata(path.Join(pth, id, "json"))
  102. if err != nil {
  103. return err
  104. }
  105. m[id] = current
  106. }
  107. }
  108. }
  109. for _, v := range m {
  110. v.parent = m[v.ParentID]
  111. }
  112. migrated := make(map[string]bool)
  113. for _, v := range m {
  114. if err := a.migrateImage(v, pth, migrated); err != nil {
  115. return err
  116. }
  117. }
  118. return nil
  119. }
  120. func (a *Driver) migrateImage(m *metadata, pth string, migrated map[string]bool) error {
  121. if !migrated[m.ID] {
  122. if m.parent != nil {
  123. a.migrateImage(m.parent, pth, migrated)
  124. }
  125. if err := tryRelocate(path.Join(pth, m.ID, "layer"), path.Join(a.rootPath(), "diff", m.ID)); err != nil {
  126. return err
  127. }
  128. if !a.Exists(m.ID) {
  129. if err := a.Create(m.ID, m.ParentID); err != nil {
  130. return err
  131. }
  132. }
  133. migrated[m.ID] = true
  134. }
  135. return nil
  136. }
  137. // tryRelocate will try to rename the old path to the new pack and if
  138. // the operation fails, it will fallback to a symlink
  139. func tryRelocate(oldPath, newPath string) error {
  140. s, err := os.Lstat(newPath)
  141. if err != nil && !os.IsNotExist(err) {
  142. return err
  143. }
  144. // If the destination is a symlink then we already tried to relocate once before
  145. // and it failed so we delete it and try to remove
  146. if s != nil && s.Mode()&os.ModeSymlink == os.ModeSymlink {
  147. if err := os.RemoveAll(newPath); err != nil {
  148. return err
  149. }
  150. }
  151. if err := os.Rename(oldPath, newPath); err != nil {
  152. if sErr := os.Symlink(oldPath, newPath); sErr != nil {
  153. return fmt.Errorf("Unable to relocate %s to %s: Rename err %s Symlink err %s", oldPath, newPath, err, sErr)
  154. }
  155. }
  156. return nil
  157. }
  158. func loadMetadata(pth string) (*metadata, error) {
  159. f, err := os.Open(pth)
  160. if err != nil {
  161. return nil, err
  162. }
  163. defer f.Close()
  164. var (
  165. out = &metadata{}
  166. dec = json.NewDecoder(f)
  167. )
  168. if err := dec.Decode(out); err != nil {
  169. return nil, err
  170. }
  171. return out, nil
  172. }