-
Notifications
You must be signed in to change notification settings - Fork 66
/
disk_usage.go
50 lines (42 loc) · 1.29 KB
/
disk_usage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package daemon // import "github.com/docker/docker/daemon"
import (
"context"
"fmt"
"sync/atomic"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
)
// SystemDiskUsage returns information about the daemon data disk usage
func (daemon *Daemon) SystemDiskUsage(ctx context.Context) (*types.DiskUsage, error) {
if !atomic.CompareAndSwapInt32(&daemon.diskUsageRunning, 0, 1) {
return nil, fmt.Errorf("a disk usage operation is already running")
}
defer atomic.StoreInt32(&daemon.diskUsageRunning, 0)
// 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
allImages, err := daemon.imageService.Images(filters.NewArgs(), false, true)
if err != nil {
return nil, fmt.Errorf("failed to retrieve image list: %v", err)
}
localVolumes, err := daemon.volumes.LocalVolumesSize(ctx)
if err != nil {
return nil, err
}
allLayersSize, err := daemon.imageService.LayerDiskUsage(ctx)
if err != nil {
return nil, err
}
return &types.DiskUsage{
LayersSize: allLayersSize,
Containers: allContainers,
Volumes: localVolumes,
Images: allImages,
}, nil
}