mount.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // +build linux
  2. package overlay2
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "flag"
  7. "fmt"
  8. "os"
  9. "runtime"
  10. "github.com/docker/docker/pkg/reexec"
  11. "golang.org/x/sys/unix"
  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 string, flags uintptr, label string) error {
  28. options := &mountOptions{
  29. Device: device,
  30. Target: target,
  31. Type: mType,
  32. Flag: uint32(flags),
  33. Label: label,
  34. }
  35. cmd := reexec.Command("docker-mountfrom", dir)
  36. w, err := cmd.StdinPipe()
  37. if err != nil {
  38. return fmt.Errorf("mountfrom error on pipe creation: %v", err)
  39. }
  40. output := bytes.NewBuffer(nil)
  41. cmd.Stdout = output
  42. cmd.Stderr = output
  43. if err := cmd.Start(); err != nil {
  44. w.Close()
  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. w.Close()
  50. return fmt.Errorf("mountfrom json encode to pipe failed: %v", err)
  51. }
  52. w.Close()
  53. if err := cmd.Wait(); err != nil {
  54. return fmt.Errorf("mountfrom re-exec error: %v: output: %v", err, output)
  55. }
  56. return nil
  57. }
  58. // mountfromMain is the entry-point for docker-mountfrom on re-exec.
  59. func mountFromMain() {
  60. runtime.LockOSThread()
  61. flag.Parse()
  62. var options *mountOptions
  63. if err := json.NewDecoder(os.Stdin).Decode(&options); err != nil {
  64. fatal(err)
  65. }
  66. if err := os.Chdir(flag.Arg(0)); err != nil {
  67. fatal(err)
  68. }
  69. if err := unix.Mount(options.Device, options.Target, options.Type, uintptr(options.Flag), options.Label); err != nil {
  70. fatal(err)
  71. }
  72. os.Exit(0)
  73. }