archive_unix.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // +build !windows
  2. package chrootarchive
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "flag"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "os"
  11. "runtime"
  12. "github.com/docker/docker/pkg/archive"
  13. "github.com/docker/docker/pkg/reexec"
  14. )
  15. // untar is the entry-point for docker-untar on re-exec. This is not used on
  16. // Windows as it does not support chroot, hence no point sandboxing through
  17. // chroot and rexec.
  18. func untar() {
  19. runtime.LockOSThread()
  20. flag.Parse()
  21. var options *archive.TarOptions
  22. //read the options from the pipe "ExtraFiles"
  23. if err := json.NewDecoder(os.NewFile(3, "options")).Decode(&options); err != nil {
  24. fatal(err)
  25. }
  26. if err := chroot(flag.Arg(0)); err != nil {
  27. fatal(err)
  28. }
  29. if err := archive.Unpack(os.Stdin, "/", options); err != nil {
  30. fatal(err)
  31. }
  32. // fully consume stdin in case it is zero padded
  33. if _, err := flush(os.Stdin); err != nil {
  34. fatal(err)
  35. }
  36. os.Exit(0)
  37. }
  38. func invokeUnpack(decompressedArchive io.Reader, dest string, options *archive.TarOptions) error {
  39. // We can't pass a potentially large exclude list directly via cmd line
  40. // because we easily overrun the kernel's max argument/environment size
  41. // when the full image list is passed (e.g. when this is used by
  42. // `docker load`). We will marshall the options via a pipe to the
  43. // child
  44. r, w, err := os.Pipe()
  45. if err != nil {
  46. return fmt.Errorf("Untar pipe failure: %v", err)
  47. }
  48. cmd := reexec.Command("docker-untar", dest)
  49. cmd.Stdin = decompressedArchive
  50. cmd.ExtraFiles = append(cmd.ExtraFiles, r)
  51. output := bytes.NewBuffer(nil)
  52. cmd.Stdout = output
  53. cmd.Stderr = output
  54. if err := cmd.Start(); err != nil {
  55. w.Close()
  56. return fmt.Errorf("Untar error on re-exec cmd: %v", err)
  57. }
  58. //write the options to the pipe for the untar exec to read
  59. if err := json.NewEncoder(w).Encode(options); err != nil {
  60. w.Close()
  61. return fmt.Errorf("Untar json encode to pipe failed: %v", err)
  62. }
  63. w.Close()
  64. if err := cmd.Wait(); err != nil {
  65. // when `xz -d -c -q | docker-untar ...` failed on docker-untar side,
  66. // we need to exhaust `xz`'s output, otherwise the `xz` side will be
  67. // pending on write pipe forever
  68. io.Copy(ioutil.Discard, decompressedArchive)
  69. return fmt.Errorf("Error processing tar file(%v): %s", err, output)
  70. }
  71. return nil
  72. }