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 (
|
|
|
|
"fmt"
|
2017-04-12 16:59:59 -04:00
|
|
|
"sync/atomic"
|
2016-08-23 19:24:15 -04:00
|
|
|
|
2017-04-12 16:59:31 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
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
|
|
|
"github.com/docker/docker/layer"
|
|
|
|
"github.com/docker/docker/pkg/directory"
|
|
|
|
"github.com/docker/docker/volume"
|
2017-01-06 20:23:18 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-08-23 19:24:15 -04:00
|
|
|
)
|
|
|
|
|
2017-08-24 14:48:16 -04:00
|
|
|
func (daemon *Daemon) getLayerRefs() map[layer.ChainID]int {
|
|
|
|
tmpImages := daemon.imageStore.Map()
|
2016-08-23 19:24:15 -04:00
|
|
|
layerRefs := map[layer.ChainID]int{}
|
|
|
|
for id, img := range tmpImages {
|
|
|
|
dgst := digest.Digest(id)
|
2017-08-24 14:48:16 -04:00
|
|
|
if len(daemon.referenceStore.References(dgst)) == 0 && len(daemon.imageStore.Children(id)) != 0 {
|
2016-08-23 19:24:15 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rootFS := *img.RootFS
|
|
|
|
rootFS.DiffIDs = nil
|
|
|
|
for _, id := range img.RootFS.DiffIDs {
|
|
|
|
rootFS.Append(id)
|
|
|
|
chid := rootFS.ChainID()
|
|
|
|
layerRefs[chid]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return layerRefs
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-11-11 09:34:01 -05:00
|
|
|
allImages, err := daemon.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all local volumes
|
|
|
|
allVolumes := []*types.Volume{}
|
|
|
|
getLocalVols := func(v volume.Volume) error {
|
2017-04-12 16:59:31 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
2017-06-09 18:25:05 -04:00
|
|
|
if d, ok := v.(volume.DetailedVolume); ok {
|
|
|
|
// skip local volumes with mount options since these could have external
|
|
|
|
// mounted filesystems that will be slow to enumerate.
|
|
|
|
if len(d.Options()) > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2017-04-12 16:59:31 -04:00
|
|
|
name := v.Name()
|
|
|
|
refs := daemon.volumes.Refs(v)
|
2016-08-23 19:24:15 -04:00
|
|
|
|
2017-04-12 16:59:31 -04:00
|
|
|
tv := volumeToAPIType(v)
|
|
|
|
sz, err := directory.Size(v.Path())
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("failed to determine size of volume %v", name)
|
|
|
|
sz = -1
|
|
|
|
}
|
|
|
|
tv.UsageData = &types.VolumeUsageData{Size: sz, RefCount: int64(len(refs))}
|
|
|
|
allVolumes = append(allVolumes, tv)
|
2016-08-23 19:24:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = daemon.traverseLocalVolumes(getLocalVols)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get total layers size on disk
|
|
|
|
var allLayersSize int64
|
2017-08-24 14:48:16 -04:00
|
|
|
layerRefs := daemon.getLayerRefs()
|
2017-09-19 15:14:46 -04:00
|
|
|
for _, ls := range daemon.layerStores {
|
|
|
|
allLayers := ls.Map()
|
|
|
|
for _, l := range allLayers {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
default:
|
|
|
|
size, err := l.DiffSize()
|
|
|
|
if err == nil {
|
|
|
|
if _, ok := layerRefs[l.ChainID()]; ok {
|
|
|
|
allLayersSize += size
|
|
|
|
} else {
|
|
|
|
logrus.Warnf("found leaked image layer %v", l.ChainID())
|
|
|
|
}
|
2017-04-12 16:59:31 -04:00
|
|
|
} else {
|
2017-09-19 15:14:46 -04:00
|
|
|
logrus.Warnf("failed to get diff size for layer %v", l.ChainID())
|
2017-04-12 16:59:31 -04:00
|
|
|
}
|
2016-08-23 19:24:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.DiskUsage{
|
|
|
|
LayersSize: allLayersSize,
|
|
|
|
Containers: allContainers,
|
|
|
|
Volumes: allVolumes,
|
|
|
|
Images: allImages,
|
|
|
|
}, nil
|
|
|
|
}
|