From cbe8b458b2cc44c79e075cc061f8b452566e9de7 Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Mon, 17 Oct 2022 14:26:03 +0200 Subject: [PATCH] libimage: add LayersDiskUsage Add an API to query the sum of the layer sizes. This data is needed to fix containers/podman/issues/16135. Signed-off-by: Valentin Rothberg --- libimage/corrupted_test.go | 2 ++ libimage/disk_usage.go | 14 ++++++++++++++ 2 files changed, 16 insertions(+) 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.