forked from virtuozzo/docker-volume-ploop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
389 lines (328 loc) · 8.67 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"sync"
"github.com/Sirupsen/logrus"
"github.com/docker/go-plugins-helpers/volume"
"github.com/docker/go-units"
"github.com/kolyshkin/goploop"
)
/* Driver options:
* - home path (required)
* - debug level
* - defaults (all optional)
* - volume size
* - ploop format (expanded/preallocated/raw)
* - cluster block log size in 512-byte sectors,
* (values are from 6 to 15, default is 11: 2^11 * 512 = 1 MB)
*
* Volume options (for description see above):
* - size (optional)
* - format
* - cluster block size
*/
type volumeOptions struct {
size uint64 // ploop image size, in kilobytes
mode ploop.ImageMode // ploop image format (expanded/prealloc/raw)
clog uint // cluster block log size in 512-byte sectors
tier int8 // Virtuozzo storage tier (-1: use default)
scope string // Volume scope (global/local/auto)
}
type mount struct {
count int32
device string
}
type ploopDriver struct {
home string
work string
opts volumeOptions
mountsM sync.RWMutex
mounts map[string]*mount
}
func (o *volumeOptions) setSize(str string) error {
sizeBytes, err := units.RAMInBytes(str)
if err != nil {
return fmt.Errorf("Can't parse size %s: %s", str, err)
}
o.size = uint64(sizeBytes >> 10) // convert to KB
return nil
}
func (o *volumeOptions) setMode(str string) error {
mode, err := ploop.ParseImageMode(str)
if err != nil {
return fmt.Errorf("Can't parse mode %s: %s", str, err)
}
o.mode = mode
return nil
}
func (o *volumeOptions) setCLog(str string) error {
clog, err := strconv.ParseUint(str, 0, 32)
if err != nil {
return fmt.Errorf("Can't parse clog %s: %s", str, err)
}
o.clog = uint(clog)
return nil
}
func (o *volumeOptions) setTier(str string) error {
tier, err := strconv.ParseInt(str, 0, 8)
if err != nil {
return fmt.Errorf("Can't parse tier %s: %s", str, err)
}
o.tier = int8(tier)
return nil
}
func (o *volumeOptions) setScope(str string) error {
// FIXME: check for local/global/auto?
o.scope = str
return nil
}
func newPloopDriver(home, work string, opts *volumeOptions) *ploopDriver {
// home must exist
_, err := os.Stat(home)
if err != nil {
if os.IsNotExist(err) {
logrus.Fatalf("Error %s", err)
} else {
logrus.Fatalf("Unexpected error from stat(%s): %s", home, err)
}
}
// Autodetect scope: global if home is on vstorage, local otherwise
if opts.scope == "auto" {
if isOnVstorage(home) {
opts.scope = "global"
} else {
opts.scope = "local"
}
logrus.Infof("Autodetecting driver scope: %s", opts.scope)
}
d := ploopDriver{
home: home,
work: work,
opts: *opts,
mounts: make(map[string]*mount),
}
// Make sure to create base paths we'll use
err = os.MkdirAll(d.dir(""), 0700)
if err != nil {
logrus.Fatalf("Error %s", err)
}
err = os.MkdirAll(d.mnt(""), 0700)
if err != nil {
logrus.Fatalf("Error %s", err)
}
return &d
}
func (d *ploopDriver) Create(r volume.Request) volume.Response {
// check if it already exists
dd := d.dd(r.Name)
_, err := os.Stat(dd)
if err == nil {
// volume already exists
return volume.Response{}
}
if !os.IsNotExist(err) {
logrus.Errorf("Unexpected error from stat(): %s", err)
return volume.Response{Err: err.Error()}
}
// Parse options
o := d.opts
if val, ok := r.Options["size"]; ok {
err := o.setSize(val)
if err != nil {
logrus.Errorf(err.Error())
return volume.Response{Err: err.Error()}
}
}
if val, ok := r.Options["mode"]; ok {
err := o.setMode(val)
if err != nil {
logrus.Errorf(err.Error())
return volume.Response{Err: err.Error()}
}
}
if val, ok := r.Options["clog"]; ok {
err := o.setCLog(val)
if err != nil {
logrus.Errorf(err.Error())
return volume.Response{Err: err.Error()}
}
}
if val, ok := r.Options["tier"]; ok {
err := o.setTier(val)
if err != nil {
logrus.Errorf(err.Error())
return volume.Response{Err: err.Error()}
}
}
logrus.Debugf("Creating volume %s", r.Name)
// Create containing directory
dir := d.dir(r.Name)
err = os.Mkdir(dir, 0700)
if err != nil {
return volume.Response{Err: err.Error()}
}
// set storage tier
if err := vstorageSetTier(dir, o.tier); err != nil {
logrus.Warnf("Can't set tier %d: %s", o.tier, err)
}
// Create an image
file := d.img(r.Name)
cp := ploop.CreateParam{Size: o.size, Mode: o.mode, File: file, CLog: o.clog, Flags: ploop.NoLazy}
if err := ploop.Create(&cp); err != nil {
logrus.Errorf("Can't create ploop image: %s", err)
os.RemoveAll(dir)
return volume.Response{Err: err.Error()}
}
// all went well
return volume.Response{}
}
func (d *ploopDriver) Remove(r volume.Request) volume.Response {
logrus.Debugf("Removing volume %s", r.Name)
/* The ploop image to be removed might be mounted.
* The question is, what is the more correct thing to do:
* 1. Auto-unmount and proceed
* 2. Reject removing mounted image
*/
p, err := ploop.Open(d.dd(r.Name))
if err == nil {
if m, _ := p.IsMounted(); m {
//err := fmt.Error("Rejecting to remove mounted image %s\n", r.Name)
logrus.Error(err)
return volume.Response{Err: err.Error()}
/*
err = p.Umount()
if err != nil && !ploop.IsNotMounted(err) {
logrus.Errorf("Can't umount %s: %s", r.Name, err)
return volume.Response{Err: err.Error()}
}
*/
}
p.Close()
}
// Proceed with removal
err = os.RemoveAll(d.dir(r.Name))
if err != nil {
logrus.Error(err)
return volume.Response{Err: err.Error()}
}
// all went well
return volume.Response{}
}
func (d *ploopDriver) Mount(r volume.MountRequest) volume.Response {
logrus.Debugf("Mounting volume %s", r.Name)
p, err := ploop.Open(d.dd(r.Name))
if err != nil {
logrus.Errorf("Can't open ploop: %s", err)
return volume.Response{Err: err.Error()}
}
defer p.Close()
mnt := d.mnt(r.Name)
err = os.Mkdir(mnt, 0700)
if err != nil && !os.IsExist(err) {
logrus.Errorf("Error %s", err)
return volume.Response{Err: err.Error()}
}
mp := ploop.MountParam{Target: mnt}
dev, err := p.Mount(&mp)
if err != nil {
logrus.Errorf("Can't mount ploop: %s", err)
return volume.Response{Err: err.Error()}
}
logrus.Debugf("Mounted %s to %s (dev=%s)", r.Name, d.mnt(r.Name), dev)
// all went well
return volume.Response{Mountpoint: mnt}
}
func (d *ploopDriver) Unmount(r volume.UnmountRequest) volume.Response {
logrus.Debugf("Unmounting volume %s", r.Name)
p, err := ploop.Open(d.dd(r.Name))
if err != nil {
logrus.Errorf("Can't open ploop: %s", err)
return volume.Response{Err: err.Error()}
}
defer p.Close()
if m, _ := p.IsMounted(); !m {
// not mounted, nothing to do
return volume.Response{}
}
err = p.Umount()
// ignore "is not mounted" error
if err != nil && !ploop.IsNotMounted(err) {
logrus.Errorf("Can't unmount ploop: %s", err)
return volume.Response{Err: err.Error()}
}
// all went well
return volume.Response{}
}
func (d *ploopDriver) Get(r volume.Request) volume.Response {
logrus.Debugf("Called Get(%s)", r.Name)
exist, err := d.volExist(r.Name)
if err != nil {
return volume.Response{Err: err.Error()}
}
if !exist {
// no such volume
return volume.Response{Err: "Can't find volume"}
}
// TODO: check if it's mounted
return volume.Response{Volume: &volume.Volume{Name: r.Name, Mountpoint: d.mnt(r.Name)}}
}
func (d *ploopDriver) List(r volume.Request) volume.Response {
logrus.Debugf("Called List()")
dir := d.dir("")
files, err := ioutil.ReadDir(dir)
if err != nil {
logrus.Errorf("Can't list directory %s: %s", dir, err)
return volume.Response{Err: err.Error()}
}
vols := make([]*volume.Volume, 0, len(files))
for _, f := range files {
if f.IsDir() {
name := f.Name()
// Check if DiskDescriptor.xml is there
exist, _ := d.volExist(name)
if !exist {
continue
}
vol := &volume.Volume{
Name: name,
Mountpoint: d.mnt(name),
}
vols = append(vols, vol)
}
}
return volume.Response{Volumes: vols}
}
func (d *ploopDriver) Path(r volume.Request) volume.Response {
logrus.Debugf("Called Path (%s)", r.Name)
exist, err := d.volExist(r.Name)
if err != nil {
return volume.Response{Err: err.Error()}
}
if !exist {
return volume.Response{Err: "Can't find volume"}
}
// TODO: check if mounted?
return volume.Response{Mountpoint: d.mnt(r.Name)}
}
func (d *ploopDriver) Capabilities(r volume.Request) volume.Response {
return volume.Response{
Capabilities: volume.Capability{
Scope: d.opts.scope}}
}
// Check if a given volume exist
func (d *ploopDriver) volExist(name string) (bool, error) {
dd := d.dd(name)
_, err := os.Stat(dd)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
// no such volume
return false, nil
}
logrus.Errorf("Unexpected error from stat(%s): %s", dd, err)
return false, err
}