graph.go
ef711962
 package docker
33f6a0aa
 
 import (
 	"fmt"
96d1e9bb
 	"github.com/dotcloud/docker/archive"
f2bab155
 	"github.com/dotcloud/docker/graphdriver"
2e69e172
 	"github.com/dotcloud/docker/utils"
09f1cbab
 	"io"
33f6a0aa
 	"io/ioutil"
 	"os"
 	"path"
 	"path/filepath"
f9359f59
 	"runtime"
004a5310
 	"strings"
29fa1b66
 	"syscall"
33f6a0aa
 	"time"
 )
 
13d2b086
 // A Graph is a store for versioned filesystem images and the relationship between them.
33f6a0aa
 type Graph struct {
8ca7b064
 	Root    string
 	idIndex *utils.TruncIndex
752bfba2
 	driver  graphdriver.Driver
33f6a0aa
 }
 
13d2b086
 // NewGraph instantiates a new graph at the given root path in the filesystem.
99b36c2c
 // `root` will be created if it doesn't exist.
f2bab155
 func NewGraph(root string, driver graphdriver.Driver) (*Graph, error) {
33f6a0aa
 	abspath, err := filepath.Abs(root)
 	if err != nil {
 		return nil, err
 	}
 	// Create the root directory if it doesn't exists
0e23b4e1
 	if err := os.MkdirAll(root, 0700); err != nil && !os.IsExist(err) {
33f6a0aa
 		return nil, err
 	}
752bfba2
 
1632566e
 	graph := &Graph{
8ca7b064
 		Root:    abspath,
 		idIndex: utils.NewTruncIndex(),
752bfba2
 		driver:  driver,
1632566e
 	}
 	if err := graph.restore(); err != nil {
 		return nil, err
 	}
 	return graph, nil
 }
 
 func (graph *Graph) restore() error {
 	dir, err := ioutil.ReadDir(graph.Root)
 	if err != nil {
 		return err
 	}
 	for _, v := range dir {
 		id := v.Name()
1b28cdc7
 		if graph.driver.Exists(id) {
 			graph.idIndex.Add(id)
 		}
1632566e
 	}
fde909ff
 	utils.Debugf("Restored %d elements", len(dir))
1632566e
 	return nil
33f6a0aa
 }
 
004a5310
 // FIXME: Implement error subclass instead of looking at the error text
 // Note: This is the way golang implements os.IsNotExists on Plan9
 func (graph *Graph) IsNotExist(err error) bool {
0a5d86d7
 	return err != nil && (strings.Contains(err.Error(), "does not exist") || strings.Contains(err.Error(), "No such"))
004a5310
 }
 
99b36c2c
 // Exists returns true if an image is registered at the given id.
 // If the image doesn't exist or if an error is encountered, false is returned.
33f6a0aa
 func (graph *Graph) Exists(id string) bool {
 	if _, err := graph.Get(id); err != nil {
 		return false
 	}
 	return true
 }
 
99b36c2c
 // Get returns the image with the given id, or an error if the image doesn't exist.
1632566e
 func (graph *Graph) Get(name string) (*Image, error) {
 	id, err := graph.idIndex.Get(name)
 	if err != nil {
 		return nil, err
 	}
379d449c
 	// FIXME: return nil when the image doesn't exist, instead of an error
33f6a0aa
 	img, err := LoadImage(graph.imageRoot(id))
 	if err != nil {
 		return nil, err
 	}
f2bab155
 	// Check that the filesystem layer exists
 	rootfs, err := graph.driver.Get(img.ID)
 	if err != nil {
 		return nil, fmt.Errorf("Driver %s failed to get image rootfs %s: %s", graph.driver, img.ID, err)
 	}
fd224ee5
 	if img.ID != id {
 		return nil, fmt.Errorf("Image stored at '%s' has wrong id '%s'", id, img.ID)
33f6a0aa
 	}
1c946ef0
 	img.graph = graph
697707e4
 
 	if img.Size < 0 {
 		var size int64
 		if img.Parent == "" {
 			if size, err = utils.TreeSize(rootfs); err != nil {
 				return nil, err
 			}
 		} else {
 			parentFs, err := graph.driver.Get(img.Parent)
 			if err != nil {
 				return nil, err
 			}
 			changes, err := archive.ChangesDirs(rootfs, parentFs)
 			if err != nil {
 				return nil, err
 			}
 			size = archive.ChangesSize(rootfs, changes)
a91b7109
 		}
697707e4
 
f2bab155
 		img.Size = size
 		if err := img.SaveSize(graph.imageRoot(id)); err != nil {
a91b7109
 			return nil, err
 		}
 	}
33f6a0aa
 	return img, nil
 }
 
99b36c2c
 // Create creates a new image and registers it in the graph.
96d1e9bb
 func (graph *Graph) Create(layerData archive.Archive, container *Container, comment, author string, config *Config) (*Image, error) {
33f6a0aa
 	img := &Image{
fd224ee5
 		ID:            GenerateID(),
8bfbdd7a
 		Comment:       comment,
806abe90
 		Created:       time.Now().UTC(),
8bfbdd7a
 		DockerVersion: VERSION,
4ef2d5c1
 		Author:        author,
51d62282
 		Config:        config,
f9359f59
 		Architecture:  runtime.GOARCH,
 		OS:            runtime.GOOS,
33f6a0aa
 	}
05ae69a6
 	if container != nil {
 		img.Parent = container.Image
fd224ee5
 		img.Container = container.ID
0146c80c
 		img.ContainerConfig = *container.Config
05ae69a6
 	}
0f134b4b
 	if err := graph.Register(nil, layerData, img); err != nil {
33f6a0aa
 		return nil, err
 	}
 	return img, nil
 }
 
99b36c2c
 // Register imports a pre-existing image into the graph.
 // FIXME: pass img as first argument
02cb7f45
 func (graph *Graph) Register(jsonData []byte, layerData archive.Archive, img *Image) (err error) {
 	defer func() {
 		// If any error occurs, remove the new dir from the driver.
 		// Don't check for errors since the dir might not have been created.
 		// FIXME: this leaves a possible race condition.
 		if err != nil {
 			graph.driver.Remove(img.ID)
 		}
 	}()
fd224ee5
 	if err := ValidateID(img.ID); err != nil {
33f6a0aa
 		return err
 	}
 	// (This is a convenience to save time. Race conditions are taken care of by os.Rename)
fd224ee5
 	if graph.Exists(img.ID) {
 		return fmt.Errorf("Image %s already exists", img.ID)
33f6a0aa
 	}
1b28cdc7
 
 	// Ensure that the image root does not exist on the filesystem
 	// when it is not registered in the graph.
 	// This is common when you switch from one graph driver to another
 	if err := os.RemoveAll(graph.imageRoot(img.ID)); err != nil && !os.IsNotExist(err) {
 		return err
 	}
 
02cb7f45
 	// If the driver has this ID but the graph doesn't, remove it from the driver to start fresh.
 	// (the graph is the source of truth).
 	// Ignore errors, since we don't know if the driver correctly returns ErrNotExist.
 	// (FIXME: make that mandatory for drivers).
 	graph.driver.Remove(img.ID)
 
13d9e26e
 	tmp, err := graph.Mktemp("")
33f6a0aa
 	defer os.RemoveAll(tmp)
 	if err != nil {
 		return fmt.Errorf("Mktemp failed: %s", err)
 	}
f2bab155
 
 	// Create root filesystem in the driver
 	if err := graph.driver.Create(img.ID, img.Parent); err != nil {
 		return fmt.Errorf("Driver %s failed to create image rootfs %s: %s", graph.driver, img.ID, err)
 	}
 	// Mount the root filesystem so we can apply the diff/layer
 	rootfs, err := graph.driver.Get(img.ID)
 	if err != nil {
 		return fmt.Errorf("Driver %s failed to get image rootfs %s: %s", graph.driver, img.ID, err)
 	}
5d972300
 	img.graph = graph
f2bab155
 	if err := StoreImage(img, jsonData, layerData, tmp, rootfs); err != nil {
33f6a0aa
 		return err
 	}
 	// Commit
fd224ee5
 	if err := os.Rename(tmp, graph.imageRoot(img.ID)); err != nil {
33f6a0aa
 		return err
 	}
fd224ee5
 	graph.idIndex.Add(img.ID)
33f6a0aa
 	return nil
 }
 
baacae83
 // TempLayerArchive creates a temporary archive of the given image's filesystem layer.
 //   The archive is stored on disk and will be automatically deleted as soon as has been read.
965e8a02
 //   If output is not nil, a human-readable progress bar will be written to it.
 //   FIXME: does this belong in Graph? How about MktempFile, let the caller use it for archives?
96d1e9bb
 func (graph *Graph) TempLayerArchive(id string, compression archive.Compression, sf *utils.StreamFormatter, output io.Writer) (*archive.TempArchive, error) {
baacae83
 	image, err := graph.Get(id)
 	if err != nil {
 		return nil, err
 	}
f2bab155
 	tmp, err := graph.Mktemp("")
baacae83
 	if err != nil {
 		return nil, err
 	}
4e0c76b3
 	a, err := image.TarLayer()
baacae83
 	if err != nil {
 		return nil, err
 	}
95f061b4
 	return archive.NewTempArchive(utils.ProgressReader(ioutil.NopCloser(a), 0, output, sf, false, utils.TruncateID(id), "Buffering to disk"), tmp)
baacae83
 }
 
99b36c2c
 // Mktemp creates a temporary sub-directory inside the graph's filesystem.
33f6a0aa
 func (graph *Graph) Mktemp(id string) (string, error) {
948bb29d
 	dir := path.Join(graph.Root, "_tmp", GenerateID())
 	if err := os.MkdirAll(dir, 0700); err != nil {
 		return "", err
33f6a0aa
 	}
f2bab155
 	return dir, nil
33f6a0aa
 }
 
f2bab155
 // setupInitLayer populates a directory with mountpoints suitable
5d8efc10
 // for bind-mounting dockerinit into the container. The mountpoint is simply an
 // empty file at /.dockerinit
 //
 // This extra layer is used by all containers as the top-most ro layer. It protects
 // the container from unwanted side-effects on the rw layer.
f2bab155
 func setupInitLayer(initLayer string) error {
18fc707f
 	for pth, typ := range map[string]string{
 		"/dev/pts":         "dir",
 		"/dev/shm":         "dir",
 		"/proc":            "dir",
 		"/sys":             "dir",
 		"/.dockerinit":     "file",
be7eb4bf
 		"/.dockerenv":      "file",
18fc707f
 		"/etc/resolv.conf": "file",
446ca4b5
 		"/etc/hosts":       "file",
 		"/etc/hostname":    "file",
18fc707f
 		// "var/run": "dir",
 		// "var/lock": "dir",
 	} {
29fa1b66
 		parts := strings.Split(pth, "/")
 		prev := "/"
 		for _, p := range parts[1:] {
 			prev = path.Join(prev, p)
 			syscall.Unlink(path.Join(initLayer, prev))
 		}
 
18fc707f
 		if _, err := os.Stat(path.Join(initLayer, pth)); err != nil {
 			if os.IsNotExist(err) {
 				switch typ {
 				case "dir":
 					if err := os.MkdirAll(path.Join(initLayer, pth), 0755); err != nil {
f2bab155
 						return err
18fc707f
 					}
 				case "file":
 					if err := os.MkdirAll(path.Join(initLayer, path.Dir(pth)), 0755); err != nil {
f2bab155
 						return err
18fc707f
 					}
5e941f1c
 					f, err := os.OpenFile(path.Join(initLayer, pth), os.O_CREATE, 0755)
 					if err != nil {
f2bab155
 						return err
18fc707f
 					}
5e941f1c
 					f.Close()
18fc707f
 				}
 			} else {
f2bab155
 				return err
18fc707f
 			}
 		}
5d8efc10
 	}
18fc707f
 
5d8efc10
 	// Layer is ready to use, if it wasn't before.
f2bab155
 	return nil
baacae83
 }
 
13d2b086
 // Check if given error is "not empty".
 // Note: this is the way golang does it internally with os.IsNotExists.
069918e1
 func isNotEmpty(err error) bool {
 	switch pe := err.(type) {
 	case nil:
 		return false
 	case *os.PathError:
 		err = pe.Err
 	case *os.LinkError:
 		err = pe.Err
 	}
 	return strings.Contains(err.Error(), " not empty")
 }
 
99b36c2c
 // Delete atomically removes an image from the graph.
ff5cb8e8
 func (graph *Graph) Delete(name string) error {
 	id, err := graph.idIndex.Get(name)
 	if err != nil {
 		return err
 	}
5d3c0767
 	tmp, err := graph.Mktemp("")
33f6a0aa
 	if err != nil {
 		return err
 	}
1632566e
 	graph.idIndex.Delete(id)
5d3c0767
 	err = os.Rename(graph.imageRoot(id), tmp)
33f6a0aa
 	if err != nil {
 		return err
 	}
f2bab155
 	// Remove rootfs data from the driver
 	graph.driver.Remove(id)
 	// Remove the trashed image directory
5d3c0767
 	return os.RemoveAll(tmp)
33f6a0aa
 }
 
13d2b086
 // Map returns a list of all images in the graph, addressable by ID.
44faa07b
 func (graph *Graph) Map() (map[string]*Image, error) {
1fca99ad
 	images := make(map[string]*Image)
113bb396
 	err := graph.walkAll(func(image *Image) {
1fca99ad
 		images[image.ID] = image
 	})
44faa07b
 	if err != nil {
 		return nil, err
 	}
 	return images, nil
 }
 
113bb396
 // walkAll iterates over each image in the graph, and passes it to a handler.
99b36c2c
 // The walking order is undetermined.
113bb396
 func (graph *Graph) walkAll(handler func(*Image)) error {
33f6a0aa
 	files, err := ioutil.ReadDir(graph.Root)
 	if err != nil {
d301c7b9
 		return err
33f6a0aa
 	}
 	for _, st := range files {
 		if img, err := graph.Get(st.Name()); err != nil {
 			// Skip image
 			continue
d301c7b9
 		} else if handler != nil {
 			handler(img)
 		}
 	}
 	return nil
 }
 
99b36c2c
 // ByParent returns a lookup table of images by their parent.
 // If an image of id ID has 3 children images, then the value for key ID
 // will be a list of 3 images.
 // If an image has no children, it will not have an entry in the table.
d301c7b9
 func (graph *Graph) ByParent() (map[string][]*Image, error) {
 	byParent := make(map[string][]*Image)
113bb396
 	err := graph.walkAll(func(image *Image) {
23c5c130
 		parent, err := graph.Get(image.Parent)
d301c7b9
 		if err != nil {
 			return
 		}
fd224ee5
 		if children, exists := byParent[parent.ID]; exists {
 			byParent[parent.ID] = append(children, image)
025c759e
 		} else {
 			byParent[parent.ID] = []*Image{image}
33f6a0aa
 		}
d301c7b9
 	})
 	return byParent, err
 }
 
99b36c2c
 // Heads returns all heads in the graph, keyed by id.
 // A head is an image which is not the parent of another image in the graph.
d301c7b9
 func (graph *Graph) Heads() (map[string]*Image, error) {
 	heads := make(map[string]*Image)
 	byParent, err := graph.ByParent()
 	if err != nil {
 		return nil, err
33f6a0aa
 	}
113bb396
 	err = graph.walkAll(func(image *Image) {
d301c7b9
 		// If it's not in the byParent lookup table, then
 		// it's not a parent -> so it's a head!
fd224ee5
 		if _, exists := byParent[image.ID]; !exists {
 			heads[image.ID] = image
d301c7b9
 		}
 	})
 	return heads, err
33f6a0aa
 }
 
 func (graph *Graph) imageRoot(id string) string {
 	return path.Join(graph.Root, id)
 }
fb3d60f2
 
 func (graph *Graph) Driver() graphdriver.Driver {
 	return graph.driver
 }