This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathstorage.go
629 lines (524 loc) · 16.9 KB
/
storage.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
package daemon
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
dockertypes "github.com/docker/engine-api/types"
"github.com/golang/glog"
"github.com/hyperhq/hyperd/daemon/daemondb"
"github.com/hyperhq/hyperd/storage"
"github.com/hyperhq/hyperd/storage/aufs"
dm "github.com/hyperhq/hyperd/storage/devicemapper"
"github.com/hyperhq/hyperd/storage/graphdriver/rawblock"
"github.com/hyperhq/hyperd/storage/overlay"
"github.com/hyperhq/hyperd/storage/vbox"
apitypes "github.com/hyperhq/hyperd/types"
"github.com/hyperhq/hyperd/utils"
runv "github.com/hyperhq/runv/api"
)
type Storage interface {
Type() string
RootPath() string
Init() error
CleanUp() error
PrepareContainer(mountId, sharedDir string, readonly bool) (*runv.VolumeDescription, error)
CleanupContainer(id, sharedDir string) error
InjectFile(src io.Reader, containerId, target, baseDir string, perm, uid, gid int) error
CreateVolume(podId string, spec *apitypes.UserVolume) error
RemoveVolume(podId string, record []byte) error
}
var StorageDrivers map[string]func(*dockertypes.Info, *daemondb.DaemonDB) (Storage, error) = map[string]func(*dockertypes.Info, *daemondb.DaemonDB) (Storage, error){
"devicemapper": DMFactory,
"aufs": AufsFactory,
"overlay": OverlayFsFactory,
"btrfs": BtrfsFactory,
"rawblock": RawBlockFactory,
"vbox": VBoxStorageFactory,
}
func StorageFactory(sysinfo *dockertypes.Info, db *daemondb.DaemonDB) (Storage, error) {
if factory, ok := StorageDrivers[sysinfo.Driver]; ok {
return factory(sysinfo, db)
}
return nil, fmt.Errorf("hyperd can not support docker's backing storage: %s", sysinfo.Driver)
}
type DevMapperStorage struct {
db *daemondb.DaemonDB
CtnPoolName string
VolPoolName string
DevPrefix string
FsType string
rootPath string
DmPoolData *dm.DeviceMapper
}
func DMFactory(sysinfo *dockertypes.Info, db *daemondb.DaemonDB) (Storage, error) {
driver := &DevMapperStorage{
db: db,
}
driver.VolPoolName = storage.DEFAULT_DM_POOL
for _, pair := range sysinfo.DriverStatus {
if pair[0] == "Pool Name" {
driver.CtnPoolName = pair[1]
}
if pair[0] == "Backing Filesystem" {
if strings.Contains(pair[1], "ext") {
driver.FsType = "ext4"
} else if strings.Contains(pair[1], "xfs") {
driver.FsType = "xfs"
} else {
driver.FsType = "dir"
}
break
}
}
driver.DevPrefix = driver.CtnPoolName[:strings.Index(driver.CtnPoolName, "-pool")]
driver.rootPath = filepath.Join(utils.HYPER_ROOT, "devicemapper")
return driver, nil
}
func (dms *DevMapperStorage) Type() string {
return "devicemapper"
}
func (dms *DevMapperStorage) RootPath() string {
return dms.rootPath
}
func (dms *DevMapperStorage) Init() error {
dmPool := dm.DeviceMapper{
Datafile: filepath.Join(utils.HYPER_ROOT, "lib") + "/data",
Metadatafile: filepath.Join(utils.HYPER_ROOT, "lib") + "/metadata",
DataLoopFile: storage.DEFAULT_DM_DATA_LOOP,
MetadataLoopFile: storage.DEFAULT_DM_META_LOOP,
PoolName: dms.VolPoolName,
Size: storage.DEFAULT_DM_POOL_SIZE,
}
dms.DmPoolData = &dmPool
rand.Seed(time.Now().UnixNano())
// Prepare the DeviceMapper storage
return dm.CreatePool(&dmPool)
}
func (dms *DevMapperStorage) CleanUp() error {
return dm.DMCleanup(dms.DmPoolData)
}
func (dms *DevMapperStorage) PrepareContainer(mountId, sharedDir string, readonly bool) (*runv.VolumeDescription, error) {
if err := dm.CreateNewDevice(mountId, dms.DevPrefix, dms.RootPath()); err != nil {
return nil, err
}
devFullName, err := dm.MountContainerToSharedDir(mountId, sharedDir, dms.DevPrefix)
if err != nil {
glog.Error("got error when mount container to share dir ", err.Error())
return nil, err
}
fstype, err := dm.ProbeFsType(devFullName)
if err != nil {
fstype = storage.DEFAULT_VOL_FS
}
vol := &runv.VolumeDescription{
Name: devFullName,
Source: devFullName,
Fstype: fstype,
Format: "raw",
ReadOnly: readonly,
}
return vol, nil
}
func (dms *DevMapperStorage) CleanupContainer(id, sharedDir string) error {
devFullName, err := dm.MountContainerToSharedDir(id, sharedDir, dms.DevPrefix)
if err != nil {
glog.Error("got error when mount container to share dir ", err.Error())
return err
}
return dm.UnmapVolume(devFullName)
}
func (dms *DevMapperStorage) InjectFile(src io.Reader, mountId, target, baseDir string, perm, uid, gid int) error {
if err := dm.CreateNewDevice(mountId, dms.DevPrefix, dms.RootPath()); err != nil {
return err
}
return dm.InjectFile(src, mountId, dms.DevPrefix, target, baseDir, perm, uid, gid)
}
func (dms *DevMapperStorage) getPersistedId(podId, volName string) (int, error) {
vols, err := dms.db.ListPodVolumes(podId)
if err != nil {
return -1, err
}
dev_id := 0
for _, vol := range vols {
fields := strings.Split(string(vol), ":")
if fields[0] == volName {
dev_id, _ = strconv.Atoi(fields[1])
}
}
return dev_id, nil
}
func (dms *DevMapperStorage) CreateVolume(podId string, spec *apitypes.UserVolume) error {
var err error
// kernel dm has limitation of 128 bytes on device name length
// include/uapi/linux/dm-ioctl.h#L16
// #define DM_NAME_LEN 128
// Use sha256 so it is fixed 64 bytes
chksum := sha256.Sum256([]byte(podId + spec.Name))
deviceName := fmt.Sprintf("%s-%s", dms.VolPoolName, hex.EncodeToString(chksum[:sha256.Size]))
dev_id, _ := dms.getPersistedId(podId, deviceName)
glog.Infof("DeviceID is %d for %s of pod %s container %s", dev_id, deviceName, podId, spec.Name)
restore := dev_id > 0
for {
if !restore {
dev_id = dms.randDevId()
}
dev_id_str := strconv.Itoa(dev_id)
err = dm.CreateVolume(dms.VolPoolName, deviceName, dev_id_str, storage.DEFAULT_VOL_MKFS, storage.DEFAULT_DM_VOL_SIZE, restore)
if err != nil && !restore && strings.Contains(err.Error(), "failed: File exists") {
glog.V(1).Infof("retry for dev_id #%d creating collision: %v", dev_id, err)
continue
} else if err != nil {
glog.V(1).Infof("failed to create dev_id #%d: %v", dev_id, err)
return err
}
glog.V(3).Infof("device (%d) created (restore:%v) for %s: %s", dev_id, restore, podId, deviceName)
dms.db.UpdatePodVolume(podId, deviceName, []byte(fmt.Sprintf("%s:%s", deviceName, dev_id_str)))
break
}
fstype := storage.DEFAULT_VOL_FS
if !restore {
if spec.Fstype == "" {
fstype, err = dm.ProbeFsType("/dev/mapper/" + deviceName)
if err != nil {
fstype = storage.DEFAULT_VOL_FS
}
} else {
fstype = spec.Fstype
}
}
glog.V(1).Infof("volume %s created with dm as %s", spec.Name, deviceName)
spec.Source = filepath.Join("/dev/mapper/", deviceName)
spec.Format = "raw"
spec.Fstype = fstype
return nil
}
func (dms *DevMapperStorage) RemoveVolume(podId string, record []byte) error {
fields := strings.SplitN(string(record), ":", 2)
if len(fields) == 1 {
record, err := dms.db.GetPodVolume(podId, fields[0])
if err != nil {
glog.Error(err)
return err
}
fields = strings.SplitN(string(record), ":", 2)
if len(fields) == 1 {
err = fmt.Errorf("cannot get valid volume %s/%s from db", podId, record)
glog.Error(err)
return err
}
}
dev_id, _ := strconv.Atoi(fields[1])
if err := dm.DeleteVolume(dms.DmPoolData, dev_id); err != nil {
glog.Error(err.Error())
return err
}
if err := dms.db.DeletePodVolume(podId, fields[0]); err != nil {
glog.Error(err.Error())
return err
}
return nil
}
func (dms *DevMapperStorage) randDevId() int {
return rand.Intn(1<<24-1) + 1 // 0 reserved for pool device
}
type AufsStorage struct {
rootPath string
}
func AufsFactory(sysinfo *dockertypes.Info, _ *daemondb.DaemonDB) (Storage, error) {
driver := &AufsStorage{}
for _, pair := range sysinfo.DriverStatus {
if pair[0] == "Root Dir" {
driver.rootPath = pair[1]
}
}
return driver, nil
}
func (a *AufsStorage) Type() string {
return "aufs"
}
func (a *AufsStorage) RootPath() string {
return a.rootPath
}
func (*AufsStorage) Init() error { return nil }
func (*AufsStorage) CleanUp() error { return nil }
func (a *AufsStorage) PrepareContainer(mountId, sharedDir string, readonly bool) (*runv.VolumeDescription, error) {
_, err := aufs.MountContainerToSharedDir(mountId, a.RootPath(), sharedDir, "", readonly)
if err != nil {
glog.Error("got error when mount container to share dir ", err.Error())
return nil, err
}
containerPath := "/" + mountId
vol := &runv.VolumeDescription{
Name: containerPath,
Source: containerPath,
Fstype: "dir",
Format: "vfs",
ReadOnly: readonly,
}
return vol, nil
}
func (a *AufsStorage) CleanupContainer(id, sharedDir string) error {
return aufs.Unmount(filepath.Join(sharedDir, id, "rootfs"))
}
func (a *AufsStorage) InjectFile(src io.Reader, containerId, target, baseDir string, perm, uid, gid int) error {
_, err := aufs.MountContainerToSharedDir(containerId, a.RootPath(), baseDir, "", false)
if err != nil {
glog.Error("got error when mount container to share dir ", err.Error())
return err
}
defer aufs.Unmount(filepath.Join(baseDir, containerId, "rootfs"))
return storage.FsInjectFile(src, containerId, target, baseDir, perm, uid, gid)
}
func (a *AufsStorage) CreateVolume(podId string, spec *apitypes.UserVolume) error {
volName, err := storage.CreateVFSVolume(podId, spec.Name)
if err != nil {
return err
}
spec.Source = volName
spec.Format = "vfs"
spec.Fstype = "dir"
return nil
}
func (a *AufsStorage) RemoveVolume(podId string, record []byte) error {
return nil
}
type OverlayFsStorage struct {
rootPath string
}
func OverlayFsFactory(_ *dockertypes.Info, _ *daemondb.DaemonDB) (Storage, error) {
driver := &OverlayFsStorage{
rootPath: filepath.Join(utils.HYPER_ROOT, "overlay"),
}
return driver, nil
}
func (o *OverlayFsStorage) Type() string {
return "overlay"
}
func (o *OverlayFsStorage) RootPath() string {
return o.rootPath
}
func (*OverlayFsStorage) Init() error { return nil }
func (*OverlayFsStorage) CleanUp() error { return nil }
func (o *OverlayFsStorage) PrepareContainer(mountId, sharedDir string, readonly bool) (*runv.VolumeDescription, error) {
_, err := overlay.MountContainerToSharedDir(mountId, o.RootPath(), sharedDir, "", readonly)
if err != nil {
glog.Error("got error when mount container to share dir ", err.Error())
return nil, err
}
containerPath := "/" + mountId
vol := &runv.VolumeDescription{
Name: containerPath,
Source: containerPath,
Fstype: "dir",
Format: "vfs",
ReadOnly: readonly,
}
return vol, nil
}
func (o *OverlayFsStorage) CleanupContainer(id, sharedDir string) error {
return syscall.Unmount(filepath.Join(sharedDir, id, "rootfs"), 0)
}
func (o *OverlayFsStorage) InjectFile(src io.Reader, mountId, target, baseDir string, perm, uid, gid int) error {
_, err := overlay.MountContainerToSharedDir(mountId, o.RootPath(), baseDir, "", false)
if err != nil {
glog.Error("got error when mount container to share dir ", err.Error())
return err
}
defer syscall.Unmount(filepath.Join(baseDir, mountId, "rootfs"), 0)
return storage.FsInjectFile(src, mountId, target, baseDir, perm, uid, gid)
}
func (o *OverlayFsStorage) CreateVolume(podId string, spec *apitypes.UserVolume) error {
volName, err := storage.CreateVFSVolume(podId, spec.Name)
if err != nil {
return err
}
spec.Source = volName
spec.Format = "vfs"
spec.Fstype = "dir"
return nil
}
func (o *OverlayFsStorage) RemoveVolume(podId string, record []byte) error {
return nil
}
type BtrfsStorage struct {
rootPath string
}
func BtrfsFactory(_ *dockertypes.Info, _ *daemondb.DaemonDB) (Storage, error) {
driver := &BtrfsStorage{
rootPath: filepath.Join(utils.HYPER_ROOT, "btrfs"),
}
return driver, nil
}
func (s *BtrfsStorage) Type() string {
return "btrfs"
}
func (s *BtrfsStorage) RootPath() string {
return s.rootPath
}
func (s *BtrfsStorage) subvolumesDirID(id string) string {
return filepath.Join(s.RootPath(), "subvolumes", id)
}
func (*BtrfsStorage) Init() error { return nil }
func (*BtrfsStorage) CleanUp() error { return nil }
func (s *BtrfsStorage) PrepareContainer(containerId, sharedDir string, readonly bool) (*runv.VolumeDescription, error) {
btrfsRootfs := s.subvolumesDirID(containerId)
mountPoint := filepath.Join(sharedDir, containerId, "rootfs")
if _, err := os.Stat(mountPoint); err != nil {
if err = os.MkdirAll(mountPoint, 0755); err != nil {
return nil, err
}
}
if err := syscall.Mount(btrfsRootfs, mountPoint, "bind", syscall.MS_BIND, ""); err != nil {
return nil, fmt.Errorf("failed to mount %s to %s: %v", btrfsRootfs, mountPoint, err)
}
if readonly {
if err := syscall.Mount(btrfsRootfs, mountPoint, "bind", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY, ""); err != nil {
syscall.Unmount(mountPoint, syscall.MNT_DETACH)
return nil, fmt.Errorf("failed to mount %s to %s readonly: %v", btrfsRootfs, mountPoint, err)
}
}
containerPath := "/" + containerId
vol := &runv.VolumeDescription{
Name: containerPath,
Source: containerPath,
Fstype: "dir",
Format: "vfs",
ReadOnly: readonly,
}
return vol, nil
}
func (s *BtrfsStorage) CleanupContainer(id, sharedDir string) error {
return syscall.Unmount(filepath.Join(sharedDir, id, "rootfs"), 0)
}
func (s *BtrfsStorage) InjectFile(src io.Reader, mountId, target, baseDir string, perm, uid, gid int) error {
return storage.FsInjectFile(src, mountId, target, filepath.Dir(s.subvolumesDirID(mountId)), perm, uid, gid)
}
func (s *BtrfsStorage) CreateVolume(podId string, spec *apitypes.UserVolume) error {
volName, err := storage.CreateVFSVolume(podId, spec.Name)
if err != nil {
return err
}
spec.Source = volName
spec.Format = "vfs"
spec.Fstype = "dir"
return nil
}
func (s *BtrfsStorage) RemoveVolume(podId string, record []byte) error {
return nil
}
type RawBlockStorage struct {
rootPath string
}
func RawBlockFactory(_ *dockertypes.Info, _ *daemondb.DaemonDB) (Storage, error) {
driver := &RawBlockStorage{
rootPath: filepath.Join(utils.HYPER_ROOT, "rawblock"),
}
return driver, nil
}
func (s *RawBlockStorage) Type() string {
return "rawblock"
}
func (s *RawBlockStorage) RootPath() string {
return s.rootPath
}
func (s *RawBlockStorage) Init() error {
if err := os.MkdirAll(filepath.Join(s.RootPath(), "volumes"), 0700); err != nil {
return err
}
return nil
}
func (*RawBlockStorage) CleanUp() error { return nil }
func (s *RawBlockStorage) PrepareContainer(containerId, sharedDir string, readonly bool) (*runv.VolumeDescription, error) {
devFullName := filepath.Join(s.RootPath(), "blocks", containerId)
vol := &runv.VolumeDescription{
Name: devFullName,
Source: devFullName,
Fstype: "xfs",
Format: "raw",
ReadOnly: readonly,
}
return vol, nil
}
func (s *RawBlockStorage) CleanupContainer(id, sharedDir string) error {
return nil
}
func (s *RawBlockStorage) InjectFile(src io.Reader, mountId, target, baseDir string, perm, uid, gid int) error {
if err := rawblock.GetImage(filepath.Join(s.RootPath(), "blocks"), baseDir, mountId, "xfs", "", uid, gid); err != nil {
return err
}
defer rawblock.PutImage(baseDir, mountId)
return storage.FsInjectFile(src, mountId, target, baseDir, perm, uid, gid)
}
func (s *RawBlockStorage) CreateVolume(podId string, spec *apitypes.UserVolume) error {
block := filepath.Join(s.RootPath(), "volumes", fmt.Sprintf("%s-%s", podId, spec.Name))
if err := rawblock.CreateBlock(block, "xfs", "", uint64(storage.DEFAULT_DM_VOL_SIZE)); err != nil {
return err
}
spec.Source = block
spec.Fstype = "xfs"
spec.Format = "raw"
return nil
}
func (s *RawBlockStorage) RemoveVolume(podId string, record []byte) error {
return nil
}
type VBoxStorage struct {
rootPath string
}
func VBoxStorageFactory(_ *dockertypes.Info, _ *daemondb.DaemonDB) (Storage, error) {
driver := &VBoxStorage{
rootPath: filepath.Join(utils.HYPER_ROOT, "vbox"),
}
return driver, nil
}
func (v *VBoxStorage) Type() string {
return "vbox"
}
func (v *VBoxStorage) RootPath() string {
return v.rootPath
}
func (*VBoxStorage) Init() error { return nil }
func (*VBoxStorage) CleanUp() error { return nil }
func (v *VBoxStorage) PrepareContainer(mountId, sharedDir string, readonly bool) (*runv.VolumeDescription, error) {
devFullName, err := vbox.MountContainerToSharedDir(mountId, v.RootPath(), "")
if err != nil {
glog.Error("got error when mount container to share dir ", err.Error())
return nil, err
}
vol := &runv.VolumeDescription{
Name: devFullName,
Source: devFullName,
Fstype: "ext4",
Format: "vdi",
ReadOnly: readonly,
}
return vol, nil
}
func (v *VBoxStorage) CleanupContainer(id, sharedDir string) error {
return nil
}
func (v *VBoxStorage) InjectFile(src io.Reader, containerId, target, rootDir string, perm, uid, gid int) error {
return errors.New("vbox storage driver does not support file insert yet")
}
func (v *VBoxStorage) CreateVolume(podId string, spec *apitypes.UserVolume) error {
volName, err := storage.CreateVFSVolume(podId, spec.Name)
if err != nil {
return err
}
spec.Source = volName
spec.Format = "vfs"
spec.Fstype = "dir"
return nil
}
func (v *VBoxStorage) RemoveVolume(podId string, record []byte) error {
return nil
}