migrate.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.migrateImages(path.Join(pth, "graph")); err != nil {
  37. return err
  38. }
  39. return a.migrateContainers(path.Join(pth, "containers"), setupInit)
  40. }
  41. return nil
  42. }
  43. func (a *Driver) migrateContainers(pth string, setupInit func(p string) error) error {
  44. fis, err := ioutil.ReadDir(pth)
  45. if err != nil {
  46. return err
  47. }
  48. for _, fi := range fis {
  49. if id := fi.Name(); fi.IsDir() && pathExists(path.Join(pth, id, "rw")) {
  50. if err := tryRelocate(path.Join(pth, id, "rw"), path.Join(a.rootPath(), "diff", id)); err != nil {
  51. return err
  52. }
  53. if !a.Exists(id) {
  54. metadata, err := loadMetadata(path.Join(pth, id, "config.json"))
  55. if err != nil {
  56. return err
  57. }
  58. initID := fmt.Sprintf("%s-init", id)
  59. if err := a.Create(initID, metadata.Image); err != nil {
  60. return err
  61. }
  62. initPath, err := a.Get(initID)
  63. if err != nil {
  64. return err
  65. }
  66. // setup init layer
  67. if err := setupInit(initPath); err != nil {
  68. return err
  69. }
  70. if err := a.Create(id, initID); err != nil {
  71. return err
  72. }
  73. }
  74. }
  75. }
  76. return nil
  77. }
  78. func (a *Driver) migrateImages(pth string) error {
  79. fis, err := ioutil.ReadDir(pth)
  80. if err != nil {
  81. return err
  82. }
  83. var (
  84. m = make(map[string]*metadata)
  85. current *metadata
  86. exists bool
  87. )
  88. for _, fi := range fis {
  89. if id := fi.Name(); fi.IsDir() && pathExists(path.Join(pth, id, "layer")) {
  90. if current, exists = m[id]; !exists {
  91. current, err = loadMetadata(path.Join(pth, id, "json"))
  92. if err != nil {
  93. return err
  94. }
  95. m[id] = current
  96. }
  97. }
  98. }
  99. for _, v := range m {
  100. v.parent = m[v.ParentID]
  101. }
  102. migrated := make(map[string]bool)
  103. for _, v := range m {
  104. if err := a.migrateImage(v, pth, migrated); err != nil {
  105. return err
  106. }
  107. }
  108. return nil
  109. }
  110. func (a *Driver) migrateImage(m *metadata, pth string, migrated map[string]bool) error {
  111. if !migrated[m.ID] {
  112. if m.parent != nil {
  113. a.migrateImage(m.parent, pth, migrated)
  114. }
  115. if err := tryRelocate(path.Join(pth, m.ID, "layer"), path.Join(a.rootPath(), "diff", m.ID)); err != nil {
  116. return err
  117. }
  118. if !a.Exists(m.ID) {
  119. if err := a.Create(m.ID, m.ParentID); err != nil {
  120. return err
  121. }
  122. }
  123. migrated[m.ID] = true
  124. }
  125. return nil
  126. }
  127. // tryRelocate will try to rename the old path to the new pack and if
  128. // the operation fails, it will fallback to a symlink
  129. func tryRelocate(oldPath, newPath string) error {
  130. s, err := os.Lstat(newPath)
  131. if err != nil && !os.IsNotExist(err) {
  132. return err
  133. }
  134. // If the destination is a symlink then we already tried to relocate once before
  135. // and it failed so we delete it and try to remove
  136. if s != nil && s.Mode()&os.ModeSymlink == os.ModeSymlink {
  137. if err := os.RemoveAll(newPath); err != nil {
  138. return err
  139. }
  140. }
  141. if err := os.Rename(oldPath, newPath); err != nil {
  142. if sErr := os.Symlink(oldPath, newPath); sErr != nil {
  143. return fmt.Errorf("Unable to relocate %s to %s: Rename err %s Symlink err %s", oldPath, newPath, err, sErr)
  144. }
  145. }
  146. return nil
  147. }
  148. func loadMetadata(pth string) (*metadata, error) {
  149. f, err := os.Open(pth)
  150. if err != nil {
  151. return nil, err
  152. }
  153. defer f.Close()
  154. var (
  155. out = &metadata{}
  156. dec = json.NewDecoder(f)
  157. )
  158. if err := dec.Decode(out); err != nil {
  159. return nil, err
  160. }
  161. return out, nil
  162. }