2013-11-15 20:16:30 -05:00
|
|
|
package aufs
|
|
|
|
|
|
|
|
import (
|
2013-11-18 19:17:43 -05:00
|
|
|
"encoding/json"
|
2013-11-18 16:16:28 -05:00
|
|
|
"fmt"
|
2013-11-15 20:16:30 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
2013-11-18 20:20:03 -05:00
|
|
|
type metadata struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
ParentID string `json:"parent,omitempty"`
|
|
|
|
Image string `json:"Image,omitempty"`
|
2013-11-18 19:17:43 -05:00
|
|
|
|
2013-11-18 20:20:03 -05:00
|
|
|
parent *metadata
|
2013-11-18 19:17:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func pathExists(pth string) bool {
|
2013-11-15 20:16:30 -05:00
|
|
|
if _, err := os.Stat(pth); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-11-18 19:17:43 -05:00
|
|
|
// Migrate existing images and containers from docker < 0.7.x
|
2013-11-18 20:20:03 -05:00
|
|
|
//
|
|
|
|
// The format pre 0.7 is for docker to store the metadata and filesystem
|
|
|
|
// content in the same directory. For the migration to work we need to move Image layer
|
|
|
|
// data from /var/lib/docker/graph/<id>/layers to the diff of the registered id.
|
|
|
|
//
|
|
|
|
// Next we need to migrate the container's rw layer to diff of the driver. After the
|
|
|
|
// contents are migrated we need to register the image and container ids with the
|
|
|
|
// driver.
|
|
|
|
//
|
|
|
|
// For the migration we try to move the folder containing the layer files, if that
|
|
|
|
// fails because the data is currently mounted we will fallback to creating a
|
|
|
|
// symlink.
|
2013-11-19 06:27:59 -05:00
|
|
|
func (a *Driver) Migrate(pth string, setupInit func(p string) error) error {
|
2013-11-18 20:20:03 -05:00
|
|
|
if pathExists(path.Join(pth, "graph")) {
|
2013-11-19 05:32:08 -05:00
|
|
|
if err := a.migrateRepositories(pth); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-18 20:20:03 -05:00
|
|
|
if err := a.migrateImages(path.Join(pth, "graph")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return a.migrateContainers(path.Join(pth, "containers"), setupInit)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-19 05:32:08 -05:00
|
|
|
func (a *Driver) migrateRepositories(pth string) error {
|
|
|
|
name := path.Join(pth, "repositories")
|
|
|
|
if err := os.Rename(name, name+"-aufs"); err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-19 06:27:59 -05:00
|
|
|
func (a *Driver) migrateContainers(pth string, setupInit func(p string) error) error {
|
2013-11-18 20:20:03 -05:00
|
|
|
fis, err := ioutil.ReadDir(pth)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fi := range fis {
|
2013-11-19 02:28:45 -05:00
|
|
|
if id := fi.Name(); fi.IsDir() && pathExists(path.Join(pth, id, "rw")) {
|
2013-11-18 20:20:03 -05:00
|
|
|
if err := tryRelocate(path.Join(pth, id, "rw"), path.Join(a.rootPath(), "diff", id)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-11-19 02:28:45 -05:00
|
|
|
if !a.Exists(id) {
|
2013-11-18 20:20:03 -05:00
|
|
|
|
2013-11-19 02:28:45 -05:00
|
|
|
metadata, err := loadMetadata(path.Join(pth, id, "config.json"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-18 20:20:03 -05:00
|
|
|
|
2013-11-19 02:28:45 -05:00
|
|
|
initID := fmt.Sprintf("%s-init", id)
|
2014-04-17 19:47:27 -04:00
|
|
|
if err := a.Create(initID, metadata.Image); err != nil {
|
2013-11-19 02:28:45 -05:00
|
|
|
return err
|
|
|
|
}
|
2013-11-18 20:20:03 -05:00
|
|
|
|
2014-04-17 19:47:27 -04:00
|
|
|
initPath, err := a.Get(initID, "")
|
2013-11-19 02:28:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// setup init layer
|
|
|
|
if err := setupInit(initPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-17 19:47:27 -04:00
|
|
|
if err := a.Create(id, initID); err != nil {
|
2013-11-19 02:28:45 -05:00
|
|
|
return err
|
|
|
|
}
|
2013-11-18 20:20:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-19 06:27:59 -05:00
|
|
|
func (a *Driver) migrateImages(pth string) error {
|
2013-11-15 20:16:30 -05:00
|
|
|
fis, err := ioutil.ReadDir(pth)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-18 19:17:43 -05:00
|
|
|
var (
|
2013-11-18 20:20:03 -05:00
|
|
|
m = make(map[string]*metadata)
|
|
|
|
current *metadata
|
|
|
|
exists bool
|
2013-11-18 19:17:43 -05:00
|
|
|
)
|
|
|
|
|
2013-11-15 20:16:30 -05:00
|
|
|
for _, fi := range fis {
|
2013-11-19 02:28:45 -05:00
|
|
|
if id := fi.Name(); fi.IsDir() && pathExists(path.Join(pth, id, "layer")) {
|
2013-11-18 20:20:03 -05:00
|
|
|
if current, exists = m[id]; !exists {
|
|
|
|
current, err = loadMetadata(path.Join(pth, id, "json"))
|
2013-11-18 19:17:43 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-18 20:20:03 -05:00
|
|
|
m[id] = current
|
2013-11-15 20:16:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-18 19:17:43 -05:00
|
|
|
|
2013-11-18 20:20:03 -05:00
|
|
|
for _, v := range m {
|
|
|
|
v.parent = m[v.ParentID]
|
2013-11-18 19:17:43 -05:00
|
|
|
}
|
|
|
|
|
2013-11-19 02:28:45 -05:00
|
|
|
migrated := make(map[string]bool)
|
2013-11-18 20:20:03 -05:00
|
|
|
for _, v := range m {
|
2013-11-19 02:28:45 -05:00
|
|
|
if err := a.migrateImage(v, pth, migrated); err != nil {
|
2013-11-18 19:17:43 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-19 06:27:59 -05:00
|
|
|
func (a *Driver) migrateImage(m *metadata, pth string, migrated map[string]bool) error {
|
2013-11-19 02:28:45 -05:00
|
|
|
if !migrated[m.ID] {
|
2013-11-18 19:17:43 -05:00
|
|
|
if m.parent != nil {
|
2013-11-19 02:28:45 -05:00
|
|
|
a.migrateImage(m.parent, pth, migrated)
|
2013-11-18 19:17:43 -05:00
|
|
|
}
|
|
|
|
if err := tryRelocate(path.Join(pth, m.ID, "layer"), path.Join(a.rootPath(), "diff", m.ID)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-19 02:28:45 -05:00
|
|
|
if !a.Exists(m.ID) {
|
2014-04-17 19:47:27 -04:00
|
|
|
if err := a.Create(m.ID, m.ParentID); err != nil {
|
2013-11-19 02:28:45 -05:00
|
|
|
return err
|
|
|
|
}
|
2013-11-18 19:17:43 -05:00
|
|
|
}
|
2013-11-19 02:28:45 -05:00
|
|
|
migrated[m.ID] = true
|
2013-11-18 19:17:43 -05:00
|
|
|
}
|
2013-11-15 20:16:30 -05:00
|
|
|
return nil
|
|
|
|
}
|
2013-11-18 16:16:28 -05:00
|
|
|
|
|
|
|
// tryRelocate will try to rename the old path to the new pack and if
|
|
|
|
// the operation fails, it will fallback to a symlink
|
|
|
|
func tryRelocate(oldPath, newPath string) error {
|
2013-11-19 02:28:45 -05:00
|
|
|
s, err := os.Lstat(newPath)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// If the destination is a symlink then we already tried to relocate once before
|
|
|
|
// and it failed so we delete it and try to remove
|
|
|
|
if s != nil && s.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
if err := os.RemoveAll(newPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-11-18 16:16:28 -05:00
|
|
|
if err := os.Rename(oldPath, newPath); err != nil {
|
|
|
|
if sErr := os.Symlink(oldPath, newPath); sErr != nil {
|
|
|
|
return fmt.Errorf("Unable to relocate %s to %s: Rename err %s Symlink err %s", oldPath, newPath, err, sErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-11-18 19:17:43 -05:00
|
|
|
|
2013-11-18 20:20:03 -05:00
|
|
|
func loadMetadata(pth string) (*metadata, error) {
|
|
|
|
f, err := os.Open(pth)
|
2013-11-18 19:17:43 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var (
|
2013-11-18 20:20:03 -05:00
|
|
|
out = &metadata{}
|
2013-11-18 19:17:43 -05:00
|
|
|
dec = json.NewDecoder(f)
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := dec.Decode(out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|