-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.go
274 lines (226 loc) · 6.14 KB
/
driver.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
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
package main
import (
"encoding/gob"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"github.com/docker/go-plugins-helpers/volume"
)
// TODO: Separate the filesystems into libraries
const (
fsAUFS = iota
fsOverlay
)
type unionMountVolume struct {
Filesystem int
Layers []string
MountPoint string
RefCount uint
m sync.Mutex
}
type unionMountDriver struct {
RootDir string
DefaultFS int
Volumes map[string]*unionMountVolume
m sync.Mutex
}
func newUnionMountDriver(rootDir string, defaultFS int) *unionMountDriver {
return &unionMountDriver{
RootDir: rootDir,
DefaultFS: defaultFS,
Volumes: make(map[string]*unionMountVolume),
}
}
func (d *unionMountDriver) saveState() {
d.m.Lock()
defer d.m.Unlock()
path := filepath.Join(d.RootDir, "state.gob")
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
// TODO: Log error
return
}
defer file.Close()
enc := gob.NewEncoder(file)
if err := enc.Encode(d); err != nil {
// TODO: Log error
}
}
func (d *unionMountDriver) mountPoint(volName string) string {
return filepath.Join(d.RootDir, "volumes", volName)
}
func (d *unionMountDriver) Create(r volume.Request) volume.Response {
d.m.Lock()
defer d.m.Unlock()
// Try to read the layers option
layers := make([]string, 0)
if str, ok := r.Options["layers"]; ok && len(str) > 0 {
layers = strings.Split(str, ":")
// Check if paths are absolute
// REVIEW: the possibility of layering docker's named volumes
for _, path := range layers {
if !filepath.IsAbs(path) {
return volume.Response{Err: fmt.Sprintf("layer path \"%s\" is not relative", path)}
}
if _, err := os.Stat(path); os.IsNotExist(err) {
return volume.Response{Err: fmt.Sprintf("layer path \"%s\" does not exist", path)}
}
}
} else {
return volume.Response{Err: "no layers defined"}
}
// Try to read the filesystem option
filesystem := d.DefaultFS
if str, ok := r.Options["filesystem"]; ok {
if d, err := fsFromString(str); err == nil {
filesystem = d
} else {
return volume.Response{Err: err.Error()}
}
}
// FIXME: Support multiple layers for overlay
if filesystem == fsOverlay && len(layers) > 1 {
return volume.Response{Err: "multiple layers with the overlay filesystem is not implemented"}
}
// Create Mount Point
mountPoint := d.mountPoint(r.Name)
if err := os.MkdirAll(mountPoint, 0755); err != nil {
return volume.Response{Err: err.Error()}
}
// Check for duplicate volume name
if _, ok := d.Volumes[r.Name]; ok {
return volume.Response{Err: fmt.Sprintf("volume \"%s\" already exists", r.Name)}
}
d.Volumes[r.Name] = &unionMountVolume{
Filesystem: filesystem,
Layers: layers,
MountPoint: mountPoint,
}
go d.saveState()
return volume.Response{}
}
func (d *unionMountDriver) Remove(r volume.Request) volume.Response {
d.m.Lock()
defer d.m.Unlock()
// Check if volume exists
vol, ok := d.Volumes[r.Name]
if !ok {
return volume.Response{Err: fmt.Sprintf("volume (%s) does not exist", r.Name)}
}
vol.m.Lock()
defer vol.m.Unlock()
if vol.RefCount == 0 {
if err := os.RemoveAll(vol.MountPoint); err != nil {
return volume.Response{Err: fmt.Sprintf("error removing volume path '%s'", vol.MountPoint)}
}
delete(d.Volumes, r.Name)
}
go d.saveState()
return volume.Response{}
}
func (d *unionMountDriver) Mount(r volume.MountRequest) volume.Response {
// Check if volume exists
d.m.Lock()
vol, ok := d.Volumes[r.Name]
d.m.Unlock()
if !ok {
return volume.Response{Err: fmt.Sprintf("volume (%s) does not exist", r.Name)}
}
vol.m.Lock()
defer vol.m.Unlock()
// Mount Volume if not already mounted
if vol.RefCount == 0 {
cmd, _ := mountCmd(vol)
err := exec.Command("sh", "-c", cmd).Run()
if err != nil {
return volume.Response{Err: err.Error()}
}
}
vol.RefCount++
return volume.Response{Mountpoint: vol.MountPoint}
}
func (d *unionMountDriver) Path(r volume.Request) volume.Response {
// Check if volume exists
d.m.Lock()
vol, ok := d.Volumes[r.Name]
d.m.Unlock()
if !ok {
return volume.Response{Err: fmt.Sprintf("volume (%s) does not exist", r.Name)}
}
return volume.Response{Mountpoint: vol.MountPoint}
}
func (d *unionMountDriver) Unmount(r volume.UnmountRequest) volume.Response {
// Check if volume exists
d.m.Lock()
vol, ok := d.Volumes[r.Name]
d.m.Unlock()
if !ok {
return volume.Response{Err: fmt.Sprintf("volume (%s) does not exist", r.Name)}
}
vol.m.Lock()
defer vol.m.Unlock()
if vol.RefCount == 1 {
exec.Command("sh", "-c", fmt.Sprintf("umount -f %s", vol.MountPoint)).Run()
} else if vol.RefCount == 0 {
return volume.Response{Err: fmt.Sprintf("volume (%s) is not mounted", r.Name)}
}
vol.RefCount--
return volume.Response{}
}
func (d *unionMountDriver) Get(r volume.Request) volume.Response {
// Check if volume exists
d.m.Lock()
vol, ok := d.Volumes[r.Name]
d.m.Unlock()
if !ok {
return volume.Response{Err: fmt.Sprintf("volume (%s) does not exist", r.Name)}
}
return volume.Response{
Volume: &volume.Volume{
Name: r.Name,
Mountpoint: vol.MountPoint,
},
}
}
func (d *unionMountDriver) List(r volume.Request) volume.Response {
d.m.Lock()
defer d.m.Unlock()
volumes := []*volume.Volume{}
for n, v := range d.Volumes {
volumes = append(volumes, &volume.Volume{
Name: n,
Mountpoint: v.MountPoint,
})
}
return volume.Response{Volumes: volumes}
}
func (d *unionMountDriver) Capabilities(r volume.Request) volume.Response {
return volume.Response{
Capabilities: volume.Capability{
Scope: "local",
},
}
}
func fsFromString(fs string) (int, error) {
switch strings.ToLower(fs) {
case "aufs":
return fsAUFS, nil
case "overlay", "overlayfs":
return fsOverlay, nil
default:
return fsAUFS, fmt.Errorf("unsupported filesystem")
}
}
func mountCmd(v *unionMountVolume) (string, error) {
switch v.Filesystem {
case fsAUFS:
return fmt.Sprintf("mount -t aufs -o br=%s:%s none %s", v.MountPoint, strings.Join(v.Layers, ":"), v.MountPoint), nil
case fsOverlay:
return fmt.Sprintf("mount -t overlay overlay -o lowerdir=%s,upperdir=%s %s", v.Layers[0], v.MountPoint, v.MountPoint), nil
default:
return "", fmt.Errorf("undefined or unsupported filesystem")
}
}