Skip to content

Commit 3e4c333

Browse files
authored
Merge pull request moby#25830 from tiborvass/cherry-pick-25825
[1.12] Fix volume not working after daemon restart
2 parents 5d29b79 + e8ed523 commit 3e4c333

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

volume/local/local.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io/ioutil"
1010
"os"
1111
"path/filepath"
12+
"reflect"
1213
"sync"
1314

1415
"github.com/Sirupsen/logrus"
@@ -90,10 +91,13 @@ func New(scope string, rootUID, rootGID int) (*Root, error) {
9091
r.volumes[name] = v
9192
optsFilePath := filepath.Join(rootDirectory, name, "opts.json")
9293
if b, err := ioutil.ReadFile(optsFilePath); err == nil {
93-
v.opts = &optsConfig{}
94-
if err := json.Unmarshal(b, v.opts); err != nil {
94+
opts := optsConfig{}
95+
if err := json.Unmarshal(b, &opts); err != nil {
9596
return nil, err
9697
}
98+
if !reflect.DeepEqual(opts, optsConfig{}) {
99+
v.opts = &opts
100+
}
97101

98102
// unmount anything that may still be mounted (for example, from an unclean shutdown)
99103
for _, info := range mountInfos {
@@ -178,7 +182,7 @@ func (r *Root) Create(name string, opts map[string]string) (volume.Volume, error
178182
path: path,
179183
}
180184

181-
if opts != nil {
185+
if len(opts) != 0 {
182186
if err = setOpts(v, opts); err != nil {
183187
return nil, err
184188
}

volume/local/local_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package local
33
import (
44
"io/ioutil"
55
"os"
6+
"path/filepath"
67
"reflect"
78
"runtime"
89
"strings"
@@ -133,6 +134,11 @@ func TestCreate(t *testing.T) {
133134
}
134135
}
135136
}
137+
138+
r, err = New(rootDir, 0, 0)
139+
if err != nil {
140+
t.Fatal(err)
141+
}
136142
}
137143

138144
func TestValidateName(t *testing.T) {
@@ -262,3 +268,61 @@ func TestCreateWithOpts(t *testing.T) {
262268
t.Fatal("missing volume options on restart")
263269
}
264270
}
271+
272+
func TestRealodNoOpts(t *testing.T) {
273+
rootDir, err := ioutil.TempDir("", "volume-test-reload-no-opts")
274+
if err != nil {
275+
t.Fatal(err)
276+
}
277+
defer os.RemoveAll(rootDir)
278+
279+
r, err := New(rootDir, 0, 0)
280+
if err != nil {
281+
t.Fatal(err)
282+
}
283+
284+
if _, err := r.Create("test1", nil); err != nil {
285+
t.Fatal(err)
286+
}
287+
if _, err := r.Create("test2", nil); err != nil {
288+
t.Fatal(err)
289+
}
290+
// make sure a file with `null` (.e.g. empty opts map from older daemon) is ok
291+
if err := ioutil.WriteFile(filepath.Join(rootDir, "test2"), []byte("null"), 600); err != nil {
292+
t.Fatal(err)
293+
}
294+
295+
if _, err := r.Create("test3", nil); err != nil {
296+
t.Fatal(err)
297+
}
298+
// make sure an empty opts file doesn't break us too
299+
if err := ioutil.WriteFile(filepath.Join(rootDir, "test3"), nil, 600); err != nil {
300+
t.Fatal(err)
301+
}
302+
303+
if _, err := r.Create("test4", map[string]string{}); err != nil {
304+
t.Fatal(err)
305+
}
306+
307+
r, err = New(rootDir, 0, 0)
308+
if err != nil {
309+
t.Fatal(err)
310+
}
311+
312+
for _, name := range []string{"test1", "test2", "test3", "test4"} {
313+
v, err := r.Get(name)
314+
if err != nil {
315+
t.Fatal(err)
316+
}
317+
lv, ok := v.(*localVolume)
318+
if !ok {
319+
t.Fatalf("expected *localVolume got: %v", reflect.TypeOf(v))
320+
}
321+
if lv.opts != nil {
322+
t.Fatalf("expected opts to be nil, got: %v", lv.opts)
323+
}
324+
if _, err := lv.Mount("1234"); err != nil {
325+
t.Fatal(err)
326+
}
327+
}
328+
}

0 commit comments

Comments
 (0)