Browse code

Merge pull request #44 from crosbymichael/container-migration

Container and Image migration for aufs

Victor Vieux authored on 2013/11/20 06:47:23
Showing 3 changed files
... ...
@@ -95,18 +95,27 @@ func supportsAufs() error {
95 95
 	return fmt.Errorf("AUFS was not found in /proc/filesystems")
96 96
 }
97 97
 
98
-func (a *AufsDriver) rootPath() string {
98
+func (a AufsDriver) rootPath() string {
99 99
 	return a.root
100 100
 }
101 101
 
102
-func (a *AufsDriver) String() string {
102
+func (AufsDriver) String() string {
103 103
 	return "aufs"
104 104
 }
105 105
 
106
-func (d *AufsDriver) Status() [][2]string {
106
+func (AufsDriver) Status() [][2]string {
107 107
 	return nil
108 108
 }
109 109
 
110
+// Exists returns true if the given id is registered with
111
+// this driver
112
+func (a AufsDriver) Exists(id string) bool {
113
+	if _, err := os.Lstat(path.Join(a.rootPath(), "layers", id)); err != nil {
114
+		return false
115
+	}
116
+	return true
117
+}
118
+
110 119
 // Three folders are created for each id
111 120
 // mnt, layers, and diff
112 121
 func (a *AufsDriver) Create(id, parent string) error {
113 122
new file mode 100644
... ...
@@ -0,0 +1,183 @@
0
+package aufs
1
+
2
+import (
3
+	"encoding/json"
4
+	"fmt"
5
+	"io/ioutil"
6
+	"os"
7
+	"path"
8
+)
9
+
10
+type metadata struct {
11
+	ID       string `json:"id"`
12
+	ParentID string `json:"parent,omitempty"`
13
+	Image    string `json:"Image,omitempty"`
14
+
15
+	parent *metadata
16
+}
17
+
18
+func pathExists(pth string) bool {
19
+	if _, err := os.Stat(pth); err != nil {
20
+		return false
21
+	}
22
+	return true
23
+}
24
+
25
+// Migrate existing images and containers from docker < 0.7.x
26
+//
27
+// The format pre 0.7 is for docker to store the metadata and filesystem
28
+// content in the same directory.  For the migration to work we need to move Image layer
29
+// data from /var/lib/docker/graph/<id>/layers to the diff of the registered id.
30
+//
31
+// Next we need to migrate the container's rw layer to diff of the driver.  After the
32
+// contents are migrated we need to register the image and container ids with the
33
+// driver.
34
+//
35
+// For the migration we try to move the folder containing the layer files, if that
36
+// fails because the data is currently mounted we will fallback to creating a
37
+// symlink.
38
+func (a *AufsDriver) Migrate(pth string, setupInit func(p string) error) error {
39
+	if pathExists(path.Join(pth, "graph")) {
40
+		if err := a.migrateImages(path.Join(pth, "graph")); err != nil {
41
+			return err
42
+		}
43
+		return a.migrateContainers(path.Join(pth, "containers"), setupInit)
44
+	}
45
+	return nil
46
+}
47
+
48
+func (a *AufsDriver) migrateContainers(pth string, setupInit func(p string) error) error {
49
+	fis, err := ioutil.ReadDir(pth)
50
+	if err != nil {
51
+		return err
52
+	}
53
+
54
+	for _, fi := range fis {
55
+		if id := fi.Name(); fi.IsDir() && pathExists(path.Join(pth, id, "rw")) {
56
+			if err := tryRelocate(path.Join(pth, id, "rw"), path.Join(a.rootPath(), "diff", id)); err != nil {
57
+				return err
58
+			}
59
+
60
+			if !a.Exists(id) {
61
+
62
+				metadata, err := loadMetadata(path.Join(pth, id, "config.json"))
63
+				if err != nil {
64
+					return err
65
+				}
66
+
67
+				initID := fmt.Sprintf("%s-init", id)
68
+				if err := a.Create(initID, metadata.Image); err != nil {
69
+					return err
70
+				}
71
+
72
+				initPath, err := a.Get(initID)
73
+				if err != nil {
74
+					return err
75
+				}
76
+				// setup init layer
77
+				if err := setupInit(initPath); err != nil {
78
+					return err
79
+				}
80
+
81
+				if err := a.Create(id, initID); err != nil {
82
+					return err
83
+				}
84
+			}
85
+		}
86
+	}
87
+	return nil
88
+}
89
+
90
+func (a *AufsDriver) migrateImages(pth string) error {
91
+	fis, err := ioutil.ReadDir(pth)
92
+	if err != nil {
93
+		return err
94
+	}
95
+	var (
96
+		m       = make(map[string]*metadata)
97
+		current *metadata
98
+		exists  bool
99
+	)
100
+
101
+	for _, fi := range fis {
102
+		if id := fi.Name(); fi.IsDir() && pathExists(path.Join(pth, id, "layer")) {
103
+			if current, exists = m[id]; !exists {
104
+				current, err = loadMetadata(path.Join(pth, id, "json"))
105
+				if err != nil {
106
+					return err
107
+				}
108
+				m[id] = current
109
+			}
110
+		}
111
+	}
112
+
113
+	for _, v := range m {
114
+		v.parent = m[v.ParentID]
115
+	}
116
+
117
+	migrated := make(map[string]bool)
118
+	for _, v := range m {
119
+		if err := a.migrateImage(v, pth, migrated); err != nil {
120
+			return err
121
+		}
122
+	}
123
+	return nil
124
+}
125
+
126
+func (a *AufsDriver) migrateImage(m *metadata, pth string, migrated map[string]bool) error {
127
+	if !migrated[m.ID] {
128
+		if m.parent != nil {
129
+			a.migrateImage(m.parent, pth, migrated)
130
+		}
131
+		if err := tryRelocate(path.Join(pth, m.ID, "layer"), path.Join(a.rootPath(), "diff", m.ID)); err != nil {
132
+			return err
133
+		}
134
+		if !a.Exists(m.ID) {
135
+			if err := a.Create(m.ID, m.ParentID); err != nil {
136
+				return err
137
+			}
138
+		}
139
+		migrated[m.ID] = true
140
+	}
141
+	return nil
142
+}
143
+
144
+// tryRelocate will try to rename the old path to the new pack and if
145
+// the operation fails, it will fallback to a symlink
146
+func tryRelocate(oldPath, newPath string) error {
147
+	s, err := os.Lstat(newPath)
148
+	if err != nil && !os.IsNotExist(err) {
149
+		return err
150
+	}
151
+	// If the destination is a symlink then we already tried to relocate once before
152
+	// and it failed so we delete it and try to remove
153
+	if s != nil && s.Mode()&os.ModeSymlink == os.ModeSymlink {
154
+		if err := os.RemoveAll(newPath); err != nil {
155
+			return err
156
+		}
157
+	}
158
+	if err := os.Rename(oldPath, newPath); err != nil {
159
+		if sErr := os.Symlink(oldPath, newPath); sErr != nil {
160
+			return fmt.Errorf("Unable to relocate %s to %s: Rename err %s Symlink err %s", oldPath, newPath, err, sErr)
161
+		}
162
+	}
163
+	return nil
164
+}
165
+
166
+func loadMetadata(pth string) (*metadata, error) {
167
+	f, err := os.Open(pth)
168
+	if err != nil {
169
+		return nil, err
170
+	}
171
+	defer f.Close()
172
+
173
+	var (
174
+		out = &metadata{}
175
+		dec = json.NewDecoder(f)
176
+	)
177
+
178
+	if err := dec.Decode(out); err != nil {
179
+		return nil, err
180
+	}
181
+	return out, nil
182
+}
... ...
@@ -8,7 +8,7 @@ import (
8 8
 	"github.com/dotcloud/docker/archive"
9 9
 	"github.com/dotcloud/docker/graphdb"
10 10
 	"github.com/dotcloud/docker/graphdriver"
11
-	_ "github.com/dotcloud/docker/graphdriver/aufs"
11
+	"github.com/dotcloud/docker/graphdriver/aufs"
12 12
 	_ "github.com/dotcloud/docker/graphdriver/devmapper"
13 13
 	_ "github.com/dotcloud/docker/graphdriver/dummy"
14 14
 	"github.com/dotcloud/docker/utils"
... ...
@@ -629,6 +629,12 @@ func NewRuntimeFromDirectory(config *DaemonConfig) (*Runtime, error) {
629 629
 		return nil, err
630 630
 	}
631 631
 
632
+	if ad, ok := driver.(*aufs.AufsDriver); ok {
633
+		if err := ad.Migrate(config.Root, setupInitLayer); err != nil {
634
+			return nil, err
635
+		}
636
+	}
637
+
632 638
 	if err := linkLxcStart(config.Root); err != nil {
633 639
 		return nil, err
634 640
 	}