Skip to content

Commit

Permalink
libimage: add LayersDiskUsage
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
vrothberg committed Oct 17, 2022
1 parent bbbc188 commit 46db018
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libimage/corrupted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions libimage/disk_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ 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.
Expand Down

0 comments on commit 46db018

Please sign in to comment.