archive_unix.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. return fmt.Errorf("Untar error on re-exec cmd: %v", err)
  56. }
  57. //write the options to the pipe for the untar exec to read
  58. if err := json.NewEncoder(w).Encode(options); err != nil {
  59. return fmt.Errorf("Untar json encode to pipe failed: %v", err)
  60. }
  61. w.Close()
  62. if err := cmd.Wait(); err != nil {
  63. // when `xz -d -c -q | docker-untar ...` failed on docker-untar side,
  64. // we need to exhaust `xz`'s output, otherwise the `xz` side will be
  65. // pending on write pipe forever
  66. io.Copy(ioutil.Discard, decompressedArchive)
  67. return fmt.Errorf("Error processing tar file(%v): %s", err, output)
  68. }
  69. return nil
  70. }