mount.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. options := &mountOptions{
  29. Device: device,
  30. Target: target,
  31. Type: mType,
  32. Flag: 0,
  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. return fmt.Errorf("mountfrom error on re-exec cmd: %v", err)
  45. }
  46. //write the options to the pipe for the untar exec to read
  47. if err := json.NewEncoder(w).Encode(options); err != nil {
  48. return fmt.Errorf("mountfrom json encode to pipe failed: %v", err)
  49. }
  50. w.Close()
  51. if err := cmd.Wait(); err != nil {
  52. return fmt.Errorf("mountfrom re-exec error: %v: output: %s", err, output)
  53. }
  54. return nil
  55. }
  56. // mountfromMain is the entry-point for docker-mountfrom on re-exec.
  57. func mountFromMain() {
  58. runtime.LockOSThread()
  59. flag.Parse()
  60. var options *mountOptions
  61. if err := json.NewDecoder(os.Stdin).Decode(&options); err != nil {
  62. fatal(err)
  63. }
  64. if err := os.Chdir(flag.Arg(0)); err != nil {
  65. fatal(err)
  66. }
  67. if err := syscall.Mount(options.Device, options.Target, options.Type, uintptr(options.Flag), options.Label); err != nil {
  68. fatal(err)
  69. }
  70. os.Exit(0)
  71. }