-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
366 lines (296 loc) · 9.15 KB
/
main.go
File metadata and controls
366 lines (296 loc) · 9.15 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/docker/go-plugins-helpers/volume"
version_pkg "github.com/StudioEtrange/docker-volume-bindfs/version"
)
const socketAddress = "/run/docker/plugins/bindfs.sock"
const (
// VolumeDirMode sets the permissions for the volume directory
//VolumeDirMode = 0755
VolumeDirMode = 0700
// VolumeFileMode sets permissions for the volume files
//VolumeFileMode = 0644
VolumeFileMode = 0600
)
type bindfsVolume struct {
Options []string
Sourcepath string
Mountpoint string
connections int
}
type bindfsDriver struct {
//mutex *sync.Mutex
mutex *sync.RWMutex
volumes map[string]*bindfsVolume
volumePath string
statePath string
}
func newBindfsDriver(basePath string) (*bindfsDriver, error) {
log.Infof("Creating a new driver instance %s", basePath)
volumePath := filepath.Join(basePath, "volumes")
statePath := filepath.Join(basePath, "state", "bindfs-state-" + version_pkg.Version + ".json")
if verr := os.MkdirAll(volumePath, VolumeDirMode); verr != nil {
return nil, verr
}
log.Infof("Initialized driver, volumes='%s' state='%s", volumePath, statePath)
driver := &bindfsDriver{
volumes: map[string]*bindfsVolume{},
volumePath: volumePath,
statePath: statePath,
//mutex: &sync.Mutex{},
mutex: &sync.RWMutex{},
}
data, err := ioutil.ReadFile(driver.statePath)
if err != nil {
if os.IsNotExist(err) {
log.Debugf("No state file found at %s", driver.statePath)
} else {
return nil, err
}
} else {
if err := json.Unmarshal(data, &driver.volumes); err != nil {
return nil, err
}
}
return driver, nil
}
func (d *bindfsDriver) saveState() {
data, err := json.Marshal(d.volumes)
if err != nil {
log.Errorf("saveState file failed %s", err)
return
}
if err := ioutil.WriteFile(d.statePath, data, VolumeFileMode); err != nil {
log.Errorf("Failed to write file state %s to %s (%s)", data, d.statePath, err)
}
}
// Driver API
func (d *bindfsDriver) Create(r *volume.CreateRequest) error {
log.Debugf("Create Request %s", r)
d.mutex.Lock()
defer d.mutex.Unlock()
v := &bindfsVolume{}
for key, val := range r.Options {
switch key {
case "sourcePath":
v.Sourcepath = val
default:
if val != "" {
v.Options = append(v.Options, key+"="+val)
} else {
v.Options = append(v.Options, key)
}
}
}
if v.Sourcepath == "" {
msg := fmt.Sprintf("'sourcePath' option required for volume %s", r.Name)
log.Error(msg)
return fmt.Errorf(msg)
}
v.Mountpoint = filepath.Join(d.volumePath, fmt.Sprintf("%x%x", md5.Sum([]byte(r.Name)),md5.Sum([]byte(v.Sourcepath))))
v.connections = 0
d.volumes[r.Name] = v
d.saveState()
return nil
}
func (d *bindfsDriver) List() (*volume.ListResponse, error) {
log.Debugf("List Request")
d.mutex.Lock()
defer d.mutex.Unlock()
var vols []*volume.Volume
for name, v := range d.volumes {
vols = append(vols,
&volume.Volume{Name: name, Mountpoint: v.Mountpoint})
}
return &volume.ListResponse{Volumes: vols}, nil
}
func (d *bindfsDriver) Get(r *volume.GetRequest) (*volume.GetResponse, error) {
log.Debugf("Get Request %s", r)
d.mutex.Lock()
defer d.mutex.Unlock()
v, ok := d.volumes[r.Name]
if !ok {
msg := fmt.Sprintf("Failed to get volume %s because it doesn't exists", r.Name)
log.Debug(msg)
return &volume.GetResponse{}, fmt.Errorf(msg)
}
return &volume.GetResponse{Volume: &volume.Volume{Name: r.Name, Mountpoint: v.Mountpoint}}, nil
}
func (d *bindfsDriver) Remove(r *volume.RemoveRequest) error {
log.Debugf("Remove Request %s", r)
d.mutex.Lock()
defer d.mutex.Unlock()
v, ok := d.volumes[r.Name]
if !ok {
msg := fmt.Sprintf("Failed to remove volume %s because it doesn't exists", r.Name)
log.Error(msg)
return fmt.Errorf(msg)
}
if v.connections > 0 {
msg := fmt.Sprintf("Can't remove volume %s because it is mounted by %d containers", r.Name, v.connections)
log.Error(msg)
return fmt.Errorf(msg)
}
msg := fmt.Sprintf("calling os.Remove method on mountpoint folder %s which was mapped to folder %s for volume named %s",v.Mountpoint, v.Sourcepath, r.Name )
log.Debugf(msg)
if err := d.removeVolume(v); err != nil {
return err
}
delete(d.volumes, r.Name)
d.saveState()
return nil
}
func (d *bindfsDriver) Path(r *volume.PathRequest) (*volume.PathResponse, error) {
log.Debugf("Path Request %s", r)
d.mutex.RLock()
defer d.mutex.RUnlock()
v, ok := d.volumes[r.Name]
if !ok {
msg := fmt.Sprintf("Failed to find path for volume %s because volume doesn't exists", r.Name)
log.Error(msg)
return &volume.PathResponse{}, fmt.Errorf(msg)
}
return &volume.PathResponse{Mountpoint: v.Mountpoint}, nil
}
func (d *bindfsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
log.Debugf("Mount Request %s", r)
d.mutex.Lock()
defer d.mutex.Unlock()
v, ok := d.volumes[r.Name]
if !ok {
msg := fmt.Sprintf("Failed to mount volume %s because volume doesn't exists", r.Name)
log.Error(msg)
return &volume.MountResponse{}, fmt.Errorf(msg)
}
if v.connections == 0 {
fi, err := os.Lstat(v.Mountpoint)
if os.IsNotExist(err) {
if err := os.MkdirAll(v.Mountpoint, VolumeDirMode); err != nil {
msg := fmt.Sprintf("Error MkdirAll for %s, %s", r.Name, err)
log.Error(msg)
return &volume.MountResponse{}, fmt.Errorf(msg)
}
} else if err != nil {
msg := fmt.Sprintf("Error %s, %s", r.Name, err)
log.Error(msg)
return &volume.MountResponse{}, fmt.Errorf(msg)
}
if fi != nil && !fi.IsDir() {
msg := fmt.Sprintf("%v already exist and it's not a directory", v.Mountpoint)
log.Error(msg)
return &volume.MountResponse{}, fmt.Errorf(msg)
}
if err := d.mountVolume(v); err != nil {
msg := fmt.Sprintf("Failed to mount %s, %s", r.Name, err)
log.Error(msg)
return &volume.MountResponse{}, fmt.Errorf(msg)
}
}
v.connections++
d.saveState()
return &volume.MountResponse{Mountpoint: v.Mountpoint}, nil
}
func (d *bindfsDriver) Unmount(r *volume.UnmountRequest) error {
log.Debugf("Umount Request %s", r)
d.mutex.Lock()
defer d.mutex.Unlock()
v, ok := d.volumes[r.Name]
if !ok {
msg := fmt.Sprintf("Failed to unmount volume %s because it doesn't exists", r.Name)
log.Error(msg)
return fmt.Errorf(msg)
}
v.connections--
if v.connections <= 0 {
if err := d.unmountVolume(v); err != nil {
return err
}
v.connections = 0
}
d.saveState()
return nil
}
func (d *bindfsDriver) Capabilities() *volume.CapabilitiesResponse {
log.Debugf("Capabilities Request")
return &volume.CapabilitiesResponse{Capabilities: volume.Capability{Scope: "local"}}
}
// Helper methods ---------------------------------------------------
func (d *bindfsDriver) removeVolume(v *bindfsVolume) error {
log.Debugf("removeVolume %#v", v)
// Remove MountPoint
// https://github.com/vieux/docker-volume-sshfs/commit/e9c9710a9a7c049cd92a1facd442e53cf5dbc459
// Use os.remove instead of os.RemoveAll to ensure ensure that after unmount no files exists in the mountpoint
// https://pkg.go.dev/os@go1.20.1#Remove
// Remove removes the named file or (empty) directory. If there is an error, it will be of type *PathError.
// if err := os.RemoveAll(v.Mountpoint); err != nil {
// msg := fmt.Sprintf("error when calling os.RemoveAll method on mountpoint folder %s which was mapped to folder %s",v.Mountpoint, v.Sourcepath,)
// log.Error(msg)
// return fmt.Errorf(msg)
// }
// If the Mountpoint directory exist, remove it
if _, err := os.Stat(v.Mountpoint); !os.IsNotExist(err) {
// Else remove everything in that mountpoint
if err := os.Remove(v.Mountpoint); err != nil {
// If the mount is not mounted, remove legacy
msg := fmt.Sprintf("Failed to remove the volume with sourcePath mountpoint %s (%s)", v.Sourcepath, v.Mountpoint, err)
log.Error(msg)
return fmt.Errorf(msg)
}
}
return nil
}
func (d *bindfsDriver) mountVolume(v *bindfsVolume) error {
log.Debugf("mountVolume %#v", v)
cmd := exec.Command("bindfs", "/mnt/host" + v.Sourcepath, v.Mountpoint)
for _, option := range v.Options {
cmd.Args = append(cmd.Args, "-o", option)
}
log.Debugf("Executing mount command %v", cmd)
log.Debug(cmd.Args)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("bindfs command failed %v %v (%s)", cmd, err, output)
}
return nil
}
func (d *bindfsDriver) unmountVolume(v *bindfsVolume) error {
log.Debugf("unmountVolume %#v", v)
cmd := fmt.Sprintf("umount %s", v.Mountpoint)
log.Debugf("Executing umount command %v", cmd)
if err := exec.Command("sh", "-c", cmd).Run(); err != nil {
return err
}
// Check that the mountpoint is empty
files, err := ioutil.ReadDir(v.Mountpoint)
if err != nil {
return err
}
if len(files) > 0 {
return fmt.Errorf("after unmount %d files still exists in %s", len(files), v.Mountpoint)
}
return nil
}
// Main ---------------------------------------------------
func main() {
debug := os.Getenv("DEBUG")
if ok, _ := strconv.ParseBool(debug); ok {
log.SetLevel(log.DebugLevel)
}
d, err := newBindfsDriver("/mnt")
if err != nil {
log.Fatal(err)
}
h := volume.NewHandler(d)
log.Infof("plugin bindfs listening on %s", socketAddress)
log.Error(h.ServeUnix(socketAddress, 0))
}