forked from ContainX/docker-volume-netshare
/
nfsccache.go
206 lines (171 loc) · 5.59 KB
/
nfsccache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package drivers
import (
"fmt"
"os"
"path/filepath"
"strconv"
"github.com/docker/go-plugins-helpers/volume"
log "github.com/sirupsen/logrus"
)
const (
NfsCacheOptions = "nfsopts"
DefaultNfsCacheV3 = "port=2049,nolock,proto=tcp"
)
type nfsCacheDriver struct {
cacheDriver
version int
nfsopts map[string]string
lazyUmount bool
timeoutMount int
}
func NewNFSCacheDriver(root string, version int, nfsopts string, mounts *MountManager, path string, state string, lazyUmount bool, timeoutMount int) nfsCacheDriver {
d := nfsCacheDriver{
cacheDriver: newCacheDriver(root, mounts, path, state),
version: version,
nfsopts: map[string]string{},
lazyUmount: lazyUmount,
timeoutMount: timeoutMount,
}
if len(nfsopts) > 0 {
d.nfsopts[NfsCacheOptions] = nfsopts
}
return d
}
func (n nfsCacheDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
log.Debugf("Entering Mount: %v", r)
n.m.Lock()
defer n.m.Unlock()
if _, err := strconv.Atoi(r.Name); err == nil {
if path, ok := n.cacheDriver.dirs[r.Name]["Path"]; ok {
log.Printf("Mount volume: %s with mountpoint: %s", r.Name, path)
return &volume.MountResponse{Mountpoint: path}, nil
}
} else {
resolvedName, resOpts := resolveName(r.Name)
hostdir := mountpoint(n.root, resolvedName)
source := n.fixSource(resolvedName)
// Support adhoc mounts (outside of docker volume create)
// need to adjust source for ShareOpt
if resOpts != nil {
if share, found := resOpts[ShareOpt]; found {
source = n.fixSource(share)
}
}
if n.mountm.HasMount(resolvedName) {
log.Infof("Using existing NFS volume mount: %s", hostdir)
n.mountm.Increment(resolvedName)
regexp1 := "(^|\\s)\\K"
regexp2 := "(?=\\s|$)"
if err := run(fmt.Sprintf("grep -c -P '%s%s%s' /proc/mounts", regexp1, hostdir, regexp2)); err != nil {
log.Infof("Existing NFS volume not mounted, force remount.")
// maintain count
if n.mountm.Count(resolvedName) > 0 {
n.mountm.Decrement(resolvedName)
}
} else {
//n.mountm.Increment(resolvedName)
return &volume.MountResponse{Mountpoint: hostdir}, nil
}
}
log.Infof("Mounting NFS volume %s on %s", source, hostdir)
if err := createDest(hostdir); err != nil {
if n.mountm.Count(resolvedName) > 0 {
n.mountm.Decrement(resolvedName)
}
return nil, err
}
if n.mountm.HasMount(resolvedName) == false {
n.mountm.Create(resolvedName, hostdir, resOpts)
}
n.mountm.Add(resolvedName, hostdir)
if err := n.mountVolume(resolvedName, source, hostdir, n.version, n.timeoutMount); err != nil {
n.mountm.Decrement(resolvedName)
return nil, err
}
if n.mountm.GetOption(resolvedName, ShareOpt) != "" && n.mountm.GetOptionAsBool(resolvedName, CreateOpt) {
log.Infof("Mount: Share and Create options enabled - using %s as sub-dir mount", resolvedName)
datavol := filepath.Join(hostdir, resolvedName)
if err := createDest(filepath.Join(hostdir, resolvedName)); err != nil {
n.mountm.Decrement(resolvedName)
return nil, err
}
hostdir = datavol
}
return &volume.MountResponse{Mountpoint: hostdir}, nil
}
return &volume.MountResponse{Mountpoint: ""}, fmt.Errorf("Can't find volume: %s", r.Name)
}
func (n nfsCacheDriver) Unmount(r *volume.UnmountRequest) error {
log.Debugf("Entering Unmount: %v", r)
n.m.Lock()
defer n.m.Unlock()
if _, err := strconv.Atoi(r.Name); err != nil {
resolvedName, _ := resolveName(r.Name)
hostdir := mountpoint(n.root, resolvedName)
if n.mountm.HasMount(resolvedName) {
if n.mountm.Count(resolvedName) > 1 {
log.Printf("Skipping unmount for %s - in use by other containers", resolvedName)
n.mountm.Decrement(resolvedName)
return nil
}
n.mountm.Decrement(resolvedName)
}
lazy := ""
if n.lazyUmount {
lazy = "-l"
log.Infof("Unmounting volume name %s from %s with -l", resolvedName, hostdir)
} else {
log.Infof("Unmounting volume name %s from %s", resolvedName, hostdir)
}
if err := run(fmt.Sprintf("umount %s %s", lazy, hostdir)); err != nil {
log.Errorf("Error unmounting volume from host: %s", err.Error())
return err
}
n.mountm.DeleteIfNotManaged(resolvedName)
// Check if directory is empty. This command will return "err" if empty
if err := run(fmt.Sprintf("ls -1 %s | grep .", hostdir)); err == nil {
log.Warnf("Directory %s not empty after unmount. Skipping RemoveAll call.", hostdir)
} else {
if err := os.RemoveAll(hostdir); err != nil {
return err
}
}
}
return nil
}
func (n nfsCacheDriver) fixSource(name string) string {
if n.mountm.HasOption(name, ShareOpt) {
return addShareColon(n.mountm.GetOption(name, ShareOpt))
}
return addShareColon(name)
}
func (n nfsCacheDriver) mountVolume(name, source, dest string, version int, timeoutMount int) error {
var cmd string
options := merge(n.mountm.GetOptions(name), n.nfsopts)
opts := ""
if val, ok := options[NfsCacheOptions]; ok {
opts = val
}
mountCmd := "mount"
timeoutCmd := fmt.Sprintf("timeout %d", timeoutMount)
if log.GetLevel() == log.DebugLevel {
mountCmd = mountCmd + " -v"
}
switch version {
case 3:
log.Debugf("Mounting with NFSv3 - src: %s, dest: %s", source, dest)
if len(opts) < 1 {
opts = DefaultNfsCacheV3
}
cmd = fmt.Sprintf("%s %s -t nfs -o %s %s %s", timeoutCmd, mountCmd, opts, source, dest)
default:
log.Debugf("Mounting with NFSv4 - src: %s, dest: %s", source, dest)
if len(opts) > 0 {
cmd = fmt.Sprintf("%s %s -t nfs4 -o %s %s %s", timeoutCmd, mountCmd, opts, source, dest)
} else {
cmd = fmt.Sprintf("%s %s -t nfs4 %s %s", timeoutCmd, mountCmd, source, dest)
}
}
log.Debugf("exec: %s\n", cmd)
return run(cmd)
}