-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdriver.go
679 lines (567 loc) · 17.2 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
//-----------------------------------------------------------------------------
// Package membership:
//-----------------------------------------------------------------------------
package main
//-----------------------------------------------------------------------------
// Imports:
//-----------------------------------------------------------------------------
import (
// Standard library:
"errors"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
// Community:
dkvolume "github.com/docker/go-plugins-helpers/volume"
)
//-----------------------------------------------------------------------------
// Package constant declarations:
//-----------------------------------------------------------------------------
const lockID = "dockerLock"
//-----------------------------------------------------------------------------
// Package variable declarations factored into a block:
//-----------------------------------------------------------------------------
var (
commands = [...]string{"modprobe", "rbd", "mount", "umount"}
nameRegex = regexp.MustCompile(`^(([-_.[:alnum:]]+)/)?([-_.[:alnum:]]+)(@([0-9]+))?$`)
lockRegex = regexp.MustCompile(`^(client.[0-9]+) ` + lockID)
)
//-----------------------------------------------------------------------------
// Structs definitions:
//-----------------------------------------------------------------------------
type volume struct {
name string
device string
locker string
fstype string
pool string
}
type rbdDriver struct {
volRoot string
defPool string
defFsType string
defSize int
cmd map[string]string
volumes map[string]*volume
}
//-----------------------------------------------------------------------------
// initDriver
//-----------------------------------------------------------------------------
func initDriver(volRoot, defPool, defFsType string, defSize int) rbdDriver {
// Variables
var err error
cmd := make(map[string]string)
// Search for binaries
for _, i := range commands {
cmd[i], err = exec.LookPath(i)
if err != nil {
log.Fatal("[Init] ERROR make sure binary %s is in your PATH", i)
}
}
// Load RBD kernel module
log.Printf("[Init] INFO loading RBD kernel module...")
if err = exec.Command(cmd["modprobe"], "rbd").Run(); err != nil {
log.Fatal("[Init] ERROR unable to load RBD kernel module")
}
// Initialize the struct
driver := rbdDriver{
volRoot: volRoot,
defPool: defPool,
defFsType: defFsType,
defSize: defSize,
cmd: cmd,
volumes: map[string]*volume{},
}
return driver
}
//-----------------------------------------------------------------------------
// /VolumeDriver.Create
//
// Request:
//
// {
// "Name": "volume_name",
// "Opts": {}
// }
//
// Instruct the plugin that the user wants to create a volume, given a user
// specified volume name. The plugin does not need to actually manifest the
// volume on the filesystem yet (until Mount is called). Opts is a map of
// driver specific options passed through from the user request.
//
// Response:
//
// {
// "Err": ""
// }
//
// Respond with a string error if an error occurred.
//-----------------------------------------------------------------------------
func (d *rbdDriver) Create(r dkvolume.Request) dkvolume.Response {
// Parse the docker --volume option
pool, name, size, err := d.parsePoolNameSize(r.Name)
if err != nil {
log.Printf("[Create] ERROR parsing volume: %s", err)
return dkvolume.Response{Err: err.Error()}
}
// Check if volume already exists
mountpoint := filepath.Join(d.volRoot, pool, name)
if _, found := d.volumes[mountpoint]; found {
log.Println("[Create] INFO volume is already in known mounts: " + mountpoint)
return dkvolume.Response{}
}
// Create RBD image if not exists
if exists, err := d.imageExists(pool, name); !exists && err == nil {
log.Println("[Create] INFO image does not exists. Creating it now...")
if err = d.createImage(pool, name, d.defFsType, size); err != nil {
return dkvolume.Response{Err: err.Error()}
}
} else if err != nil {
log.Printf("[Create] ERROR checking for RBD Image: %s", err)
return dkvolume.Response{Err: err.Error()}
}
return dkvolume.Response{}
}
//-----------------------------------------------------------------------------
// /VolumeDriver.Remove
//
// Request:
//
// {
// "Name": "volume_name"
// }
//
// Delete the specified volume from disk. This request is issued when a user
// invokes docker rm -v to remove volumes associated with a container.
//
// Response:
//
// {
// "Err": ""
// }
//
// Respond with a string error if an error occurred.
//-----------------------------------------------------------------------------
func (d *rbdDriver) Remove(r dkvolume.Request) dkvolume.Response {
return dkvolume.Response{}
}
//-----------------------------------------------------------------------------
// /VolumeDriver.Path
//
// Request:
//
// {
// "Name": "volume_name"
// }
//
// Docker needs reminding of the path to the volume on the host.
//
// Response:
//
// {
// "Mountpoint": "/path/to/directory/on/host",
// "Err": ""
// }
//
// Respond with the path on the host filesystem where the volume has been
// made available, and/or a string error if an error occurred.
//-----------------------------------------------------------------------------
func (d *rbdDriver) Path(r dkvolume.Request) dkvolume.Response {
// Parse the docker --volume option
pool, name, _, err := d.parsePoolNameSize(r.Name)
if err != nil {
log.Printf("[Path] ERROR parsing volume: %s", err)
return dkvolume.Response{Err: err.Error()}
}
mountpoint := filepath.Join(d.volRoot, pool, name)
return dkvolume.Response{Mountpoint: mountpoint}
}
//-----------------------------------------------------------------------------
// /VolumeDriver.Mount
//
// Request:
//
// {
// "Name": "volume_name"
// }
//
// Docker requires the plugin to provide a volume, given a user specified
// volume name. This is called once per container start. If the same
// volume_name is requested more than once, the plugin may need to keep track
// of each new mount request and provision at the first mount request and
// deprovision at the last corresponding unmount request.
//
// Response:
//
// {
// "Mountpoint": "/path/to/directory/on/host",
// "Err": null
// }
//
// Respond with the path on the host filesystem where the volume has been
// made available, and/or a string error if an error occurred.
//-----------------------------------------------------------------------------
func (d *rbdDriver) Mount(r dkvolume.Request) dkvolume.Response {
// Parse the docker --volume option
pool, name, _, err := d.parsePoolNameSize(r.Name)
if err != nil {
log.Printf("[Mount] ERROR parsing volume: %s", err)
return dkvolume.Response{Err: err.Error()}
}
// Add image lock
log.Printf("[Mount] INFO locking image %s", name)
locker, err := d.lockImage(pool, name, lockID)
if err != nil {
log.Printf("[Mount] ERROR locking image: %s", err)
return dkvolume.Response{Err: err.Error()}
}
// Map the image to a kernel device
log.Printf("[Mount] INFO mapping image %s", name)
device, err := d.mapImage(pool, name)
if err != nil {
defer d.unlockImage(pool, name, lockID, locker)
log.Printf("[Mount] ERROR mapping image: %s", err)
return dkvolume.Response{Err: err.Error()}
}
// Create mountpoint
mountpoint := filepath.Join(d.volRoot, pool, name)
log.Printf("[Mount] INFO creating %s", mountpoint)
err = os.MkdirAll(mountpoint, os.ModeDir|os.FileMode(int(0775)))
if err != nil {
defer d.unmapImage(device)
defer d.unlockImage(pool, name, lockID, locker)
log.Printf("[Mount] ERROR creating mount point: %s", err)
return dkvolume.Response{Err: err.Error()}
}
// Mount the device
log.Printf("[Mount] INFO mounting device %s", device)
if err = d.mountDevice(device, mountpoint, d.defFsType); err != nil {
defer d.unmapImage(device)
defer d.unlockImage(pool, name, lockID, locker)
log.Printf("[Mount] ERROR mounting device: %s", err)
return dkvolume.Response{Err: err.Error()}
}
// Add to list of volumes
d.volumes[mountpoint] = &volume{
name: name,
device: device,
locker: locker,
fstype: d.defFsType,
pool: pool,
}
return dkvolume.Response{Mountpoint: mountpoint}
}
//-----------------------------------------------------------------------------
// /VolumeDriver.Unmount
//
// Request:
//
// {
// "Name": "volume_name"
// }
//
// Indication that Docker no longer is using the named volume. This is called
// once per container stop. Plugin may deduce that it is safe to deprovision
// it at this point.
//
// Response:
//
// {
// "Err": ""
// }
//
// Respond with a string error if an error occurred.
//-----------------------------------------------------------------------------
func (d *rbdDriver) Unmount(r dkvolume.Request) dkvolume.Response {
// Parse the docker --volume option
pool, name, _, err := d.parsePoolNameSize(r.Name)
if err != nil {
log.Printf("[Unmount] ERROR parsing volume: %s", err)
return dkvolume.Response{Err: err.Error()}
}
// Retrieve volume state
mountpoint := filepath.Join(d.volRoot, pool, name)
vol, found := d.volumes[mountpoint]
if !found {
err = errors.New("No state found")
log.Printf("[Unmount] ERROR retrieving state: %s", err)
return dkvolume.Response{Err: err.Error()}
}
// Unmount the device
log.Printf("[Unmount] INFO unmounting device %s", vol.device)
if err := d.unmountDevice(vol.device); err != nil {
log.Printf("[Unmount] ERROR unmounting device: %s", err)
return dkvolume.Response{Err: err.Error()}
}
// Unmap the image
log.Printf("[Unmount] INFO unmapping image %s", name)
if err = d.unmapImage(vol.device); err != nil {
log.Printf("[Unmount] ERROR unmapping image: %s", err)
return dkvolume.Response{Err: err.Error()}
}
// Unlock the image
log.Printf("[Unmount] INFO unlocking image %s", name)
if err = d.unlockImage(vol.pool, vol.name, lockID, vol.locker); err != nil {
log.Printf("[Unmount] ERROR unlocking image: %s", err)
return dkvolume.Response{Err: err.Error()}
}
// Forget the volume
delete(d.volumes, mountpoint)
return dkvolume.Response{}
}
//-----------------------------------------------------------------------------
// /VolumeDriver.Get
//
// Request:
//
// {
// "Name": "volume_name"
// }
//
// Get the volume info.
//
// Response:
//
// {
// "Volume": {
// "Name": "volume_name",
// "Mountpoint": "/path/to/directory/on/host",
// },
// "Err": ""
// }
//
// Respond with a string error if an error occurred.
//-----------------------------------------------------------------------------
func (d *rbdDriver) Get(r dkvolume.Request) dkvolume.Response {
return dkvolume.Response{}
}
//-----------------------------------------------------------------------------
// /VolumeDriver.List
//
// Request:
//
// {}
//
// Get the list of volumes registered with the plugin.
//
// Response:
//
// {
// "Volumes": [
// {
// "Name": "volume_name",
// "Mountpoint": "/path/to/directory/on/host"
// }
// ],
// "Err": ""
// }
//
// Respond with a string error if an error occurred.
//-----------------------------------------------------------------------------
func (d *rbdDriver) List(r dkvolume.Request) dkvolume.Response {
return dkvolume.Response{}
}
//-----------------------------------------------------------------------------
// parsePoolNameSize
//-----------------------------------------------------------------------------
func (d *rbdDriver) parsePoolNameSize(src string) (string, string, int, error) {
sub := nameRegex.FindStringSubmatch(src)
if len(sub) != 6 {
return "", "", 0, errors.New("Unable to parse docker --volume option: %s" + src)
}
// Set defaults
pool := d.defPool
name := sub[3]
size := d.defSize
// Pool overwrite
if sub[2] != "" {
pool = sub[2]
}
// Size overwrite
if sub[5] != "" {
var err error
size, err = strconv.Atoi(sub[5])
if err != nil {
size = d.defSize
}
}
return pool, name, size, nil
}
//-----------------------------------------------------------------------------
// imageExists
//-----------------------------------------------------------------------------
func (d *rbdDriver) imageExists(pool, name string) (bool, error) {
// List RBD images
out, err := exec.Command(d.cmd["rbd"], "ls", pool).Output()
if err != nil {
return false, errors.New("Unable to list images")
}
// Parse the output
list := strings.Split(string(out), "\n")
for _, item := range list {
if item == name {
return true, nil
}
}
return false, nil
}
//-----------------------------------------------------------------------------
// createImage
//-----------------------------------------------------------------------------
func (d *rbdDriver) createImage(pool, name, fstype string, size int) error {
// Create the image device
err := exec.Command(
d.cmd["rbd"], "create",
"--pool", pool,
"--size", strconv.Itoa(size),
name,
).Run()
if err != nil {
return errors.New("Unable to create the image device")
}
// Add image lock
locker, err := d.lockImage(pool, name, lockID)
if err != nil {
return err
}
// Map the image to a kernel device
device, err := d.mapImage(pool, name)
if err != nil {
defer d.unlockImage(pool, name, lockID, locker)
return err
}
// Make the filesystem
if err = d.makeFs(device, d.defFsType); err != nil {
defer d.unmapImage(device)
defer d.unlockImage(pool, name, lockID, locker)
return err
}
// Unmap the image from kernel device
if err = d.unmapImage(device); err != nil {
return err
}
// Remove image lock
if err = d.unlockImage(pool, name, lockID, locker); err != nil {
return err
}
return nil
}
//-----------------------------------------------------------------------------
// lockImage
//-----------------------------------------------------------------------------
func (d *rbdDriver) lockImage(pool, name, lockID string) (string, error) {
// Lock the image
err := exec.Command(
d.cmd["rbd"], "lock",
"add", "--pool", pool,
name, lockID,
).Run()
if err != nil {
return "", errors.New("Unable to lock the image")
}
// List the locks
out, err := exec.Command(
d.cmd["rbd"], "lock", "list",
"--pool", pool, name,
).Output()
if err != nil {
return "", errors.New("Unable to list the image locks")
}
// Parse the locker ID
lines := strings.Split(string(out), "\n")
if len(lines) > 1 {
for _, line := range lines[1:] {
sub := lockRegex.FindStringSubmatch(line)
if len(sub) == 2 {
return sub[1], nil
}
}
}
return "", errors.New("Unable to parse locker ID")
}
//-----------------------------------------------------------------------------
// unlockImage
//-----------------------------------------------------------------------------
func (d *rbdDriver) unlockImage(pool, name, lockID, locker string) error {
// Unlock the image
err := exec.Command(
d.cmd["rbd"], "lock", "remove",
name, lockID, locker,
).Run()
if err != nil {
return errors.New("Unable to unlock the image")
}
return nil
}
//-----------------------------------------------------------------------------
// mapImage
//-----------------------------------------------------------------------------
func (d *rbdDriver) mapImage(pool, name string) (string, error) {
// Map the image to a kernel device
out, err := exec.Command(
d.cmd["rbd"], "map",
"--pool", pool, name,
).Output()
if err != nil {
return "", errors.New("Unable to map the image to a kernel device")
}
// Parse the device
return strings.TrimSpace(string(out)), nil
}
//-----------------------------------------------------------------------------
// unmapImage
//-----------------------------------------------------------------------------
func (d *rbdDriver) unmapImage(device string) error {
// Unmap the image from a kernel device
err := exec.Command(
d.cmd["rbd"], "unmap", device,
).Run()
if err != nil {
return errors.New("Unable to unmap the image from " + device)
}
return nil
}
//-----------------------------------------------------------------------------
// makeFs
//-----------------------------------------------------------------------------
func (d *rbdDriver) makeFs(device, fsType string) error {
// Search for mkfs
mkfs, err := exec.LookPath("mkfs." + d.defFsType)
if err != nil {
return errors.New("Unable to find mkfs." + d.defFsType)
}
// Make the file system
if err = exec.Command(mkfs, device).Run(); err != nil {
return errors.New("Unable to make file system on " + device)
}
return nil
}
//-----------------------------------------------------------------------------
// mountDevice
//-----------------------------------------------------------------------------
func (d *rbdDriver) mountDevice(device, mountpoint, fsType string) error {
// Mount the device
err := exec.Command(
d.cmd["mount"],
"-t", fsType,
device, mountpoint,
).Run()
if err != nil {
return errors.New("Unable to mount " + device + "on " + mountpoint)
}
return nil
}
//-----------------------------------------------------------------------------
// unmountDevice
//-----------------------------------------------------------------------------
func (d *rbdDriver) unmountDevice(device string) error {
// Unmount the device
if err := exec.Command(d.cmd["umount"], device).Run(); err != nil {
return errors.New("Unable to umount " + device)
}
return nil
}