diff --git a/libimage/corrupted_test.go b/libimage/corrupted_test.go index 011d9953a..685419d0a 100644 --- a/libimage/corrupted_test.go +++ b/libimage/corrupted_test.go @@ -43,6 +43,8 @@ func TestCorruptedLayers(t *testing.T) { // Disk usage works. _, err = runtime.DiskUsage(ctx) require.NoError(t, err, "disk usage works on healthy image") + _, err = runtime.LayersDiskUsage(ctx) + require.NoError(t, err, "layers 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 diff --git a/libimage/disk_usage.go b/libimage/disk_usage.go index 2cde09846..455bbec55 100644 --- a/libimage/disk_usage.go +++ b/libimage/disk_usage.go @@ -5,6 +5,20 @@ import ( "time" ) +// LayersDiskUsage returns the sum of the size of all layers in the current store. +func (r *Runtime) LayersDiskUsage(ctx context.Context) (int64, error) { + layers, err := r.store.Layers() + if err != nil { + return -1, err + } + + var size int64 + for _, l := range layers { + size += l.UncompressedSize + } + + return size, nil +} // ImageDiskUsage reports the total size of an image. That is the size type ImageDiskUsage struct { // Number of containers using the image.