2014-10-29 15:06:51 -04:00
|
|
|
package chrootarchive
|
|
|
|
|
|
|
|
import (
|
2015-04-10 14:23:09 -04:00
|
|
|
"bytes"
|
2014-11-08 10:38:42 -05:00
|
|
|
"encoding/json"
|
2014-10-29 15:06:51 -04:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2014-12-08 16:19:24 -05:00
|
|
|
"path/filepath"
|
2014-10-29 15:06:51 -04:00
|
|
|
"runtime"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
|
|
|
)
|
|
|
|
|
2014-12-12 13:38:48 -05:00
|
|
|
var chrootArchiver = &archive.Archiver{Untar: Untar}
|
2014-12-08 16:14:56 -05:00
|
|
|
|
|
|
|
func chroot(path string) error {
|
|
|
|
if err := syscall.Chroot(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return syscall.Chdir("/")
|
|
|
|
}
|
|
|
|
|
2014-10-29 15:06:51 -04:00
|
|
|
func untar() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
flag.Parse()
|
2015-01-29 10:28:44 -05:00
|
|
|
|
|
|
|
var options *archive.TarOptions
|
|
|
|
|
2015-04-10 14:23:09 -04:00
|
|
|
//read the options from the pipe "ExtraFiles"
|
|
|
|
if err := json.NewDecoder(os.NewFile(3, "options")).Decode(&options); err != nil {
|
2014-10-29 15:06:51 -04:00
|
|
|
fatal(err)
|
|
|
|
}
|
2015-01-29 10:28:44 -05:00
|
|
|
|
|
|
|
if err := chroot(flag.Arg(0)); err != nil {
|
2014-11-08 10:38:42 -05:00
|
|
|
fatal(err)
|
|
|
|
}
|
2014-12-08 16:19:24 -05:00
|
|
|
if err := archive.Unpack(os.Stdin, "/", options); err != nil {
|
2014-10-29 15:06:51 -04:00
|
|
|
fatal(err)
|
|
|
|
}
|
2014-12-05 21:30:03 -05:00
|
|
|
// fully consume stdin in case it is zero padded
|
2014-12-08 18:04:34 -05:00
|
|
|
flush(os.Stdin)
|
2014-10-29 15:06:51 -04:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2014-12-08 16:14:56 -05:00
|
|
|
func Untar(tarArchive io.Reader, dest string, options *archive.TarOptions) error {
|
|
|
|
if tarArchive == nil {
|
|
|
|
return fmt.Errorf("Empty archive")
|
|
|
|
}
|
|
|
|
if options == nil {
|
|
|
|
options = &archive.TarOptions{}
|
|
|
|
}
|
2014-10-23 17:30:11 -04:00
|
|
|
if options.ExcludePatterns == nil {
|
|
|
|
options.ExcludePatterns = []string{}
|
2014-12-08 16:14:56 -05:00
|
|
|
}
|
2014-10-29 15:06:51 -04:00
|
|
|
|
2015-01-29 10:28:44 -05:00
|
|
|
dest = filepath.Clean(dest)
|
2014-10-29 15:06:51 -04:00
|
|
|
if _, err := os.Stat(dest); os.IsNotExist(err) {
|
|
|
|
if err := os.MkdirAll(dest, 0777); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-01-29 10:28:44 -05:00
|
|
|
|
2014-12-08 16:19:24 -05:00
|
|
|
decompressedArchive, err := archive.DecompressStream(tarArchive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer decompressedArchive.Close()
|
2014-11-08 10:38:42 -05:00
|
|
|
|
2015-04-10 14:23:09 -04:00
|
|
|
// We can't pass a potentially large exclude list directly via cmd line
|
|
|
|
// because we easily overrun the kernel's max argument/environment size
|
|
|
|
// when the full image list is passed (e.g. when this is used by
|
|
|
|
// `docker load`). We will marshall the options via a pipe to the
|
|
|
|
// child
|
|
|
|
r, w, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Untar pipe failure: %v", err)
|
|
|
|
}
|
2015-01-29 10:28:44 -05:00
|
|
|
cmd := reexec.Command("docker-untar", dest)
|
2014-12-08 16:19:24 -05:00
|
|
|
cmd.Stdin = decompressedArchive
|
2015-04-10 14:23:09 -04:00
|
|
|
cmd.ExtraFiles = append(cmd.ExtraFiles, r)
|
2015-04-14 12:13:50 -04:00
|
|
|
output := bytes.NewBuffer(nil)
|
|
|
|
cmd.Stdout = output
|
|
|
|
cmd.Stderr = output
|
2015-04-10 14:23:09 -04:00
|
|
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return fmt.Errorf("Untar error on re-exec cmd: %v", err)
|
|
|
|
}
|
|
|
|
//write the options to the pipe for the untar exec to read
|
|
|
|
if err := json.NewEncoder(w).Encode(options); err != nil {
|
|
|
|
return fmt.Errorf("Untar json encode to pipe failed: %v", err)
|
|
|
|
}
|
|
|
|
w.Close()
|
|
|
|
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
return fmt.Errorf("Untar re-exec error: %v: output: %s", err, output)
|
2014-10-29 15:06:51 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TarUntar(src, dst string) error {
|
|
|
|
return chrootArchiver.TarUntar(src, dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyWithTar creates a tar archive of filesystem path `src`, and
|
|
|
|
// unpacks it at filesystem path `dst`.
|
|
|
|
// The archive is streamed directly with fixed buffering and no
|
|
|
|
// intermediary disk IO.
|
|
|
|
func CopyWithTar(src, dst string) error {
|
|
|
|
return chrootArchiver.CopyWithTar(src, dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyFileWithTar emulates the behavior of the 'cp' command-line
|
|
|
|
// for a single file. It copies a regular file from path `src` to
|
|
|
|
// path `dst`, and preserves all its metadata.
|
|
|
|
//
|
|
|
|
// If `dst` ends with a trailing slash '/', the final destination path
|
|
|
|
// will be `dst/base(src)`.
|
|
|
|
func CopyFileWithTar(src, dst string) (err error) {
|
|
|
|
return chrootArchiver.CopyFileWithTar(src, dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UntarPath is a convenience function which looks for an archive
|
|
|
|
// at filesystem path `src`, and unpacks it at `dst`.
|
|
|
|
func UntarPath(src, dst string) error {
|
|
|
|
return chrootArchiver.UntarPath(src, dst)
|
|
|
|
}
|