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:59 -04:00
|
|
|
"sync/atomic"
|
2017-04-12 16:59:31 -04:00
|
|
|
|
2016-08-23 19:24:15 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-11-11 09:34:01 -05:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2016-08-23 19:24:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// SystemDiskUsage returns information about the daemon data disk usage
|
2017-04-12 16:59:31 -04:00
|
|
|
func (daemon *Daemon) SystemDiskUsage(ctx context.Context) (*types.DiskUsage, error) {
|
2017-04-12 16:59:59 -04:00
|
|
|
if !atomic.CompareAndSwapInt32(&daemon.diskUsageRunning, 0, 1) {
|
|
|
|
return nil, fmt.Errorf("a disk usage operation is already running")
|
|
|
|
}
|
|
|
|
defer atomic.StoreInt32(&daemon.diskUsageRunning, 0)
|
|
|
|
|
2016-08-23 19:24:15 -04:00
|
|
|
// Retrieve container list
|
|
|
|
allContainers, err := daemon.Containers(&types.ContainerListOptions{
|
|
|
|
Size: true,
|
|
|
|
All: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to retrieve container list: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all top images with extra attributes
|
2018-02-02 17:18:46 -05:00
|
|
|
allImages, err := daemon.imageService.Images(filters.NewArgs(), false, true)
|
2016-08-23 19:24:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to retrieve image list: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-22 17:11:03 -04:00
|
|
|
localVolumes, err := daemon.volumes.LocalVolumesSize(ctx)
|
2018-03-19 11:43:54 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-02 17:18:46 -05:00
|
|
|
allLayersSize, err := daemon.imageService.LayerDiskUsage(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-08-23 19:24:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &types.DiskUsage{
|
|
|
|
LayersSize: allLayersSize,
|
|
|
|
Containers: allContainers,
|
2018-03-22 17:11:03 -04:00
|
|
|
Volumes: localVolumes,
|
2016-08-23 19:24:15 -04:00
|
|
|
Images: allImages,
|
|
|
|
}, nil
|
|
|
|
}
|