2016-03-11 10:02:17 -05:00
|
|
|
// Package layer is package for managing read-only
|
2015-11-18 17:15:00 -05:00
|
|
|
// and read-write mounts on the union file system
|
2015-12-13 11:00:39 -05:00
|
|
|
// driver. Read-only mounts are referenced using a
|
2015-11-18 17:15:00 -05:00
|
|
|
// content hash and are protected from mutation in
|
|
|
|
// the exposed interface. The tar format is used
|
2016-03-11 10:02:17 -05:00
|
|
|
// to create read-only layers and export both
|
|
|
|
// read-only and writable layers. The exported
|
|
|
|
// tar data for a read-only layer should match
|
2015-11-18 17:15:00 -05:00
|
|
|
// the tar used to create the layer.
|
2018-02-05 16:05:59 -05:00
|
|
|
package layer // import "github.com/docker/docker/layer"
|
2015-11-18 17:15:00 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
|
2016-05-25 22:11:51 -04:00
|
|
|
"github.com/docker/distribution"
|
2015-11-18 17:15:00 -05:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2017-08-03 20:22:00 -04:00
|
|
|
"github.com/docker/docker/pkg/containerfs"
|
2022-03-04 08:49:42 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-11-18 17:15:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrLayerDoesNotExist is used when an operation is
|
|
|
|
// attempted on a layer which does not exist.
|
|
|
|
ErrLayerDoesNotExist = errors.New("layer does not exist")
|
|
|
|
|
|
|
|
// ErrLayerNotRetained is used when a release is
|
|
|
|
// attempted on a layer which is not retained.
|
|
|
|
ErrLayerNotRetained = errors.New("layer not retained")
|
|
|
|
|
|
|
|
// ErrMountDoesNotExist is used when an operation is
|
|
|
|
// attempted on a mount layer which does not exist.
|
|
|
|
ErrMountDoesNotExist = errors.New("mount does not exist")
|
|
|
|
|
2015-12-16 17:13:50 -05:00
|
|
|
// ErrMountNameConflict is used when a mount is attempted
|
|
|
|
// to be created but there is already a mount with the name
|
|
|
|
// used for creation.
|
|
|
|
ErrMountNameConflict = errors.New("mount already exists with name")
|
|
|
|
|
2015-11-18 17:15:00 -05:00
|
|
|
// ErrMaxDepthExceeded is used when a layer is attempted
|
|
|
|
// to be created which would result in a layer depth
|
|
|
|
// greater than the 125 max.
|
|
|
|
ErrMaxDepthExceeded = errors.New("max depth exceeded")
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChainID is the content-addressable ID of a layer.
|
|
|
|
type ChainID digest.Digest
|
|
|
|
|
|
|
|
// String returns a string rendition of a layer ID
|
|
|
|
func (id ChainID) String() string {
|
|
|
|
return string(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DiffID is the hash of an individual layer tar.
|
|
|
|
type DiffID digest.Digest
|
|
|
|
|
|
|
|
// String returns a string rendition of a layer DiffID
|
|
|
|
func (diffID DiffID) String() string {
|
|
|
|
return string(diffID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TarStreamer represents an object which may
|
|
|
|
// have its contents exported as a tar stream.
|
|
|
|
type TarStreamer interface {
|
|
|
|
// TarStream returns a tar archive stream
|
|
|
|
// for the contents of a layer.
|
2015-11-25 19:39:54 -05:00
|
|
|
TarStream() (io.ReadCloser, error)
|
2015-11-18 17:15:00 -05:00
|
|
|
}
|
|
|
|
|
2016-03-11 10:02:17 -05:00
|
|
|
// Layer represents a read-only layer
|
2015-11-18 17:15:00 -05:00
|
|
|
type Layer interface {
|
|
|
|
TarStreamer
|
|
|
|
|
2016-11-01 17:44:27 -04:00
|
|
|
// TarStreamFrom returns a tar archive stream for all the layer chain with
|
|
|
|
// arbitrary depth.
|
|
|
|
TarStreamFrom(ChainID) (io.ReadCloser, error)
|
|
|
|
|
2015-11-18 17:15:00 -05:00
|
|
|
// ChainID returns the content hash of the entire layer chain. The hash
|
|
|
|
// chain is made up of DiffID of top layer and all of its parents.
|
|
|
|
ChainID() ChainID
|
|
|
|
|
|
|
|
// DiffID returns the content hash of the layer
|
|
|
|
// tar stream used to create this layer.
|
|
|
|
DiffID() DiffID
|
|
|
|
|
|
|
|
// Parent returns the next layer in the layer chain.
|
|
|
|
Parent() Layer
|
|
|
|
|
|
|
|
// Size returns the size of the entire layer chain. The size
|
|
|
|
// is calculated from the total size of all files in the layers.
|
2022-01-21 13:01:34 -05:00
|
|
|
Size() int64
|
2015-11-18 17:15:00 -05:00
|
|
|
|
|
|
|
// DiffSize returns the size difference of the top layer
|
|
|
|
// from parent layer.
|
2022-01-21 13:01:34 -05:00
|
|
|
DiffSize() int64
|
2015-11-18 17:15:00 -05:00
|
|
|
|
|
|
|
// Metadata returns the low level storage metadata associated
|
|
|
|
// with layer.
|
|
|
|
Metadata() (map[string]string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RWLayer represents a layer which is
|
|
|
|
// read and writable
|
|
|
|
type RWLayer interface {
|
|
|
|
TarStreamer
|
|
|
|
|
2015-12-16 17:13:50 -05:00
|
|
|
// Name of mounted layer
|
|
|
|
Name() string
|
2015-11-18 17:15:00 -05:00
|
|
|
|
|
|
|
// Parent returns the layer which the writable
|
|
|
|
// layer was created from.
|
|
|
|
Parent() Layer
|
|
|
|
|
2015-12-16 17:13:50 -05:00
|
|
|
// Mount mounts the RWLayer and returns the filesystem path
|
2021-03-02 10:03:04 -05:00
|
|
|
// to the writable layer.
|
2017-08-03 20:22:00 -04:00
|
|
|
Mount(mountLabel string) (containerfs.ContainerFS, error)
|
2015-12-16 17:13:50 -05:00
|
|
|
|
|
|
|
// Unmount unmounts the RWLayer. This should be called
|
|
|
|
// for every mount. If there are multiple mount calls
|
|
|
|
// this operation will only decrement the internal mount counter.
|
|
|
|
Unmount() error
|
|
|
|
|
2015-11-18 17:15:00 -05:00
|
|
|
// Size represents the size of the writable layer
|
|
|
|
// as calculated by the total size of the files
|
|
|
|
// changed in the mutable layer.
|
|
|
|
Size() (int64, error)
|
2015-12-16 17:13:50 -05:00
|
|
|
|
|
|
|
// Changes returns the set of changes for the mutable layer
|
|
|
|
// from the base layer.
|
|
|
|
Changes() ([]archive.Change, error)
|
|
|
|
|
|
|
|
// Metadata returns the low level metadata for the mutable layer
|
|
|
|
Metadata() (map[string]string, error)
|
2019-04-25 10:23:48 -04:00
|
|
|
|
|
|
|
// ApplyDiff applies the diff to the RW layer
|
|
|
|
ApplyDiff(diff io.Reader) (int64, error)
|
2015-11-18 17:15:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Metadata holds information about a
|
2016-03-11 10:02:17 -05:00
|
|
|
// read-only layer
|
2015-11-18 17:15:00 -05:00
|
|
|
type Metadata struct {
|
|
|
|
// ChainID is the content hash of the layer
|
|
|
|
ChainID ChainID
|
|
|
|
|
|
|
|
// DiffID is the hash of the tar data used to
|
|
|
|
// create the layer
|
|
|
|
DiffID DiffID
|
|
|
|
|
|
|
|
// Size is the size of the layer and all parents
|
|
|
|
Size int64
|
|
|
|
|
|
|
|
// DiffSize is the size of the top layer
|
|
|
|
DiffSize int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// MountInit is a function to initialize a
|
|
|
|
// writable mount. Changes made here will
|
|
|
|
// not be included in the Tar stream of the
|
|
|
|
// RWLayer.
|
2017-08-03 20:22:00 -04:00
|
|
|
type MountInit func(root containerfs.ContainerFS) error
|
2015-11-18 17:15:00 -05:00
|
|
|
|
2016-11-16 16:31:23 -05:00
|
|
|
// CreateRWLayerOpts contains optional arguments to be passed to CreateRWLayer
|
|
|
|
type CreateRWLayerOpts struct {
|
|
|
|
MountLabel string
|
|
|
|
InitFunc MountInit
|
|
|
|
StorageOpt map[string]string
|
|
|
|
}
|
|
|
|
|
2015-11-18 17:15:00 -05:00
|
|
|
// Store represents a backend for managing both
|
|
|
|
// read-only and read-write layers.
|
|
|
|
type Store interface {
|
2017-09-19 15:14:46 -04:00
|
|
|
Register(io.Reader, ChainID) (Layer, error)
|
2015-11-18 17:15:00 -05:00
|
|
|
Get(ChainID) (Layer, error)
|
2016-08-23 19:08:43 -04:00
|
|
|
Map() map[ChainID]Layer
|
2015-11-18 17:15:00 -05:00
|
|
|
Release(Layer) ([]Metadata, error)
|
|
|
|
|
2017-09-19 15:14:46 -04:00
|
|
|
CreateRWLayer(id string, parent ChainID, opts *CreateRWLayerOpts) (RWLayer, error)
|
2015-12-16 17:13:50 -05:00
|
|
|
GetRWLayer(id string) (RWLayer, error)
|
2016-03-18 14:50:19 -04:00
|
|
|
GetMountID(id string) (string, error)
|
2015-12-16 17:13:50 -05:00
|
|
|
ReleaseRWLayer(RWLayer) ([]Metadata, error)
|
2015-12-16 15:32:16 -05:00
|
|
|
|
|
|
|
Cleanup() error
|
2017-09-19 15:14:46 -04:00
|
|
|
DriverStatus() [][2]string
|
|
|
|
DriverName() string
|
2015-11-18 17:15:00 -05:00
|
|
|
}
|
|
|
|
|
2016-06-06 20:49:34 -04:00
|
|
|
// DescribableStore represents a layer store capable of storing
|
|
|
|
// descriptors for layers.
|
|
|
|
type DescribableStore interface {
|
2017-09-19 15:14:46 -04:00
|
|
|
RegisterWithDescriptor(io.Reader, ChainID, distribution.Descriptor) (Layer, error)
|
2016-06-06 20:49:34 -04:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:15:00 -05:00
|
|
|
// CreateChainID returns ID for a layerDigest slice
|
|
|
|
func CreateChainID(dgsts []DiffID) ChainID {
|
|
|
|
return createChainIDFromParent("", dgsts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createChainIDFromParent(parent ChainID, dgsts ...DiffID) ChainID {
|
|
|
|
if len(dgsts) == 0 {
|
|
|
|
return parent
|
|
|
|
}
|
|
|
|
if parent == "" {
|
|
|
|
return createChainIDFromParent(ChainID(dgsts[0]), dgsts[1:]...)
|
|
|
|
}
|
|
|
|
// H = "H(n-1) SHA256(n)"
|
2015-12-08 14:14:02 -05:00
|
|
|
dgst := digest.FromBytes([]byte(string(parent) + " " + string(dgsts[0])))
|
2015-11-18 17:15:00 -05:00
|
|
|
return createChainIDFromParent(ChainID(dgst), dgsts[1:]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReleaseAndLog releases the provided layer from the given layer
|
|
|
|
// store, logging any error and release metadata
|
|
|
|
func ReleaseAndLog(ls Store, l Layer) {
|
|
|
|
metadata, err := ls.Release(l)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Error releasing layer %s: %v", l.ChainID(), err)
|
|
|
|
}
|
|
|
|
LogReleaseMetadata(metadata)
|
|
|
|
}
|
|
|
|
|
2016-03-11 10:02:17 -05:00
|
|
|
// LogReleaseMetadata logs a metadata array, uses this to
|
2015-11-18 17:15:00 -05:00
|
|
|
// ensure consistent logging for release metadata
|
|
|
|
func LogReleaseMetadata(metadatas []Metadata) {
|
|
|
|
for _, metadata := range metadatas {
|
|
|
|
logrus.Infof("Layer %s cleaned up", metadata.ChainID)
|
|
|
|
}
|
|
|
|
}
|