Browse code

Allow to downgrade local volumes from > 1.7 to 1.6.

Signed-off-by: David Calavera <david.calavera@gmail.com>

David Calavera authored on 2015/06/04 01:26:41
Showing 7 changed files
... ...
@@ -73,7 +73,10 @@ type CommonContainer struct {
73 73
 	MountLabel, ProcessLabel string
74 74
 	RestartCount             int
75 75
 	UpdateDns                bool
76
-	MountPoints              map[string]*mountPoint
76
+
77
+	MountPoints map[string]*mountPoint
78
+	Volumes     map[string]string // Deprecated since 1.7, kept for backwards compatibility
79
+	VolumesRW   map[string]bool   // Deprecated since 1.7, kept for backwards compatibility
77 80
 
78 81
 	hostConfig *runconfig.HostConfig
79 82
 	command    *execdriver.Command
... ...
@@ -50,8 +50,6 @@ import (
50 50
 	"github.com/docker/docker/volume/local"
51 51
 )
52 52
 
53
-const defaultVolumesPathName = "volumes"
54
-
55 53
 var (
56 54
 	validContainerNameChars   = `[a-zA-Z0-9][a-zA-Z0-9_.-]`
57 55
 	validContainerNamePattern = regexp.MustCompile(`^/?` + validContainerNameChars + `+$`)
... ...
@@ -158,12 +156,7 @@ func (daemon *Daemon) containerRoot(id string) string {
158 158
 // This is typically done at startup.
159 159
 func (daemon *Daemon) load(id string) (*Container, error) {
160 160
 	container := &Container{
161
-		CommonContainer: CommonContainer{
162
-			State:        NewState(),
163
-			root:         daemon.containerRoot(id),
164
-			MountPoints:  make(map[string]*mountPoint),
165
-			execCommands: newExecStore(),
166
-		},
161
+		CommonContainer: daemon.newBaseContainer(id),
167 162
 	}
168 163
 
169 164
 	if err := container.FromDisk(); err != nil {
... ...
@@ -509,25 +502,21 @@ func (daemon *Daemon) newContainer(name string, config *runconfig.Config, imgID
509 509
 	daemon.generateHostname(id, config)
510 510
 	entrypoint, args := daemon.getEntrypointAndArgs(config.Entrypoint, config.Cmd)
511 511
 
512
+	base := daemon.newBaseContainer(id)
513
+	base.Created = time.Now().UTC()
514
+	base.Path = entrypoint
515
+	base.Args = args //FIXME: de-duplicate from config
516
+	base.Config = config
517
+	base.hostConfig = &runconfig.HostConfig{}
518
+	base.ImageID = imgID
519
+	base.NetworkSettings = &network.Settings{}
520
+	base.Name = name
521
+	base.Driver = daemon.driver.String()
522
+	base.ExecDriver = daemon.execDriver.Name()
523
+
512 524
 	container := &Container{
513
-		CommonContainer: CommonContainer{
514
-			ID:              id, // FIXME: we should generate the ID here instead of receiving it as an argument
515
-			Created:         time.Now().UTC(),
516
-			Path:            entrypoint,
517
-			Args:            args, //FIXME: de-duplicate from config
518
-			Config:          config,
519
-			hostConfig:      &runconfig.HostConfig{},
520
-			ImageID:         imgID,
521
-			NetworkSettings: &network.Settings{},
522
-			Name:            name,
523
-			Driver:          daemon.driver.String(),
524
-			ExecDriver:      daemon.execDriver.Name(),
525
-			State:           NewState(),
526
-			execCommands:    newExecStore(),
527
-			MountPoints:     map[string]*mountPoint{},
528
-		},
529
-	}
530
-	container.root = daemon.containerRoot(container.ID)
525
+		CommonContainer: base,
526
+	}
531 527
 
532 528
 	return container, err
533 529
 }
... ...
@@ -775,7 +764,7 @@ func NewDaemon(config *Config, registryService *registry.Service) (daemon *Daemo
775 775
 		return nil, err
776 776
 	}
777 777
 
778
-	volumesDriver, err := local.New(filepath.Join(config.Root, defaultVolumesPathName))
778
+	volumesDriver, err := local.New(config.Root)
779 779
 	if err != nil {
780 780
 		return nil, err
781 781
 	}
... ...
@@ -1244,3 +1233,15 @@ func (daemon *Daemon) setHostConfig(container *Container, hostConfig *runconfig.
1244 1244
 	container.toDisk()
1245 1245
 	return nil
1246 1246
 }
1247
+
1248
+func (daemon *Daemon) newBaseContainer(id string) CommonContainer {
1249
+	return CommonContainer{
1250
+		ID:           id,
1251
+		State:        NewState(),
1252
+		MountPoints:  make(map[string]*mountPoint),
1253
+		Volumes:      make(map[string]string),
1254
+		VolumesRW:    make(map[string]bool),
1255
+		execCommands: newExecStore(),
1256
+		root:         daemon.containerRoot(id),
1257
+	}
1258
+}
... ...
@@ -12,6 +12,8 @@ import (
12 12
 	"github.com/docker/docker/pkg/stringid"
13 13
 	"github.com/docker/docker/pkg/truncindex"
14 14
 	"github.com/docker/docker/volume"
15
+	"github.com/docker/docker/volume/drivers"
16
+	"github.com/docker/docker/volume/local"
15 17
 )
16 18
 
17 19
 //
... ...
@@ -178,10 +180,11 @@ func TestLoadWithVolume(t *testing.T) {
178 178
 		t.Fatal(err)
179 179
 	}
180 180
 
181
-	daemon := &Daemon{
182
-		repository: tmp,
183
-		root:       tmp,
181
+	daemon, err := initDaemonForVolumesTest(tmp)
182
+	if err != nil {
183
+		t.Fatal(err)
184 184
 	}
185
+	defer volumedrivers.Unregister(volume.DefaultDriverName)
185 186
 
186 187
 	c, err := daemon.load(containerId)
187 188
 	if err != nil {
... ...
@@ -214,7 +217,7 @@ func TestLoadWithVolume(t *testing.T) {
214 214
 		t.Fatalf("Expected mount driver local, was %s\n", m.Driver)
215 215
 	}
216 216
 
217
-	newVolumeContent := filepath.Join(volumePath, "helo")
217
+	newVolumeContent := filepath.Join(volumePath, local.VolumeDataPathName, "helo")
218 218
 	b, err := ioutil.ReadFile(newVolumeContent)
219 219
 	if err != nil {
220 220
 		t.Fatal(err)
... ...
@@ -265,10 +268,11 @@ func TestLoadWithBindMount(t *testing.T) {
265 265
 		t.Fatal(err)
266 266
 	}
267 267
 
268
-	daemon := &Daemon{
269
-		repository: tmp,
270
-		root:       tmp,
268
+	daemon, err := initDaemonForVolumesTest(tmp)
269
+	if err != nil {
270
+		t.Fatal(err)
271 271
 	}
272
+	defer volumedrivers.Unregister(volume.DefaultDriverName)
272 273
 
273 274
 	c, err := daemon.load(containerId)
274 275
 	if err != nil {
... ...
@@ -301,3 +305,212 @@ func TestLoadWithBindMount(t *testing.T) {
301 301
 		t.Fatalf("Expected mount point to be RW but it was not\n")
302 302
 	}
303 303
 }
304
+
305
+func TestLoadWithVolume17RC(t *testing.T) {
306
+	tmp, err := ioutil.TempDir("", "docker-daemon-test-")
307
+	if err != nil {
308
+		t.Fatal(err)
309
+	}
310
+	defer os.RemoveAll(tmp)
311
+
312
+	containerId := "d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e"
313
+	containerPath := filepath.Join(tmp, containerId)
314
+	if err := os.MkdirAll(containerPath, 0755); err != nil {
315
+		t.Fatal(err)
316
+	}
317
+
318
+	hostVolumeId := "6a3c03fc4a4e588561a543cc3bdd50089e27bd11bbb0e551e19bf735e2514101"
319
+	volumePath := filepath.Join(tmp, "volumes", hostVolumeId)
320
+
321
+	if err := os.MkdirAll(volumePath, 0755); err != nil {
322
+		t.Fatal(err)
323
+	}
324
+
325
+	content := filepath.Join(volumePath, "helo")
326
+	if err := ioutil.WriteFile(content, []byte("HELO"), 0644); err != nil {
327
+		t.Fatal(err)
328
+	}
329
+
330
+	config := `{"State":{"Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":2464,"ExitCode":0,
331
+"Error":"","StartedAt":"2015-05-26T16:48:53.869308965Z","FinishedAt":"0001-01-01T00:00:00Z"},
332
+"ID":"d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e","Created":"2015-05-26T16:48:53.7987917Z","Path":"top",
333
+"Args":[],"Config":{"Hostname":"d59df5276e7b","Domainname":"","User":"","Memory":0,"MemorySwap":0,"CpuShares":0,"Cpuset":"",
334
+"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"PortSpecs":null,"ExposedPorts":null,"Tty":true,"OpenStdin":true,
335
+"StdinOnce":false,"Env":null,"Cmd":["top"],"Image":"ubuntu:latest","Volumes":null,"WorkingDir":"","Entrypoint":null,
336
+"NetworkDisabled":false,"MacAddress":"","OnBuild":null,"Labels":{}},"Image":"07f8e8c5e66084bef8f848877857537ffe1c47edd01a93af27e7161672ad0e95",
337
+"NetworkSettings":{"IPAddress":"172.17.0.1","IPPrefixLen":16,"MacAddress":"02:42:ac:11:00:01","LinkLocalIPv6Address":"fe80::42:acff:fe11:1",
338
+"LinkLocalIPv6PrefixLen":64,"GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"Gateway":"172.17.42.1","IPv6Gateway":"","Bridge":"docker0","PortMapping":null,"Ports":{}},
339
+"ResolvConfPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/resolv.conf",
340
+"HostnamePath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/hostname",
341
+"HostsPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/hosts",
342
+"LogPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e-json.log",
343
+"Name":"/ubuntu","Driver":"aufs","ExecDriver":"native-0.2","MountLabel":"","ProcessLabel":"","AppArmorProfile":"","RestartCount":0,
344
+"UpdateDns":false,"MountPoints":{"/vol1":{"Name":"6a3c03fc4a4e588561a543cc3bdd50089e27bd11bbb0e551e19bf735e2514101","Destination":"/vol1","Driver":"local","RW":true,"Source":"","Relabel":""}},"AppliedVolumesFrom":null}`
345
+
346
+	if err = ioutil.WriteFile(filepath.Join(containerPath, "config.json"), []byte(config), 0644); err != nil {
347
+		t.Fatal(err)
348
+	}
349
+
350
+	hostConfig := `{"Binds":[],"ContainerIDFile":"","LxcConf":[],"Memory":0,"MemorySwap":0,"CpuShares":0,"CpusetCpus":"",
351
+"Privileged":false,"PortBindings":{},"Links":null,"PublishAllPorts":false,"Dns":null,"DnsSearch":null,"ExtraHosts":null,"VolumesFrom":null,
352
+"Devices":[],"NetworkMode":"bridge","IpcMode":"","PidMode":"","CapAdd":null,"CapDrop":null,"RestartPolicy":{"Name":"no","MaximumRetryCount":0},
353
+"SecurityOpt":null,"ReadonlyRootfs":false,"Ulimits":null,"LogConfig":{"Type":"","Config":null},"CgroupParent":""}`
354
+	if err = ioutil.WriteFile(filepath.Join(containerPath, "hostconfig.json"), []byte(hostConfig), 0644); err != nil {
355
+		t.Fatal(err)
356
+	}
357
+
358
+	daemon, err := initDaemonForVolumesTest(tmp)
359
+	if err != nil {
360
+		t.Fatal(err)
361
+	}
362
+	defer volumedrivers.Unregister(volume.DefaultDriverName)
363
+
364
+	c, err := daemon.load(containerId)
365
+	if err != nil {
366
+		t.Fatal(err)
367
+	}
368
+
369
+	err = daemon.verifyVolumesInfo(c)
370
+	if err != nil {
371
+		t.Fatal(err)
372
+	}
373
+
374
+	if len(c.MountPoints) != 1 {
375
+		t.Fatalf("Expected 1 volume mounted, was 0\n")
376
+	}
377
+
378
+	m := c.MountPoints["/vol1"]
379
+	if m.Name != hostVolumeId {
380
+		t.Fatalf("Expected mount name to be %s, was %s\n", hostVolumeId, m.Name)
381
+	}
382
+
383
+	if m.Destination != "/vol1" {
384
+		t.Fatalf("Expected mount destination /vol1, was %s\n", m.Destination)
385
+	}
386
+
387
+	if !m.RW {
388
+		t.Fatalf("Expected mount point to be RW but it was not\n")
389
+	}
390
+
391
+	if m.Driver != volume.DefaultDriverName {
392
+		t.Fatalf("Expected mount driver local, was %s\n", m.Driver)
393
+	}
394
+
395
+	newVolumeContent := filepath.Join(volumePath, local.VolumeDataPathName, "helo")
396
+	b, err := ioutil.ReadFile(newVolumeContent)
397
+	if err != nil {
398
+		t.Fatal(err)
399
+	}
400
+	if string(b) != "HELO" {
401
+		t.Fatalf("Expected HELO, was %s\n", string(b))
402
+	}
403
+}
404
+
405
+func TestRemoveLocalVolumesFollowingSymlinks(t *testing.T) {
406
+	tmp, err := ioutil.TempDir("", "docker-daemon-test-")
407
+	if err != nil {
408
+		t.Fatal(err)
409
+	}
410
+	defer os.RemoveAll(tmp)
411
+
412
+	containerId := "d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e"
413
+	containerPath := filepath.Join(tmp, containerId)
414
+	if err := os.MkdirAll(containerPath, 0755); err != nil {
415
+		t.Fatal(err)
416
+	}
417
+
418
+	hostVolumeId := stringid.GenerateRandomID()
419
+	vfsPath := filepath.Join(tmp, "vfs", "dir", hostVolumeId)
420
+	volumePath := filepath.Join(tmp, "volumes", hostVolumeId)
421
+
422
+	if err := os.MkdirAll(vfsPath, 0755); err != nil {
423
+		t.Fatal(err)
424
+	}
425
+	if err := os.MkdirAll(volumePath, 0755); err != nil {
426
+		t.Fatal(err)
427
+	}
428
+
429
+	content := filepath.Join(vfsPath, "helo")
430
+	if err := ioutil.WriteFile(content, []byte("HELO"), 0644); err != nil {
431
+		t.Fatal(err)
432
+	}
433
+
434
+	config := `{"State":{"Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":2464,"ExitCode":0,
435
+"Error":"","StartedAt":"2015-05-26T16:48:53.869308965Z","FinishedAt":"0001-01-01T00:00:00Z"},
436
+"ID":"d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e","Created":"2015-05-26T16:48:53.7987917Z","Path":"top",
437
+"Args":[],"Config":{"Hostname":"d59df5276e7b","Domainname":"","User":"","Memory":0,"MemorySwap":0,"CpuShares":0,"Cpuset":"",
438
+"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"PortSpecs":null,"ExposedPorts":null,"Tty":true,"OpenStdin":true,
439
+"StdinOnce":false,"Env":null,"Cmd":["top"],"Image":"ubuntu:latest","Volumes":null,"WorkingDir":"","Entrypoint":null,
440
+"NetworkDisabled":false,"MacAddress":"","OnBuild":null,"Labels":{}},"Image":"07f8e8c5e66084bef8f848877857537ffe1c47edd01a93af27e7161672ad0e95",
441
+"NetworkSettings":{"IPAddress":"172.17.0.1","IPPrefixLen":16,"MacAddress":"02:42:ac:11:00:01","LinkLocalIPv6Address":"fe80::42:acff:fe11:1",
442
+"LinkLocalIPv6PrefixLen":64,"GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"Gateway":"172.17.42.1","IPv6Gateway":"","Bridge":"docker0","PortMapping":null,"Ports":{}},
443
+"ResolvConfPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/resolv.conf",
444
+"HostnamePath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/hostname",
445
+"HostsPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/hosts",
446
+"LogPath":"/var/lib/docker/containers/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e/d59df5276e7b219d510fe70565e0404bc06350e0d4b43fe961f22f339980170e-json.log",
447
+"Name":"/ubuntu","Driver":"aufs","ExecDriver":"native-0.2","MountLabel":"","ProcessLabel":"","AppArmorProfile":"","RestartCount":0,
448
+"UpdateDns":false,"Volumes":{"/vol1":"%s"},"VolumesRW":{"/vol1":true},"AppliedVolumesFrom":null}`
449
+
450
+	cfg := fmt.Sprintf(config, vfsPath)
451
+	if err = ioutil.WriteFile(filepath.Join(containerPath, "config.json"), []byte(cfg), 0644); err != nil {
452
+		t.Fatal(err)
453
+	}
454
+
455
+	hostConfig := `{"Binds":[],"ContainerIDFile":"","LxcConf":[],"Memory":0,"MemorySwap":0,"CpuShares":0,"CpusetCpus":"",
456
+"Privileged":false,"PortBindings":{},"Links":null,"PublishAllPorts":false,"Dns":null,"DnsSearch":null,"ExtraHosts":null,"VolumesFrom":null,
457
+"Devices":[],"NetworkMode":"bridge","IpcMode":"","PidMode":"","CapAdd":null,"CapDrop":null,"RestartPolicy":{"Name":"no","MaximumRetryCount":0},
458
+"SecurityOpt":null,"ReadonlyRootfs":false,"Ulimits":null,"LogConfig":{"Type":"","Config":null},"CgroupParent":""}`
459
+	if err = ioutil.WriteFile(filepath.Join(containerPath, "hostconfig.json"), []byte(hostConfig), 0644); err != nil {
460
+		t.Fatal(err)
461
+	}
462
+
463
+	daemon, err := initDaemonForVolumesTest(tmp)
464
+	if err != nil {
465
+		t.Fatal(err)
466
+	}
467
+	defer volumedrivers.Unregister(volume.DefaultDriverName)
468
+
469
+	c, err := daemon.load(containerId)
470
+	if err != nil {
471
+		t.Fatal(err)
472
+	}
473
+
474
+	err = daemon.verifyVolumesInfo(c)
475
+	if err != nil {
476
+		t.Fatal(err)
477
+	}
478
+
479
+	if len(c.MountPoints) != 1 {
480
+		t.Fatalf("Expected 1 volume mounted, was 0\n")
481
+	}
482
+
483
+	m := c.MountPoints["/vol1"]
484
+	v, err := createVolume(m.Name, m.Driver)
485
+	if err != nil {
486
+		t.Fatal(err)
487
+	}
488
+
489
+	if err := removeVolume(v); err != nil {
490
+		t.Fatal(err)
491
+	}
492
+
493
+	fi, err := os.Stat(vfsPath)
494
+	if err == nil || !os.IsNotExist(err) {
495
+		t.Fatalf("Expected vfs path to not exist: %v - %v\n", fi, err)
496
+	}
497
+}
498
+
499
+func initDaemonForVolumesTest(tmp string) (*Daemon, error) {
500
+	daemon := &Daemon{
501
+		repository: tmp,
502
+		root:       tmp,
503
+	}
504
+
505
+	volumesDriver, err := local.New(tmp)
506
+	if err != nil {
507
+		return nil, err
508
+	}
509
+	volumedrivers.Register(volumesDriver, volumesDriver.Name())
510
+
511
+	return daemon, nil
512
+}
... ...
@@ -1,16 +1,17 @@
1 1
 package daemon
2 2
 
3 3
 import (
4
-	"encoding/json"
5 4
 	"fmt"
6 5
 	"io/ioutil"
7 6
 	"os"
8 7
 	"path/filepath"
9 8
 	"strings"
10 9
 
10
+	"github.com/Sirupsen/logrus"
11 11
 	"github.com/docker/docker/pkg/chrootarchive"
12 12
 	"github.com/docker/docker/runconfig"
13 13
 	"github.com/docker/docker/volume"
14
+	"github.com/docker/docker/volume/local"
14 15
 	"github.com/docker/libcontainer/label"
15 16
 )
16 17
 
... ...
@@ -52,6 +53,13 @@ func (m *mountPoint) Path() string {
52 52
 	return m.Source
53 53
 }
54 54
 
55
+// BackwardsCompatible decides whether this mount point can be
56
+// used in old versions of Docker or not.
57
+// Only bind mounts and local volumes can be used in old versions of Docker.
58
+func (m *mountPoint) BackwardsCompatible() bool {
59
+	return len(m.Source) > 0 || m.Driver == volume.DefaultDriverName
60
+}
61
+
55 62
 func parseBindMount(spec string, mountLabel string, config *runconfig.Config) (*mountPoint, error) {
56 63
 	bind := &mountPoint{
57 64
 		RW: true,
... ...
@@ -231,8 +239,20 @@ func (daemon *Daemon) registerMountPoints(container *Container, hostConfig *runc
231 231
 		mountPoints[bind.Destination] = bind
232 232
 	}
233 233
 
234
+	// Keep backwards compatible structures
235
+	bcVolumes := map[string]string{}
236
+	bcVolumesRW := map[string]bool{}
237
+	for _, m := range mountPoints {
238
+		if m.BackwardsCompatible() {
239
+			bcVolumes[m.Destination] = m.Path()
240
+			bcVolumesRW[m.Destination] = m.RW
241
+		}
242
+	}
243
+
234 244
 	container.Lock()
235 245
 	container.MountPoints = mountPoints
246
+	container.Volumes = bcVolumes
247
+	container.VolumesRW = bcVolumesRW
236 248
 	container.Unlock()
237 249
 
238 250
 	return nil
... ...
@@ -241,53 +261,77 @@ func (daemon *Daemon) registerMountPoints(container *Container, hostConfig *runc
241 241
 // verifyVolumesInfo ports volumes configured for the containers pre docker 1.7.
242 242
 // It reads the container configuration and creates valid mount points for the old volumes.
243 243
 func (daemon *Daemon) verifyVolumesInfo(container *Container) error {
244
-	jsonPath, err := container.jsonPath()
245
-	if err != nil {
246
-		return err
247
-	}
248
-	f, err := os.Open(jsonPath)
249
-	if err != nil {
250
-		if os.IsNotExist(err) {
251
-			return nil
244
+	// Inspect old structures only when we're upgrading from old versions
245
+	// to versions >= 1.7 and the MountPoints has not been populated with volumes data.
246
+	if len(container.MountPoints) == 0 && len(container.Volumes) > 0 {
247
+		for destination, hostPath := range container.Volumes {
248
+			vfsPath := filepath.Join(daemon.root, "vfs", "dir")
249
+			rw := container.VolumesRW != nil && container.VolumesRW[destination]
250
+
251
+			if strings.HasPrefix(hostPath, vfsPath) {
252
+				id := filepath.Base(hostPath)
253
+				if err := migrateVolume(id, hostPath); err != nil {
254
+					return err
255
+				}
256
+				container.addLocalMountPoint(id, destination, rw)
257
+			} else { // Bind mount
258
+				id, source, err := parseVolumeSource(hostPath)
259
+				// We should not find an error here coming
260
+				// from the old configuration, but who knows.
261
+				if err != nil {
262
+					return err
263
+				}
264
+				container.addBindMountPoint(id, source, destination, rw)
265
+			}
266
+		}
267
+	} else if len(container.MountPoints) > 0 {
268
+		// Volumes created with a Docker version >= 1.7. We verify integrity in case of data created
269
+		// with Docker 1.7 RC versions that put the information in
270
+		// DOCKER_ROOT/volumes/VOLUME_ID rather than DOCKER_ROOT/volumes/VOLUME_ID/_container_data.
271
+		l, err := getVolumeDriver(volume.DefaultDriverName)
272
+		if err != nil {
273
+			return err
252 274
 		}
253
-		return err
254
-	}
255
-
256
-	type oldContVolCfg struct {
257
-		Volumes   map[string]string
258
-		VolumesRW map[string]bool
259
-	}
260 275
 
261
-	vols := oldContVolCfg{
262
-		Volumes:   make(map[string]string),
263
-		VolumesRW: make(map[string]bool),
264
-	}
265
-	if err := json.NewDecoder(f).Decode(&vols); err != nil {
266
-		return err
267
-	}
276
+		for _, m := range container.MountPoints {
277
+			if m.Driver != volume.DefaultDriverName {
278
+				continue
279
+			}
280
+			dataPath := l.(*local.Root).DataPath(m.Name)
281
+			volumePath := filepath.Dir(dataPath)
268 282
 
269
-	for destination, hostPath := range vols.Volumes {
270
-		vfsPath := filepath.Join(daemon.root, "vfs", "dir")
271
-		rw := vols.VolumesRW[destination]
283
+			d, err := ioutil.ReadDir(volumePath)
284
+			if err != nil {
285
+				// If the volume directory doesn't exist yet it will be recreated,
286
+				// so we only return the error when there is a different issue.
287
+				if !os.IsNotExist(err) {
288
+					return err
289
+				}
290
+				// Do not check when the volume directory does not exist.
291
+				continue
292
+			}
293
+			if validVolumeLayout(d) {
294
+				continue
295
+			}
272 296
 
273
-		if strings.HasPrefix(hostPath, vfsPath) {
274
-			id := filepath.Base(hostPath)
275
-			if err := daemon.migrateVolume(id, hostPath); err != nil {
297
+			if err := os.Mkdir(dataPath, 0755); err != nil {
276 298
 				return err
277 299
 			}
278
-			container.addLocalMountPoint(id, destination, rw)
279
-		} else { // Bind mount
280
-			id, source, err := parseVolumeSource(hostPath)
281
-			// We should not find an error here coming
282
-			// from the old configuration, but who knows.
283
-			if err != nil {
284
-				return err
300
+
301
+			// Move data inside the data directory
302
+			for _, f := range d {
303
+				oldp := filepath.Join(volumePath, f.Name())
304
+				newp := filepath.Join(dataPath, f.Name())
305
+				if err := os.Rename(oldp, newp); err != nil {
306
+					logrus.Errorf("Unable to move %s to %s\n", oldp, newp)
307
+				}
285 308
 			}
286
-			container.addBindMountPoint(id, source, destination, rw)
287 309
 		}
310
+
311
+		return container.ToDisk()
288 312
 	}
289 313
 
290
-	return container.ToDisk()
314
+	return nil
291 315
 }
292 316
 
293 317
 func createVolume(name, driverName string) (volume.Volume, error) {
... ...
@@ -8,9 +8,10 @@ import (
8 8
 	"sort"
9 9
 	"strings"
10 10
 
11
-	"github.com/Sirupsen/logrus"
12 11
 	"github.com/docker/docker/daemon/execdriver"
13 12
 	"github.com/docker/docker/pkg/system"
13
+	"github.com/docker/docker/volume"
14
+	"github.com/docker/docker/volume/local"
14 15
 )
15 16
 
16 17
 // copyOwnership copies the permissions and uid:gid of the source file
... ...
@@ -70,33 +71,49 @@ func (m mounts) parts(i int) int {
70 70
 	return len(strings.Split(filepath.Clean(m[i].Destination), string(os.PathSeparator)))
71 71
 }
72 72
 
73
-// migrateVolume moves the contents of a volume created pre Docker 1.7
74
-// to the location expected by the local driver. Steps:
75
-// 1. Save old directory that includes old volume's config json file.
76
-// 2. Move virtual directory with content to where the local driver expects it to be.
77
-// 3. Remove the backup of the old volume config.
78
-func (daemon *Daemon) migrateVolume(id, vfs string) error {
79
-	volumeInfo := filepath.Join(daemon.root, defaultVolumesPathName, id)
80
-	backup := filepath.Join(daemon.root, defaultVolumesPathName, id+".back")
81
-
82
-	var err error
83
-	if err = os.Rename(volumeInfo, backup); err != nil {
73
+// migrateVolume links the contents of a volume created pre Docker 1.7
74
+// into the location expected by the local driver.
75
+// It creates a symlink from DOCKER_ROOT/vfs/dir/VOLUME_ID to DOCKER_ROOT/volumes/VOLUME_ID/_container_data.
76
+// It preserves the volume json configuration generated pre Docker 1.7 to be able to
77
+// downgrade from Docker 1.7 to Docker 1.6 without losing volume compatibility.
78
+func migrateVolume(id, vfs string) error {
79
+	l, err := getVolumeDriver(volume.DefaultDriverName)
80
+	if err != nil {
84 81
 		return err
85 82
 	}
86
-	defer func() {
87
-		// Put old configuration back in place in case one of the next steps fails.
88
-		if err != nil {
89
-			os.Rename(backup, volumeInfo)
90
-		}
91
-	}()
92 83
 
93
-	if err = os.Rename(vfs, volumeInfo); err != nil {
84
+	newDataPath := l.(*local.Root).DataPath(id)
85
+	fi, err := os.Stat(newDataPath)
86
+	if err != nil && !os.IsNotExist(err) {
94 87
 		return err
95 88
 	}
96 89
 
97
-	if err = os.RemoveAll(backup); err != nil {
98
-		logrus.Errorf("Unable to remove volume info backup directory %s: %v", backup, err)
90
+	if fi != nil && fi.IsDir() {
91
+		return nil
92
+	}
93
+
94
+	return os.Symlink(vfs, newDataPath)
95
+}
96
+
97
+// validVolumeLayout checks whether the volume directory layout
98
+// is valid to work with Docker post 1.7 or not.
99
+func validVolumeLayout(files []os.FileInfo) bool {
100
+	if len(files) == 1 && files[0].Name() == local.VolumeDataPathName && files[0].IsDir() {
101
+		return true
102
+	}
103
+
104
+	if len(files) != 2 {
105
+		return false
106
+	}
107
+
108
+	for _, f := range files {
109
+		if f.Name() == "config.json" ||
110
+			(f.Name() == local.VolumeDataPathName && f.Mode()&os.ModeSymlink == os.ModeSymlink) {
111
+			// Old volume configuration, we ignore it
112
+			continue
113
+		}
114
+		return false
99 115
 	}
100 116
 
101
-	return nil
117
+	return true
102 118
 }
... ...
@@ -2,7 +2,11 @@
2 2
 
3 3
 package daemon
4 4
 
5
-import "github.com/docker/docker/daemon/execdriver"
5
+import (
6
+	"os"
7
+
8
+	"github.com/docker/docker/daemon/execdriver"
9
+)
6 10
 
7 11
 // Not supported on Windows
8 12
 func copyOwnership(source, destination string) error {
... ...
@@ -13,6 +17,10 @@ func (container *Container) setupMounts() ([]execdriver.Mount, error) {
13 13
 	return nil, nil
14 14
 }
15 15
 
16
-func (daemon *Daemon) migrateVolume(id, vfs string) error {
16
+func migrateVolume(id, vfs string) error {
17 17
 	return nil
18 18
 }
19
+
20
+func validVolumeLayout(files []os.FileInfo) bool {
21
+	return true
22
+}
... ...
@@ -6,29 +6,46 @@ import (
6 6
 	"io/ioutil"
7 7
 	"os"
8 8
 	"path/filepath"
9
+	"strings"
9 10
 	"sync"
10 11
 
11 12
 	"github.com/docker/docker/volume"
12 13
 )
13 14
 
14
-func New(rootDirectory string) (*Root, error) {
15
+// VolumeDataPathName is the name of the directory where the volume data is stored.
16
+// It uses a very distintive name to avoid colissions migrating data between
17
+// Docker versions.
18
+const (
19
+	VolumeDataPathName = "_data"
20
+	volumesPathName    = "volumes"
21
+)
22
+
23
+var oldVfsDir = filepath.Join("vfs", "dir")
24
+
25
+func New(scope string) (*Root, error) {
26
+	rootDirectory := filepath.Join(scope, volumesPathName)
27
+
15 28
 	if err := os.MkdirAll(rootDirectory, 0700); err != nil {
16 29
 		return nil, err
17 30
 	}
31
+
18 32
 	r := &Root{
33
+		scope:   scope,
19 34
 		path:    rootDirectory,
20 35
 		volumes: make(map[string]*Volume),
21 36
 	}
37
+
22 38
 	dirs, err := ioutil.ReadDir(rootDirectory)
23 39
 	if err != nil {
24 40
 		return nil, err
25 41
 	}
42
+
26 43
 	for _, d := range dirs {
27 44
 		name := filepath.Base(d.Name())
28 45
 		r.volumes[name] = &Volume{
29 46
 			driverName: r.Name(),
30 47
 			name:       name,
31
-			path:       filepath.Join(rootDirectory, name),
48
+			path:       r.DataPath(name),
32 49
 		}
33 50
 	}
34 51
 	return r, nil
... ...
@@ -36,10 +53,15 @@ func New(rootDirectory string) (*Root, error) {
36 36
 
37 37
 type Root struct {
38 38
 	m       sync.Mutex
39
+	scope   string
39 40
 	path    string
40 41
 	volumes map[string]*Volume
41 42
 }
42 43
 
44
+func (r *Root) DataPath(volumeName string) string {
45
+	return filepath.Join(r.path, volumeName, VolumeDataPathName)
46
+}
47
+
43 48
 func (r *Root) Name() string {
44 49
 	return "local"
45 50
 }
... ...
@@ -47,12 +69,13 @@ func (r *Root) Name() string {
47 47
 func (r *Root) Create(name string) (volume.Volume, error) {
48 48
 	r.m.Lock()
49 49
 	defer r.m.Unlock()
50
+
50 51
 	v, exists := r.volumes[name]
51 52
 	if !exists {
52
-		path := filepath.Join(r.path, name)
53
-		if err := os.Mkdir(path, 0755); err != nil {
53
+		path := r.DataPath(name)
54
+		if err := os.MkdirAll(path, 0755); err != nil {
54 55
 			if os.IsExist(err) {
55
-				return nil, fmt.Errorf("volume already exists under %s", path)
56
+				return nil, fmt.Errorf("volume already exists under %s", filepath.Dir(path))
56 57
 			}
57 58
 			return nil, err
58 59
 		}
... ...
@@ -76,12 +99,40 @@ func (r *Root) Remove(v volume.Volume) error {
76 76
 	}
77 77
 	lv.release()
78 78
 	if lv.usedCount == 0 {
79
+		realPath, err := filepath.EvalSymlinks(lv.path)
80
+		if err != nil {
81
+			return err
82
+		}
83
+		if !r.scopedPath(realPath) {
84
+			return fmt.Errorf("Unable to remove a directory of out the Docker root: %s", realPath)
85
+		}
86
+
87
+		if err := os.RemoveAll(realPath); err != nil {
88
+			return err
89
+		}
90
+
79 91
 		delete(r.volumes, lv.name)
80
-		return os.RemoveAll(lv.path)
92
+		return os.RemoveAll(filepath.Dir(lv.path))
81 93
 	}
82 94
 	return nil
83 95
 }
84 96
 
97
+// scopedPath verifies that the path where the volume is located
98
+// is under Docker's root and the valid local paths.
99
+func (r *Root) scopedPath(realPath string) bool {
100
+	// Volumes path for Docker version >= 1.7
101
+	if strings.HasPrefix(realPath, filepath.Join(r.scope, volumesPathName)) {
102
+		return true
103
+	}
104
+
105
+	// Volumes path for Docker version < 1.7
106
+	if strings.HasPrefix(realPath, filepath.Join(r.scope, oldVfsDir)) {
107
+		return true
108
+	}
109
+
110
+	return false
111
+}
112
+
85 113
 type Volume struct {
86 114
 	m         sync.Mutex
87 115
 	usedCount int