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