2013-03-21 20:47:23 -04:00
|
|
|
package docker
|
2013-03-18 03:15:35 -04:00
|
|
|
|
|
|
|
import (
|
2013-03-26 16:46:27 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2013-03-18 03:15:35 -04:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2013-10-31 19:57:45 -04:00
|
|
|
"github.com/dotcloud/docker/archive"
|
2013-05-14 18:37:35 -04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-03-21 04:07:07 -04:00
|
|
|
"io"
|
2013-03-18 03:15:35 -04:00
|
|
|
"io/ioutil"
|
2013-04-15 06:05:46 -04:00
|
|
|
"log"
|
2013-03-18 03:15:35 -04:00
|
|
|
"os"
|
2013-04-15 06:05:46 -04:00
|
|
|
"os/exec"
|
2013-03-18 03:15:35 -04:00
|
|
|
"path"
|
2013-05-13 09:10:26 -04:00
|
|
|
"path/filepath"
|
2013-07-22 18:40:33 -04:00
|
|
|
"strconv"
|
2013-03-18 03:15:35 -04:00
|
|
|
"strings"
|
2013-10-18 21:17:00 -04:00
|
|
|
"syscall"
|
2013-03-18 03:15:35 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Image struct {
|
2013-06-04 14:00:22 -04:00
|
|
|
ID string `json:"id"`
|
2013-03-21 09:35:57 -04:00
|
|
|
Parent string `json:"parent,omitempty"`
|
|
|
|
Comment string `json:"comment,omitempty"`
|
|
|
|
Created time.Time `json:"created"`
|
2013-03-23 17:48:16 -04:00
|
|
|
Container string `json:"container,omitempty"`
|
|
|
|
ContainerConfig Config `json:"container_config,omitempty"`
|
2013-04-04 21:38:43 -04:00
|
|
|
DockerVersion string `json:"docker_version,omitempty"`
|
2013-04-17 22:58:17 -04:00
|
|
|
Author string `json:"author,omitempty"`
|
2013-04-25 19:48:31 -04:00
|
|
|
Config *Config `json:"config,omitempty"`
|
2013-05-24 14:40:56 -04:00
|
|
|
Architecture string `json:"architecture,omitempty"`
|
2013-03-22 00:13:27 -04:00
|
|
|
graph *Graph
|
2013-05-13 09:10:26 -04:00
|
|
|
Size int64
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func LoadImage(root string) (*Image, error) {
|
|
|
|
// Load the json data
|
|
|
|
jsonData, err := ioutil.ReadFile(jsonPath(root))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-05-08 13:42:29 -04:00
|
|
|
img := &Image{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(jsonData, img); err != nil {
|
2013-03-18 03:15:35 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := ValidateID(img.ID); err != nil {
|
2013-03-18 03:15:35 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-22 18:40:33 -04:00
|
|
|
|
|
|
|
if buf, err := ioutil.ReadFile(path.Join(root, "layersize")); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2013-11-18 18:35:56 -05:00
|
|
|
size, err := strconv.Atoi(string(buf))
|
|
|
|
if err != nil {
|
2013-07-22 18:40:33 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-11-18 18:35:56 -05:00
|
|
|
img.Size = int64(size)
|
2013-07-22 18:40:33 -04:00
|
|
|
}
|
|
|
|
|
2013-03-18 03:15:35 -04:00
|
|
|
// Check that the filesystem layer exists
|
|
|
|
if stat, err := os.Stat(layerPath(root)); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2013-06-04 14:00:22 -04:00
|
|
|
return nil, fmt.Errorf("Couldn't load image %s: no filesystem layer", img.ID)
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
2013-06-04 09:51:12 -04:00
|
|
|
return nil, err
|
2013-03-18 03:15:35 -04:00
|
|
|
} else if !stat.IsDir() {
|
2013-06-04 14:00:22 -04:00
|
|
|
return nil, fmt.Errorf("Couldn't load image %s: %s is not a directory", img.ID, layerPath(root))
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
2013-05-08 13:42:29 -04:00
|
|
|
return img, nil
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
|
|
|
|
2013-10-31 19:57:45 -04:00
|
|
|
func StoreImage(img *Image, jsonData []byte, layerData archive.Archive, root string) error {
|
2013-03-18 03:15:35 -04:00
|
|
|
// Check that root doesn't already exist
|
|
|
|
if _, err := os.Stat(root); err == nil {
|
2013-06-04 14:00:22 -04:00
|
|
|
return fmt.Errorf("Image %s already exists", img.ID)
|
2013-03-18 03:15:35 -04:00
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Store the layer
|
|
|
|
layer := layerPath(root)
|
2013-07-25 11:45:15 -04:00
|
|
|
if err := os.MkdirAll(layer, 0755); err != nil {
|
2013-03-18 03:15:35 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-08 22:08:11 -04:00
|
|
|
|
2013-06-22 15:29:42 -04:00
|
|
|
// If layerData is not nil, unpack it into the new layer
|
|
|
|
if layerData != nil {
|
2013-07-08 16:30:03 -04:00
|
|
|
start := time.Now()
|
|
|
|
utils.Debugf("Start untar layer")
|
2013-10-31 19:57:45 -04:00
|
|
|
if err := archive.Untar(layerData, layer); err != nil {
|
2013-06-22 15:29:42 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-10-25 20:50:40 -04:00
|
|
|
utils.Debugf("Untar time: %vs", time.Now().Sub(start).Seconds())
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
2013-05-13 09:10:26 -04:00
|
|
|
|
2013-07-22 18:40:33 -04:00
|
|
|
// If raw json is provided, then use it
|
|
|
|
if jsonData != nil {
|
|
|
|
return ioutil.WriteFile(jsonPath(root), jsonData, 0600)
|
2013-11-18 18:35:56 -05:00
|
|
|
}
|
|
|
|
// Otherwise, unmarshal the image
|
|
|
|
jsonData, err := json.Marshal(img)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(jsonPath(root), jsonData, 0600); err != nil {
|
|
|
|
return err
|
2013-07-22 18:40:33 -04:00
|
|
|
}
|
|
|
|
|
2013-05-13 09:10:26 -04:00
|
|
|
return StoreSize(img, root)
|
|
|
|
}
|
|
|
|
|
|
|
|
func StoreSize(img *Image, root string) error {
|
|
|
|
layer := layerPath(root)
|
2013-10-18 21:17:00 -04:00
|
|
|
data := make(map[uint64]bool)
|
2013-05-13 09:10:26 -04:00
|
|
|
|
2013-11-18 18:35:56 -05:00
|
|
|
var totalSize int64
|
2013-05-13 09:10:26 -04:00
|
|
|
filepath.Walk(layer, func(path string, fileInfo os.FileInfo, err error) error {
|
2013-10-18 21:17:00 -04:00
|
|
|
size := fileInfo.Size()
|
|
|
|
if size == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
inode := fileInfo.Sys().(*syscall.Stat_t).Ino
|
|
|
|
if _, entryExists := data[inode]; entryExists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
data[inode] = false
|
|
|
|
|
|
|
|
totalSize += size
|
2013-05-13 09:10:26 -04:00
|
|
|
return nil
|
|
|
|
})
|
2013-07-22 18:40:33 -04:00
|
|
|
img.Size = totalSize
|
2013-05-13 09:10:26 -04:00
|
|
|
|
2013-07-22 18:40:33 -04:00
|
|
|
if err := ioutil.WriteFile(path.Join(root, "layersize"), []byte(strconv.Itoa(int(totalSize))), 0600); err != nil {
|
|
|
|
return nil
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
2013-07-22 18:40:33 -04:00
|
|
|
|
2013-03-18 03:15:35 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func layerPath(root string) string {
|
|
|
|
return path.Join(root, "layer")
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonPath(root string) string {
|
|
|
|
return path.Join(root, "json")
|
|
|
|
}
|
|
|
|
|
|
|
|
func MountAUFS(ro []string, rw string, target string) error {
|
|
|
|
// FIXME: Now mount the layers
|
|
|
|
rwBranch := fmt.Sprintf("%v=rw", rw)
|
|
|
|
roBranches := ""
|
|
|
|
for _, layer := range ro {
|
2013-04-19 19:33:25 -04:00
|
|
|
roBranches += fmt.Sprintf("%v=ro+wh:", layer)
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
|
|
|
branches := fmt.Sprintf("br:%v:%v", rwBranch, roBranches)
|
2013-04-15 06:05:46 -04:00
|
|
|
|
2013-06-05 08:59:05 -04:00
|
|
|
branches += ",xino=/dev/shm/aufs.xino"
|
|
|
|
|
2013-04-15 06:05:46 -04:00
|
|
|
//if error, try to load aufs kernel module
|
|
|
|
if err := mount("none", target, "aufs", 0, branches); err != nil {
|
|
|
|
log.Printf("Kernel does not support AUFS, trying to load the AUFS module with modprobe...")
|
|
|
|
if err := exec.Command("modprobe", "aufs").Run(); err != nil {
|
|
|
|
return fmt.Errorf("Unable to load the AUFS module")
|
|
|
|
}
|
|
|
|
log.Printf("...module loaded.")
|
|
|
|
if err := mount("none", target, "aufs", 0, branches); err != nil {
|
|
|
|
return fmt.Errorf("Unable to mount using aufs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
|
|
|
|
2013-04-21 17:23:55 -04:00
|
|
|
// TarLayer returns a tar archive of the image's filesystem layer.
|
2013-11-18 18:35:56 -05:00
|
|
|
func (img *Image) TarLayer(compression archive.Compression) (archive.Archive, error) {
|
|
|
|
layerPath, err := img.layer()
|
2013-04-21 17:23:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-10-31 19:57:45 -04:00
|
|
|
return archive.Tar(layerPath, compression)
|
2013-04-21 17:23:55 -04:00
|
|
|
}
|
|
|
|
|
2013-11-18 18:35:56 -05:00
|
|
|
func (img *Image) Mount(root, rw string) error {
|
2013-03-21 01:41:03 -04:00
|
|
|
if mounted, err := Mounted(root); err != nil {
|
2013-03-18 03:15:35 -04:00
|
|
|
return err
|
2013-03-21 01:41:03 -04:00
|
|
|
} else if mounted {
|
2013-03-21 01:13:57 -04:00
|
|
|
return fmt.Errorf("%s is already mounted", root)
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
2013-11-18 18:35:56 -05:00
|
|
|
layers, err := img.layers()
|
2013-03-18 03:15:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-21 01:13:28 -04:00
|
|
|
// Create the target directories if they don't exist
|
|
|
|
if err := os.Mkdir(root, 0755); err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Mkdir(rw, 0755); err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-18 03:15:35 -04:00
|
|
|
if err := MountAUFS(layers, rw, root); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-18 18:35:56 -05:00
|
|
|
func (img *Image) Changes(rw string) ([]Change, error) {
|
|
|
|
layers, err := img.layers()
|
2013-03-21 01:12:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return Changes(layers, rw)
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
func ValidateID(id string) error {
|
2013-03-18 03:15:35 -04:00
|
|
|
if id == "" {
|
|
|
|
return fmt.Errorf("Image id can't be empty")
|
|
|
|
}
|
|
|
|
if strings.Contains(id, ":") {
|
|
|
|
return fmt.Errorf("Invalid character in image id: ':'")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
func GenerateID() string {
|
2013-03-26 16:46:27 -04:00
|
|
|
id := make([]byte, 32)
|
|
|
|
_, err := io.ReadFull(rand.Reader, id)
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // This shouldn't happen
|
2013-03-21 04:07:07 -04:00
|
|
|
}
|
2013-03-26 16:46:27 -04:00
|
|
|
return hex.EncodeToString(id)
|
2013-03-18 03:15:35 -04: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 13:10:14 -04:00
|
|
|
func(img *Image) error {
|
2013-03-18 03:15:35 -04:00
|
|
|
parents = append(parents, img)
|
2013-03-21 13:10:14 -04:00
|
|
|
return nil
|
2013-03-18 03:15:35 -04:00
|
|
|
},
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return parents, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// layers returns all the filesystem layers needed to mount an image
|
2013-03-21 13:10:14 -04:00
|
|
|
// FIXME: @shykes refactor this function with the new error handling
|
|
|
|
// (I'll do it if I have time tonight, I focus on the rest)
|
2013-03-18 03:15:35 -04:00
|
|
|
func (img *Image) layers() ([]string, error) {
|
2013-11-18 18:35:56 -05:00
|
|
|
var (
|
|
|
|
list []string
|
|
|
|
e error
|
|
|
|
)
|
2013-03-18 03:15:35 -04:00
|
|
|
if err := img.WalkHistory(
|
2013-03-21 13:10:14 -04:00
|
|
|
func(img *Image) (err error) {
|
2013-03-18 03:15:35 -04:00
|
|
|
if layer, err := img.layer(); err != nil {
|
|
|
|
e = err
|
|
|
|
} else if layer != "" {
|
|
|
|
list = append(list, layer)
|
|
|
|
}
|
2013-03-21 13:10:14 -04:00
|
|
|
return err
|
2013-03-18 03:15:35 -04:00
|
|
|
},
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if e != nil { // Did an error occur inside the handler?
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
if len(list) == 0 {
|
2013-06-04 14:00:22 -04:00
|
|
|
return nil, fmt.Errorf("No layer found for image %s\n", img.ID)
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
2013-06-14 19:56:08 -04:00
|
|
|
|
|
|
|
// Inject the dockerinit layer (empty place-holder for mount-binding dockerinit)
|
2013-11-18 18:35:56 -05:00
|
|
|
dockerinitLayer, err := img.getDockerInitLayer()
|
|
|
|
if err != nil {
|
2013-06-14 19:56:08 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-11-18 18:35:56 -05:00
|
|
|
return append([]string{dockerinitLayer}, list...), nil
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
|
|
|
|
2013-03-21 13:10:14 -04:00
|
|
|
func (img *Image) WalkHistory(handler func(*Image) error) (err error) {
|
2013-03-18 03:15:35 -04:00
|
|
|
currentImg := img
|
|
|
|
for currentImg != nil {
|
|
|
|
if handler != nil {
|
2013-03-21 13:10:14 -04:00
|
|
|
if err := handler(currentImg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-18 03:15:35 -04: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)
|
|
|
|
}
|
|
|
|
|
2013-06-14 19:56:08 -04:00
|
|
|
func (img *Image) getDockerInitLayer() (string, error) {
|
|
|
|
if img.graph == nil {
|
|
|
|
return "", fmt.Errorf("Can't lookup dockerinit layer of unregistered image")
|
|
|
|
}
|
|
|
|
return img.graph.getDockerInitLayer()
|
|
|
|
}
|
|
|
|
|
2013-03-18 03:15:35 -04:00
|
|
|
func (img *Image) root() (string, error) {
|
|
|
|
if img.graph == nil {
|
|
|
|
return "", fmt.Errorf("Can't lookup root of unregistered image")
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
return img.graph.imageRoot(img.ID), nil
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the path of an image's layer
|
|
|
|
func (img *Image) layer() (string, error) {
|
|
|
|
root, err := img.root()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return layerPath(root), nil
|
|
|
|
}
|
2013-04-26 17:00:09 -04:00
|
|
|
|
2013-06-14 06:05:01 -04:00
|
|
|
func (img *Image) getParentsSize(size int64) int64 {
|
2013-05-13 09:10:26 -04:00
|
|
|
parentImage, err := img.GetParent()
|
|
|
|
if err != nil || parentImage == nil {
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
size += parentImage.Size
|
2013-06-14 06:05:01 -04:00
|
|
|
return parentImage.getParentsSize(size)
|
2013-05-13 09:10:26 -04:00
|
|
|
}
|
2013-05-22 09:41:29 -04:00
|
|
|
|
2013-05-14 21:41:39 -04:00
|
|
|
// Build an Image object from raw json data
|
2013-06-04 14:00:22 -04:00
|
|
|
func NewImgJSON(src []byte) (*Image, error) {
|
2013-05-14 21:41:39 -04:00
|
|
|
ret := &Image{}
|
|
|
|
|
2013-10-25 20:50:40 -04:00
|
|
|
utils.Debugf("Json string: {%s}", src)
|
2013-05-14 21:41:39 -04: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
|
|
|
|
}
|