-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphdriver.go
929 lines (780 loc) · 27.4 KB
/
graphdriver.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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
package graphdriver
import (
// "github.com/docker/docker/pkg/archive"
// "github.com/docker/docker/pkg/containerfs"
// "log"
// "context"
"fmt"
"io"
"os"
goPath "path"
"time"
"os/exec"
"path/filepath"
"sync"
"net/http"
"net/url"
"strings"
// "time"
"archive/tar"
gzip "github.com/klauspost/pgzip"
"io/ioutil"
// "strconv"
"github.com/docker/docker/pkg/ioutils"
"github.com/seveirbian/gear/pkg"
"github.com/seveirbian/gear/fs"
"github.com/seveirbian/gear/types"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/chrootarchive"
"github.com/docker/docker/pkg/containerfs"
"github.com/docker/docker/pkg/idtools"
"github.com/docker/docker/pkg/locker"
"github.com/docker/docker/pkg/mount"
"golang.org/x/sys/unix"
// rsystem "github.com/opencontainers/runc/libcontainer/system"
"github.com/docker/docker/daemon/graphdriver/overlay2"
"github.com/docker/docker/daemon/graphdriver"
// "github.com/docker/docker/pkg/directory"
graphPlugin "github.com/docker/go-plugins-helpers/graphdriver"
"github.com/seveirbian/gear/push"
"github.com/opencontainers/selinux/go-selinux/label"
"github.com/sirupsen/logrus"
)
var (
GearPath = "/var/lib/gear/"
GearPrivateCachePath = filepath.Join(GearPath, "private")
GearPublicCachePath = filepath.Join(GearPath, "public")
GearNFSPath = filepath.Join(GearPath, "nfs")
GearBuildPath = filepath.Join(GearPath, "build")
GearImagesPath = filepath.Join(GearPath, "images")
GearContainersPath = filepath.Join(GearPath, "containers")
GearPushPath = filepath.Join(GearPath, "push")
)
var (
// 监控gearfs的时间
monitorTime = 600
// 监测是否是第二次挂载容器层
secondGet = map[string]bool{}
// 监控gearfs关闭
umountGearFs = map[string]chan time.Time{}
)
var (
// untar defines the untar method
untar = chrootarchive.UntarUncompressed
// ApplyUncompressedLayer defines the unpack method used by the graph
// driver
ApplyUncompressedLayer = chrootarchive.ApplyUncompressedLayer
gearCtr = map[string]int{}
gearCommit = map[string]int{}
)
const (
driverName = "geargraphdriver"
linkDir = "l"
lowerFile = "lower"
maxDepth = 128
// idLength represents the number of random characters
// which can be used to create the unique link identifier
// for every layer. If this value is too long then the
// page size limit for the mount command may be exceeded.
// The idLength should be selected such that following equation
// is true (512 is a buffer for label metadata).
// ((idLength + len(linkDir) + 1) * maxDepth) <= (pageSize - 512)
idLength = 26
)
type Driver struct {
home string
uidMaps []idtools.IDMap
gidMaps []idtools.IDMap
ctr *graphdriver.RefCounter
naiveDiff graphdriver.DiffDriver
supportsDType bool
locker *locker.Locker
dockerDriver graphdriver.Driver
ManagerIp string
ManagerPort string
MonitorIp string
MonitorPort string
}
var (
logger = logrus.WithField("storage-driver", "geargraphdriver")
backingFs = "<unknown>"
projectQuotaSupported = false
useNaiveDiffLock sync.Once
useNaiveDiffOnly bool
indexOff string
)
// 初始化驱动
func (d *Driver) Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) error {
fmt.Println("\nInit func parameters: ")
fmt.Printf(" home: %s\n", home)
fmt.Printf(" options: %s\n", options)
fmt.Println(" uidMaps: ", uidMaps)
fmt.Println(" gidMaps: ", gidMaps)
d.uidMaps = uidMaps
d.gidMaps = gidMaps
d.home = home
driver, err := overlay2.Init(home, []string{}, nil, nil)
if err != nil {
logger.WithField("err", err).Warn("Fail to create overlay2 driver...")
return err
}
d.dockerDriver = driver
// d.naiveDiff = graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps)
return nil
}
// 为镜像层创建目录
func (d *Driver) Create(id, parent, mountlabel string, storageOpt map[string]string) (retErr error) {
fmt.Println("\nCreate func parameters: ")
fmt.Printf(" id: %s\n", id)
fmt.Printf(" parent: %s\n", parent)
fmt.Printf(" mountLabel: %s\n", mountlabel)
fmt.Printf(" storageOpt: %s\n", storageOpt)
// 使用d.dockerDriver为docker镜像和gear镜像创建镜像层文件夹
retErr = d.dockerDriver.Create(id, parent, &graphdriver.CreateOpts{
MountLabel: mountlabel,
StorageOpt:storageOpt,
})
return
}
// 为容器层创建目录
func (d *Driver) CreateReadWrite(id, parent, mountlabel string, storageOpt map[string]string) (retErr error) {
fmt.Println("\nCreateReadWrite func parameters: ")
fmt.Printf(" id: %s\n", id)
fmt.Printf(" parent: %s\n", parent)
fmt.Printf(" mountLabel: %s\n", mountlabel)
fmt.Printf(" storageOpt: %s\n", storageOpt)
retErr = d.dockerDriver.CreateReadWrite(id, parent, &graphdriver.CreateOpts{
MountLabel: mountlabel,
StorageOpt:storageOpt,
})
// 1. 检测parent的目录中是否有gear-lower
path := filepath.Join(d.home, parent, "gear-lower")
_, err := os.Lstat(path)
if err == nil {
// 2. 检测gear镜像目录中是否有gear-lower
gearLower := filepath.Join(d.home, parent, "gear-lower")
target, err := os.Readlink(gearLower)
if err != nil {
logger.Fatal("No gear-lower...")
}
// 3. 在孩子目录中也复制一份gear-lower
childGearLink := filepath.Join(d.home, id, "gear-lower")
err = os.Symlink(target, childGearLink)
if err != nil {
logger.Warnf("Fail to read gear-lower for %v", err)
}
//
// 4. 修改gear容器层的lower文件,添加gear镜像层目录中的gear-work目录到其中
gearPath := target
data, err := ioutil.ReadFile(filepath.Join(gearPath, "link"))
if err != nil {
logger.Warnf("Fail to get gear image link for %v", err)
}
gearLink := "l/" + string(data)
gearLinkPath, err := os.Readlink(filepath.Join(d.home, "l", string(data)))
if err != nil {
logger.Warnf("Fail to read link for %v", err)
}
gearWorkLink := filepath.Join(gearLinkPath, "..", "gear-work")
gearWorkLink = strings.TrimPrefix(gearWorkLink, "../")
cData, err := ioutil.ReadFile(filepath.Join(d.home, id, "lower"))
if err != nil {
logger.Warnf("Fail to read container lower for %v", err)
}
cLower := string(cData)
cLowerSlice := strings.Split(cLower, ":")
var index int
hasAdded := false
for index = 0; index < len(cLowerSlice); index++ {
if cLowerSlice[index] == gearWorkLink {
hasAdded = true
}
}
if !hasAdded {
for index = 0; index < len(cLowerSlice); index++ {
if cLowerSlice[index] == gearLink {
break
}
}
finalLower := []string{}
finalLower = append(finalLower, cLowerSlice[:index]...)
finalLower = append(finalLower, gearWorkLink)
finalLower = append(finalLower, cLowerSlice[index:]...)
stringLower := strings.Join(finalLower, ":")
// 删除原lower文件
err = os.Remove(filepath.Join(d.home, id, "lower"))
if err != nil {
logger.Warnf("Fail to remove container lower file for %v", err)
}
// 写lower文件
err = ioutil.WriteFile(filepath.Join(d.home, id, "lower"), []byte(stringLower), os.ModePerm)
if err != nil {
logger.Warnf("Fail to write new lower file for %v", err)
}
}
}
return
}
// 删除id目录
func (d *Driver) Remove(id string) error {
fmt.Printf("\nRemove func parameters: \n")
fmt.Printf(" id: %s\n", id)
err := d.dockerDriver.Remove(id)
return err
}
// Get creates and mounts the required file system for the given id and returns the mount path
func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
fmt.Printf("\nGet func parameters: \n")
fmt.Printf(" id: %s\n", id)
fmt.Printf(" mountlabel: %s\n", mountLabel)
// 判断是否需要监控gearfs
needMonitor := false
// 1. 检测目录中是否有gear-lower
path := filepath.Join(d.home, id, "gear-lower")
gearPath, err := os.Readlink(path)
if err != nil {
containerFs, err := d.dockerDriver.Get(id, mountLabel)
return containerFs, err
} else {
// 检测该目录下是否有lower文件
_, err := os.Lstat(filepath.Join(d.home, id, "lower"))
if err != nil {
// 有gear-lower但没有lower的是gear镜像
// 当前目录是gear镜像目录,不需要gear fs的帮助
// 直接将gear-diff目录返回???
gearDiffDir := filepath.Join(d.home, id, "gear-diff")
return containerfs.NewLocalContainerFS(gearDiffDir), nil
} else {
// 既有gear-lower也有lower的是gear容器目录
// 当前目录是gear容器目录,需要gear fs的挂载
// 2. 有,需要gear fs目录的挂载
gearDiffDir := filepath.Join(gearPath, "diff")
gearGearDir := filepath.Join(gearPath, "gear-diff")
// 3. 查找gear-diff目录下,gear-image软链接的镜像名和tag
gearImage, err := os.Readlink(filepath.Join(gearGearDir, "gear-image"))
if err != nil {
logger.Warnf("Fail to read gear-image symlink for %v", err)
}
// 4. 获取镜像自己的私有cache
gearImagePrivateCache := filepath.Join(GearPrivateCachePath, gearImage)
_, err = os.Lstat(gearImagePrivateCache)
if err != nil {
// 创建一个
err := os.MkdirAll(gearImagePrivateCache, 0700)
if err != nil {
logger.Warnf("Fail to create image private cache dir for %v", err)
}
}
recordChan := make(chan types.MonitorFile, 100)
// recordFilesChan := make(chan string, 100)
// recordFileNamesChan := make(chan string, 100)
initLayerPath := filepath.Join(gearPath, "gear-work")
// if !strings.Contains(id, "-init") {
// // 读取镜像层中lower文件内容
// data, err := ioutil.ReadFile(filepath.Join(d.home, id, "lower"))
// if err != nil {
// logger.Warnf("Fail to read lower file for %v", err)
// }
// // 获取-init目录
// dataSlices := strings.Split(string(data), ":")
// lPath := dataSlices[0]
// initLayerDir, err := os.Readlink(filepath.Join(d.home, lPath))
// initLayerPath = filepath.Join(d.home, id, initLayerDir)
// fmt.Println(initLayerPath)
// }
_, err = os.Lstat(filepath.Join(gearPath, "gear-diff", "RecordFiles"))
if err != nil {
// 需要监控该镜像
logger.Warnf("Monitoring...")
needMonitor = true
recordFileNames := []string{}
recordFiles := []string{}
go func(id string) {
// 判断是否需要监测
if strings.Contains(id, "-init") {
return
}
if _, ok := secondGet[id]; !ok {
secondGet[id] = true
return
}
fmt.Println("Monitoring...")
t := time.NewTimer(time.Duration(monitorTime) * time.Second)
reportChan := make(chan time.Time, 1)
if _, ok := umountGearFs[id]; !ok {
umountGearFs[id] = reportChan
}
dupFiles := map[string]bool{}
for {
select {
case file := <- recordChan:
if _, ok := dupFiles[file.RelativePath]; !ok {
dupFiles[file.RelativePath] = true
recordFiles = append(recordFiles, file.Hash)
recordFileNames = append(recordFileNames, file.RelativePath)
}
// TODO: 对于软连接或者多个文件指向同一个哈希文件的情况
case <- t.C:
// 向manager汇报
v := url.Values{"files": recordFiles, "filenames": recordFileNames, "image": []string{gearImage}}
// 在本地创建RecordFiles文件
if len(recordFiles) != len(recordFileNames) {
logger.Warnf("Something went error that len(recordFiles) != len(recordFileNames)")
} else {
content := ""
for i := 0; i < len(recordFiles) - 1; i++ {
content = content+recordFileNames[i]+" "+recordFiles[i]+"\n"
}
content = content+recordFileNames[len(recordFiles)-1]+" "+recordFiles[len(recordFiles)-1]
err = ioutil.WriteFile(filepath.Join(gearPath, "gear-diff", "RecordFiles"), []byte(content), os.ModePerm)
if err != nil {
logger.Warnf("Fail to write recordfiles for %v", err)
}
}
resp, err := http.PostForm("http://"+d.MonitorIp+":"+d.MonitorPort+"/event", v)
if err != nil {
logger.Warnf("Fail to report to monitor for %v", err)
}
fmt.Println("send event!")
defer resp.Body.Close()
delete(umountGearFs, id)
return
case <- umountGearFs[id]:
// 向manager汇报
v := url.Values{"files": recordFiles, "filenames": recordFileNames, "image": []string{gearImage}}
// 在本地创建RecordFiles文件
if len(recordFiles) != len(recordFileNames) {
logger.Warnf("Something went error that len(recordFiles) != len(recordFileNames)")
} else {
content := ""
for i := 0; i < len(recordFiles) - 1; i++ {
content = content+recordFileNames[i]+" "+recordFiles[i]+"\n"
}
content = content+recordFileNames[len(recordFiles)-1]+" "+recordFiles[len(recordFiles)-1]
err = ioutil.WriteFile(filepath.Join(gearPath, "gear-diff", "RecordFiles"), []byte(content), os.ModePerm)
if err != nil {
logger.Warnf("Fail to write recordfiles for %v", err)
}
}
resp, err := http.PostForm("http://"+d.MonitorIp+":"+d.MonitorPort+"/event", v)
if err != nil {
logger.Warnf("Fail to report to monitor for %v", err)
}
fmt.Println("send event!")
defer resp.Body.Close()
delete(umountGearFs, id)
return
}
}
}(id)
} else {
// 判断是否存在prefetched文件
_, err = os.Lstat(filepath.Join(gearPath, "gear-diff", "prefetched"))
if err != nil {
// 从远程文件系统prefetch文件
files := []string{}
tamplate := map[string]string{}
dedup := map[string]bool{}
b, err := ioutil.ReadFile(filepath.Join(gearPath, "gear-diff", "RecordFiles"))
if err != nil {
logger.Warnf("Fail to read file for %v", err)
}
values := string(b)
nameAndFiles := strings.Split(values, "\n")
tmp := []string{}
for _, nameAndFile := range nameAndFiles {
c := strings.Split(nameAndFile, " ")
if c[1] != "" {
if _, ok := dedup[c[1]]; !ok {
dedup[c[1]] = true
tmp = append(tmp, c[1])
}
if _, ok := tamplate[c[0]]; !ok {
tamplate[c[0]] = c[1]
}
}
}
files = tmp
t := time.Now()
needToDownloadFiles := []string{}
for _, file := range files {
_, err := os.Lstat(filepath.Join(GearPublicCachePath, file))
if err != nil {
needToDownloadFiles = append(needToDownloadFiles, file)
}
}
v := url.Values{"files": needToDownloadFiles, "image": []string{gearImage}}
resp, err := http.PostForm("http://"+d.ManagerIp+":"+d.ManagerPort+"/prefetch", v)
if err != nil {
logger.Warnf("Fail to prefetch for %v", err)
}
defer resp.Body.Close()
tr := tar.NewReader(resp.Body)
for {
th, err := tr.Next()
if err == io.EOF {
break;
}
tgt, err := os.Create(filepath.Join(GearPublicCachePath, th.Name))
if err != nil {
logger.Warnf("Fail to create tgt file for %v", err)
}
gr, err := gzip.NewReader(tr)
_, err = io.Copy(tgt, gr)
if err != nil {
logger.Fatalf("Fail to copy for %v", err)
}
gr.Close()
tgt.Close()
err = os.Link(filepath.Join(GearPublicCachePath, th.Name), filepath.Join(gearImagePrivateCache, th.Name))
if err != nil {
if !strings.Contains(err.Error(), "file exists") {
logger.Fatalf("Fail to create hard link for %v", err)
}
}
// 设置文件权限
err = os.Chmod(filepath.Join(gearImagePrivateCache, th.Name), 0777)
if err != nil {
logger.Warnf("Fail to chmod file for %v", err)
}
}
_, err = os.Create(filepath.Join(gearGearDir, "prefetched"))
if err != nil {
logger.Warnf("Fail to create file for %v", err)
}
fmt.Println("Time used: ", time.Since(t))
// 结束从远程文件系统prefetch文件
// 将文件link到gear-work层目录
lt := time.Now()
fmt.Println(lt)
if initLayerPath != "" {
// 将文件link到gear-work层目录
for relativePath, file := range tamplate {
_, err = os.Lstat(filepath.Join(initLayerPath, relativePath))
if err != nil {
initDir := goPath.Dir(filepath.Join(initLayerPath, relativePath))
_, err = os.Lstat(initDir)
if err != nil {
// 复制路径
if pkg.CopyPath(gearGearDir, initLayerPath, relativePath) != true {
fileInfo, err := os.Lstat(filepath.Join(gearGearDir, relativePath))
if err != nil {
logger.Warnf("Fail to get file info for %v", err)
}
err = os.MkdirAll(initDir, fileInfo.Mode())
if err != nil {
logger.Warnf("Fail to create initDir for %v", err)
}
}
}
err = os.Link(filepath.Join("/var/lib/gear/public", file), filepath.Join(initLayerPath, relativePath))
if err != nil {
if !strings.Contains(err.Error(), "file exists") {
logger.Warnf("Fail to create hard link for %v", err)
}
}
err = os.Chmod(filepath.Join(initLayerPath, relativePath), 0777)
if err != nil {
logger.Warnf("Fail to chmod file for %v", err)
}
}
}
}
fmt.Println(time.Since(lt))
}
}
// 5. 将/gear目录使用gear fs挂载到/diff目录下
gearFS := &fs.GearFS {
MountPoint: gearDiffDir,
IndexImagePath: gearGearDir,
PrivateCachePath: gearImagePrivateCache,
UpperPath: filepath.Join(d.home, id, "diff"),
ManagerIp: d.ManagerIp,
ManagerPort: d.ManagerPort,
RecordChan: recordChan,
InitLayerPath: initLayerPath,
NeedMonitor: false,
}
notify := make(chan int)
// 判断是否是第一次挂载
if _, ok := gearCtr[gearDiffDir]; !ok {
// 第一次挂载
gearCtr[gearDiffDir] = 1
go gearFS.StartAndNotify(notify, needMonitor)
<- notify
} else {
gearCtr[gearDiffDir] += 1
}
containerFs, err := d.dockerDriver.Get(id, mountLabel)
return containerFs, err
}
}
}
// Put unmounts the mount path created for the give id.
// It also removes the 'merged' directory to force the kernel to unmount the
// overlay mount in other namespaces.
func (d *Driver) Put(id string) error {
fmt.Printf("\nPut func parameters: \n")
fmt.Printf(" id: %s\n", id)
Eterr := d.dockerDriver.Put(id)
// 1. 检测孩子的diff目录中是否有gear-image软链接
path := filepath.Join(d.home, id, "gear-lower")
gearPath, err := os.Readlink(path)
if err == nil {
// 2. 有,卸载diff目录
gearDiffDir := filepath.Join(gearPath, "diff")
if ctr, _ := gearCtr[gearDiffDir]; ctr <= 1 {
fmt.Println("该镜像只有一个容器!")
delete(gearCtr, gearDiffDir)
fmt.Println("删除umountgearfs中对应的id词条!")
if _, ok := umountGearFs[id]; ok {
umountGearFs[id] <- time.Now()
}
fmt.Println("卸载gearfs!")
cmd := exec.Command("umount", gearDiffDir)
err := cmd.Run()
if err != nil {
logger.Warnf("Fail to umount diff for %v", err)
}
// 3. 强制删除diff目录
err = os.RemoveAll(gearDiffDir)
if err != nil {
logger.Warnf("Fail to remove diff dir for %v", err)
}
// 4. 新建diff目录
err = os.MkdirAll(gearDiffDir, 0700)
if err != nil {
logger.Warnf("Fail to create diff dir for %v", err)
}
fmt.Println("Put完成!")
} else {
gearCtr[gearDiffDir] -= 1
}
}
t := time.Now()
fmt.Println("Put time: ", time.Since(t))
return Eterr
}
// 查看id是否已经被挂载了
func (d *Driver) Exists(id string) bool {
fmt.Printf("\nExists func parameters: \n")
fmt.Printf(" id: %s\n", id)
_, err := os.Stat(goPath.Join(d.home, id))
return err == nil
}
// Status returns current driver information in a two dimensional string array.
// Output contains "Backing Filesystem" used in this implementation
func (d *Driver) Status() [][2]string {
fmt.Printf("\nStatus func parameters: \n")
return [][2]string{
{"Backing Filesystem", backingFs},
// {"Supports d_type", strconv.FormatBool(d.supportsDType)},
// {"Native Overlay Diff", strconv.FormatBool(!useNaiveDiff(d.home))},
}
}
// GetMetadata returns metadata about the overlay driver such as the LowerDir,
// UpperDir, WorkDir, and MergeDir used to store data
func (d *Driver) GetMetadata(id string) (map[string]string, error) {
fmt.Printf("\nGetMetadata func parameters: \n")
fmt.Printf(" id: %s\n", id)
// _, err := os.Lstat(filepath.Join(d.home, id, "gear-lower"))
// if err == nil {
// _, err := os.Lstat(filepath.Join(d.home, id, "lower"))
// if err == nil {
// return nil, nil
// }
// }
metadata, err := d.dockerDriver.GetMetadata(id)
return metadata, err
}
// Cleanup any state created by overlay which should be cleaned when daemon
// is being shutdown. For now, we just have to unmount the bind mounted
// we had created
func (d *Driver) Cleanup() error {
fmt.Printf("\nCleanup func parameters: \n")
return mount.Unmount(d.home)
}
// Diff produces an archive of the changes between the specified
// layer and its parent layer which may be ""
func (d *Driver) Diff(id, parent string) io.ReadCloser {
fmt.Printf("\nDiff func parameters: \n")
fmt.Printf(" id: %s\n", id)
fmt.Printf(" parent: %s\n", parent)
// 检测当前镜像是否是gear镜像
path := filepath.Join(d.home, id, "gear-lower")
_, err := os.Lstat(path)
if err != nil {
// 不是gear镜像
diff, err := d.dockerDriver.Diff(id, parent)
if err != nil {
logger.Warnf("Fail to diff for %v", err)
}
return diff
} else {
// 是gear镜像
// 1. 将该层目录下的文件构建成gear镜像的目录树,即:将普通文件内容替换成内容的哈希值,内容以哈希值
// 命名上传到remote storage
currentDir := filepath.Join(d.home, id, "diff")
if err != nil {
logger.Fatalf("Fail to get image dir for %v\n", err)
}
pushDir := filepath.Join(GearPushPath, id)
err := os.MkdirAll(pushDir, os.ModePerm)
if err != nil {
logger.Warnf("Fail to make push dir for %v", err)
}
// 将目标容器的私有diff目录的文件,去内容后,并将提取的内容存存储到push目录下
err = extractAndSaveRegularFiles(currentDir, pushDir)
if err != nil {
logger.Warnf("Fail to walk id dir for %v", err)
}
// 将所有push目录下的gear文件推送到远程
pusher, err := push.InitPusher(pushDir, d.ManagerIp, d.ManagerPort, false)
if err != nil {
logger.Fatal("Fail to init a pusher to push gear image...")
}
pusher.Push()
err = os.RemoveAll(pushDir)
if err != nil {
logger.Warnf("Fail to remove push dir for %v", err)
}
// 2. 联合挂载,对上下层目录树做Diff
parent, err := os.Readlink(filepath.Join(d.home, id, "gear-lower"))
if err != nil {
logger.Warnf("Fail to readlink for %v", err)
}
parentDir := filepath.Join(parent, "gear-diff")
opts := "lowerdir=" + parentDir + ",upperdir=" + currentDir + ",workdir=" + filepath.Join(d.home, id, "work")
mountData := label.FormatMountLabel(opts, "")
mount := unix.Mount
mergedDir := filepath.Join(d.home, id, "merged")
mountTarget := mergedDir
rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
if err != nil {
logger.Fatalf("Fail to get rootUIDGID for %v", err)
}
if err := idtools.MkdirAndChown(mergedDir, 0700, idtools.Identity{UID: rootUID, GID: rootGID}); err != nil {
logger.Fatalf("Fail to mk merged dir for %v", err)
}
if err := mount("overlay", mountTarget, "overlay", 0, mountData); err != nil {
logger.Fatalf("Fail to mount overlay for %v", err)
}
if err := os.Chown(filepath.Join(filepath.Join(d.home, id, "work"), "work"), rootUID, rootGID); err != nil {
logger.Fatalf("Fail to get chown for %v", err)
}
layerFs := mountTarget
// 删除RecordFiles和prefetched文件
recordFiles := filepath.Join(mountTarget, "RecordFiles")
prefetchedFile := filepath.Join(mountTarget, "prefetched")
err = os.Remove(recordFiles)
if err != nil {
logger.Warnf("Fail to remove RecordFiles for %v", err)
}
err = os.Remove(prefetchedFile)
if err != nil {
logger.Warnf("Fail to remove prefetchedFile for %v", err)
}
archive, err := archive.Tar(layerFs, archive.Uncompressed)
if err != nil {
logger.Fatalf("Fail to get tar for %v", err)
}
return ioutils.NewReadCloserWrapper(archive, func() error {
err := archive.Close()
if err := unix.Unmount(mountTarget, unix.MNT_DETACH); err != nil {
logger.Debugf("Failed to unmount %s overlay: %s - %v", id, mountTarget, err)
}
if err := unix.Rmdir(mountTarget); err != nil && !os.IsNotExist(err) {
logger.Debugf("Failed to remove %s overlay: %v", id, err)
}
return err
})
}
}
// Changes produces a list of changes between the specified layer
// and its parent layer. If parent is "", then all changes will be ADD changes
func (d *Driver) Changes(id, parent string) ([]graphPlugin.Change, error) {
fmt.Printf("\nChanges func parameters: \n")
fmt.Printf(" id: %s\n", id)
fmt.Printf(" parent: %s\n", parent)
dockerChanges, err := d.dockerDriver.Changes(id, parent)
if err != nil {
return []graphPlugin.Change{}, err
}
var changes = []graphPlugin.Change{}
for _, change := range dockerChanges {
changes = append(changes, graphPlugin.Change{
Path: change.Path,
Kind: graphPlugin.ChangeKind(change.Kind),
})
}
return changes, nil
}
// ApplyDiff applies the new layer into a root
// docker pull will call this func
// TODO: this func has bug
func (d *Driver) ApplyDiff(id, parent string, diff io.Reader) (int64, error) {
fmt.Printf("\nApplyDiff func parameters: \n")
fmt.Printf(" id: %s\n", id)
fmt.Printf(" parent: %s\n", parent)
// 1. 直接将数据解压到diff文件中
size, err := d.dockerDriver.ApplyDiff(id, parent, diff)
// 4. 检测当前镜像是否是gear镜像
path := filepath.Join(d.home, id, "diff", "gear-image")
_, err = os.Lstat(path)
// 判断gear镜像
if err != nil {
// 不是gear镜像
// 直接返回
return size, nil
} else {
// 是gear镜像,将diff文件夹重命名为gear-diff
err := os.Rename(filepath.Join(d.home, id, "diff"), filepath.Join(d.home, id, "gear-diff"))
if err != nil {
logger.Warnf("Fail to rename diff dir to gear-diff for %v", err)
}
// 2. 创建gearlink
gearLower := filepath.Join(d.home, id, "gear-lower")
err = os.Symlink(filepath.Join(d.home, id), gearLower)
if err != nil {
logger.Warnf("Fail to create gear link for %v", err)
}
// 3. 删除overlay driver创建的lower文件
_, err = os.Lstat(filepath.Join(d.home, id, "lower"))
if err == nil {
err := os.Remove(filepath.Join(d.home, id, "lower"))
if err != nil {
logger.Warnf("Fail to remove lower for %v", err)
}
}
// 4. 创建diff文件夹
err = os.MkdirAll(filepath.Join(d.home, id, "diff"), os.ModePerm)
if err != nil {
logger.Warnf("Fail to mkdir diff for %v", err)
}
// 5. 创建gear-work文件夹,用来存放使用过的文件
err = os.MkdirAll(filepath.Join(d.home, id, "gear-work"), os.ModePerm)
if err != nil {
logger.Warnf("Fail to mkdir gear-work for %v", err)
}
return size, nil
}
}
// DiffSize calculates the changes between the specified id
// and its parent and returns the size in bytes of the changes
// relative to its base filesystem directory
func (d *Driver) DiffSize(id, parent string) (int64, error) {
fmt.Printf("\nDiffSizee func parameters: \n")
fmt.Printf(" id: %s\n", id)
fmt.Printf(" parent: %s\n", parent)
return d.dockerDriver.DiffSize(id, parent)
}
func (d *Driver) Capabilities() graphdriver.Capabilities {
fmt.Printf("\nCapabilities func parameters: \n")
return graphdriver.Capabilities{
ReproducesExactDiffs: false,
}
}
// protoDriver需要实现该方法
func (d *Driver) String() string {
return "Gear Graphdriver"
}