mount.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // +build linux
  2. package overlay2
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "flag"
  7. "fmt"
  8. "os"
  9. "runtime"
  10. "syscall"
  11. "github.com/docker/docker/pkg/reexec"
  12. )
  13. func init() {
  14. reexec.Register("docker-mountfrom", mountFromMain)
  15. }
  16. func fatal(err error) {
  17. fmt.Fprint(os.Stderr, err)
  18. os.Exit(1)
  19. }
  20. type mountOptions struct {
  21. Device string
  22. Target string
  23. Type string
  24. Label string
  25. Flag uint32
  26. }
  27. func mountFrom(dir, device, target, mType, label string) error {
  28. r, w, err := os.Pipe()
  29. if err != nil {
  30. return fmt.Errorf("mountfrom pipe failure: %v", err)
  31. }
  32. options := &mountOptions{
  33. Device: device,
  34. Target: target,
  35. Type: mType,
  36. Flag: 0,
  37. Label: label,
  38. }
  39. cmd := reexec.Command("docker-mountfrom", dir)
  40. cmd.Stdin = r
  41. output := bytes.NewBuffer(nil)
  42. cmd.Stdout = output
  43. cmd.Stderr = output
  44. if err := cmd.Start(); err != nil {
  45. return fmt.Errorf("mountfrom error on re-exec cmd: %v", err)
  46. }
  47. //write the options to the pipe for the untar exec to read
  48. if err := json.NewEncoder(w).Encode(options); err != nil {
  49. return fmt.Errorf("mountfrom json encode to pipe failed: %v", err)
  50. }
  51. w.Close()
  52. if err := cmd.Wait(); err != nil {
  53. return fmt.Errorf("mountfrom re-exec error: %v: output: %s", err, output)
  54. }
  55. return nil
  56. }
  57. // mountfromMain is the entry-point for docker-mountfrom on re-exec.
  58. func mountFromMain() {
  59. runtime.LockOSThread()
  60. flag.Parse()
  61. var options *mountOptions
  62. if err := json.NewDecoder(os.Stdin).Decode(&options); err != nil {
  63. fatal(err)
  64. }
  65. if err := os.Chdir(flag.Arg(0)); err != nil {
  66. fatal(err)
  67. }
  68. if err := syscall.Mount(options.Device, options.Target, options.Type, uintptr(options.Flag), options.Label); err != nil {
  69. fatal(err)
  70. }
  71. os.Exit(0)
  72. }