2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2016-08-23 19:24:15 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2016-08-23 19:24:15 -04:00
|
|
|
"fmt"
|
2017-04-12 16:59:31 -04:00
|
|
|
|
2021-06-23 09:26:54 -04:00
|
|
|
"github.com/docker/docker/api/server/router/system"
|
2016-08-23 19:24:15 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2021-08-04 12:42:50 -04:00
|
|
|
"golang.org/x/sync/errgroup"
|
2016-08-23 19:24:15 -04:00
|
|
|
)
|
|
|
|
|
2021-06-24 13:06:17 -04: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 16:59:59 -04:00
|
|
|
}
|
2021-06-24 13:06:17 -04:00
|
|
|
}
|
2017-04-12 16:59:59 -04:00
|
|
|
|
2021-06-24 13:06:17 -04: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 12:42:50 -04:00
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
2016-08-23 19:24:15 -04:00
|
|
|
|
2021-06-23 09:26:54 -04:00
|
|
|
var containers []*types.Container
|
|
|
|
if opts.Containers {
|
2021-08-04 12:42:50 -04:00
|
|
|
eg.Go(func() error {
|
|
|
|
var err error
|
2021-06-24 13:06:17 -04:00
|
|
|
containers, err = daemon.ContainerDiskUsage(ctx)
|
|
|
|
return err
|
2021-06-23 09:26:54 -04:00
|
|
|
})
|
2016-08-23 19:24:15 -04:00
|
|
|
}
|
|
|
|
|
2021-06-23 09:26:54 -04:00
|
|
|
var (
|
|
|
|
images []*types.ImageSummary
|
|
|
|
layersSize int64
|
|
|
|
)
|
|
|
|
if opts.Images {
|
2021-08-04 12:42:50 -04:00
|
|
|
eg.Go(func() error {
|
|
|
|
var err error
|
2021-06-24 13:06:17 -04:00
|
|
|
images, err = daemon.imageService.ImageDiskUsage(ctx)
|
|
|
|
return err
|
2021-08-04 12:42:50 -04:00
|
|
|
})
|
|
|
|
eg.Go(func() error {
|
|
|
|
var err error
|
|
|
|
layersSize, err = daemon.imageService.LayerDiskUsage(ctx)
|
|
|
|
return err
|
2021-06-23 09:26:54 -04:00
|
|
|
})
|
2016-08-23 19:24:15 -04:00
|
|
|
}
|
|
|
|
|
2021-06-23 09:26:54 -04:00
|
|
|
var volumes []*types.Volume
|
|
|
|
if opts.Volumes {
|
2021-08-04 12:42:50 -04: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 09:26:54 -04:00
|
|
|
}
|
2016-08-23 19:24:15 -04:00
|
|
|
return &types.DiskUsage{
|
2021-06-23 09:26:54 -04:00
|
|
|
LayersSize: layersSize,
|
|
|
|
Containers: containers,
|
|
|
|
Volumes: volumes,
|
|
|
|
Images: images,
|
2016-08-23 19:24:15 -04:00
|
|
|
}, nil
|
|
|
|
}
|