2016-11-22 19:32:10 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2017-07-20 20:31:51 +00:00
|
|
|
"fmt"
|
2016-11-22 19:32:10 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
// register all of the built-in drivers
|
|
|
|
_ "github.com/containers/storage/drivers/register"
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
2016-11-22 19:32:10 +00:00
|
|
|
drivers "github.com/containers/storage/drivers"
|
|
|
|
"github.com/containers/storage/pkg/archive"
|
|
|
|
"github.com/containers/storage/pkg/idtools"
|
|
|
|
"github.com/containers/storage/pkg/ioutils"
|
|
|
|
"github.com/containers/storage/pkg/stringid"
|
2017-10-14 09:38:04 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2017-07-20 20:31:51 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-11-22 19:32:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-02-01 00:45:59 +00:00
|
|
|
// DefaultStoreOptions is a reasonable default set of options.
|
|
|
|
DefaultStoreOptions StoreOptions
|
|
|
|
stores []*store
|
|
|
|
storesLock sync.Mutex
|
2016-11-22 19:32:10 +00:00
|
|
|
)
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// ROFileBasedStore wraps up the methods of the various types of file-based
|
|
|
|
// data stores that we implement which are needed for both read-only and
|
|
|
|
// read-write files.
|
|
|
|
type ROFileBasedStore interface {
|
2016-11-22 19:32:10 +00:00
|
|
|
Locker
|
|
|
|
|
|
|
|
// Load reloads the contents of the store from disk. It should be called
|
|
|
|
// with the lock held.
|
|
|
|
Load() error
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// RWFileBasedStore wraps up the methods of various types of file-based data
|
|
|
|
// stores that we implement using read-write files.
|
|
|
|
type RWFileBasedStore interface {
|
2016-11-22 19:32:10 +00:00
|
|
|
// Save saves the contents of the store to disk. It should be called with
|
|
|
|
// the lock held, and Touch() should be called afterward before releasing the
|
|
|
|
// lock.
|
|
|
|
Save() error
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// FileBasedStore wraps up the common methods of various types of file-based
|
|
|
|
// data stores that we implement.
|
|
|
|
type FileBasedStore interface {
|
|
|
|
ROFileBasedStore
|
|
|
|
RWFileBasedStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// ROMetadataStore wraps a method for reading metadata associated with an ID.
|
|
|
|
type ROMetadataStore interface {
|
2017-05-17 17:18:35 +00:00
|
|
|
// Metadata reads metadata associated with an item with the specified ID.
|
|
|
|
Metadata(id string) (string, error)
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// RWMetadataStore wraps a method for setting metadata associated with an ID.
|
|
|
|
type RWMetadataStore interface {
|
2016-11-22 19:32:10 +00:00
|
|
|
// SetMetadata updates the metadata associated with the item with the specified ID.
|
|
|
|
SetMetadata(id, metadata string) error
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// MetadataStore wraps up methods for getting and setting metadata associated with IDs.
|
|
|
|
type MetadataStore interface {
|
|
|
|
ROMetadataStore
|
|
|
|
RWMetadataStore
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// An ROBigDataStore wraps up the read-only big-data related methods of the
|
|
|
|
// various types of file-based lookaside stores that we implement.
|
|
|
|
type ROBigDataStore interface {
|
2017-05-17 17:18:35 +00:00
|
|
|
// BigData retrieves a (potentially large) piece of data associated with
|
2016-11-22 19:32:10 +00:00
|
|
|
// this ID, if it has previously been set.
|
2017-05-17 17:18:35 +00:00
|
|
|
BigData(id, key string) ([]byte, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// BigDataSize retrieves the size of a (potentially large) piece of
|
2016-11-22 19:32:10 +00:00
|
|
|
// data associated with this ID, if it has previously been set.
|
2017-05-17 17:18:35 +00:00
|
|
|
BigDataSize(id, key string) (int64, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-10-14 09:38:04 +00:00
|
|
|
// BigDataDigest retrieves the digest of a (potentially large) piece of
|
|
|
|
// data associated with this ID, if it has previously been set.
|
|
|
|
BigDataDigest(id, key string) (digest.Digest, error)
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// BigDataNames() returns a list of the names of previously-stored pieces of
|
2016-11-22 19:32:10 +00:00
|
|
|
// data.
|
2017-05-17 17:18:35 +00:00
|
|
|
BigDataNames(id string) ([]string, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// A RWBigDataStore wraps up the read-write big-data related methods of the
|
|
|
|
// various types of file-based lookaside stores that we implement.
|
|
|
|
type RWBigDataStore interface {
|
|
|
|
// SetBigData stores a (potentially large) piece of data associated with this
|
|
|
|
// ID.
|
|
|
|
SetBigData(id, key string, data []byte) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// A BigDataStore wraps up the most common big-data related methods of the
|
|
|
|
// various types of file-based lookaside stores that we implement.
|
|
|
|
type BigDataStore interface {
|
|
|
|
ROBigDataStore
|
|
|
|
RWBigDataStore
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
// A FlaggableStore can have flags set and cleared on items which it manages.
|
|
|
|
type FlaggableStore interface {
|
|
|
|
// ClearFlag removes a named flag from an item in the store.
|
|
|
|
ClearFlag(id string, flag string) error
|
|
|
|
|
|
|
|
// SetFlag sets a named flag and its value on an item in the store.
|
|
|
|
SetFlag(id string, flag string, value interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreOptions is used for passing initialization options to GetStore(), for
|
|
|
|
// initializing a Store object and the underlying storage that it controls.
|
|
|
|
type StoreOptions struct {
|
|
|
|
// RunRoot is the filesystem path under which we can store run-time
|
|
|
|
// information, such as the locations of active mount points, that we
|
|
|
|
// want to lose if the host is rebooted.
|
|
|
|
RunRoot string `json:"runroot,omitempty"`
|
|
|
|
// GraphRoot is the filesystem path under which we will store the
|
|
|
|
// contents of layers, images, and containers.
|
|
|
|
GraphRoot string `json:"root,omitempty"`
|
|
|
|
// GraphDriverName is the underlying storage driver that we'll be
|
|
|
|
// using. It only needs to be specified the first time a Store is
|
|
|
|
// initialized for a given RunRoot and GraphRoot.
|
|
|
|
GraphDriverName string `json:"driver,omitempty"`
|
|
|
|
// GraphDriverOptions are driver-specific options.
|
|
|
|
GraphDriverOptions []string `json:"driver-options,omitempty"`
|
2017-02-01 00:45:59 +00:00
|
|
|
// UIDMap and GIDMap are used mainly for deciding on the ownership of
|
2016-11-22 19:32:10 +00:00
|
|
|
// files in layers as they're stored on disk, which is often necessary
|
|
|
|
// when user namespaces are being used.
|
2017-02-01 00:45:59 +00:00
|
|
|
UIDMap []idtools.IDMap `json:"uidmap,omitempty"`
|
|
|
|
GIDMap []idtools.IDMap `json:"gidmap,omitempty"`
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store wraps up the various types of file-based stores that we use into a
|
|
|
|
// singleton object that initializes and manages them all together.
|
|
|
|
type Store interface {
|
2017-05-17 17:18:35 +00:00
|
|
|
// RunRoot, GraphRoot, GraphDriverName, and GraphOptions retrieve
|
2016-11-22 19:32:10 +00:00
|
|
|
// settings that were passed to GetStore() when the object was created.
|
2017-05-17 17:18:35 +00:00
|
|
|
RunRoot() string
|
|
|
|
GraphRoot() string
|
|
|
|
GraphDriverName() string
|
|
|
|
GraphOptions() []string
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// GraphDriver obtains and returns a handle to the graph Driver object used
|
2016-11-22 19:32:10 +00:00
|
|
|
// by the Store.
|
2017-05-17 17:18:35 +00:00
|
|
|
GraphDriver() (drivers.Driver, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// CreateLayer creates a new layer in the underlying storage driver,
|
|
|
|
// optionally having the specified ID (one will be assigned if none is
|
|
|
|
// specified), with the specified layer (or no layer) as its parent,
|
|
|
|
// and with optional names. (The writeable flag is ignored.)
|
2016-11-22 19:32:10 +00:00
|
|
|
CreateLayer(id, parent string, names []string, mountLabel string, writeable bool) (*Layer, error)
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// PutLayer combines the functions of CreateLayer and ApplyDiff,
|
|
|
|
// marking the layer for automatic removal if applying the diff fails
|
|
|
|
// for any reason.
|
|
|
|
//
|
|
|
|
// Note that we do some of this work in a child process. The calling
|
|
|
|
// process's main() function needs to import our pkg/reexec package and
|
|
|
|
// should begin with something like this in order to allow us to
|
|
|
|
// properly start that child process:
|
|
|
|
// if reexec.Init {
|
|
|
|
// return
|
|
|
|
// }
|
2017-09-12 23:29:24 +00:00
|
|
|
PutLayer(id, parent string, names []string, mountLabel string, writeable bool, diff io.Reader) (*Layer, int64, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
|
|
|
// CreateImage creates a new image, optionally with the specified ID
|
|
|
|
// (one will be assigned if none is specified), with optional names,
|
|
|
|
// referring to a specified image, and with optional metadata. An
|
|
|
|
// image is a record which associates the ID of a layer with a
|
|
|
|
// additional bookkeeping information which the library stores for the
|
|
|
|
// convenience of its caller.
|
|
|
|
CreateImage(id string, names []string, layer, metadata string, options *ImageOptions) (*Image, error)
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// CreateContainer creates a new container, optionally with the
|
|
|
|
// specified ID (one will be assigned if none is specified), with
|
|
|
|
// optional names, using the specified image's top layer as the basis
|
|
|
|
// for the container's layer, and assigning the specified ID to that
|
|
|
|
// layer (one will be created if none is specified). A container is a
|
|
|
|
// layer which is associated with additional bookkeeping information
|
|
|
|
// which the library stores for the convenience of its caller.
|
2016-11-22 19:32:10 +00:00
|
|
|
CreateContainer(id string, names []string, image, layer, metadata string, options *ContainerOptions) (*Container, error)
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// Metadata retrieves the metadata which is associated with a layer,
|
|
|
|
// image, or container (whichever the passed-in ID refers to).
|
2017-05-17 17:18:35 +00:00
|
|
|
Metadata(id string) (string, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// SetMetadata updates the metadata which is associated with a layer,
|
|
|
|
// image, or container (whichever the passed-in ID refers to) to match
|
|
|
|
// the specified value. The metadata value can be retrieved at any
|
|
|
|
// time using Metadata, or using Layer, Image, or Container and reading
|
|
|
|
// the object directly.
|
2016-11-22 19:32:10 +00:00
|
|
|
SetMetadata(id, metadata string) error
|
|
|
|
|
|
|
|
// Exists checks if there is a layer, image, or container which has the
|
|
|
|
// passed-in ID or name.
|
|
|
|
Exists(id string) bool
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// Status asks for a status report, in the form of key-value pairs,
|
|
|
|
// from the underlying storage driver. The contents vary from driver
|
|
|
|
// to driver.
|
2016-11-22 19:32:10 +00:00
|
|
|
Status() ([][2]string, error)
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// Delete removes the layer, image, or container which has the
|
|
|
|
// passed-in ID or name. Note that no safety checks are performed, so
|
|
|
|
// this can leave images with references to layers which do not exist,
|
|
|
|
// and layers with references to parents which no longer exist.
|
2016-11-22 19:32:10 +00:00
|
|
|
Delete(id string) error
|
|
|
|
|
|
|
|
// DeleteLayer attempts to remove the specified layer. If the layer is the
|
|
|
|
// parent of any other layer, or is referred to by any images, it will return
|
|
|
|
// an error.
|
|
|
|
DeleteLayer(id string) error
|
|
|
|
|
|
|
|
// DeleteImage removes the specified image if it is not referred to by
|
|
|
|
// any containers. If its top layer is then no longer referred to by
|
|
|
|
// any other images and is not the parent of any other layers, its top
|
|
|
|
// layer will be removed. If that layer's parent is no longer referred
|
|
|
|
// to by any other images and is not the parent of any other layers,
|
|
|
|
// then it, too, will be removed. This procedure will be repeated
|
|
|
|
// until a layer which should not be removed, or the base layer, is
|
|
|
|
// reached, at which point the list of removed layers is returned. If
|
|
|
|
// the commit argument is false, the image and layers are not removed,
|
|
|
|
// but the list of layers which would be removed is still returned.
|
|
|
|
DeleteImage(id string, commit bool) (layers []string, err error)
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// DeleteContainer removes the specified container and its layer. If
|
|
|
|
// there is no matching container, or if the container exists but its
|
|
|
|
// layer does not, an error will be returned.
|
2016-11-22 19:32:10 +00:00
|
|
|
DeleteContainer(id string) error
|
|
|
|
|
|
|
|
// Wipe removes all known layers, images, and containers.
|
|
|
|
Wipe() error
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// Mount attempts to mount a layer, image, or container for access, and
|
|
|
|
// returns the pathname if it succeeds.
|
|
|
|
//
|
|
|
|
// Note that we do some of this work in a child process. The calling
|
|
|
|
// process's main() function needs to import our pkg/reexec package and
|
|
|
|
// should begin with something like this in order to allow us to
|
|
|
|
// properly start that child process:
|
|
|
|
// if reexec.Init {
|
|
|
|
// return
|
|
|
|
// }
|
2016-11-22 19:32:10 +00:00
|
|
|
Mount(id, mountLabel string) (string, error)
|
|
|
|
|
|
|
|
// Unmount attempts to unmount a layer, image, or container, given an ID, a
|
|
|
|
// name, or a mount path.
|
|
|
|
Unmount(id string) error
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// Changes returns a summary of the changes which would need to be made
|
|
|
|
// to one layer to make its contents the same as a second layer. If
|
|
|
|
// the first layer is not specified, the second layer's parent is
|
|
|
|
// assumed. Each Change structure contains a Path relative to the
|
|
|
|
// layer's root directory, and a Kind which is either ChangeAdd,
|
|
|
|
// ChangeModify, or ChangeDelete.
|
2016-11-22 19:32:10 +00:00
|
|
|
Changes(from, to string) ([]archive.Change, error)
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// DiffSize returns a count of the size of the tarstream which would
|
|
|
|
// specify the changes returned by Changes.
|
2016-11-22 19:32:10 +00:00
|
|
|
DiffSize(from, to string) (int64, error)
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// Diff returns the tarstream which would specify the changes returned
|
|
|
|
// by Changes. If options are passed in, they can override default
|
|
|
|
// behaviors.
|
|
|
|
Diff(from, to string, options *DiffOptions) (io.ReadCloser, error)
|
|
|
|
|
|
|
|
// ApplyDiff applies a tarstream to a layer. Information about the
|
|
|
|
// tarstream is cached with the layer. Typically, a layer which is
|
|
|
|
// populated using a tarstream will be expected to not be modified in
|
|
|
|
// any other way, either before or after the diff is applied.
|
|
|
|
//
|
|
|
|
// Note that we do some of this work in a child process. The calling
|
|
|
|
// process's main() function needs to import our pkg/reexec package and
|
|
|
|
// should begin with something like this in order to allow us to
|
|
|
|
// properly start that child process:
|
|
|
|
// if reexec.Init {
|
|
|
|
// return
|
|
|
|
// }
|
2017-09-12 23:29:24 +00:00
|
|
|
ApplyDiff(to string, diff io.Reader) (int64, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// LayersByCompressedDigest returns a slice of the layers with the
|
|
|
|
// specified compressed digest value recorded for them.
|
|
|
|
LayersByCompressedDigest(d digest.Digest) ([]Layer, error)
|
|
|
|
|
|
|
|
// LayersByUncompressedDigest returns a slice of the layers with the
|
|
|
|
// specified uncompressed digest value recorded for them.
|
|
|
|
LayersByUncompressedDigest(d digest.Digest) ([]Layer, error)
|
|
|
|
|
|
|
|
// LayerSize returns a cached approximation of the layer's size, or -1
|
|
|
|
// if we don't have a value on hand.
|
|
|
|
LayerSize(id string) (int64, error)
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
// Layers returns a list of the currently known layers.
|
|
|
|
Layers() ([]Layer, error)
|
|
|
|
|
|
|
|
// Images returns a list of the currently known images.
|
|
|
|
Images() ([]Image, error)
|
|
|
|
|
|
|
|
// Containers returns a list of the currently known containers.
|
|
|
|
Containers() ([]Container, error)
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// Names returns the list of names for a layer, image, or container.
|
|
|
|
Names(id string) ([]string, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
|
|
|
// SetNames changes the list of names for a layer, image, or container.
|
2017-09-12 23:29:24 +00:00
|
|
|
// Duplicate names are removed from the list automatically.
|
2016-11-22 19:32:10 +00:00
|
|
|
SetNames(id string, names []string) error
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// ListImageBigData retrieves a list of the (possibly large) chunks of
|
|
|
|
// named data associated with an image.
|
2016-11-22 19:32:10 +00:00
|
|
|
ListImageBigData(id string) ([]string, error)
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
// ImageBigData retrieves a (possibly large) chunk of named data
|
|
|
|
// associated with an image.
|
2017-05-17 17:18:35 +00:00
|
|
|
ImageBigData(id, key string) ([]byte, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// ImageBigDataSize retrieves the size of a (possibly large) chunk
|
2016-11-22 19:32:10 +00:00
|
|
|
// of named data associated with an image.
|
2017-05-17 17:18:35 +00:00
|
|
|
ImageBigDataSize(id, key string) (int64, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-10-14 09:38:04 +00:00
|
|
|
// ImageBigDataDigest retrieves the digest of a (possibly large) chunk
|
|
|
|
// of named data associated with an image.
|
|
|
|
ImageBigDataDigest(id, key string) (digest.Digest, error)
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
// SetImageBigData stores a (possibly large) chunk of named data associated
|
|
|
|
// with an image.
|
|
|
|
SetImageBigData(id, key string, data []byte) error
|
|
|
|
|
|
|
|
// ListContainerBigData retrieves a list of the (possibly large) chunks of
|
|
|
|
// named data associated with a container.
|
|
|
|
ListContainerBigData(id string) ([]string, error)
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// ContainerBigData retrieves a (possibly large) chunk of named data
|
2016-11-22 19:32:10 +00:00
|
|
|
// associated with a container.
|
2017-05-17 17:18:35 +00:00
|
|
|
ContainerBigData(id, key string) ([]byte, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// ContainerBigDataSize retrieves the size of a (possibly large)
|
2016-11-22 19:32:10 +00:00
|
|
|
// chunk of named data associated with a container.
|
2017-05-17 17:18:35 +00:00
|
|
|
ContainerBigDataSize(id, key string) (int64, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-10-14 09:38:04 +00:00
|
|
|
// ContainerBigDataDigest retrieves the digest of a (possibly large)
|
|
|
|
// chunk of named data associated with a container.
|
|
|
|
ContainerBigDataDigest(id, key string) (digest.Digest, error)
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
// SetContainerBigData stores a (possibly large) chunk of named data
|
|
|
|
// associated with a container.
|
|
|
|
SetContainerBigData(id, key string, data []byte) error
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// Layer returns a specific layer.
|
|
|
|
Layer(id string) (*Layer, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// Image returns a specific image.
|
|
|
|
Image(id string) (*Image, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// ImagesByTopLayer returns a list of images which reference the specified
|
2016-11-22 19:32:10 +00:00
|
|
|
// layer as their top layer. They will have different IDs and names
|
|
|
|
// and may have different metadata, big data items, and flags.
|
2017-05-17 17:18:35 +00:00
|
|
|
ImagesByTopLayer(id string) ([]*Image, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// Container returns a specific container.
|
|
|
|
Container(id string) (*Container, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// ContainerByLayer returns a specific container based on its layer ID or
|
2016-11-22 19:32:10 +00:00
|
|
|
// name.
|
2017-05-17 17:18:35 +00:00
|
|
|
ContainerByLayer(id string) (*Container, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// ContainerDirectory returns a path of a directory which the caller
|
2016-11-22 19:32:10 +00:00
|
|
|
// can use to store data, specific to the container, which the library
|
|
|
|
// does not directly manage. The directory will be deleted when the
|
|
|
|
// container is deleted.
|
2017-05-17 17:18:35 +00:00
|
|
|
ContainerDirectory(id string) (string, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
|
|
|
// SetContainerDirectoryFile is a convenience function which stores
|
|
|
|
// a piece of data in the specified file relative to the container's
|
|
|
|
// directory.
|
|
|
|
SetContainerDirectoryFile(id, file string, data []byte) error
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// FromContainerDirectory is a convenience function which reads
|
2016-11-22 19:32:10 +00:00
|
|
|
// the contents of the specified file relative to the container's
|
|
|
|
// directory.
|
2017-05-17 17:18:35 +00:00
|
|
|
FromContainerDirectory(id, file string) ([]byte, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// ContainerRunDirectory returns a path of a directory which the
|
2016-11-22 19:32:10 +00:00
|
|
|
// caller can use to store data, specific to the container, which the
|
|
|
|
// library does not directly manage. The directory will be deleted
|
|
|
|
// when the host system is restarted.
|
2017-05-17 17:18:35 +00:00
|
|
|
ContainerRunDirectory(id string) (string, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
|
|
|
// SetContainerRunDirectoryFile is a convenience function which stores
|
|
|
|
// a piece of data in the specified file relative to the container's
|
|
|
|
// run directory.
|
|
|
|
SetContainerRunDirectoryFile(id, file string, data []byte) error
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
// FromContainerRunDirectory is a convenience function which reads
|
2016-11-22 19:32:10 +00:00
|
|
|
// the contents of the specified file relative to the container's run
|
|
|
|
// directory.
|
2017-05-17 17:18:35 +00:00
|
|
|
FromContainerRunDirectory(id, file string) ([]byte, error)
|
2016-11-22 19:32:10 +00:00
|
|
|
|
|
|
|
// Lookup returns the ID of a layer, image, or container with the specified
|
|
|
|
// name or ID.
|
|
|
|
Lookup(name string) (string, error)
|
|
|
|
|
|
|
|
// Shutdown attempts to free any kernel resources which are being used
|
|
|
|
// by the underlying driver. If "force" is true, any mounted (i.e., in
|
|
|
|
// use) layers are unmounted beforehand. If "force" is not true, then
|
|
|
|
// layers being in use is considered to be an error condition. A list
|
|
|
|
// of still-mounted layers is returned along with possible errors.
|
|
|
|
Shutdown(force bool) (layers []string, err error)
|
|
|
|
|
|
|
|
// Version returns version information, in the form of key-value pairs, from
|
|
|
|
// the storage package.
|
|
|
|
Version() ([][2]string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImageOptions is used for passing options to a Store's CreateImage() method.
|
|
|
|
type ImageOptions struct {
|
2017-07-20 20:31:51 +00:00
|
|
|
// CreationDate, if not zero, will override the default behavior of marking the image as having been
|
|
|
|
// created when CreateImage() was called, recording CreationDate instead.
|
|
|
|
CreationDate time.Time
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerOptions is used for passing options to a Store's CreateContainer() method.
|
|
|
|
type ContainerOptions struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type store struct {
|
|
|
|
lastLoaded time.Time
|
|
|
|
runRoot string
|
|
|
|
graphLock Locker
|
|
|
|
graphRoot string
|
|
|
|
graphDriverName string
|
|
|
|
graphOptions []string
|
|
|
|
uidMap []idtools.IDMap
|
|
|
|
gidMap []idtools.IDMap
|
|
|
|
graphDriver drivers.Driver
|
|
|
|
layerStore LayerStore
|
2017-07-20 20:31:51 +00:00
|
|
|
roLayerStores []ROLayerStore
|
2016-11-22 19:32:10 +00:00
|
|
|
imageStore ImageStore
|
2017-07-20 20:31:51 +00:00
|
|
|
roImageStores []ROImageStore
|
2016-11-22 19:32:10 +00:00
|
|
|
containerStore ContainerStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStore attempts to find an already-created Store object matching the
|
|
|
|
// specified location and graph driver, and if it can't, it creates and
|
|
|
|
// initializes a new Store object, and the underlying storage that it controls.
|
|
|
|
func GetStore(options StoreOptions) (Store, error) {
|
|
|
|
if options.RunRoot == "" && options.GraphRoot == "" && options.GraphDriverName == "" && len(options.GraphDriverOptions) == 0 {
|
|
|
|
options = DefaultStoreOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.GraphRoot != "" {
|
|
|
|
options.GraphRoot = filepath.Clean(options.GraphRoot)
|
|
|
|
}
|
|
|
|
if options.RunRoot != "" {
|
|
|
|
options.RunRoot = filepath.Clean(options.RunRoot)
|
|
|
|
}
|
|
|
|
|
|
|
|
storesLock.Lock()
|
|
|
|
defer storesLock.Unlock()
|
|
|
|
|
|
|
|
for _, s := range stores {
|
|
|
|
if s.graphRoot == options.GraphRoot && (options.GraphDriverName == "" || s.graphDriverName == options.GraphDriverName) {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.GraphRoot == "" {
|
|
|
|
return nil, ErrIncompleteOptions
|
|
|
|
}
|
|
|
|
if options.RunRoot == "" {
|
|
|
|
return nil, ErrIncompleteOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(options.RunRoot, 0700); err != nil && !os.IsExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, subdir := range []string{} {
|
|
|
|
if err := os.MkdirAll(filepath.Join(options.RunRoot, subdir), 0700); err != nil && !os.IsExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(options.GraphRoot, 0700); err != nil && !os.IsExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, subdir := range []string{"mounts", "tmp", options.GraphDriverName} {
|
|
|
|
if err := os.MkdirAll(filepath.Join(options.GraphRoot, subdir), 0700); err != nil && !os.IsExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
graphLock, err := GetLockfile(filepath.Join(options.GraphRoot, "storage.lock"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s := &store{
|
|
|
|
runRoot: options.RunRoot,
|
|
|
|
graphLock: graphLock,
|
|
|
|
graphRoot: options.GraphRoot,
|
|
|
|
graphDriverName: options.GraphDriverName,
|
|
|
|
graphOptions: options.GraphDriverOptions,
|
2017-02-01 00:45:59 +00:00
|
|
|
uidMap: copyIDMap(options.UIDMap),
|
|
|
|
gidMap: copyIDMap(options.GIDMap),
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
if err := s.load(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stores = append(stores, s)
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyIDMap(idmap []idtools.IDMap) []idtools.IDMap {
|
|
|
|
m := []idtools.IDMap{}
|
|
|
|
if idmap != nil {
|
|
|
|
m = make([]idtools.IDMap, len(idmap))
|
|
|
|
copy(m, idmap)
|
|
|
|
}
|
|
|
|
if len(m) > 0 {
|
|
|
|
return m[:]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) RunRoot() string {
|
2016-11-22 19:32:10 +00:00
|
|
|
return s.runRoot
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) GraphDriverName() string {
|
2016-11-22 19:32:10 +00:00
|
|
|
return s.graphDriverName
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) GraphRoot() string {
|
2016-11-22 19:32:10 +00:00
|
|
|
return s.graphRoot
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) GraphOptions() []string {
|
2016-11-22 19:32:10 +00:00
|
|
|
return s.graphOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) load() error {
|
2017-05-17 17:18:35 +00:00
|
|
|
driver, err := s.GraphDriver()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.graphDriver = driver
|
|
|
|
s.graphDriverName = driver.String()
|
|
|
|
driverPrefix := s.graphDriverName + "-"
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
rls, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.layerStore = rls
|
2017-07-20 20:31:51 +00:00
|
|
|
if _, err := s.ROLayerStores(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
|
|
|
|
gipath := filepath.Join(s.graphRoot, driverPrefix+"images")
|
|
|
|
if err := os.MkdirAll(gipath, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ris, err := newImageStore(gipath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.imageStore = ris
|
2017-07-20 20:31:51 +00:00
|
|
|
if _, err := s.ROImageStores(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
gcpath := filepath.Join(s.graphRoot, driverPrefix+"containers")
|
|
|
|
if err := os.MkdirAll(gcpath, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rcs, err := newContainerStore(gcpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rcpath := filepath.Join(s.runRoot, driverPrefix+"containers")
|
|
|
|
if err := os.MkdirAll(rcpath, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.containerStore = rcs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) getGraphDriver() (drivers.Driver, error) {
|
|
|
|
if s.graphDriver != nil {
|
|
|
|
return s.graphDriver, nil
|
|
|
|
}
|
2017-09-26 19:58:51 +00:00
|
|
|
config := drivers.Options{
|
|
|
|
Root: s.graphRoot,
|
|
|
|
DriverOptions: s.graphOptions,
|
|
|
|
UIDMaps: s.uidMap,
|
|
|
|
GIDMaps: s.gidMap,
|
|
|
|
}
|
|
|
|
driver, err := drivers.New(s.graphDriverName, config)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.graphDriver = driver
|
|
|
|
s.graphDriverName = driver.String()
|
|
|
|
return driver, nil
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) GraphDriver() (drivers.Driver, error) {
|
2016-11-22 19:32:10 +00:00
|
|
|
s.graphLock.Lock()
|
|
|
|
defer s.graphLock.Unlock()
|
|
|
|
if s.graphLock.TouchedSince(s.lastLoaded) {
|
|
|
|
s.graphDriver = nil
|
|
|
|
s.layerStore = nil
|
|
|
|
s.lastLoaded = time.Now()
|
|
|
|
}
|
|
|
|
return s.getGraphDriver()
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
// LayerStore obtains and returns a handle to the writeable layer store object
|
|
|
|
// used by the Store. Accessing this store directly will bypass locking and
|
|
|
|
// synchronization, so it is not a part of the exported Store interface.
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) LayerStore() (LayerStore, error) {
|
2016-11-22 19:32:10 +00:00
|
|
|
s.graphLock.Lock()
|
|
|
|
defer s.graphLock.Unlock()
|
|
|
|
if s.graphLock.TouchedSince(s.lastLoaded) {
|
|
|
|
s.graphDriver = nil
|
|
|
|
s.layerStore = nil
|
|
|
|
s.lastLoaded = time.Now()
|
|
|
|
}
|
|
|
|
if s.layerStore != nil {
|
|
|
|
return s.layerStore, nil
|
|
|
|
}
|
|
|
|
driver, err := s.getGraphDriver()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
driverPrefix := s.graphDriverName + "-"
|
|
|
|
rlpath := filepath.Join(s.runRoot, driverPrefix+"layers")
|
|
|
|
if err := os.MkdirAll(rlpath, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
glpath := filepath.Join(s.graphRoot, driverPrefix+"layers")
|
|
|
|
if err := os.MkdirAll(glpath, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rls, err := newLayerStore(rlpath, glpath, driver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.layerStore = rls
|
|
|
|
return s.layerStore, nil
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
// ROLayerStores obtains additional read/only layer store objects used by the
|
|
|
|
// Store. Accessing these stores directly will bypass locking and
|
|
|
|
// synchronization, so it is not part of the exported Store interface.
|
2017-07-20 20:31:51 +00:00
|
|
|
func (s *store) ROLayerStores() ([]ROLayerStore, error) {
|
|
|
|
s.graphLock.Lock()
|
|
|
|
defer s.graphLock.Unlock()
|
|
|
|
if s.roLayerStores != nil {
|
|
|
|
return s.roLayerStores, nil
|
|
|
|
}
|
|
|
|
driver, err := s.getGraphDriver()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
driverPrefix := s.graphDriverName + "-"
|
|
|
|
rlpath := filepath.Join(s.runRoot, driverPrefix+"layers")
|
|
|
|
if err := os.MkdirAll(rlpath, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, store := range driver.AdditionalImageStores() {
|
|
|
|
glpath := filepath.Join(store, driverPrefix+"layers")
|
|
|
|
rls, err := newROLayerStore(rlpath, glpath, driver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.roLayerStores = append(s.roLayerStores, rls)
|
|
|
|
}
|
|
|
|
return s.roLayerStores, nil
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
// ImageStore obtains and returns a handle to the writable image store object
|
|
|
|
// used by the Store. Accessing this store directly will bypass locking and
|
|
|
|
// synchronization, so it is not a part of the exported Store interface.
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) ImageStore() (ImageStore, error) {
|
2016-11-22 19:32:10 +00:00
|
|
|
if s.imageStore != nil {
|
|
|
|
return s.imageStore, nil
|
|
|
|
}
|
|
|
|
return nil, ErrLoadError
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
// ROImageStores obtains additional read/only image store objects used by the
|
|
|
|
// Store. Accessing these stores directly will bypass locking and
|
|
|
|
// synchronization, so it is not a part of the exported Store interface.
|
2017-07-20 20:31:51 +00:00
|
|
|
func (s *store) ROImageStores() ([]ROImageStore, error) {
|
|
|
|
if len(s.roImageStores) != 0 {
|
|
|
|
return s.roImageStores, nil
|
|
|
|
}
|
|
|
|
driver, err := s.getGraphDriver()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
driverPrefix := s.graphDriverName + "-"
|
|
|
|
for _, store := range driver.AdditionalImageStores() {
|
|
|
|
gipath := filepath.Join(store, driverPrefix+"images")
|
|
|
|
ris, err := newROImageStore(gipath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.roImageStores = append(s.roImageStores, ris)
|
|
|
|
}
|
|
|
|
return s.roImageStores, nil
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
// ContainerStore obtains and returns a handle to the container store object
|
|
|
|
// used by the Store. Accessing this store directly will bypass locking and
|
|
|
|
// synchronization, so it is not a part of the exported Store interface.
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) ContainerStore() (ContainerStore, error) {
|
2016-11-22 19:32:10 +00:00
|
|
|
if s.containerStore != nil {
|
|
|
|
return s.containerStore, nil
|
|
|
|
}
|
|
|
|
return nil, ErrLoadError
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
func (s *store) PutLayer(id, parent string, names []string, mountLabel string, writeable bool, diff io.Reader) (*Layer, int64, error) {
|
2017-05-17 17:18:35 +00:00
|
|
|
rlstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
2017-10-14 09:38:04 +00:00
|
|
|
rlstores, err := s.ROLayerStores()
|
|
|
|
if err != nil {
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
rlstore.Lock()
|
|
|
|
defer rlstore.Unlock()
|
|
|
|
if modified, err := rlstore.Modified(); modified || err != nil {
|
|
|
|
rlstore.Load()
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
if id == "" {
|
|
|
|
id = stringid.GenerateRandomID()
|
|
|
|
}
|
|
|
|
if parent != "" {
|
2017-10-14 09:38:04 +00:00
|
|
|
var ilayer *Layer
|
|
|
|
for _, lstore := range append([]ROLayerStore{rlstore}, rlstores...) {
|
|
|
|
if l, err := lstore.Get(parent); err == nil && l != nil {
|
|
|
|
ilayer = l
|
|
|
|
parent = ilayer.ID
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ilayer == nil {
|
2016-11-22 19:32:10 +00:00
|
|
|
return nil, -1, ErrLayerUnknown
|
|
|
|
}
|
|
|
|
containers, err := rcstore.Containers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
for _, container := range containers {
|
|
|
|
if container.LayerID == parent {
|
|
|
|
return nil, -1, ErrParentIsContainer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rlstore.Put(id, parent, names, mountLabel, nil, writeable, nil, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) CreateLayer(id, parent string, names []string, mountLabel string, writeable bool) (*Layer, error) {
|
|
|
|
layer, _, err := s.PutLayer(id, parent, names, mountLabel, writeable, nil)
|
|
|
|
return layer, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) CreateImage(id string, names []string, layer, metadata string, options *ImageOptions) (*Image, error) {
|
2017-07-20 20:31:51 +00:00
|
|
|
if id == "" {
|
|
|
|
id = stringid.GenerateRandomID()
|
|
|
|
}
|
|
|
|
|
2017-09-26 19:58:51 +00:00
|
|
|
if layer != "" {
|
|
|
|
lstore, err := s.LayerStore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-26 19:58:51 +00:00
|
|
|
lstores, err := s.ROLayerStores()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-26 19:58:51 +00:00
|
|
|
var ilayer *Layer
|
|
|
|
for _, store := range append([]ROLayerStore{lstore}, lstores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
|
|
|
}
|
|
|
|
ilayer, err = store.Get(layer)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ilayer == nil {
|
|
|
|
return nil, ErrLayerUnknown
|
|
|
|
}
|
|
|
|
layer = ilayer.ID
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
ristore, err := s.ImageStore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
ristore.Lock()
|
|
|
|
defer ristore.Unlock()
|
|
|
|
if modified, err := ristore.Modified(); modified || err != nil {
|
|
|
|
ristore.Load()
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
creationDate := time.Now().UTC()
|
|
|
|
if options != nil {
|
|
|
|
creationDate = options.CreationDate
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
|
|
|
|
return ristore.Create(id, names, layer, metadata, creationDate)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) CreateContainer(id string, names []string, image, layer, metadata string, options *ContainerOptions) (*Container, error) {
|
2017-05-17 17:18:35 +00:00
|
|
|
rlstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rlstore.Lock()
|
|
|
|
defer rlstore.Unlock()
|
|
|
|
if modified, err := rlstore.Modified(); modified || err != nil {
|
|
|
|
rlstore.Load()
|
|
|
|
}
|
|
|
|
if id == "" {
|
|
|
|
id = stringid.GenerateRandomID()
|
|
|
|
}
|
|
|
|
|
2017-03-28 19:56:09 +00:00
|
|
|
imageTopLayer := ""
|
|
|
|
imageID := ""
|
|
|
|
if image != "" {
|
2017-09-12 23:29:24 +00:00
|
|
|
istore, err := s.ImageStore()
|
2017-07-20 20:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
istores, err := s.ROImageStores()
|
2017-03-28 19:56:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
var cimage *Image
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROImageStore{istore}, istores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
cimage, err = store.Get(image)
|
2017-07-20 20:31:51 +00:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-03-28 19:56:09 +00:00
|
|
|
if cimage == nil {
|
|
|
|
return nil, ErrImageUnknown
|
|
|
|
}
|
|
|
|
imageTopLayer = cimage.TopLayer
|
|
|
|
imageID = cimage.ID
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-03-28 19:56:09 +00:00
|
|
|
clayer, err := rlstore.Create(layer, imageTopLayer, nil, "", nil, true)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
layer = clayer.ID
|
2017-07-20 20:31:51 +00:00
|
|
|
rcstore, err := s.ContainerStore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
2017-03-28 19:56:09 +00:00
|
|
|
container, err := rcstore.Create(id, names, imageID, layer, metadata)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil || container == nil {
|
|
|
|
rlstore.Delete(layer)
|
|
|
|
}
|
|
|
|
return container, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) SetMetadata(id, metadata string) error {
|
2017-05-17 17:18:35 +00:00
|
|
|
rlstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
ristore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rlstore.Lock()
|
|
|
|
defer rlstore.Unlock()
|
|
|
|
if modified, err := rlstore.Modified(); modified || err != nil {
|
|
|
|
rlstore.Load()
|
|
|
|
}
|
|
|
|
ristore.Lock()
|
|
|
|
defer ristore.Unlock()
|
|
|
|
if modified, err := ristore.Modified(); modified || err != nil {
|
|
|
|
ristore.Load()
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
if rlstore.Exists(id) {
|
|
|
|
return rlstore.SetMetadata(id, metadata)
|
|
|
|
}
|
|
|
|
if ristore.Exists(id) {
|
|
|
|
return ristore.SetMetadata(id, metadata)
|
|
|
|
}
|
|
|
|
if rcstore.Exists(id) {
|
|
|
|
return rcstore.SetMetadata(id, metadata)
|
|
|
|
}
|
|
|
|
return ErrNotAnID
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) Metadata(id string) (string, error) {
|
2017-09-12 23:29:24 +00:00
|
|
|
lstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
lstores, err := s.ROLayerStores()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROLayerStore{lstore}, lstores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
if store.Exists(id) {
|
|
|
|
return store.Metadata(id)
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
istore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
istores, err := s.ROImageStores()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROImageStore{istore}, istores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
if store.Exists(id) {
|
|
|
|
return store.Metadata(id)
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
cstore, err := s.ContainerStore()
|
2017-07-20 20:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
cstore.Lock()
|
|
|
|
defer cstore.Unlock()
|
|
|
|
if modified, err := cstore.Modified(); modified || err != nil {
|
|
|
|
cstore.Load()
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
if cstore.Exists(id) {
|
|
|
|
return cstore.Metadata(id)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
return "", ErrNotAnID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) ListImageBigData(id string) ([]string, error) {
|
2017-09-12 23:29:24 +00:00
|
|
|
istore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
istores, err := s.ROImageStores()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROImageStore{istore}, istores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
bigDataNames, err := store.BigDataNames(id)
|
2017-07-20 20:31:51 +00:00
|
|
|
if err == nil {
|
|
|
|
return bigDataNames, err
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
return nil, ErrImageUnknown
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) ImageBigDataSize(id, key string) (int64, error) {
|
2017-09-12 23:29:24 +00:00
|
|
|
istore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
istores, err := s.ROImageStores()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROImageStore{istore}, istores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
size, err := store.BigDataSize(id, key)
|
2017-07-20 20:31:51 +00:00
|
|
|
if err == nil {
|
|
|
|
return size, nil
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
return -1, ErrSizeUnknown
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-14 09:38:04 +00:00
|
|
|
func (s *store) ImageBigDataDigest(id, key string) (digest.Digest, error) {
|
|
|
|
ristore, err := s.ImageStore()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
stores, err := s.ROImageStores()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
stores = append([]ROImageStore{ristore}, stores...)
|
|
|
|
for _, ristore := range stores {
|
|
|
|
ristore.Lock()
|
|
|
|
defer ristore.Unlock()
|
|
|
|
if modified, err := ristore.Modified(); modified || err != nil {
|
|
|
|
ristore.Load()
|
|
|
|
}
|
|
|
|
d, err := ristore.BigDataDigest(id, key)
|
|
|
|
if err == nil && d.Validate() == nil {
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", ErrDigestUnknown
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) ImageBigData(id, key string) ([]byte, error) {
|
2017-09-12 23:29:24 +00:00
|
|
|
istore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
istores, err := s.ROImageStores()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROImageStore{istore}, istores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
data, err := store.BigData(id, key)
|
2017-07-20 20:31:51 +00:00
|
|
|
if err == nil {
|
|
|
|
return data, nil
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
return nil, ErrImageUnknown
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) SetImageBigData(id, key string, data []byte) error {
|
2017-05-17 17:18:35 +00:00
|
|
|
ristore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ristore.Lock()
|
|
|
|
defer ristore.Unlock()
|
|
|
|
if modified, err := ristore.Modified(); modified || err != nil {
|
|
|
|
ristore.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ristore.SetBigData(id, key, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) ListContainerBigData(id string) ([]string, error) {
|
2017-05-17 17:18:35 +00:00
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
return rcstore.BigDataNames(id)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) ContainerBigDataSize(id, key string) (int64, error) {
|
2017-07-20 20:31:51 +00:00
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
return rcstore.BigDataSize(id, key)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-14 09:38:04 +00:00
|
|
|
func (s *store) ContainerBigDataDigest(id, key string) (digest.Digest, error) {
|
|
|
|
rcstore, err := s.ContainerStore()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
return rcstore.BigDataDigest(id, key)
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) ContainerBigData(id, key string) ([]byte, error) {
|
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
return rcstore.BigData(id, key)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) SetContainerBigData(id, key string, data []byte) error {
|
2017-05-17 17:18:35 +00:00
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
return rcstore.SetBigData(id, key, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) Exists(id string) bool {
|
2017-07-20 20:31:51 +00:00
|
|
|
lstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
lstores, err := s.ROLayerStores()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROLayerStore{lstore}, lstores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
if store.Exists(id) {
|
2017-07-20 20:31:51 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
istore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
istores, err := s.ROImageStores()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROImageStore{istore}, istores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
if store.Exists(id) {
|
2017-07-20 20:31:51 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
|
|
|
|
rcstore, err := s.ContainerStore()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
if rcstore.Exists(id) {
|
|
|
|
return true
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
|
|
|
|
return false
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 19:58:51 +00:00
|
|
|
func dedupeNames(names []string) []string {
|
2017-07-20 20:31:51 +00:00
|
|
|
seen := make(map[string]bool)
|
2017-09-26 19:58:51 +00:00
|
|
|
deduped := make([]string, 0, len(names))
|
2017-07-20 20:31:51 +00:00
|
|
|
for _, name := range names {
|
|
|
|
if _, wasSeen := seen[name]; !wasSeen {
|
|
|
|
seen[name] = true
|
|
|
|
deduped = append(deduped, name)
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-09-26 19:58:51 +00:00
|
|
|
return deduped
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) SetNames(id string, names []string) error {
|
|
|
|
deduped := dedupeNames(names)
|
2017-07-20 20:31:51 +00:00
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
rlstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rlstore.Lock()
|
|
|
|
defer rlstore.Unlock()
|
|
|
|
if modified, err := rlstore.Modified(); modified || err != nil {
|
|
|
|
rlstore.Load()
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
if rlstore.Exists(id) {
|
|
|
|
return rlstore.SetNames(id, deduped)
|
|
|
|
}
|
|
|
|
|
|
|
|
ristore, err := s.ImageStore()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
ristore.Lock()
|
|
|
|
defer ristore.Unlock()
|
|
|
|
if modified, err := ristore.Modified(); modified || err != nil {
|
|
|
|
ristore.Load()
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
if ristore.Exists(id) {
|
|
|
|
return ristore.SetNames(id, deduped)
|
|
|
|
}
|
|
|
|
|
|
|
|
rcstore, err := s.ContainerStore()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
if rcstore.Exists(id) {
|
|
|
|
return rcstore.SetNames(id, deduped)
|
|
|
|
}
|
|
|
|
return ErrLayerUnknown
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) Names(id string) ([]string, error) {
|
2017-09-12 23:29:24 +00:00
|
|
|
lstore, err := s.LayerStore()
|
2017-07-20 20:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
lstores, err := s.ROLayerStores()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROLayerStore{lstore}, lstores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
if l, err := store.Get(id); l != nil && err == nil {
|
2017-07-20 20:31:51 +00:00
|
|
|
return l.Names, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
istore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
istores, err := s.ROImageStores()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROImageStore{istore}, istores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
if i, err := store.Get(id); i != nil && err == nil {
|
2017-07-20 20:31:51 +00:00
|
|
|
return i.Names, nil
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
|
|
|
|
rcstore, err := s.ContainerStore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
if c, err := rcstore.Get(id); c != nil && err == nil {
|
|
|
|
return c.Names, nil
|
|
|
|
}
|
|
|
|
return nil, ErrLayerUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) Lookup(name string) (string, error) {
|
2017-09-12 23:29:24 +00:00
|
|
|
lstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
lstores, err := s.ROLayerStores()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROLayerStore{lstore}, lstores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
|
|
|
}
|
|
|
|
if l, err := store.Get(name); l != nil && err == nil {
|
|
|
|
return l.ID, nil
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
istore, err := s.ImageStore()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
istores, err := s.ROImageStores()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROImageStore{istore}, istores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
|
|
|
}
|
|
|
|
if i, err := store.Get(name); i != nil && err == nil {
|
|
|
|
return i.ID, nil
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
cstore, err := s.ContainerStore()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
cstore.Lock()
|
|
|
|
defer cstore.Unlock()
|
|
|
|
if modified, err := cstore.Modified(); modified || err != nil {
|
|
|
|
cstore.Load()
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
if c, err := cstore.Get(name); c != nil && err == nil {
|
2016-11-22 19:32:10 +00:00
|
|
|
return c.ID, nil
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
return "", ErrLayerUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) DeleteLayer(id string) error {
|
2017-05-17 17:18:35 +00:00
|
|
|
rlstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
ristore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rlstore.Lock()
|
|
|
|
defer rlstore.Unlock()
|
|
|
|
if modified, err := rlstore.Modified(); modified || err != nil {
|
|
|
|
rlstore.Load()
|
|
|
|
}
|
|
|
|
ristore.Lock()
|
|
|
|
defer ristore.Unlock()
|
|
|
|
if modified, err := ristore.Modified(); modified || err != nil {
|
|
|
|
ristore.Load()
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
if rlstore.Exists(id) {
|
|
|
|
if l, err := rlstore.Get(id); err != nil {
|
|
|
|
id = l.ID
|
|
|
|
}
|
|
|
|
layers, err := rlstore.Layers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, layer := range layers {
|
|
|
|
if layer.Parent == id {
|
|
|
|
return ErrLayerHasChildren
|
|
|
|
}
|
|
|
|
}
|
|
|
|
images, err := ristore.Images()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, image := range images {
|
|
|
|
if image.TopLayer == id {
|
2017-07-20 20:31:51 +00:00
|
|
|
return errors.Wrapf(ErrLayerUsedByImage, "Layer %v used by image %v", id, image.ID)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
containers, err := rcstore.Containers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, container := range containers {
|
|
|
|
if container.LayerID == id {
|
2017-07-20 20:31:51 +00:00
|
|
|
return errors.Wrapf(ErrLayerUsedByContainer, "Layer %v used by container %v", id, container.ID)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rlstore.Delete(id)
|
|
|
|
}
|
2017-02-01 00:45:59 +00:00
|
|
|
return ErrNotALayer
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) DeleteImage(id string, commit bool) (layers []string, err error) {
|
2017-05-17 17:18:35 +00:00
|
|
|
rlstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
ristore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rlstore.Lock()
|
|
|
|
defer rlstore.Unlock()
|
|
|
|
if modified, err := rlstore.Modified(); modified || err != nil {
|
|
|
|
rlstore.Load()
|
|
|
|
}
|
|
|
|
ristore.Lock()
|
|
|
|
defer ristore.Unlock()
|
|
|
|
if modified, err := ristore.Modified(); modified || err != nil {
|
|
|
|
ristore.Load()
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
layersToRemove := []string{}
|
|
|
|
if ristore.Exists(id) {
|
|
|
|
image, err := ristore.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id = image.ID
|
|
|
|
containers, err := rcstore.Containers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
aContainerByImage := make(map[string]string)
|
|
|
|
for _, container := range containers {
|
|
|
|
aContainerByImage[container.ImageID] = container.ID
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
if container, ok := aContainerByImage[id]; ok {
|
|
|
|
return nil, errors.Wrapf(ErrImageUsedByContainer, "Image used by %v", container)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
images, err := ristore.Images()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
layers, err := rlstore.Layers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
childrenByParent := make(map[string]*[]string)
|
|
|
|
for _, layer := range layers {
|
|
|
|
parent := layer.Parent
|
|
|
|
if list, ok := childrenByParent[parent]; ok {
|
|
|
|
newList := append(*list, layer.ID)
|
|
|
|
childrenByParent[parent] = &newList
|
|
|
|
} else {
|
|
|
|
childrenByParent[parent] = &([]string{layer.ID})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
anyImageByTopLayer := make(map[string]string)
|
|
|
|
for _, img := range images {
|
|
|
|
if img.ID != id {
|
|
|
|
anyImageByTopLayer[img.TopLayer] = img.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if commit {
|
|
|
|
if err = ristore.Delete(id); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
layer := image.TopLayer
|
|
|
|
lastRemoved := ""
|
|
|
|
for layer != "" {
|
|
|
|
if rcstore.Exists(layer) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if _, ok := anyImageByTopLayer[layer]; ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
parent := ""
|
|
|
|
if l, err := rlstore.Get(layer); err == nil {
|
|
|
|
parent = l.Parent
|
|
|
|
}
|
|
|
|
otherRefs := 0
|
|
|
|
if childList, ok := childrenByParent[layer]; ok && childList != nil {
|
|
|
|
children := *childList
|
|
|
|
for _, child := range children {
|
|
|
|
if child != lastRemoved {
|
|
|
|
otherRefs++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if otherRefs != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
lastRemoved = layer
|
|
|
|
layersToRemove = append(layersToRemove, lastRemoved)
|
|
|
|
layer = parent
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, ErrNotAnImage
|
|
|
|
}
|
|
|
|
if commit {
|
|
|
|
for _, layer := range layersToRemove {
|
|
|
|
if err = rlstore.Delete(layer); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return layersToRemove, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) DeleteContainer(id string) error {
|
2017-05-17 17:18:35 +00:00
|
|
|
rlstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
ristore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rlstore.Lock()
|
|
|
|
defer rlstore.Unlock()
|
|
|
|
if modified, err := rlstore.Modified(); modified || err != nil {
|
|
|
|
rlstore.Load()
|
|
|
|
}
|
|
|
|
ristore.Lock()
|
|
|
|
defer ristore.Unlock()
|
|
|
|
if modified, err := ristore.Modified(); modified || err != nil {
|
|
|
|
ristore.Load()
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
if rcstore.Exists(id) {
|
|
|
|
if container, err := rcstore.Get(id); err == nil {
|
|
|
|
if rlstore.Exists(container.LayerID) {
|
|
|
|
if err = rlstore.Delete(container.LayerID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = rcstore.Delete(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
middleDir := s.graphDriverName + "-containers"
|
2017-05-17 17:18:35 +00:00
|
|
|
gcpath := filepath.Join(s.GraphRoot(), middleDir, container.ID)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err = os.RemoveAll(gcpath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
rcpath := filepath.Join(s.RunRoot(), middleDir, container.ID)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err = os.RemoveAll(rcpath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-01 00:45:59 +00:00
|
|
|
return ErrNotALayer
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-01 00:45:59 +00:00
|
|
|
return ErrNotAContainer
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) Delete(id string) error {
|
2017-05-17 17:18:35 +00:00
|
|
|
rlstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
ristore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rlstore.Lock()
|
|
|
|
defer rlstore.Unlock()
|
|
|
|
if modified, err := rlstore.Modified(); modified || err != nil {
|
|
|
|
rlstore.Load()
|
|
|
|
}
|
|
|
|
ristore.Lock()
|
|
|
|
defer ristore.Unlock()
|
|
|
|
if modified, err := ristore.Modified(); modified || err != nil {
|
|
|
|
ristore.Load()
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
if rcstore.Exists(id) {
|
|
|
|
if container, err := rcstore.Get(id); err == nil {
|
|
|
|
if rlstore.Exists(container.LayerID) {
|
|
|
|
if err = rlstore.Delete(container.LayerID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = rcstore.Delete(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
middleDir := s.graphDriverName + "-containers"
|
2017-05-17 17:18:35 +00:00
|
|
|
gcpath := filepath.Join(s.GraphRoot(), middleDir, container.ID, "userdata")
|
2016-11-22 19:32:10 +00:00
|
|
|
if err = os.RemoveAll(gcpath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
rcpath := filepath.Join(s.RunRoot(), middleDir, container.ID, "userdata")
|
2016-11-22 19:32:10 +00:00
|
|
|
if err = os.RemoveAll(rcpath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-01 00:45:59 +00:00
|
|
|
return ErrNotALayer
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ristore.Exists(id) {
|
|
|
|
return ristore.Delete(id)
|
|
|
|
}
|
|
|
|
if rlstore.Exists(id) {
|
|
|
|
return rlstore.Delete(id)
|
|
|
|
}
|
|
|
|
return ErrLayerUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) Wipe() error {
|
2017-05-17 17:18:35 +00:00
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
ristore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
rlstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rlstore.Lock()
|
|
|
|
defer rlstore.Unlock()
|
|
|
|
if modified, err := rlstore.Modified(); modified || err != nil {
|
|
|
|
rlstore.Load()
|
|
|
|
}
|
|
|
|
ristore.Lock()
|
|
|
|
defer ristore.Unlock()
|
|
|
|
if modified, err := ristore.Modified(); modified || err != nil {
|
|
|
|
ristore.Load()
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = rcstore.Wipe(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = ristore.Wipe(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return rlstore.Wipe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) Status() ([][2]string, error) {
|
2017-05-17 17:18:35 +00:00
|
|
|
rlstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rlstore.Status()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) Version() ([][2]string, error) {
|
2017-07-20 20:31:51 +00:00
|
|
|
return [][2]string{}, nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) Mount(id, mountLabel string) (string, error) {
|
2017-07-20 20:31:51 +00:00
|
|
|
if layerID, err := s.ContainerLayerID(id); err == nil {
|
|
|
|
id = layerID
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
rlstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
rlstore.Lock()
|
|
|
|
defer rlstore.Unlock()
|
|
|
|
if modified, err := rlstore.Modified(); modified || err != nil {
|
|
|
|
rlstore.Load()
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
if rlstore.Exists(id) {
|
|
|
|
return rlstore.Mount(id, mountLabel)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
return "", ErrLayerUnknown
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) Unmount(id string) error {
|
2017-07-20 20:31:51 +00:00
|
|
|
if layerID, err := s.ContainerLayerID(id); err == nil {
|
|
|
|
id = layerID
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
rlstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rlstore.Lock()
|
|
|
|
defer rlstore.Unlock()
|
|
|
|
if modified, err := rlstore.Modified(); modified || err != nil {
|
|
|
|
rlstore.Load()
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
if rlstore.Exists(id) {
|
|
|
|
return rlstore.Unmount(id)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
return ErrLayerUnknown
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) Changes(from, to string) ([]archive.Change, error) {
|
2017-09-12 23:29:24 +00:00
|
|
|
lstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
lstores, err := s.ROLayerStores()
|
2017-07-20 20:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROLayerStore{lstore}, lstores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
if store.Exists(to) {
|
|
|
|
return store.Changes(from, to)
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, ErrLayerUnknown
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) DiffSize(from, to string) (int64, error) {
|
2017-09-12 23:29:24 +00:00
|
|
|
lstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
lstores, err := s.ROLayerStores()
|
2017-07-20 20:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROLayerStore{lstore}, lstores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
if store.Exists(to) {
|
|
|
|
return store.DiffSize(from, to)
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, ErrLayerUnknown
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
func (s *store) Diff(from, to string, options *DiffOptions) (io.ReadCloser, error) {
|
2017-09-12 23:29:24 +00:00
|
|
|
lstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
lstores, err := s.ROLayerStores()
|
2017-07-20 20:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROLayerStore{lstore}, lstores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
if store.Exists(to) {
|
|
|
|
return store.Diff(from, to, options)
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, ErrLayerUnknown
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
func (s *store) ApplyDiff(to string, diff io.Reader) (int64, error) {
|
2017-05-17 17:18:35 +00:00
|
|
|
rlstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
rlstore.Lock()
|
|
|
|
defer rlstore.Unlock()
|
|
|
|
if modified, err := rlstore.Modified(); modified || err != nil {
|
|
|
|
rlstore.Load()
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
if rlstore.Exists(to) {
|
|
|
|
return rlstore.ApplyDiff(to, diff)
|
|
|
|
}
|
|
|
|
return -1, ErrLayerUnknown
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
func (s *store) layersByMappedDigest(m func(ROLayerStore, digest.Digest) ([]Layer, error), d digest.Digest) ([]Layer, error) {
|
|
|
|
var layers []Layer
|
2017-09-12 23:29:24 +00:00
|
|
|
lstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
lstores, err := s.ROLayerStores()
|
2017-07-20 20:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROLayerStore{lstore}, lstores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
storeLayers, err := m(store, d)
|
2017-07-20 20:31:51 +00:00
|
|
|
if err != nil {
|
2017-11-11 10:55:55 +00:00
|
|
|
return nil, err
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
layers = append(layers, storeLayers...)
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
|
|
|
return layers, nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
func (s *store) LayersByCompressedDigest(d digest.Digest) ([]Layer, error) {
|
2017-10-14 09:38:04 +00:00
|
|
|
if err := d.Validate(); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error looking for compressed layers matching digest %q", d)
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
return s.layersByMappedDigest(func(r ROLayerStore, d digest.Digest) ([]Layer, error) { return r.LayersByCompressedDigest(d) }, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) LayersByUncompressedDigest(d digest.Digest) ([]Layer, error) {
|
2017-10-14 09:38:04 +00:00
|
|
|
if err := d.Validate(); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error looking for layers matching digest %q", d)
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
return s.layersByMappedDigest(func(r ROLayerStore, d digest.Digest) ([]Layer, error) { return r.LayersByUncompressedDigest(d) }, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) LayerSize(id string) (int64, error) {
|
|
|
|
lstore, err := s.LayerStore()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
lstores, err := s.ROLayerStores()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROLayerStore{lstore}, lstores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
if store.Exists(id) {
|
|
|
|
return store.Size(id)
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, ErrLayerUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) Layers() ([]Layer, error) {
|
|
|
|
var layers []Layer
|
2017-09-12 23:29:24 +00:00
|
|
|
lstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
lstores, err := s.ROLayerStores()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROLayerStore{lstore}, lstores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
storeLayers, err := store.Layers()
|
2017-07-20 20:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
layers = append(layers, storeLayers...)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
return layers, nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
func (s *store) Images() ([]Image, error) {
|
|
|
|
var images []Image
|
2017-09-12 23:29:24 +00:00
|
|
|
istore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
istores, err := s.ROImageStores()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROImageStore{istore}, istores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
storeImages, err := store.Images()
|
2017-07-20 20:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
images = append(images, storeImages...)
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
|
|
|
return images, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) Containers() ([]Container, error) {
|
2017-05-17 17:18:35 +00:00
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
return rcstore.Containers()
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) Layer(id string) (*Layer, error) {
|
2017-09-12 23:29:24 +00:00
|
|
|
lstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
lstores, err := s.ROLayerStores()
|
2017-07-20 20:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROLayerStore{lstore}, lstores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
layer, err := store.Get(id)
|
2017-07-20 20:31:51 +00:00
|
|
|
if err == nil {
|
|
|
|
return layer, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, ErrLayerUnknown
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) Image(id string) (*Image, error) {
|
2017-09-12 23:29:24 +00:00
|
|
|
istore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
istores, err := s.ROImageStores()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROImageStore{istore}, istores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
image, err := store.Get(id)
|
2017-07-20 20:31:51 +00:00
|
|
|
if err == nil {
|
|
|
|
return image, nil
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
return nil, ErrImageUnknown
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) ImagesByTopLayer(id string) ([]*Image, error) {
|
2017-07-20 20:31:51 +00:00
|
|
|
images := []*Image{}
|
|
|
|
layer, err := s.Layer(id)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
istore, err := s.ImageStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
istores, err := s.ROImageStores()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
for _, store := range append([]ROImageStore{istore}, istores...) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if modified, err := store.Modified(); modified || err != nil {
|
|
|
|
store.Load()
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
imageList, err := store.Images()
|
2017-07-20 20:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, image := range imageList {
|
|
|
|
if image.TopLayer == layer.ID {
|
|
|
|
images = append(images, &image)
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return images, nil
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) Container(id string) (*Container, error) {
|
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
return rcstore.Get(id)
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
func (s *store) ContainerLayerID(id string) (string, error) {
|
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
2017-07-20 20:31:51 +00:00
|
|
|
return "", err
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
container, err := rcstore.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return container.LayerID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) ContainerByLayer(id string) (*Container, error) {
|
|
|
|
layer, err := s.Layer(id)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-17 17:18:35 +00:00
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
containerList, err := rcstore.Containers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, container := range containerList {
|
|
|
|
if container.LayerID == layer.ID {
|
|
|
|
return &container, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrContainerUnknown
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) ContainerDirectory(id string) (string, error) {
|
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err = rcstore.Lookup(id)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
middleDir := s.graphDriverName + "-containers"
|
2017-05-17 17:18:35 +00:00
|
|
|
gcpath := filepath.Join(s.GraphRoot(), middleDir, id, "userdata")
|
2016-11-22 19:32:10 +00:00
|
|
|
if err := os.MkdirAll(gcpath, 0700); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return gcpath, nil
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) ContainerRunDirectory(id string) (string, error) {
|
|
|
|
rcstore, err := s.ContainerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
rcstore.Lock()
|
|
|
|
defer rcstore.Unlock()
|
|
|
|
if modified, err := rcstore.Modified(); modified || err != nil {
|
|
|
|
rcstore.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err = rcstore.Lookup(id)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
middleDir := s.graphDriverName + "-containers"
|
2017-05-17 17:18:35 +00:00
|
|
|
rcpath := filepath.Join(s.RunRoot(), middleDir, id, "userdata")
|
2016-11-22 19:32:10 +00:00
|
|
|
if err := os.MkdirAll(rcpath, 0700); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return rcpath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) SetContainerDirectoryFile(id, file string, data []byte) error {
|
2017-05-17 17:18:35 +00:00
|
|
|
dir, err := s.ContainerDirectory(id)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.MkdirAll(filepath.Dir(filepath.Join(dir, file)), 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutils.AtomicWriteFile(filepath.Join(dir, file), data, 0600)
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) FromContainerDirectory(id, file string) ([]byte, error) {
|
|
|
|
dir, err := s.ContainerDirectory(id)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ioutil.ReadFile(filepath.Join(dir, file))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) SetContainerRunDirectoryFile(id, file string, data []byte) error {
|
2017-05-17 17:18:35 +00:00
|
|
|
dir, err := s.ContainerRunDirectory(id)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.MkdirAll(filepath.Dir(filepath.Join(dir, file)), 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutils.AtomicWriteFile(filepath.Join(dir, file), data, 0600)
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
func (s *store) FromContainerRunDirectory(id, file string) ([]byte, error) {
|
|
|
|
dir, err := s.ContainerRunDirectory(id)
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ioutil.ReadFile(filepath.Join(dir, file))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *store) Shutdown(force bool) ([]string, error) {
|
|
|
|
mounted := []string{}
|
|
|
|
modified := false
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
rlstore, err := s.LayerStore()
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return mounted, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rlstore.Lock()
|
|
|
|
defer rlstore.Unlock()
|
|
|
|
if modified, err := rlstore.Modified(); modified || err != nil {
|
|
|
|
rlstore.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.graphLock.Lock()
|
|
|
|
defer s.graphLock.Unlock()
|
|
|
|
layers, err := rlstore.Layers()
|
|
|
|
if err != nil {
|
|
|
|
return mounted, err
|
|
|
|
}
|
|
|
|
for _, layer := range layers {
|
|
|
|
if layer.MountCount == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mounted = append(mounted, layer.ID)
|
|
|
|
if force {
|
|
|
|
for layer.MountCount > 0 {
|
|
|
|
err2 := rlstore.Unmount(layer.ID)
|
|
|
|
if err2 != nil {
|
|
|
|
if err == nil {
|
|
|
|
err = err2
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(mounted) > 0 && err == nil {
|
2017-07-20 20:31:51 +00:00
|
|
|
err = errors.Wrap(ErrLayerUsedByContainer, "A layer is mounted")
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
err = s.graphDriver.Cleanup()
|
|
|
|
s.graphLock.Touch()
|
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
if modified {
|
|
|
|
rlstore.Touch()
|
|
|
|
}
|
|
|
|
return mounted, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a BigData key name into an acceptable file name.
|
|
|
|
func makeBigDataBaseName(key string) string {
|
|
|
|
reader := strings.NewReader(key)
|
|
|
|
for reader.Len() > 0 {
|
|
|
|
ch, size, err := reader.ReadRune()
|
|
|
|
if err != nil || size != 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if ch != '.' && !(ch >= '0' && ch <= '9') && !(ch >= 'a' && ch <= 'z') {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if reader.Len() > 0 {
|
|
|
|
return "=" + base64.StdEncoding.EncodeToString([]byte(key))
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2017-04-27 17:59:23 +00:00
|
|
|
func stringSliceWithoutValue(slice []string, value string) []string {
|
2017-10-14 09:38:04 +00:00
|
|
|
modified := make([]string, 0, len(slice))
|
2017-04-27 17:59:23 +00:00
|
|
|
for _, v := range slice {
|
|
|
|
if v == value {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
modified = append(modified, v)
|
|
|
|
}
|
|
|
|
return modified
|
|
|
|
}
|
|
|
|
|
2017-07-20 20:31:51 +00:00
|
|
|
const configFile = "/etc/containers/storage.conf"
|
|
|
|
|
|
|
|
// OptionsConfig represents the "storage.options" TOML config table.
|
|
|
|
type OptionsConfig struct {
|
|
|
|
// AdditionalImagesStores is the location of additional read/only
|
|
|
|
// Image stores. Usually used to access Networked File System
|
|
|
|
// for shared image content
|
|
|
|
AdditionalImageStores []string `toml:"additionalimagestores"`
|
2017-09-26 19:58:51 +00:00
|
|
|
|
|
|
|
// Size
|
|
|
|
Size string `toml:"size"`
|
|
|
|
|
|
|
|
// OverrideKernelCheck
|
|
|
|
OverrideKernelCheck string `toml:"override_kernel_check"`
|
2017-07-20 20:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TOML-friendly explicit tables used for conversions.
|
|
|
|
type tomlConfig struct {
|
|
|
|
Storage struct {
|
|
|
|
Driver string `toml:"driver"`
|
|
|
|
RunRoot string `toml:"runroot"`
|
|
|
|
GraphRoot string `toml:"graphroot"`
|
|
|
|
Options struct{ OptionsConfig } `toml:"options"`
|
|
|
|
} `toml:"storage"`
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
func init() {
|
2017-03-28 19:56:09 +00:00
|
|
|
DefaultStoreOptions.RunRoot = "/var/run/containers/storage"
|
|
|
|
DefaultStoreOptions.GraphRoot = "/var/lib/containers/storage"
|
2017-08-05 11:40:46 +00:00
|
|
|
DefaultStoreOptions.GraphDriverName = ""
|
2017-07-20 20:31:51 +00:00
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(configFile)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
fmt.Printf("Failed to read %s %v\n", configFile, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config := new(tomlConfig)
|
|
|
|
|
|
|
|
if _, err := toml.Decode(string(data), config); err != nil {
|
|
|
|
fmt.Printf("Failed to parse %s %v\n", configFile, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if config.Storage.Driver != "" {
|
|
|
|
DefaultStoreOptions.GraphDriverName = config.Storage.Driver
|
|
|
|
}
|
|
|
|
if config.Storage.RunRoot != "" {
|
|
|
|
DefaultStoreOptions.RunRoot = config.Storage.RunRoot
|
|
|
|
}
|
|
|
|
if config.Storage.GraphRoot != "" {
|
|
|
|
DefaultStoreOptions.GraphRoot = config.Storage.GraphRoot
|
|
|
|
}
|
|
|
|
for _, s := range config.Storage.Options.AdditionalImageStores {
|
|
|
|
DefaultStoreOptions.GraphDriverOptions = append(DefaultStoreOptions.GraphDriverOptions, fmt.Sprintf("%s.imagestore=%s", config.Storage.Driver, s))
|
|
|
|
}
|
2017-09-26 19:58:51 +00:00
|
|
|
if config.Storage.Options.Size != "" {
|
|
|
|
DefaultStoreOptions.GraphDriverOptions = append(DefaultStoreOptions.GraphDriverOptions, fmt.Sprintf("%s.size=%s", config.Storage.Driver, config.Storage.Options.Size))
|
|
|
|
}
|
|
|
|
if config.Storage.Options.OverrideKernelCheck != "" {
|
|
|
|
DefaultStoreOptions.GraphDriverOptions = append(DefaultStoreOptions.GraphDriverOptions, fmt.Sprintf("%s.override_kernel_check=%s", config.Storage.Driver, config.Storage.Options.OverrideKernelCheck))
|
|
|
|
}
|
2017-07-20 20:31:51 +00:00
|
|
|
if os.Getenv("STORAGE_DRIVER") != "" {
|
|
|
|
DefaultStoreOptions.GraphDriverName = os.Getenv("STORAGE_DRIVER")
|
|
|
|
}
|
|
|
|
if os.Getenv("STORAGE_OPTS") != "" {
|
|
|
|
DefaultStoreOptions.GraphDriverOptions = append(DefaultStoreOptions.GraphDriverOptions, strings.Split(os.Getenv("STORAGE_OPTS"), ",")...)
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
if len(DefaultStoreOptions.GraphDriverOptions) == 1 && DefaultStoreOptions.GraphDriverOptions[0] == "" {
|
|
|
|
DefaultStoreOptions.GraphDriverOptions = nil
|
|
|
|
}
|
|
|
|
}
|