Browse code

Handle image metadata when drivers are switched

Michael Crosby authored on 2013/11/19 19:32:08
Showing 6 changed files
... ...
@@ -52,7 +52,9 @@ func (graph *Graph) restore() error {
52 52
 	}
53 53
 	for _, v := range dir {
54 54
 		id := v.Name()
55
-		graph.idIndex.Add(id)
55
+		if graph.driver.Exists(id) {
56
+			graph.idIndex.Add(id)
57
+		}
56 58
 	}
57 59
 	return nil
58 60
 }
... ...
@@ -137,6 +139,14 @@ func (graph *Graph) Register(jsonData []byte, layerData archive.Archive, img *Im
137 137
 	if graph.Exists(img.ID) {
138 138
 		return fmt.Errorf("Image %s already exists", img.ID)
139 139
 	}
140
+
141
+	// Ensure that the image root does not exist on the filesystem
142
+	// when it is not registered in the graph.
143
+	// This is common when you switch from one graph driver to another
144
+	if err := os.RemoveAll(graph.imageRoot(img.ID)); err != nil && !os.IsNotExist(err) {
145
+		return err
146
+	}
147
+
140 148
 	tmp, err := graph.Mktemp("")
141 149
 	defer os.RemoveAll(tmp)
142 150
 	if err != nil {
... ...
@@ -38,6 +38,9 @@ func pathExists(pth string) bool {
38 38
 // symlink.
39 39
 func (a *Driver) Migrate(pth string, setupInit func(p string) error) error {
40 40
 	if pathExists(path.Join(pth, "graph")) {
41
+		if err := a.migrateRepositories(pth); err != nil {
42
+			return err
43
+		}
41 44
 		if err := a.migrateImages(path.Join(pth, "graph")); err != nil {
42 45
 			return err
43 46
 		}
... ...
@@ -46,6 +49,14 @@ func (a *Driver) Migrate(pth string, setupInit func(p string) error) error {
46 46
 	return nil
47 47
 }
48 48
 
49
+func (a *Driver) migrateRepositories(pth string) error {
50
+	name := path.Join(pth, "repositories")
51
+	if err := os.Rename(name, name+"-aufs"); err != nil && !os.IsNotExist(err) {
52
+		return err
53
+	}
54
+	return nil
55
+}
56
+
49 57
 func (a *Driver) migrateContainers(pth string, setupInit func(p string) error) error {
50 58
 	fis, err := ioutil.ReadDir(pth)
51 59
 	if err != nil {
... ...
@@ -121,3 +121,7 @@ func (d *Driver) unmount(id, mountPoint string) error {
121 121
 	// Unmount the device
122 122
 	return d.DeviceSet.UnmountDevice(id, mountPoint, true)
123 123
 }
124
+
125
+func (d *Driver) Exists(id string) bool {
126
+	return d.Devices[id] != nil
127
+}
... ...
@@ -19,6 +19,7 @@ type Driver interface {
19 19
 	Remove(id string) error
20 20
 
21 21
 	Get(id string) (dir string, err error)
22
+	Exists(id string) bool
22 23
 
23 24
 	Status() [][2]string
24 25
 
... ...
@@ -84,3 +84,8 @@ func (d *Driver) Get(id string) (string, error) {
84 84
 	}
85 85
 	return dir, nil
86 86
 }
87
+
88
+func (d *Driver) Exists(id string) bool {
89
+	_, err := os.Stat(d.dir(id))
90
+	return err == nil
91
+}
... ...
@@ -671,7 +671,7 @@ func NewRuntimeFromDirectory(config *DaemonConfig) (*Runtime, error) {
671 671
 	if err != nil {
672 672
 		return nil, err
673 673
 	}
674
-	repositories, err := NewTagStore(path.Join(config.Root, "repositories"), g)
674
+	repositories, err := NewTagStore(path.Join(config.Root, "repositories-"+driver.String()), g)
675 675
 	if err != nil {
676 676
 		return nil, fmt.Errorf("Couldn't create Tag store: %s", err)
677 677
 	}