55 log "github.com/Sirupsen/logrus"
66 "github.com/docker/go-plugins-helpers/volume"
77 "os"
8- "strings"
98 "path/filepath"
109)
1110
@@ -42,13 +41,22 @@ func (n nfsDriver) Mount(r volume.MountRequest) volume.Response {
4241 n .m .Lock ()
4342 defer n .m .Unlock ()
4443
44+ resolvedName , resOpts := resolveName (r .Name )
4545
46- hostdir := mountpoint (n .root , r . Name )
47- source := n .fixSource (r . Name , r . ID )
46+ hostdir := mountpoint (n .root , resolvedName )
47+ source := n .fixSource (resolvedName )
4848
49- if n .mountm .HasMount (r .Name ) && n .mountm .Count (r .Name ) > 0 {
49+ // Support adhoc mounts (outside of docker volume create)
50+ // need to adjust source for ShareOpt
51+ if resOpts != nil {
52+ if share , found := resOpts [ShareOpt ]; found {
53+ source = n .fixSource (share )
54+ }
55+ }
56+
57+ if n .mountm .HasMount (resolvedName ) && n .mountm .Count (resolvedName ) > 0 {
5058 log .Infof ("Using existing NFS volume mount: %s" , hostdir )
51- n .mountm .Increment (r . Name )
59+ n .mountm .Increment (resolvedName )
5260 if err := run (fmt .Sprintf ("mountpoint -q %s" , hostdir )); err != nil {
5361 log .Infof ("Existing NFS volume not mounted, force remount." )
5462 } else {
@@ -62,15 +70,19 @@ func (n nfsDriver) Mount(r volume.MountRequest) volume.Response {
6270 return volume.Response {Err : err .Error ()}
6371 }
6472
65- if err := n .mountVolume (r .Name , source , hostdir , n .version ); err != nil {
73+ if n .mountm .HasMount (resolvedName ) == false {
74+ n .mountm .Create (resolvedName , hostdir , resOpts )
75+ }
76+
77+ if err := n .mountVolume (resolvedName , source , hostdir , n .version ); err != nil {
6678 return volume.Response {Err : err .Error ()}
6779 }
68- n .mountm .Add (r . Name , hostdir )
80+ n .mountm .Add (resolvedName , hostdir )
6981
70- if n .mountm .GetOption (r . Name , ShareOpt ) != "" && n .mountm .GetOptionAsBool (r . Name , CreateOpt ) {
71- log .Infof ("Mount: Share and Create options enabled - using %s as sub-dir mount" , r . Name )
72- datavol := filepath .Join (hostdir , r . Name )
73- if err := createDest (filepath .Join (hostdir , r . Name )); err != nil {
82+ if n .mountm .GetOption (resolvedName , ShareOpt ) != "" && n .mountm .GetOptionAsBool (resolvedName , CreateOpt ) {
83+ log .Infof ("Mount: Share and Create options enabled - using %s as sub-dir mount" , resolvedName )
84+ datavol := filepath .Join (hostdir , resolvedName )
85+ if err := createDest (filepath .Join (hostdir , resolvedName )); err != nil {
7486 return volume.Response {Err : err .Error ()}
7587 }
7688 hostdir = datavol
@@ -84,24 +96,28 @@ func (n nfsDriver) Unmount(r volume.UnmountRequest) volume.Response {
8496
8597 n .m .Lock ()
8698 defer n .m .Unlock ()
87- hostdir := mountpoint (n .root , r .Name )
8899
89- if n .mountm .HasMount (r .Name ) {
90- if n .mountm .Count (r .Name ) > 1 {
91- log .Printf ("Skipping unmount for %s - in use by other containers" , r .Name )
92- n .mountm .Decrement (r .Name )
100+ resolvedName , _ := resolveName (r .Name )
101+
102+ hostdir := mountpoint (n .root , resolvedName )
103+
104+ if n .mountm .HasMount (resolvedName ) {
105+ if n .mountm .Count (resolvedName ) > 1 {
106+ log .Printf ("Skipping unmount for %s - in use by other containers" , resolvedName )
107+ n .mountm .Decrement (resolvedName )
93108 return volume.Response {}
94109 }
95- n .mountm .Decrement (r . Name )
110+ n .mountm .Decrement (resolvedName )
96111 }
97112
98- log .Infof ("Unmounting volume name %s from %s" , r . Name , hostdir )
113+ log .Infof ("Unmounting volume name %s from %s" , resolvedName , hostdir )
99114
100115 if err := run (fmt .Sprintf ("umount %s" , hostdir )); err != nil {
116+ log .Errorf ("Error unmounting volume from host: %s" , err .Error ())
101117 return volume.Response {Err : err .Error ()}
102118 }
103119
104- n .mountm .DeleteIfNotManaged (r . Name )
120+ n .mountm .DeleteIfNotManaged (resolvedName )
105121
106122 if err := os .RemoveAll (hostdir ); err != nil {
107123 return volume.Response {Err : err .Error ()}
@@ -110,13 +126,11 @@ func (n nfsDriver) Unmount(r volume.UnmountRequest) volume.Response {
110126 return volume.Response {}
111127}
112128
113- func (n nfsDriver ) fixSource (name , id string ) string {
129+ func (n nfsDriver ) fixSource (name string ) string {
114130 if n .mountm .HasOption (name , ShareOpt ) {
115- return n .mountm .GetOption (name , ShareOpt )
131+ return addShareColon ( n .mountm .GetOption (name , ShareOpt ) )
116132 }
117- source := strings .Split (name , "/" )
118- source [0 ] = source [0 ] + ":"
119- return strings .Join (source , "/" )
133+ return addShareColon (name )
120134}
121135
122136func (n nfsDriver ) mountVolume (name , source , dest string , version int ) error {
0 commit comments