2014-03-08 01:36:47 +00:00
|
|
|
package image
|
2013-03-18 07:15:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2013-07-22 22:40:33 +00:00
|
|
|
"strconv"
|
2013-03-18 07:15:35 +00:00
|
|
|
"time"
|
2014-07-24 20:37:44 +00:00
|
|
|
|
2014-10-24 17:12:35 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-10-24 22:11:48 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2014-11-04 23:23:21 +00:00
|
|
|
"github.com/docker/docker/pkg/tarsum"
|
2014-07-24 20:37:44 +00:00
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
"github.com/docker/docker/utils"
|
2013-03-18 07:15:35 +00:00
|
|
|
)
|
|
|
|
|
2014-08-15 00:43:12 +00:00
|
|
|
// Set the max depth to the aufs default that most
|
|
|
|
// kernels are compiled with
|
|
|
|
// For more information see: http://sourceforge.net/p/aufs/aufs3-standalone/ci/aufs3.12/tree/config.mk
|
|
|
|
const MaxImageDepth = 127
|
|
|
|
|
2013-03-18 07:15:35 +00:00
|
|
|
type Image struct {
|
2014-02-12 04:04:39 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Parent string `json:"parent,omitempty"`
|
|
|
|
Comment string `json:"comment,omitempty"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Container string `json:"container,omitempty"`
|
|
|
|
ContainerConfig runconfig.Config `json:"container_config,omitempty"`
|
|
|
|
DockerVersion string `json:"docker_version,omitempty"`
|
|
|
|
Author string `json:"author,omitempty"`
|
|
|
|
Config *runconfig.Config `json:"config,omitempty"`
|
|
|
|
Architecture string `json:"architecture,omitempty"`
|
|
|
|
OS string `json:"os,omitempty"`
|
2014-11-04 23:23:21 +00:00
|
|
|
Checksum string `json:"checksum"`
|
2013-05-13 13:10:26 +00:00
|
|
|
Size int64
|
2014-03-08 01:36:47 +00:00
|
|
|
|
|
|
|
graph Graph
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func LoadImage(root string) (*Image, error) {
|
2014-11-04 13:45:46 +00:00
|
|
|
// Open the JSON file to decode by streaming
|
|
|
|
jsonSource, err := os.Open(jsonPath(root))
|
2013-03-18 07:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-04 13:45:46 +00:00
|
|
|
defer jsonSource.Close()
|
|
|
|
|
2013-05-08 17:42:29 +00:00
|
|
|
img := &Image{}
|
2014-11-04 13:45:46 +00:00
|
|
|
dec := json.NewDecoder(jsonSource)
|
2013-05-08 17:42:29 +00:00
|
|
|
|
2014-11-04 13:45:46 +00:00
|
|
|
// Decode the JSON data
|
|
|
|
if err := dec.Decode(img); err != nil {
|
2013-03-18 07:15:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-08 01:36:47 +00:00
|
|
|
if err := utils.ValidateID(img.ID); err != nil {
|
2013-03-18 07:15:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-22 22:40:33 +00:00
|
|
|
|
|
|
|
if buf, err := ioutil.ReadFile(path.Join(root, "layersize")); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-05 22:03:23 +00:00
|
|
|
// If the layersize file does not exist then set the size to a negative number
|
|
|
|
// because a layer size of 0 (zero) is valid
|
|
|
|
img.Size = -1
|
2013-07-22 22:40:33 +00:00
|
|
|
} else {
|
2014-11-10 17:14:12 +00:00
|
|
|
// Using Atoi here instead would temporarily convert the size to a machine
|
|
|
|
// dependent integer type, which causes images larger than 2^31 bytes to
|
|
|
|
// display negative sizes on 32-bit machines:
|
|
|
|
size, err := strconv.ParseInt(string(buf), 10, 64)
|
2013-11-18 23:35:56 +00:00
|
|
|
if err != nil {
|
2013-07-22 22:40:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-11-18 23:35:56 +00:00
|
|
|
img.Size = int64(size)
|
2013-07-22 22:40:33 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 17:42:29 +00:00
|
|
|
return img, nil
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
|
|
|
|
2014-11-04 23:23:21 +00:00
|
|
|
// StoreImage stores file system layer data for the given image to the
|
|
|
|
// image's registered storage driver. Image metadata is stored in a file
|
|
|
|
// at the specified root directory. This function also computes the TarSum
|
|
|
|
// of `layerData` (currently using tarsum.dev).
|
2014-10-27 18:00:29 +00:00
|
|
|
func StoreImage(img *Image, layerData archive.ArchiveReader, root string) error {
|
2013-03-18 07:15:35 +00:00
|
|
|
// Store the layer
|
2013-11-20 22:51:04 +00:00
|
|
|
var (
|
2014-11-04 23:23:21 +00:00
|
|
|
size int64
|
|
|
|
err error
|
|
|
|
driver = img.graph.Driver()
|
|
|
|
layerTarSum tarsum.TarSum
|
2013-11-20 22:51:04 +00:00
|
|
|
)
|
2013-05-09 02:08:11 +00:00
|
|
|
|
2013-06-22 19:29:42 +00:00
|
|
|
// If layerData is not nil, unpack it into the new layer
|
|
|
|
if layerData != nil {
|
2014-11-04 23:23:21 +00:00
|
|
|
layerDataDecompressed, err := archive.DecompressStream(layerData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer layerDataDecompressed.Close()
|
|
|
|
|
|
|
|
if layerTarSum, err = tarsum.NewTarSum(layerDataDecompressed, true, tarsum.VersionDev); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if size, err = driver.ApplyDiff(img.ID, img.Parent, layerTarSum); err != nil {
|
2014-09-11 03:30:52 +00:00
|
|
|
return err
|
2013-06-22 19:29:42 +00:00
|
|
|
}
|
2014-11-04 23:23:21 +00:00
|
|
|
|
|
|
|
checksum := layerTarSum.Sum(nil)
|
|
|
|
|
|
|
|
if img.Checksum != "" && img.Checksum != checksum {
|
|
|
|
log.Warn("image layer checksum mismatch: computed %q, expected %q", checksum, img.Checksum)
|
|
|
|
}
|
|
|
|
|
|
|
|
img.Checksum = checksum
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
2013-05-13 13:10:26 +00:00
|
|
|
|
2013-11-07 20:34:01 +00:00
|
|
|
img.Size = size
|
|
|
|
if err := img.SaveSize(root); err != nil {
|
|
|
|
return err
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
2013-07-22 22:40:33 +00:00
|
|
|
|
2014-10-27 18:00:29 +00:00
|
|
|
f, err := os.OpenFile(jsonPath(root), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(0600))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-12-05 22:03:23 +00:00
|
|
|
}
|
2014-10-27 18:00:29 +00:00
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return json.NewEncoder(f).Encode(img)
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 01:36:47 +00:00
|
|
|
func (img *Image) SetGraph(graph Graph) {
|
|
|
|
img.graph = graph
|
|
|
|
}
|
|
|
|
|
2013-11-07 20:34:01 +00:00
|
|
|
// SaveSize stores the current `size` value of `img` in the directory `root`.
|
|
|
|
func (img *Image) SaveSize(root string) error {
|
|
|
|
if err := ioutil.WriteFile(path.Join(root, "layersize"), []byte(strconv.Itoa(int(img.Size))), 0600); err != nil {
|
|
|
|
return fmt.Errorf("Error storing image size in %s/layersize: %s", root, err)
|
|
|
|
}
|
|
|
|
return nil
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func jsonPath(root string) string {
|
|
|
|
return path.Join(root, "json")
|
|
|
|
}
|
|
|
|
|
2014-06-17 13:05:29 +00:00
|
|
|
func (img *Image) RawJson() ([]byte, error) {
|
|
|
|
root, err := img.root()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to get root for image %s: %s", img.ID, err)
|
|
|
|
}
|
|
|
|
fh, err := os.Open(jsonPath(root))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to open json for image %s: %s", img.ID, err)
|
|
|
|
}
|
|
|
|
buf, err := ioutil.ReadAll(fh)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to read json for image %s: %s", img.ID, err)
|
|
|
|
}
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2013-04-21 21:23:55 +00:00
|
|
|
// TarLayer returns a tar archive of the image's filesystem layer.
|
2013-12-05 21:18:02 +00:00
|
|
|
func (img *Image) TarLayer() (arch archive.Archive, err error) {
|
2013-11-19 22:57:53 +00:00
|
|
|
if img.graph == nil {
|
|
|
|
return nil, fmt.Errorf("Can't load storage driver for unregistered image %s", img.ID)
|
2013-03-21 05:13:28 +00:00
|
|
|
}
|
2013-12-05 21:18:02 +00:00
|
|
|
|
2014-09-11 03:30:52 +00:00
|
|
|
driver := img.graph.Driver()
|
2013-12-05 21:18:02 +00:00
|
|
|
|
2014-09-11 03:30:52 +00:00
|
|
|
return driver.Diff(img.ID, img.Parent)
|
2013-03-21 05:12:38 +00:00
|
|
|
}
|
|
|
|
|
2013-03-18 07:15:35 +00:00
|
|
|
// Image includes convenience proxy functions to its graph
|
|
|
|
// These functions will return an error if the image is not registered
|
|
|
|
// (ie. if image.graph == nil)
|
|
|
|
func (img *Image) History() ([]*Image, error) {
|
|
|
|
var parents []*Image
|
|
|
|
if err := img.WalkHistory(
|
2013-03-21 17:10:14 +00:00
|
|
|
func(img *Image) error {
|
2013-03-18 07:15:35 +00:00
|
|
|
parents = append(parents, img)
|
2013-03-21 17:10:14 +00:00
|
|
|
return nil
|
2013-03-18 07:15:35 +00:00
|
|
|
},
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return parents, nil
|
|
|
|
}
|
|
|
|
|
2013-03-21 17:10:14 +00:00
|
|
|
func (img *Image) WalkHistory(handler func(*Image) error) (err error) {
|
2013-03-18 07:15:35 +00:00
|
|
|
currentImg := img
|
|
|
|
for currentImg != nil {
|
|
|
|
if handler != nil {
|
2013-03-21 17:10:14 +00:00
|
|
|
if err := handler(currentImg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
|
|
|
currentImg, err = currentImg.GetParent()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error while getting parent image: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (img *Image) GetParent() (*Image, error) {
|
|
|
|
if img.Parent == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if img.graph == nil {
|
|
|
|
return nil, fmt.Errorf("Can't lookup parent of unregistered image")
|
|
|
|
}
|
|
|
|
return img.graph.Get(img.Parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (img *Image) root() (string, error) {
|
|
|
|
if img.graph == nil {
|
|
|
|
return "", fmt.Errorf("Can't lookup root of unregistered image")
|
|
|
|
}
|
2014-03-08 01:36:47 +00:00
|
|
|
return img.graph.ImageRoot(img.ID), nil
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 01:36:47 +00:00
|
|
|
func (img *Image) GetParentsSize(size int64) int64 {
|
2013-05-13 13:10:26 +00:00
|
|
|
parentImage, err := img.GetParent()
|
|
|
|
if err != nil || parentImage == nil {
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
size += parentImage.Size
|
2014-03-08 01:36:47 +00:00
|
|
|
return parentImage.GetParentsSize(size)
|
2013-05-13 13:10:26 +00:00
|
|
|
}
|
2013-05-22 13:41:29 +00:00
|
|
|
|
2013-11-19 08:51:16 +00:00
|
|
|
// Depth returns the number of parents for a
|
|
|
|
// current image
|
|
|
|
func (img *Image) Depth() (int, error) {
|
|
|
|
var (
|
|
|
|
count = 0
|
|
|
|
parent = img
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for parent != nil {
|
|
|
|
count++
|
|
|
|
parent, err = parent.GetParent()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2014-08-15 00:43:12 +00:00
|
|
|
// CheckDepth returns an error if the depth of an image, as returned
|
|
|
|
// by ImageDepth, is too large to support creating a container from it
|
|
|
|
// on this daemon.
|
|
|
|
func (img *Image) CheckDepth() error {
|
|
|
|
// We add 2 layers to the depth because the container's rw and
|
|
|
|
// init layer add to the restriction
|
|
|
|
depth, err := img.Depth()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if depth+2 >= MaxImageDepth {
|
|
|
|
return fmt.Errorf("Cannot create container with more than %d parents", MaxImageDepth)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-15 01:41:39 +00:00
|
|
|
// Build an Image object from raw json data
|
2013-06-04 18:00:22 +00:00
|
|
|
func NewImgJSON(src []byte) (*Image, error) {
|
2013-05-15 01:41:39 +00:00
|
|
|
ret := &Image{}
|
|
|
|
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Debugf("Json string: {%s}", src)
|
2013-05-15 01:41:39 +00:00
|
|
|
// FIXME: Is there a cleaner way to "purify" the input json?
|
|
|
|
if err := json.Unmarshal(src, ret); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|