func copyMoveRoot(rootfs string, rmUsr bool) error { usrVer := fmt.Sprintf("usr-%s", config.VERSION) usr := path.Join(rootfs, usrVer) targetUsr := path.Join(rootfs, "usr") tmpDir := path.Join(rootfs, "tmp") if rmUsr { log.Warnf("Development setup. Removing old usr: %s", usr) if err := os.RemoveAll(usr); err != nil { // Don't care if this fails log.Errorf("Failed to remove %s: %v", usr, err) } } if cont, err := cleanupTarget(rootfs, targetUsr, usr, usrVer, tmpDir); !cont { return err } log.Debugf("Creating temp dir directory %s", tmpDir) if err := os.MkdirAll(tmpDir, 0755); err != nil { return err } usrVerTmp, err := ioutil.TempDir(tmpDir, usrVer) if err != nil { return err } log.Debugf("Copying to temp dir %s", usrVerTmp) if err := archive.CopyWithTar("/usr", usrVerTmp); err != nil { return err } log.Debugf("Renaming %s => %s", usrVerTmp, usr) if err := os.Rename(usrVerTmp, usr); err != nil { return err } files, err := ioutil.ReadDir("/") if err != nil { return err } for _, file := range files { filename := path.Join("/", file.Name()) if filename == rootfs { log.Debugf("Skipping Deleting %s", filename) continue } log.Debugf("Deleting %s", filename) if err := os.RemoveAll(filename); err != nil { return err } } return nil }
// Prepare works per the snapshot specification. // // For the naive driver, the data is checked out directly into dst and no // mounts are returned. func (lm *Naive) Prepare(dst, parent string) ([]containerd.Mount, error) { metadataRoot, err := ioutil.TempDir(lm.root, "active-") if err != nil { return nil, errors.Wrap(err, "failed to created transaction dir") } // TODO(stevvooe): Write in driver metadata so it can be identified, // probably part of common manager type. if err := ioutil.WriteFile(filepath.Join(metadataRoot, "target"), []byte(dst), 0777); err != nil { return nil, errors.Wrap(err, "failed to write target to disk") } if parent != "" { if _, ok := lm.parents[parent]; !ok { return nil, errors.Wrap(err, "specified parent does not exist") } if err := ioutil.WriteFile(filepath.Join(metadataRoot, "parent"), []byte(parent), 0777); err != nil { return nil, errors.Wrap(err, "error specifying parent") } // Now, we copy the parent filesystem, just a directory, into dst. if err := archive.CopyWithTar(filepath.Join(parent, "data"), dst); err != nil { // note: src, dst args, ick! return nil, errors.Wrap(err, "copying of parent failed") } } lm.active[dst] = activeNaiveSnapshot{ parent: parent, metadata: metadataRoot, } return nil, nil // no mounts!! }
func copyMoveRoot(rootfs string) error { usrVer := fmt.Sprintf("usr-%s", config.VERSION) usr := path.Join(rootfs, usrVer) if err := archive.CopyWithTar("/usr", usr); err != nil { return err } if err := dockerlaunch.CreateSymlink(usrVer, path.Join(rootfs, "usr")); err != nil { return err } files, err := ioutil.ReadDir("/") if err != nil { return err } for _, file := range files { filename := path.Join("/", file.Name()) if filename == rootfs { log.Debugf("Skipping Deleting %s", filename) continue } log.Debugf("Deleting %s", filename) if err := os.RemoveAll(filename); err != nil { return err } } return nil }
func copyMoveRoot(rootfs string) error { usrVer := fmt.Sprintf("usr-%s", config.VERSION) usr := path.Join(rootfs, usrVer) targetUsr := path.Join(rootfs, "usr") tmpDir := path.Join(rootfs, "tmp") if cont, err := cleanupTarget(rootfs, targetUsr, usr, usrVer, tmpDir); !cont { return err } log.Debugf("Creating temp dir directory %s", tmpDir) if err := os.MkdirAll(tmpDir, 0755); err != nil { return err } usrVerTmp, err := ioutil.TempDir(tmpDir, usrVer) if err != nil { return err } log.Debugf("Copying to temp dir %s", usrVerTmp) if err := archive.CopyWithTar("/usr", usrVerTmp); err != nil { return err } log.Debugf("Renaming %s => %s", usrVerTmp, usr) if err := os.Rename(usrVerTmp, usr); err != nil { return err } files, err := ioutil.ReadDir("/") if err != nil { return err } for _, file := range files { filename := path.Join("/", file.Name()) if filename == rootfs { log.Debugf("Skipping Deleting %s", filename) continue } log.Debugf("Deleting %s", filename) if err := os.RemoveAll(filename); err != nil { return err } } return nil }
// testDirectory creates a new temporary directory and returns its path. // The contents of directory at path `templateDir` is copied into the // new directory. func testDirectory(templateDir string) (dir string, err error) { testID := stringid.GenerateNonCryptoID()[:4] prefix := fmt.Sprintf("docker-test%s-%s-", testID, getCallerName(2)) if prefix == "" { prefix = "docker-test-" } dir, err = ioutil.TempDir("", prefix) if err = os.Remove(dir); err != nil { return } if templateDir != "" { if err = archive.CopyWithTar(templateDir, dir); err != nil { return } } return }
// TestDirectory creates a new temporary directory and returns its path. // The contents of directory at path `templateDir` is copied into the // new directory. func TestDirectory(templateDir string) (dir string, err error) { if globalTestID == "" { globalTestID = common.RandomString()[:4] } prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, GetCallerName(2)) if prefix == "" { prefix = "docker-test-" } dir, err = ioutil.TempDir("", prefix) if err = os.Remove(dir); err != nil { return } if templateDir != "" { if err = archive.CopyWithTar(templateDir, dir); err != nil { return } } return }
func copyAsDirectory(source, destination string, destinationExists bool) error { if err := archive.CopyWithTar(source, destination); err != nil { return err } if destinationExists { files, err := ioutil.ReadDir(source) if err != nil { return err } for _, file := range files { if err := fixPermissions(filepath.Join(destination, file.Name()), 0, 0); err != nil { return err } } return nil } return fixPermissions(destination, 0, 0) }
// Commit just moves the metadata directory to the diff location. func (lm *Naive) Commit(diff, dst string) error { active, ok := lm.active[dst] if !ok { return errors.Errorf("%v is not an active transaction", dst) } // Move the data into our metadata directory, we could probably save disk // space if we just saved the diff, but let's get something working. if err := archive.CopyWithTar(dst, filepath.Join(active.metadata, "data")); err != nil { // note: src, dst args, ick! return errors.Wrap(err, "copying of parent failed") } if err := os.Rename(active.metadata, diff); err != nil { return errors.Wrap(err, "failed to rename metadata into diff") } lm.parents[diff] = active.parent delete(lm.active, dst) return nil }
func copyExistingContents(source, destination string) error { volList, err := ioutil.ReadDir(source) if err != nil { return err } if len(volList) > 0 { srcList, err := ioutil.ReadDir(destination) if err != nil { return err } if len(srcList) == 0 { // If the source volume is empty copy files from the root into the volume if err := archive.CopyWithTar(source, destination); err != nil { return err } } } return copyOwnership(source, destination) }
func (d *Driver) Create(id, parent string) error { dir := d.dir(id) if err := os.MkdirAll(path.Dir(dir), 0700); err != nil { return err } if err := os.Mkdir(dir, 0755); err != nil { return err } opts := []string{"level:s0"} if _, mountLabel, err := label.InitLabels(opts); err == nil { label.Relabel(dir, mountLabel, "") } if parent == "" { return nil } parentDir, err := d.Get(parent, "") if err != nil { return fmt.Errorf("%s: %s", parent, err) } if err := archive.CopyWithTar(parentDir, dir); err != nil { return err } return nil }
func (b *Builder) addContext(container *daemon.Container, orig, dest string, decompress bool) error { var ( err error destExists = true origPath = path.Join(b.contextPath, orig) destPath = path.Join(container.RootfsPath(), dest) ) if destPath != container.RootfsPath() { destPath, err = symlink.FollowSymlinkInScope(destPath, container.RootfsPath()) if err != nil { return err } } // Preserve the trailing '/' if strings.HasSuffix(dest, "/") || dest == "." { destPath = destPath + "/" } destStat, err := os.Stat(destPath) if err != nil { if !os.IsNotExist(err) { return err } destExists = false } fi, err := os.Stat(origPath) if err != nil { if os.IsNotExist(err) { return fmt.Errorf("%s: no such file or directory", orig) } return err } if fi.IsDir() { return copyAsDirectory(origPath, destPath, destExists) } // If we are adding a remote file (or we've been told not to decompress), do not try to untar it if decompress { // First try to unpack the source as an archive // to support the untar feature we need to clean up the path a little bit // because tar is very forgiving. First we need to strip off the archive's // filename from the path but this is only added if it does not end in / . tarDest := destPath if strings.HasSuffix(tarDest, "/") { tarDest = filepath.Dir(destPath) } // try to successfully untar the orig if err := archive.UntarPath(origPath, tarDest); err == nil { return nil } else if err != io.EOF { log.Debugf("Couldn't untar %s to %s: %s", origPath, tarDest, err) } } if err := os.MkdirAll(path.Dir(destPath), 0755); err != nil { return err } if err := archive.CopyWithTar(origPath, destPath); err != nil { return err } resPath := destPath if destExists && destStat.IsDir() { resPath = path.Join(destPath, path.Base(origPath)) } return fixPermissions(resPath, 0, 0) }