Просмотр исходного кода

Handle image metadata when drivers are switched

Michael Crosby 11 лет назад
Родитель
Сommit
1b28cdc7f9
6 измененных файлов с 33 добавлено и 2 удалено
  1. 11 1
      graph.go
  2. 11 0
      graphdriver/aufs/migrate.go
  3. 4 0
      graphdriver/devmapper/driver.go
  4. 1 0
      graphdriver/driver.go
  5. 5 0
      graphdriver/dummy/driver.go
  6. 1 1
      runtime.go

+ 11 - 1
graph.go

@@ -52,7 +52,9 @@ func (graph *Graph) restore() error {
 	}
 	for _, v := range dir {
 		id := v.Name()
-		graph.idIndex.Add(id)
+		if graph.driver.Exists(id) {
+			graph.idIndex.Add(id)
+		}
 	}
 	return nil
 }
@@ -137,6 +139,14 @@ func (graph *Graph) Register(jsonData []byte, layerData archive.Archive, img *Im
 	if graph.Exists(img.ID) {
 		return fmt.Errorf("Image %s already exists", img.ID)
 	}
+
+	// Ensure that the image root does not exist on the filesystem
+	// when it is not registered in the graph.
+	// This is common when you switch from one graph driver to another
+	if err := os.RemoveAll(graph.imageRoot(img.ID)); err != nil && !os.IsNotExist(err) {
+		return err
+	}
+
 	tmp, err := graph.Mktemp("")
 	defer os.RemoveAll(tmp)
 	if err != nil {

+ 11 - 0
graphdriver/aufs/migrate.go

@@ -38,6 +38,9 @@ func pathExists(pth string) bool {
 // symlink.
 func (a *Driver) Migrate(pth string, setupInit func(p string) error) error {
 	if pathExists(path.Join(pth, "graph")) {
+		if err := a.migrateRepositories(pth); err != nil {
+			return err
+		}
 		if err := a.migrateImages(path.Join(pth, "graph")); err != nil {
 			return err
 		}
@@ -46,6 +49,14 @@ func (a *Driver) Migrate(pth string, setupInit func(p string) error) error {
 	return nil
 }
 
+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
+}
+
 func (a *Driver) migrateContainers(pth string, setupInit func(p string) error) error {
 	fis, err := ioutil.ReadDir(pth)
 	if err != nil {

+ 4 - 0
graphdriver/devmapper/driver.go

@@ -121,3 +121,7 @@ func (d *Driver) unmount(id, mountPoint string) error {
 	// Unmount the device
 	return d.DeviceSet.UnmountDevice(id, mountPoint, true)
 }
+
+func (d *Driver) Exists(id string) bool {
+	return d.Devices[id] != nil
+}

+ 1 - 0
graphdriver/driver.go

@@ -19,6 +19,7 @@ type Driver interface {
 	Remove(id string) error
 
 	Get(id string) (dir string, err error)
+	Exists(id string) bool
 
 	Status() [][2]string
 

+ 5 - 0
graphdriver/dummy/driver.go

@@ -84,3 +84,8 @@ func (d *Driver) Get(id string) (string, error) {
 	}
 	return dir, nil
 }
+
+func (d *Driver) Exists(id string) bool {
+	_, err := os.Stat(d.dir(id))
+	return err == nil
+}

+ 1 - 1
runtime.go

@@ -671,7 +671,7 @@ func NewRuntimeFromDirectory(config *DaemonConfig) (*Runtime, error) {
 	if err != nil {
 		return nil, err
 	}
-	repositories, err := NewTagStore(path.Join(config.Root, "repositories"), g)
+	repositories, err := NewTagStore(path.Join(config.Root, "repositories-"+driver.String()), g)
 	if err != nil {
 		return nil, fmt.Errorf("Couldn't create Tag store: %s", err)
 	}