2018-02-07 15:52:47 -05:00
|
|
|
package images // import "github.com/docker/docker/daemon/images"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-24 13:06:17 -04:00
|
|
|
"fmt"
|
2018-02-07 15:52:47 -05:00
|
|
|
"os"
|
|
|
|
|
2020-10-30 15:47:06 -04:00
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
"github.com/containerd/containerd/leases"
|
2021-06-24 13:06:17 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2018-02-07 15:52:47 -05:00
|
|
|
"github.com/docker/docker/container"
|
|
|
|
daemonevents "github.com/docker/docker/daemon/events"
|
|
|
|
"github.com/docker/docker/distribution/metadata"
|
|
|
|
"github.com/docker/docker/distribution/xfer"
|
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/layer"
|
|
|
|
dockerreference "github.com/docker/docker/reference"
|
|
|
|
"github.com/docker/docker/registry"
|
2019-06-17 20:23:44 -04:00
|
|
|
"github.com/docker/libtrust"
|
2022-03-04 08:49:42 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2018-02-07 15:52:47 -05:00
|
|
|
"github.com/pkg/errors"
|
2021-06-24 13:06:17 -04:00
|
|
|
"golang.org/x/sync/singleflight"
|
2018-02-07 15:52:47 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type containerStore interface {
|
|
|
|
// used by image delete
|
|
|
|
First(container.StoreFilter) *container.Container
|
|
|
|
// used by image prune, and image list
|
|
|
|
List() []*container.Container
|
|
|
|
// TODO: remove, only used for CommitBuildStep
|
|
|
|
Get(string) *container.Container
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImageServiceConfig is the configuration used to create a new ImageService
|
|
|
|
type ImageServiceConfig struct {
|
|
|
|
ContainerStore containerStore
|
|
|
|
DistributionMetadataStore metadata.Store
|
|
|
|
EventsService *daemonevents.Events
|
|
|
|
ImageStore image.Store
|
2021-03-19 10:34:08 -04:00
|
|
|
LayerStore layer.Store
|
2018-02-07 15:52:47 -05:00
|
|
|
MaxConcurrentDownloads int
|
|
|
|
MaxConcurrentUploads int
|
2019-06-25 09:26:36 -04:00
|
|
|
MaxDownloadAttempts int
|
2018-02-07 15:52:47 -05:00
|
|
|
ReferenceStore dockerreference.Store
|
|
|
|
RegistryService registry.Service
|
2019-06-17 20:23:44 -04:00
|
|
|
TrustKey libtrust.PrivateKey
|
2020-10-30 15:47:06 -04:00
|
|
|
ContentStore content.Store
|
|
|
|
Leases leases.Manager
|
|
|
|
ContentNamespace string
|
2018-02-07 15:52:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewImageService returns a new ImageService from a configuration
|
|
|
|
func NewImageService(config ImageServiceConfig) *ImageService {
|
|
|
|
return &ImageService{
|
|
|
|
containers: config.ContainerStore,
|
|
|
|
distributionMetadataStore: config.DistributionMetadataStore,
|
2021-03-19 10:34:08 -04:00
|
|
|
downloadManager: xfer.NewLayerDownloadManager(config.LayerStore, config.MaxConcurrentDownloads, xfer.WithMaxDownloadAttempts(config.MaxDownloadAttempts)),
|
2018-02-07 15:52:47 -05:00
|
|
|
eventsService: config.EventsService,
|
2020-10-30 15:47:06 -04:00
|
|
|
imageStore: &imageStoreWithLease{Store: config.ImageStore, leases: config.Leases, ns: config.ContentNamespace},
|
2021-03-19 10:34:08 -04:00
|
|
|
layerStore: config.LayerStore,
|
2018-02-07 15:52:47 -05:00
|
|
|
referenceStore: config.ReferenceStore,
|
|
|
|
registryService: config.RegistryService,
|
2019-06-17 20:23:44 -04:00
|
|
|
trustKey: config.TrustKey,
|
2018-02-07 15:52:47 -05:00
|
|
|
uploadManager: xfer.NewLayerUploadManager(config.MaxConcurrentUploads),
|
2020-10-30 15:47:06 -04:00
|
|
|
leases: config.Leases,
|
|
|
|
content: config.ContentStore,
|
|
|
|
contentNamespace: config.ContentNamespace,
|
2018-02-07 15:52:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImageService provides a backend for image management
|
|
|
|
type ImageService struct {
|
|
|
|
containers containerStore
|
|
|
|
distributionMetadataStore metadata.Store
|
|
|
|
downloadManager *xfer.LayerDownloadManager
|
|
|
|
eventsService *daemonevents.Events
|
|
|
|
imageStore image.Store
|
2021-03-19 10:34:08 -04:00
|
|
|
layerStore layer.Store
|
2018-02-07 15:52:47 -05:00
|
|
|
pruneRunning int32
|
|
|
|
referenceStore dockerreference.Store
|
|
|
|
registryService registry.Service
|
2019-06-17 20:23:44 -04:00
|
|
|
trustKey libtrust.PrivateKey
|
2018-02-07 15:52:47 -05:00
|
|
|
uploadManager *xfer.LayerUploadManager
|
2020-10-30 15:47:06 -04:00
|
|
|
leases leases.Manager
|
|
|
|
content content.Store
|
|
|
|
contentNamespace string
|
2021-06-24 13:06:17 -04:00
|
|
|
usage singleflight.Group
|
2018-02-07 15:52:47 -05:00
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// DistributionServices provides daemon image storage services
|
2018-04-17 14:56:28 -04:00
|
|
|
type DistributionServices struct {
|
2022-01-21 07:48:44 -05:00
|
|
|
DownloadManager *xfer.LayerDownloadManager
|
2018-04-17 14:56:28 -04:00
|
|
|
V2MetadataService metadata.V2MetadataService
|
2021-03-19 10:34:08 -04:00
|
|
|
LayerStore layer.Store
|
2018-04-17 14:56:28 -04:00
|
|
|
ImageStore image.Store
|
|
|
|
ReferenceStore dockerreference.Store
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
// DistributionServices return services controlling daemon image storage
|
2018-04-17 14:56:28 -04:00
|
|
|
func (i *ImageService) DistributionServices() DistributionServices {
|
|
|
|
return DistributionServices{
|
|
|
|
DownloadManager: i.downloadManager,
|
|
|
|
V2MetadataService: metadata.NewV2MetadataService(i.distributionMetadataStore),
|
2021-03-19 10:34:08 -04:00
|
|
|
LayerStore: i.layerStore,
|
2018-04-17 14:56:28 -04:00
|
|
|
ImageStore: i.imageStore,
|
|
|
|
ReferenceStore: i.referenceStore,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 15:52:47 -05:00
|
|
|
// CountImages returns the number of images stored by ImageService
|
|
|
|
// called from info.go
|
|
|
|
func (i *ImageService) CountImages() int {
|
2018-03-13 11:58:05 -04:00
|
|
|
return i.imageStore.Len()
|
2018-02-07 15:52:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Children returns the children image.IDs for a parent image.
|
|
|
|
// called from list.go to filter containers
|
|
|
|
// TODO: refactor to expose an ancestry for image.ID?
|
|
|
|
func (i *ImageService) Children(id image.ID) []image.ID {
|
|
|
|
return i.imageStore.Children(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateLayer creates a filesystem layer for a container.
|
|
|
|
// called from create.go
|
|
|
|
// TODO: accept an opt struct instead of container?
|
|
|
|
func (i *ImageService) CreateLayer(container *container.Container, initFunc layer.MountInit) (layer.RWLayer, error) {
|
|
|
|
var layerID layer.ChainID
|
|
|
|
if container.ImageID != "" {
|
|
|
|
img, err := i.imageStore.Get(container.ImageID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
layerID = img.RootFS.ChainID()
|
|
|
|
}
|
|
|
|
|
|
|
|
rwLayerOpts := &layer.CreateRWLayerOpts{
|
|
|
|
MountLabel: container.MountLabel,
|
|
|
|
InitFunc: initFunc,
|
|
|
|
StorageOpt: container.HostConfig.StorageOpt,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Indexing by OS is safe here as validation of OS has already been performed in create() (the only
|
|
|
|
// caller), and guaranteed non-nil
|
2021-03-19 10:34:08 -04:00
|
|
|
return i.layerStore.CreateRWLayer(container.ID, layerID, rwLayerOpts)
|
2018-02-07 15:52:47 -05:00
|
|
|
}
|
|
|
|
|
2021-03-19 10:34:08 -04:00
|
|
|
// GetLayerByID returns a layer by ID
|
2018-02-07 15:52:47 -05:00
|
|
|
// called from daemon.go Daemon.restore(), and Daemon.containerExport()
|
2021-03-19 10:34:08 -04:00
|
|
|
func (i *ImageService) GetLayerByID(cid string) (layer.RWLayer, error) {
|
|
|
|
return i.layerStore.GetRWLayer(cid)
|
2018-02-07 15:52:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// LayerStoreStatus returns the status for each layer store
|
|
|
|
// called from info.go
|
2021-03-19 10:34:08 -04:00
|
|
|
func (i *ImageService) LayerStoreStatus() [][2]string {
|
|
|
|
return i.layerStore.DriverStatus()
|
2018-02-07 15:52:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLayerMountID returns the mount ID for a layer
|
|
|
|
// called from daemon.go Daemon.Shutdown(), and Daemon.Cleanup() (cleanup is actually continerCleanup)
|
2021-03-19 10:34:08 -04:00
|
|
|
// TODO: needs to be refactored to Unmount (see callers), or removed and replaced with GetLayerByID
|
|
|
|
func (i *ImageService) GetLayerMountID(cid string) (string, error) {
|
|
|
|
return i.layerStore.GetMountID(cid)
|
2018-02-07 15:52:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup resources before the process is shutdown.
|
|
|
|
// called from daemon.go Daemon.Shutdown()
|
2022-01-21 14:25:01 -05:00
|
|
|
func (i *ImageService) Cleanup() error {
|
2021-03-19 10:34:08 -04:00
|
|
|
if err := i.layerStore.Cleanup(); err != nil {
|
2022-01-21 14:25:01 -05:00
|
|
|
return errors.Wrap(err, "error during layerStore.Cleanup()")
|
2018-02-07 15:52:47 -05:00
|
|
|
}
|
2022-01-21 14:25:01 -05:00
|
|
|
return nil
|
2018-02-07 15:52:47 -05:00
|
|
|
}
|
|
|
|
|
2021-03-19 10:34:08 -04:00
|
|
|
// GraphDriverName returns the name of the graph drvier
|
2018-02-07 15:52:47 -05:00
|
|
|
// moved from Daemon.GraphDriverName, used by:
|
|
|
|
// - newContainer
|
|
|
|
// - to report an error in Daemon.Mount(container)
|
2021-03-19 10:34:08 -04:00
|
|
|
func (i *ImageService) GraphDriverName() string {
|
|
|
|
return i.layerStore.DriverName()
|
2018-02-07 15:52:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReleaseLayer releases a layer allowing it to be removed
|
|
|
|
// called from delete.go Daemon.cleanupContainer(), and Daemon.containerExport()
|
2022-01-25 06:34:32 -05:00
|
|
|
func (i *ImageService) ReleaseLayer(rwlayer layer.RWLayer) error {
|
2021-03-19 10:34:08 -04:00
|
|
|
metadata, err := i.layerStore.ReleaseRWLayer(rwlayer)
|
2018-02-07 15:52:47 -05:00
|
|
|
layer.LogReleaseMetadata(metadata)
|
2020-04-17 06:01:01 -04:00
|
|
|
if err != nil && !errors.Is(err, layer.ErrMountDoesNotExist) && !errors.Is(err, os.ErrNotExist) {
|
2018-02-07 15:52:47 -05:00
|
|
|
return errors.Wrapf(err, "driver %q failed to remove root filesystem",
|
2021-03-19 10:34:08 -04:00
|
|
|
i.layerStore.DriverName())
|
2018-02-07 15:52:47 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LayerDiskUsage returns the number of bytes used by layer stores
|
|
|
|
// called from disk_usage.go
|
|
|
|
func (i *ImageService) LayerDiskUsage(ctx context.Context) (int64, error) {
|
2021-06-24 13:06:17 -04:00
|
|
|
ch := i.usage.DoChan("LayerDiskUsage", func() (interface{}, error) {
|
|
|
|
var allLayersSize int64
|
|
|
|
layerRefs := i.getLayerRefs()
|
|
|
|
allLayers := i.layerStore.Map()
|
|
|
|
for _, l := range allLayers {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return allLayersSize, ctx.Err()
|
|
|
|
default:
|
2022-01-21 13:01:34 -05:00
|
|
|
size := l.DiffSize()
|
|
|
|
if _, ok := layerRefs[l.ChainID()]; ok {
|
|
|
|
allLayersSize += size
|
2018-02-07 15:52:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 13:06:17 -04:00
|
|
|
return allLayersSize, nil
|
|
|
|
})
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return 0, ctx.Err()
|
|
|
|
case res := <-ch:
|
|
|
|
if res.Err != nil {
|
|
|
|
return 0, res.Err
|
|
|
|
}
|
|
|
|
return res.Val.(int64), nil
|
2018-02-07 15:52:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ImageService) getLayerRefs() map[layer.ChainID]int {
|
|
|
|
tmpImages := i.imageStore.Map()
|
|
|
|
layerRefs := map[layer.ChainID]int{}
|
|
|
|
for id, img := range tmpImages {
|
|
|
|
dgst := digest.Digest(id)
|
|
|
|
if len(i.referenceStore.References(dgst)) == 0 && len(i.imageStore.Children(id)) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rootFS := *img.RootFS
|
|
|
|
rootFS.DiffIDs = nil
|
|
|
|
for _, id := range img.RootFS.DiffIDs {
|
|
|
|
rootFS.Append(id)
|
|
|
|
chid := rootFS.ChainID()
|
|
|
|
layerRefs[chid]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return layerRefs
|
|
|
|
}
|
|
|
|
|
2021-06-24 13:06:17 -04:00
|
|
|
// ImageDiskUsage returns information about image data disk usage.
|
|
|
|
func (i *ImageService) ImageDiskUsage(ctx context.Context) ([]*types.ImageSummary, error) {
|
|
|
|
ch := i.usage.DoChan("ImageDiskUsage", func() (interface{}, error) {
|
|
|
|
// Get all top images with extra attributes
|
|
|
|
images, err := i.Images(ctx, types.ImageListOptions{
|
|
|
|
Filters: filters.NewArgs(),
|
|
|
|
SharedSize: true,
|
|
|
|
ContainerCount: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to retrieve image list: %v", err)
|
|
|
|
}
|
|
|
|
return images, nil
|
|
|
|
})
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
case res := <-ch:
|
|
|
|
if res.Err != nil {
|
|
|
|
return nil, res.Err
|
|
|
|
}
|
|
|
|
return res.Val.([]*types.ImageSummary), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 15:52:47 -05:00
|
|
|
// UpdateConfig values
|
|
|
|
//
|
|
|
|
// called from reload.go
|
|
|
|
func (i *ImageService) UpdateConfig(maxDownloads, maxUploads *int) {
|
|
|
|
if i.downloadManager != nil && maxDownloads != nil {
|
|
|
|
i.downloadManager.SetConcurrency(*maxDownloads)
|
|
|
|
}
|
|
|
|
if i.uploadManager != nil && maxUploads != nil {
|
|
|
|
i.uploadManager.SetConcurrency(*maxUploads)
|
|
|
|
}
|
|
|
|
}
|