-
Notifications
You must be signed in to change notification settings - Fork 247
/
check.go
1153 lines (1114 loc) · 41.2 KB
/
check.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
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package storage
import (
"archive/tar"
"errors"
"fmt"
"io"
"os"
"path"
"path/filepath"
"sort"
"strings"
"sync"
"time"
drivers "github.com/containers/storage/drivers"
"github.com/containers/storage/pkg/archive"
"github.com/containers/storage/pkg/idtools"
"github.com/containers/storage/pkg/ioutils"
"github.com/containers/storage/types"
"github.com/sirupsen/logrus"
)
var (
// ErrLayerUnaccounted describes a layer that is present in the lower-level storage driver,
// but which is not known to or managed by the higher-level driver-agnostic logic.
ErrLayerUnaccounted = types.ErrLayerUnaccounted
// ErrLayerUnreferenced describes a layer which is not used by any image or container.
ErrLayerUnreferenced = types.ErrLayerUnreferenced
// ErrLayerIncorrectContentDigest describes a layer for which the contents of one or more
// files which were added in the layer appear to have changed. It may instead look like an
// unnamed "file integrity checksum failed" error.
ErrLayerIncorrectContentDigest = types.ErrLayerIncorrectContentDigest
// ErrLayerIncorrectContentSize describes a layer for which regenerating the diff that was
// used to populate the layer produced a diff of a different size. We check the digest
// first, so it's highly unlikely you'll ever see this error.
ErrLayerIncorrectContentSize = types.ErrLayerIncorrectContentSize
// ErrLayerContentModified describes a layer which contains contents which should not be
// there, or for which ownership/permissions/dates have been changed.
ErrLayerContentModified = types.ErrLayerContentModified
// ErrLayerDataMissing describes a layer which is missing a big data item.
ErrLayerDataMissing = types.ErrLayerDataMissing
// ErrLayerMissing describes a layer which is the missing parent of a layer.
ErrLayerMissing = types.ErrLayerMissing
// ErrImageLayerMissing describes an image which claims to have a layer that we don't know
// about.
ErrImageLayerMissing = types.ErrImageLayerMissing
// ErrImageDataMissing describes an image which is missing a big data item.
ErrImageDataMissing = types.ErrImageDataMissing
// ErrImageDataIncorrectSize describes an image which has a big data item which looks like
// its size has changed, likely because it's been modified somehow.
ErrImageDataIncorrectSize = types.ErrImageDataIncorrectSize
// ErrContainerImageMissing describes a container which claims to be based on an image that
// we don't know about.
ErrContainerImageMissing = types.ErrContainerImageMissing
// ErrContainerDataMissing describes a container which is missing a big data item.
ErrContainerDataMissing = types.ErrContainerDataMissing
// ErrContainerDataIncorrectSize describes a container which has a big data item which looks
// like its size has changed, likely because it's been modified somehow.
ErrContainerDataIncorrectSize = types.ErrContainerDataIncorrectSize
)
const (
defaultMaximumUnreferencedLayerAge = 24 * time.Hour
)
// CheckOptions is the set of options for Check(), specifying which tests to perform.
type CheckOptions struct {
LayerUnreferencedMaximumAge *time.Duration // maximum allowed age of unreferenced layers
LayerDigests bool // check that contents of image layer diffs can still be reconstructed
LayerMountable bool // check that layers are mountable
LayerContents bool // check that contents of image layers match their diffs, with no unexpected changes, requires LayerMountable
LayerData bool // check that associated "big" data items are present and can be read
ImageData bool // check that associated "big" data items are present, can be read, and match the recorded size
ContainerData bool // check that associated "big" data items are present and can be read
}
// checkIgnore is used to tell functions that compare the contents of a mounted
// layer to the contents that we'd expect it to have to ignore certain
// discrepancies
type checkIgnore struct {
ownership, timestamps, permissions bool
}
// CheckMost returns a CheckOptions with mostly just "quick" checks enabled.
func CheckMost() *CheckOptions {
return &CheckOptions{
LayerDigests: true,
LayerMountable: true,
LayerContents: false,
LayerData: true,
ImageData: true,
ContainerData: true,
}
}
// CheckEverything returns a CheckOptions with every check enabled.
func CheckEverything() *CheckOptions {
return &CheckOptions{
LayerDigests: true,
LayerMountable: true,
LayerContents: true,
LayerData: true,
ImageData: true,
ContainerData: true,
}
}
// CheckReport is a list of detected problems.
type CheckReport struct {
Layers map[string][]error // damaged read-write layers
ROLayers map[string][]error // damaged read-only layers
layerParentsByLayerID map[string]string
layerOrder map[string]int
Images map[string][]error // damaged read-write images (including those with damaged layers)
ROImages map[string][]error // damaged read-only images (including those with damaged layers)
Containers map[string][]error // damaged containers (including those based on damaged images)
}
// RepairOptions is the set of options for Repair().
type RepairOptions struct {
RemoveContainers bool // Remove damaged containers
}
// RepairEverything returns a RepairOptions with every optional remediation
// enabled.
func RepairEverything() *RepairOptions {
return &RepairOptions{
RemoveContainers: true,
}
}
// Check returns a list of problems with what's in the store, as a whole. It can be very expensive
// to call.
func (s *store) Check(options *CheckOptions) (CheckReport, error) {
var ignore checkIgnore
for _, o := range s.graphOptions {
if strings.Contains(o, "ignore_chown_errors=true") {
ignore.ownership = true
}
if strings.HasPrefix(o, "force_mask=") {
ignore.permissions = true
}
}
for o := range s.pullOptions {
if strings.Contains(o, "use_hard_links") {
if s.pullOptions[o] == "true" {
ignore.timestamps = true
}
}
}
if options == nil {
options = CheckMost()
}
report := CheckReport{
Layers: make(map[string][]error),
ROLayers: make(map[string][]error),
layerParentsByLayerID: make(map[string]string), // layers ID -> their parent's ID, if there is one
layerOrder: make(map[string]int), // layers ID -> order for removal, if we needed to remove them all
Images: make(map[string][]error),
ROImages: make(map[string][]error),
Containers: make(map[string][]error),
}
// This map will track known layer IDs. If we have multiple stores, read-only ones can
// contain copies of layers that are in the read-write store, but we'll only ever be
// mounting or extracting contents from the read-write versions, since we always search it
// first. The boolean will track if the layer is referenced by at least one image or
// container.
referencedLayers := make(map[string]bool)
referencedROLayers := make(map[string]bool)
// This map caches the headers for items included in layer diffs.
diffHeadersByLayer := make(map[string][]*tar.Header)
var diffHeadersByLayerMutex sync.Mutex
// Walk the list of layer stores, looking at each layer that we didn't see in a
// previously-visited store.
if _, _, err := readOrWriteAllLayerStores(s, func(store roLayerStore) (struct{}, bool, error) {
layers, err := store.Layers()
if err != nil {
return struct{}{}, true, err
}
isReadWrite := roLayerStoreIsReallyReadWrite(store)
readWriteDesc := ""
if !isReadWrite {
readWriteDesc = "read-only "
}
// Examine each layer in turn.
for i := range layers {
layer := layers[i]
id := layer.ID
// If we've already seen a layer with this ID, no need to process it again.
if _, checked := referencedLayers[id]; checked {
continue
}
if _, checked := referencedROLayers[id]; checked {
continue
}
// Note the parent of this layer, and add it to the map of known layers so
// that we know that we've visited it, but we haven't confirmed that it's
// used by anything.
report.layerParentsByLayerID[id] = layer.Parent
if isReadWrite {
referencedLayers[id] = false
} else {
referencedROLayers[id] = false
}
logrus.Debugf("checking %slayer %s", readWriteDesc, id)
// Check that all of the big data items are present and can be read. We
// have no digest or size information to compare the contents to (grumble),
// so we can't verify that the contents haven't been changed since they
// were stored.
if options.LayerData {
for _, name := range layer.BigDataNames {
func() {
rc, err := store.BigData(id, name)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
err := fmt.Errorf("%slayer %s: data item %q: %w", readWriteDesc, id, name, ErrLayerDataMissing)
if isReadWrite {
report.Layers[id] = append(report.Layers[id], err)
} else {
report.ROLayers[id] = append(report.ROLayers[id], err)
}
return
}
err = fmt.Errorf("%slayer %s: data item %q: %w", readWriteDesc, id, name, err)
if isReadWrite {
report.Layers[id] = append(report.Layers[id], err)
} else {
report.ROLayers[id] = append(report.ROLayers[id], err)
}
return
}
defer rc.Close()
if _, err = io.Copy(io.Discard, rc); err != nil {
err = fmt.Errorf("%slayer %s: data item %q: %w", readWriteDesc, id, name, err)
if isReadWrite {
report.Layers[id] = append(report.Layers[id], err)
} else {
report.ROLayers[id] = append(report.ROLayers[id], err)
}
return
}
}()
}
}
// Check that the content we get back when extracting the layer's contents
// match the recorded digest and size. A layer for which they're not given
// isn't a part of an image, and is likely the read-write layer for a
// container, and we can't vouch for the integrity of its contents.
// For each layer with known contents, record the headers for the layer's
// diff, which we can use to reconstruct the expected contents for the tree
// we see when the layer is mounted.
if options.LayerDigests && layer.UncompressedDigest != "" {
func() {
expectedDigest := layer.UncompressedDigest
// Double-check that the digest isn't invalid somehow.
if err := layer.UncompressedDigest.Validate(); err != nil {
err := fmt.Errorf("%slayer %s: %w", readWriteDesc, id, err)
if isReadWrite {
report.Layers[id] = append(report.Layers[id], err)
} else {
report.ROLayers[id] = append(report.ROLayers[id], err)
}
return
}
// Extract the diff.
uncompressed := archive.Uncompressed
diffOptions := DiffOptions{
Compression: &uncompressed,
}
diff, err := store.Diff("", id, &diffOptions)
if err != nil {
err := fmt.Errorf("%slayer %s: %w", readWriteDesc, id, err)
if isReadWrite {
report.Layers[id] = append(report.Layers[id], err)
} else {
report.ROLayers[id] = append(report.ROLayers[id], err)
}
return
}
// Digest and count the length of the diff.
digester := expectedDigest.Algorithm().Digester()
counter := ioutils.NewWriteCounter(digester.Hash())
reader := io.TeeReader(diff, counter)
var wg sync.WaitGroup
var archiveErr error
wg.Add(1)
go func(layerID string, diffReader io.Reader) {
// Read the diff, one item at a time.
tr := tar.NewReader(diffReader)
hdr, err := tr.Next()
for err == nil {
diffHeadersByLayerMutex.Lock()
diffHeadersByLayer[layerID] = append(diffHeadersByLayer[layerID], hdr)
diffHeadersByLayerMutex.Unlock()
hdr, err = tr.Next()
}
if !errors.Is(err, io.EOF) {
archiveErr = err
}
// consume any trailer after the EOF marker
io.Copy(io.Discard, diffReader)
wg.Done()
}(id, reader)
wg.Wait()
diff.Close()
if archiveErr != nil {
// Reading the diff didn't end as expected
diffHeadersByLayerMutex.Lock()
delete(diffHeadersByLayer, id)
diffHeadersByLayerMutex.Unlock()
archiveErr = fmt.Errorf("%slayer %s: %w", readWriteDesc, id, archiveErr)
if isReadWrite {
report.Layers[id] = append(report.Layers[id], archiveErr)
} else {
report.ROLayers[id] = append(report.ROLayers[id], archiveErr)
}
return
}
if digester.Digest() != layer.UncompressedDigest {
// The diff digest didn't match.
diffHeadersByLayerMutex.Lock()
delete(diffHeadersByLayer, id)
diffHeadersByLayerMutex.Unlock()
err := fmt.Errorf("%slayer %s: %w", readWriteDesc, id, ErrLayerIncorrectContentDigest)
if isReadWrite {
report.Layers[id] = append(report.Layers[id], err)
} else {
report.ROLayers[id] = append(report.ROLayers[id], err)
}
}
if layer.UncompressedSize != -1 && counter.Count != layer.UncompressedSize {
// We expected the diff to have a specific size, and
// it didn't match.
diffHeadersByLayerMutex.Lock()
delete(diffHeadersByLayer, id)
diffHeadersByLayerMutex.Unlock()
err := fmt.Errorf("%slayer %s: read %d bytes instead of %d bytes: %w", readWriteDesc, id, counter.Count, layer.UncompressedSize, ErrLayerIncorrectContentSize)
if isReadWrite {
report.Layers[id] = append(report.Layers[id], err)
} else {
report.ROLayers[id] = append(report.ROLayers[id], err)
}
}
}()
}
}
// At this point we're out of things that we can be sure will work in read-only
// stores, so skip the rest for any stores that aren't also read-write stores.
if !isReadWrite {
return struct{}{}, false, nil
}
// Content and mount checks are also things that we can only be sure will work in
// read-write stores.
for i := range layers {
layer := layers[i]
id := layer.ID
// Compare to what we see when we mount the layer and walk the tree, and
// flag cases where content is in the layer that shouldn't be there. The
// tar-split implementation of Diff() won't catch this problem by itself.
if options.LayerMountable {
func() {
// Mount the layer.
mountPoint, err := s.graphDriver.Get(id, drivers.MountOpts{MountLabel: layer.MountLabel})
if err != nil {
err := fmt.Errorf("%slayer %s: %w", readWriteDesc, id, err)
if isReadWrite {
report.Layers[id] = append(report.Layers[id], err)
} else {
report.ROLayers[id] = append(report.ROLayers[id], err)
}
return
}
// Unmount the layer when we're done in here.
defer func() {
if err := s.graphDriver.Put(id); err != nil {
err := fmt.Errorf("%slayer %s: %w", readWriteDesc, id, err)
if isReadWrite {
report.Layers[id] = append(report.Layers[id], err)
} else {
report.ROLayers[id] = append(report.ROLayers[id], err)
}
return
}
}()
// If we're not looking at layer contents, or we didn't
// look at the diff for this layer, we're done here.
if !options.LayerDigests || layer.UncompressedDigest == "" || !options.LayerContents {
return
}
// Build a list of all of the changes in all of the layers
// that make up the tree we're looking at.
diffHeaderSet := [][]*tar.Header{}
// If we don't know _all_ of the changes that produced this
// layer, it's not part of an image, so we're done here.
for layerID := id; layerID != ""; layerID = report.layerParentsByLayerID[layerID] {
diffHeadersByLayerMutex.Lock()
layerChanges, haveChanges := diffHeadersByLayer[layerID]
diffHeadersByLayerMutex.Unlock()
if !haveChanges {
return
}
// The diff headers for this layer go _before_ those of
// layers that inherited some of its contents.
diffHeaderSet = append([][]*tar.Header{layerChanges}, diffHeaderSet...)
}
expectedCheckDirectory := newCheckDirectoryDefaults()
for _, diffHeaders := range diffHeaderSet {
expectedCheckDirectory.headers(diffHeaders)
}
// Scan the directory tree under the mount point.
var idmap *idtools.IDMappings
if !s.canUseShifting(layer.UIDMap, layer.GIDMap) {
// we would have had to chown() layer contents to match ID maps
idmap = idtools.NewIDMappingsFromMaps(layer.UIDMap, layer.GIDMap)
}
actualCheckDirectory, err := newCheckDirectoryFromDirectory(mountPoint)
if err != nil {
err := fmt.Errorf("scanning contents of %slayer %s: %w", readWriteDesc, id, err)
if isReadWrite {
report.Layers[id] = append(report.Layers[id], err)
} else {
report.ROLayers[id] = append(report.ROLayers[id], err)
}
return
}
// Every departure from our expectations is an error.
diffs := compareCheckDirectory(expectedCheckDirectory, actualCheckDirectory, idmap, ignore)
for _, diff := range diffs {
err := fmt.Errorf("%slayer %s: %s, %w", readWriteDesc, id, diff, ErrLayerContentModified)
if isReadWrite {
report.Layers[id] = append(report.Layers[id], err)
} else {
report.ROLayers[id] = append(report.ROLayers[id], err)
}
}
}()
}
}
// Check that we don't have any dangling parent layer references.
for id, parent := range report.layerParentsByLayerID {
// If this layer doesn't have a parent, no problem.
if parent == "" {
continue
}
// If we've already seen a layer with this parent ID, skip it.
if _, checked := referencedLayers[parent]; checked {
continue
}
if _, checked := referencedROLayers[parent]; checked {
continue
}
// We haven't seen a layer with the ID that this layer's record
// says is its parent's ID.
err := fmt.Errorf("%slayer %s: %w", readWriteDesc, parent, ErrLayerMissing)
report.Layers[id] = append(report.Layers[id], err)
}
return struct{}{}, false, nil
}); err != nil {
return CheckReport{}, err
}
// This map will track examined images. If we have multiple stores, read-only ones can
// contain copies of images that are also in the read-write store, or the read-write store
// may contain a duplicate entry that refers to layers in the read-only stores, but when
// trying to export them, we only look at the first copy of the image.
examinedImages := make(map[string]struct{})
// Walk the list of image stores, looking at each image that we didn't see in a
// previously-visited store.
if _, _, err := readAllImageStores(s, func(store roImageStore) (struct{}, bool, error) {
images, err := store.Images()
if err != nil {
return struct{}{}, true, err
}
isReadWrite := roImageStoreIsReallyReadWrite(store)
readWriteDesc := ""
if !isReadWrite {
readWriteDesc = "read-only "
}
// Examine each image in turn.
for i := range images {
image := images[i]
id := image.ID
// If we've already seen an image with this ID, skip it.
if _, checked := examinedImages[id]; checked {
continue
}
examinedImages[id] = struct{}{}
logrus.Debugf("checking %simage %s", readWriteDesc, id)
if options.ImageData {
// Check that all of the big data items are present and reading them
// back gives us the right amount of data. Even though we record
// digests that can be used to look them up, we don't know how they
// were calculated (they're only used as lookup keys), so do not try
// to check them.
for _, key := range image.BigDataNames {
func() {
data, err := store.BigData(id, key)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
err = fmt.Errorf("%simage %s: data item %q: %w", readWriteDesc, id, key, ErrImageDataMissing)
if isReadWrite {
report.Images[id] = append(report.Images[id], err)
} else {
report.ROImages[id] = append(report.ROImages[id], err)
}
return
}
err = fmt.Errorf("%simage %s: data item %q: %w", readWriteDesc, id, key, err)
if isReadWrite {
report.Images[id] = append(report.Images[id], err)
} else {
report.ROImages[id] = append(report.ROImages[id], err)
}
return
}
if int64(len(data)) != image.BigDataSizes[key] {
err = fmt.Errorf("%simage %s: data item %q: %w", readWriteDesc, id, key, ErrImageDataIncorrectSize)
if isReadWrite {
report.Images[id] = append(report.Images[id], err)
} else {
report.ROImages[id] = append(report.ROImages[id], err)
}
return
}
}()
}
}
// Walk the layers list for the image. For every layer that the image uses
// that has errors, the layer's errors are also the image's errors.
examinedImageLayers := make(map[string]struct{})
for _, topLayer := range append([]string{image.TopLayer}, image.MappedTopLayers...) {
if topLayer == "" {
continue
}
if _, checked := examinedImageLayers[topLayer]; checked {
continue
}
examinedImageLayers[topLayer] = struct{}{}
for layer := topLayer; layer != ""; layer = report.layerParentsByLayerID[layer] {
// The referenced layer should have a corresponding entry in
// one map or the other.
_, checked := referencedLayers[layer]
_, checkedRO := referencedROLayers[layer]
if !checked && !checkedRO {
err := fmt.Errorf("layer %s: %w", layer, ErrImageLayerMissing)
err = fmt.Errorf("%simage %s: %w", readWriteDesc, id, err)
if isReadWrite {
report.Images[id] = append(report.Images[id], err)
} else {
report.ROImages[id] = append(report.ROImages[id], err)
}
} else {
// Count this layer as referenced. Whether by the
// image or one of its child layers doesn't matter
// at this point.
if _, ok := referencedLayers[layer]; ok {
referencedLayers[layer] = true
}
if _, ok := referencedROLayers[layer]; ok {
referencedROLayers[layer] = true
}
}
if isReadWrite {
if len(report.Layers[layer]) > 0 {
report.Images[id] = append(report.Images[id], report.Layers[layer]...)
}
if len(report.ROLayers[layer]) > 0 {
report.Images[id] = append(report.Images[id], report.ROLayers[layer]...)
}
} else {
if len(report.Layers[layer]) > 0 {
report.ROImages[id] = append(report.ROImages[id], report.Layers[layer]...)
}
if len(report.ROLayers[layer]) > 0 {
report.ROImages[id] = append(report.ROImages[id], report.ROLayers[layer]...)
}
}
}
}
}
return struct{}{}, false, nil
}); err != nil {
return CheckReport{}, err
}
// Iterate over each container in turn.
if _, _, err := readContainerStore(s, func() (struct{}, bool, error) {
containers, err := s.containerStore.Containers()
if err != nil {
return struct{}{}, true, err
}
for i := range containers {
container := containers[i]
id := container.ID
logrus.Debugf("checking container %s", id)
if options.ContainerData {
// Check that all of the big data items are present and reading them
// back gives us the right amount of data.
for _, key := range container.BigDataNames {
func() {
data, err := s.containerStore.BigData(id, key)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
err = fmt.Errorf("container %s: data item %q: %w", id, key, ErrContainerDataMissing)
report.Containers[id] = append(report.Containers[id], err)
return
}
err = fmt.Errorf("container %s: data item %q: %w", id, key, err)
report.Containers[id] = append(report.Containers[id], err)
return
}
if int64(len(data)) != container.BigDataSizes[key] {
err = fmt.Errorf("container %s: data item %q: %w", id, key, ErrContainerDataIncorrectSize)
report.Containers[id] = append(report.Containers[id], err)
return
}
}()
}
}
// Look at the container's base image. If the image has errors, the image's errors
// are the container's errors.
if container.ImageID != "" {
if _, checked := examinedImages[container.ImageID]; !checked {
err := fmt.Errorf("image %s: %w", container.ImageID, ErrContainerImageMissing)
report.Containers[id] = append(report.Containers[id], err)
}
if len(report.Images[container.ImageID]) > 0 {
report.Containers[id] = append(report.Containers[id], report.Images[container.ImageID]...)
}
if len(report.ROImages[container.ImageID]) > 0 {
report.Containers[id] = append(report.Containers[id], report.ROImages[container.ImageID]...)
}
}
// Count the container's layer as referenced.
if container.LayerID != "" {
referencedLayers[container.LayerID] = true
}
}
return struct{}{}, false, nil
}); err != nil {
return CheckReport{}, err
}
// Now go back through all of the layer stores, and flag any layers which don't belong
// to an image or a container, and has been around longer than we can reasonably expect
// such a layer to be present before a corresponding image record is added.
if _, _, err := readAllLayerStores(s, func(store roLayerStore) (struct{}, bool, error) {
if isReadWrite := roLayerStoreIsReallyReadWrite(store); !isReadWrite {
return struct{}{}, false, nil
}
layers, err := store.Layers()
if err != nil {
return struct{}{}, true, err
}
for _, layer := range layers {
maximumAge := defaultMaximumUnreferencedLayerAge
if options.LayerUnreferencedMaximumAge != nil {
maximumAge = *options.LayerUnreferencedMaximumAge
}
if referenced := referencedLayers[layer.ID]; !referenced {
if layer.Created.IsZero() || layer.Created.Add(maximumAge).Before(time.Now()) {
// Either we don't (and never will) know when this layer was
// created, or it was created far enough in the past that we're
// reasonably sure it's not part of an image that's being written
// right now.
err := fmt.Errorf("layer %s: %w", layer.ID, ErrLayerUnreferenced)
report.Layers[layer.ID] = append(report.Layers[layer.ID], err)
}
}
}
return struct{}{}, false, nil
}); err != nil {
return CheckReport{}, err
}
// If the driver can tell us about which layers it knows about, we should have previously
// examined all of them. Any that we didn't are probably just wasted space.
// Note: if the driver doesn't support enumerating layers, it returns ErrNotSupported.
if err := s.startUsingGraphDriver(); err != nil {
return CheckReport{}, err
}
defer s.stopUsingGraphDriver()
layerList, err := s.graphDriver.ListLayers()
if err != nil && !errors.Is(err, drivers.ErrNotSupported) {
return CheckReport{}, err
}
if !errors.Is(err, drivers.ErrNotSupported) {
for i, id := range layerList {
if _, known := referencedLayers[id]; !known {
err := fmt.Errorf("layer %s: %w", id, ErrLayerUnaccounted)
report.Layers[id] = append(report.Layers[id], err)
}
report.layerOrder[id] = i + 1
}
}
return report, nil
}
func roLayerStoreIsReallyReadWrite(store roLayerStore) bool {
return store.(*layerStore).lockfile.IsReadWrite()
}
func roImageStoreIsReallyReadWrite(store roImageStore) bool {
return store.(*imageStore).lockfile.IsReadWrite()
}
// Repair removes items which are themselves damaged, or which depend on items which are damaged.
// Errors are returned if an attempt to delete an item fails.
func (s *store) Repair(report CheckReport, options *RepairOptions) []error {
if options == nil {
options = RepairEverything()
}
var errs []error
// Just delete damaged containers.
if options.RemoveContainers {
for id := range report.Containers {
err := s.DeleteContainer(id)
if err != nil && !errors.Is(err, ErrContainerUnknown) {
err := fmt.Errorf("deleting container %s: %w", id, err)
errs = append(errs, err)
}
}
}
// Now delete damaged images. Note which layers were removed as part of removing those images.
deletedLayers := make(map[string]struct{})
for id := range report.Images {
layers, err := s.DeleteImage(id, true)
if err != nil {
if !errors.Is(err, ErrImageUnknown) && !errors.Is(err, ErrLayerUnknown) {
err := fmt.Errorf("deleting image %s: %w", id, err)
errs = append(errs, err)
}
} else {
for _, layer := range layers {
logrus.Debugf("deleted layer %s", layer)
deletedLayers[layer] = struct{}{}
}
logrus.Debugf("deleted image %s", id)
}
}
// Build a list of the layers that we need to remove, sorted with parents of layers before
// layers that they are parents of.
layersToDelete := make([]string, 0, len(report.Layers))
for id := range report.Layers {
layersToDelete = append(layersToDelete, id)
}
depth := func(id string) int {
d := 0
parent := report.layerParentsByLayerID[id]
for parent != "" {
d++
parent = report.layerParentsByLayerID[parent]
}
return d
}
isUnaccounted := func(errs []error) bool {
for _, err := range errs {
if errors.Is(err, ErrLayerUnaccounted) {
return true
}
}
return false
}
sort.Slice(layersToDelete, func(i, j int) bool {
// we've not heard of either of them, so remove them in the order the driver suggested
if isUnaccounted(report.Layers[layersToDelete[i]]) &&
isUnaccounted(report.Layers[layersToDelete[j]]) &&
report.layerOrder[layersToDelete[i]] != 0 && report.layerOrder[layersToDelete[j]] != 0 {
return report.layerOrder[layersToDelete[i]] < report.layerOrder[layersToDelete[j]]
}
// always delete the one we've heard of first
if isUnaccounted(report.Layers[layersToDelete[i]]) && !isUnaccounted(report.Layers[layersToDelete[j]]) {
return false
}
// always delete the one we've heard of first
if !isUnaccounted(report.Layers[layersToDelete[i]]) && isUnaccounted(report.Layers[layersToDelete[j]]) {
return true
}
// we've heard of both of them; the one that's on the end of a longer chain goes first
return depth(layersToDelete[i]) > depth(layersToDelete[j]) // closer-to-a-notional-base layers get removed later
})
// Now delete the layers that haven't been removed along with images.
for _, id := range layersToDelete {
if _, ok := deletedLayers[id]; ok {
continue
}
for _, reportedErr := range report.Layers[id] {
var err error
// If a layer was unaccounted for, remove it at the storage driver level.
// Otherwise, remove it at the higher level and let the higher level
// logic worry about telling the storage driver to delete the layer.
if errors.Is(reportedErr, ErrLayerUnaccounted) {
if err = s.graphDriver.Remove(id); err != nil {
err = fmt.Errorf("deleting storage layer %s: %v", id, err)
} else {
logrus.Debugf("deleted storage layer %s", id)
}
} else {
var stillMounted bool
if stillMounted, err = s.Unmount(id, true); err == nil && !stillMounted {
logrus.Debugf("unmounted layer %s", id)
} else if err != nil {
logrus.Debugf("unmounting layer %s: %v", id, err)
} else {
logrus.Debugf("layer %s still mounted", id)
}
if err = s.DeleteLayer(id); err != nil {
err = fmt.Errorf("deleting layer %s: %w", id, err)
logrus.Debugf("deleted layer %s", id)
}
}
if err != nil && !errors.Is(err, ErrLayerUnknown) && !errors.Is(err, ErrNotALayer) && !errors.Is(err, os.ErrNotExist) {
errs = append(errs, err)
}
}
}
return errs
}
// compareFileInfo returns a string summarizing what's different between the two checkFileInfos
func compareFileInfo(a, b checkFileInfo, idmap *idtools.IDMappings, ignore checkIgnore) string {
var comparison []string
if a.typeflag != b.typeflag {
comparison = append(comparison, fmt.Sprintf("filetype:%v→%v", a.typeflag, b.typeflag))
}
if idmap != nil && !idmap.Empty() {
mappedUID, mappedGID, err := idmap.ToContainer(idtools.IDPair{UID: b.uid, GID: b.gid})
if err != nil {
return err.Error()
}
b.uid, b.gid = mappedUID, mappedGID
}
if a.uid != b.uid && !ignore.ownership {
comparison = append(comparison, fmt.Sprintf("uid:%d→%d", a.uid, b.uid))
}
if a.gid != b.gid && !ignore.ownership {
comparison = append(comparison, fmt.Sprintf("gid:%d→%d", a.gid, b.gid))
}
if a.size != b.size {
comparison = append(comparison, fmt.Sprintf("size:%d→%d", a.size, b.size))
}
if (os.ModeType|os.ModePerm)&a.mode != (os.ModeType|os.ModePerm)&b.mode && !ignore.permissions {
comparison = append(comparison, fmt.Sprintf("mode:%04o→%04o", a.mode, b.mode))
}
if a.mtime != b.mtime && !ignore.timestamps {
comparison = append(comparison, fmt.Sprintf("mtime:0x%x→0x%x", a.mtime, b.mtime))
}
return strings.Join(comparison, ",")
}
// checkFileInfo is what we care about for files
type checkFileInfo struct {
typeflag byte
uid, gid int
size int64
mode os.FileMode
mtime int64 // unix-style whole seconds
}
// checkDirectory is a node in a filesystem record, possibly the top
type checkDirectory struct {
directory map[string]*checkDirectory // subdirectories
file map[string]checkFileInfo // non-directories
checkFileInfo
}
// newCheckDirectory creates an empty checkDirectory
func newCheckDirectory(uid, gid int, size int64, mode os.FileMode, mtime int64) *checkDirectory {
return &checkDirectory{
directory: make(map[string]*checkDirectory),
file: make(map[string]checkFileInfo),
checkFileInfo: checkFileInfo{
typeflag: tar.TypeDir,
uid: uid,
gid: gid,
size: size,
mode: mode,
mtime: mtime,
},
}
}
// newCheckDirectoryDefaults creates an empty checkDirectory with hardwired defaults for the UID
// (0), GID (0), size (0) and permissions (0o555)
func newCheckDirectoryDefaults() *checkDirectory {
return newCheckDirectory(0, 0, 0, 0o555, time.Now().Unix())
}
// newCheckDirectoryFromDirectory creates a checkDirectory for an on-disk directory tree
func newCheckDirectoryFromDirectory(dir string) (*checkDirectory, error) {
cd := newCheckDirectoryDefaults()
err := filepath.Walk(dir, func(walkpath string, info os.FileInfo, err error) error {
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
rel, err := filepath.Rel(dir, walkpath)
if err != nil {
return err
}
hdr, err := tar.FileInfoHeader(info, "") // we don't record link targets, so don't bother looking it up
if err != nil {
return err
}
hdr.Name = filepath.ToSlash(rel)
cd.header(hdr)
return nil
})
if err != nil {
return nil, err
}
return cd, nil
}
// add adds an item to a checkDirectory
func (c *checkDirectory) add(path string, typeflag byte, uid, gid int, size int64, mode os.FileMode, mtime int64) {
components := strings.Split(path, "/")
if components[len(components)-1] == "" {
components = components[:len(components)-1]
}
if components[0] == "." {
components = components[1:]
}
if typeflag != tar.TypeReg {
size = 0
}
switch len(components) {
case 0:
c.uid = uid
c.gid = gid
c.mode = mode
c.mtime = mtime
return
case 1:
switch typeflag {
case tar.TypeDir:
delete(c.file, components[0])
// directory entries are mergers, not replacements
if _, present := c.directory[components[0]]; !present {
c.directory[components[0]] = newCheckDirectory(uid, gid, size, mode, mtime)
} else {
c.directory[components[0]].checkFileInfo = checkFileInfo{
typeflag: tar.TypeDir,
uid: uid,
gid: gid,
size: size,
mode: mode,
mtime: mtime,
}
}
default:
// treat these as TypeReg items
delete(c.directory, components[0])
c.file[components[0]] = checkFileInfo{
typeflag: typeflag,
uid: uid,
gid: gid,
size: size,
mode: mode,
mtime: mtime,
}
case tar.TypeXGlobalHeader:
// ignore, since even though it looks like a valid pathname, it doesn't end
// up on the filesystem
}
return
}
subdirectory := c.directory[components[0]]
if subdirectory == nil {
subdirectory = newCheckDirectory(uid, gid, size, mode, mtime)
c.directory[components[0]] = subdirectory
}
subdirectory.add(strings.Join(components[1:], "/"), typeflag, uid, gid, size, mode, mtime)
}
// remove removes an item from a checkDirectory
func (c *checkDirectory) remove(path string) {
components := strings.Split(path, "/")
if len(components) == 1 {
delete(c.directory, components[0])
delete(c.file, components[0])
return
}
subdirectory := c.directory[components[0]]
if subdirectory != nil {
subdirectory.remove(strings.Join(components[1:], "/"))
}
}
// header updates a checkDirectory using information from the passed-in header
func (c *checkDirectory) header(hdr *tar.Header) {
name := path.Clean(hdr.Name)
dir, base := path.Split(name)