func (i *Importer) extract(hash, tmpPath string) *env.Image { outPath := path.Join(env.DIR, "graph", hash) fmt.Printf("Extracting data...\n") jsonData, err := ioutil.ReadFile(path.Join(tmpPath, "metadata.js")) if err != nil { panic(err) } img := &env.Image{} err = json.Unmarshal(jsonData, &img) if err != nil { panic(err) } utils.Run("cp", path.Join(tmpPath, "metadata.js"), path.Join(outPath, "json")) utils.Run("tar", "--numeric-owner", "-f", path.Join(tmpPath, "data.tar.bz2"), "-C", path.Join(outPath, "layer"), "-xj") utils.Run("cp", path.Join(tmpPath, "data.tar.bz2"), path.Join(outPath, "layer.tar.bz2")) os.RemoveAll(tmpPath) fmt.Printf("Importing tags...\n") i.tags.CopyTo(i.sysTags, hash, false) return img }
func (image *Image) layers() ([]string, error) { os.MkdirAll(path.Join(DIR, "graph", "_init"), 0755) os.MkdirAll(path.Join(DIR, "graph", "_init", "proc"), 0755) os.MkdirAll(path.Join(DIR, "graph", "_init", "dev"), 0755) os.MkdirAll(path.Join(DIR, "graph", "_init", "dev", "pts"), 0755) os.MkdirAll(path.Join(DIR, "graph", "_init", "sys"), 0755) os.MkdirAll(path.Join(DIR, "graph", "_init", "etc"), 0755) f, err := os.Create(path.Join(DIR, "graph", "_init", ".dockerinit")) if err != nil { return nil, err } f.Close() f, err = os.Create(path.Join(DIR, "graph", "_init", "etc", "resolv.conf")) if err != nil { return nil, err } f.Close() var layers []string if image.ID != "" { cur := image for cur != nil { lp := path.Join(DIR, "graph", cur.ID, "layer") os.MkdirAll(lp, 0755) lst, _ := ioutil.ReadDir(lp) if len(lst) == 0 { lpfs := path.Join(DIR, "graph", cur.ID, "layer.fs") if _, err := os.Stat(lpfs); err == nil { utils.Run("mount", lpfs, lp) } else { return nil, fmt.Errorf("No layer.fs file to mount") } } layers = append(layers, lp) cur = cur.parentImage } } layers = append(layers, path.Join(DIR, "graph", "_init")) return layers, nil }
func (container *Container) Commit(comment, author string, config *Config, squash bool, fast bool) (*Image, error) { if config == nil { config = container.Config } else { MergeConfig(config, container.Config) } img := &Image{ ID: utils.GenerateID(), Parent: container.Image, Comment: comment, Created: time.Now(), ContainerConfig: *container.Config, Author: author, Config: config, Architecture: "x86_64", } logv("Creating image %s", utils.TruncateID(img.ID)) root := path.Join(DIR, "graph", "_armktmp-"+img.ID) os.MkdirAll(root, 0755) layerPath := path.Join(root, "layer") if squash { layerFs := path.Join(root, "layer.fs") logv("Generating squashfs...") utils.Run("mksquashfs", container.rwPath(), layerFs, "-comp", "xz") } else { if false { logv("Moving data directly into image...") utils.Run("mv", container.rwPath(), layerPath) } else { logv("Copying data into image...") utils.Run("cp", "-a", container.rwPath(), layerPath) } } jsonData, err := json.Marshal(img) if err != nil { panic(err) } err = ioutil.WriteFile(path.Join(root, "json"), jsonData, 0644) if err != nil { panic(err) } err = os.Rename(root, path.Join(DIR, "graph", img.ID)) if err != nil { panic(err) } return img, nil }