2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2016-08-23 23:24:15 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-08-23 23:24:15 +00:00
|
|
|
"fmt"
|
2017-04-12 20:59:31 +00:00
|
|
|
|
2021-06-23 13:26:54 +00:00
|
|
|
"github.com/docker/docker/api/server/router/system"
|
2016-08-23 23:24:15 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2022-03-18 15:33:43 +00:00
|
|
|
"github.com/docker/docker/api/types/volume"
|
2021-08-04 16:42:50 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2016-08-23 23:24:15 +00:00
|
|
|
)
|
|
|
|
|
2021-06-24 17:06:17 +00:00
|
|
|
// ContainerDiskUsage returns information about container data disk usage.
|
|
|
|
func (daemon *Daemon) ContainerDiskUsage(ctx context.Context) ([]*types.Container, error) {
|
|
|
|
ch := daemon.usage.DoChan("ContainerDiskUsage", func() (interface{}, error) {
|
|
|
|
// Retrieve container list
|
|
|
|
containers, err := daemon.Containers(&types.ContainerListOptions{
|
|
|
|
Size: true,
|
|
|
|
All: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to retrieve container list: %v", err)
|
|
|
|
}
|
|
|
|
return containers, nil
|
|
|
|
})
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
case res := <-ch:
|
|
|
|
if res.Err != nil {
|
|
|
|
return nil, res.Err
|
|
|
|
}
|
|
|
|
return res.Val.([]*types.Container), nil
|
2017-04-12 20:59:59 +00:00
|
|
|
}
|
2021-06-24 17:06:17 +00:00
|
|
|
}
|
2017-04-12 20:59:59 +00:00
|
|
|
|
2021-06-24 17:06:17 +00:00
|
|
|
// SystemDiskUsage returns information about the daemon data disk usage.
|
|
|
|
// Callers must not mutate contents of the returned fields.
|
|
|
|
func (daemon *Daemon) SystemDiskUsage(ctx context.Context, opts system.DiskUsageOptions) (*types.DiskUsage, error) {
|
2021-08-04 16:42:50 +00:00
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
2016-08-23 23:24:15 +00:00
|
|
|
|
2021-06-23 13:26:54 +00:00
|
|
|
var containers []*types.Container
|
|
|
|
if opts.Containers {
|
2021-08-04 16:42:50 +00:00
|
|
|
eg.Go(func() error {
|
|
|
|
var err error
|
2021-06-24 17:06:17 +00:00
|
|
|
containers, err = daemon.ContainerDiskUsage(ctx)
|
|
|
|
return err
|
2021-06-23 13:26:54 +00:00
|
|
|
})
|
2016-08-23 23:24:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 13:26:54 +00:00
|
|
|
var (
|
|
|
|
images []*types.ImageSummary
|
|
|
|
layersSize int64
|
|
|
|
)
|
|
|
|
if opts.Images {
|
2021-08-04 16:42:50 +00:00
|
|
|
eg.Go(func() error {
|
|
|
|
var err error
|
2021-06-24 17:06:17 +00:00
|
|
|
images, err = daemon.imageService.ImageDiskUsage(ctx)
|
|
|
|
return err
|
2021-08-04 16:42:50 +00:00
|
|
|
})
|
|
|
|
eg.Go(func() error {
|
|
|
|
var err error
|
|
|
|
layersSize, err = daemon.imageService.LayerDiskUsage(ctx)
|
|
|
|
return err
|
2021-06-23 13:26:54 +00:00
|
|
|
})
|
2016-08-23 23:24:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 15:33:43 +00:00
|
|
|
var volumes []*volume.Volume
|
2021-06-23 13:26:54 +00:00
|
|
|
if opts.Volumes {
|
2021-08-04 16:42:50 +00:00
|
|
|
eg.Go(func() error {
|
|
|
|
var err error
|
|
|
|
volumes, err = daemon.volumes.LocalVolumesSize(ctx)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return nil, err
|
2021-06-23 13:26:54 +00:00
|
|
|
}
|
2016-08-23 23:24:15 +00:00
|
|
|
return &types.DiskUsage{
|
2021-06-23 13:26:54 +00:00
|
|
|
LayersSize: layersSize,
|
|
|
|
Containers: containers,
|
|
|
|
Volumes: volumes,
|
|
|
|
Images: images,
|
2016-08-23 23:24:15 +00:00
|
|
|
}, nil
|
|
|
|
}
|