Skip to content

Commit

Permalink
libimage: disk usage: catch corrupted images
Browse files Browse the repository at this point in the history
Make sure to check an image for corruption before running disk usage on
it.  Such checks are already applied on various execution paths but not
yet on disk usage.

Further update the corrupted-image error to include that the image
should be removed to resolve the error.  This should ultimately guide
users to resolve the issue.

Fixes: #751
Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg committed Aug 31, 2021
1 parent 6143ffe commit ade9985
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
11 changes: 10 additions & 1 deletion libimage/corrupted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestCorruptedImage(t *testing.T) {
func TestCorruptedLayers(t *testing.T) {
// Regression tests for https://bugzilla.redhat.com/show_bug.cgi?id=1966872.
runtime, cleanup := testNewRuntime(t)
defer cleanup()
Expand All @@ -41,6 +41,10 @@ func TestCorruptedImage(t *testing.T) {
require.NoError(t, err, "healthy image exists")
require.True(t, exists, "healthy image exists")

// Disk usage works.
_, err = runtime.DiskUsage(ctx)
require.NoError(t, err, "disk usage works on healthy image")

// Now remove one layer from the layers.json index in the storage. The
// image will still be listed in the container storage but attempting
// to use it will yield "layer not known" errors.
Expand Down Expand Up @@ -71,6 +75,11 @@ func TestCorruptedImage(t *testing.T) {
require.NoError(t, err, "corrupted image exists should not fail")
require.False(t, exists, "corrupted image should not be marked to exist")

// Disk usage does not work.
_, err = runtime.DiskUsage(ctx)
require.Error(t, err, "disk usage does not work on corrupted image")
require.Contains(t, err.Error(), "exists in local storage but may be corrupted", "disk usage reports corrupted image")

// Now make sure that pull will detect the corrupted image and repulls
// if needed which will repair the data corruption.
pulledImages, err = runtime.Pull(ctx, imageName, config.PullPolicyNewer, pullOptions)
Expand Down
4 changes: 4 additions & 0 deletions libimage/disk_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func (r *Runtime) DiskUsage(ctx context.Context) ([]ImageDiskUsage, error) {

// diskUsageForImage returns the disk-usage baseistics for the specified image.
func diskUsageForImage(ctx context.Context, image *Image, tree *layerTree) ([]ImageDiskUsage, error) {
if err := image.isCorrupted(""); err != nil {
return nil, err
}

base := ImageDiskUsage{
ID: image.ID(),
Created: image.Created(),
Expand Down
5 changes: 4 additions & 1 deletion libimage/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ func (i *Image) isCorrupted(name string) error {
}

if _, err := ref.NewImage(context.Background(), nil); err != nil {
return errors.Errorf("Image %s exists in local storage but may be corrupted: %v", name, err)
if name == "" {
name = i.ID()[:12]
}
return errors.Errorf("Image %s exists in local storage but may be corrupted (remove the image to resolve the issue): %v", name, err)
}
return nil
}
Expand Down

0 comments on commit ade9985

Please sign in to comment.