mount.go 1.8 KB

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