You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "github.com/chrislusf/seaweedfs/weed/command"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/kardianos/osext"
  10. "github.com/jacobsa/daemonize"
  11. )
  12. var (
  13. fuseCommand = flag.NewFlagSet("weedfuse", flag.ContinueOnError)
  14. options = fuseCommand.String("o", "", "comma separated options rw,uid=xxx,gid=xxx")
  15. isForeground = fuseCommand.Bool("foreground", false, "starts as a daemon")
  16. )
  17. func main() {
  18. err := fuseCommand.Parse(os.Args[1:])
  19. if err != nil {
  20. glog.Fatal(err)
  21. }
  22. fmt.Printf("options: %v\n", *options)
  23. // seems this value is always empty, need to parse it differently
  24. optionsString := *options
  25. prev := ""
  26. for i, arg := range os.Args {
  27. fmt.Printf("args[%d]: %v\n", i, arg)
  28. if prev == "-o" {
  29. optionsString = arg
  30. }
  31. prev = arg
  32. }
  33. device := fuseCommand.Arg(0)
  34. mountPoint := fuseCommand.Arg(1)
  35. fmt.Printf("source: %v\n", device)
  36. fmt.Printf("target: %v\n", mountPoint)
  37. nouser := true
  38. for _, option := range strings.Split(optionsString, ",") {
  39. fmt.Printf("option: %v\n", option)
  40. switch option {
  41. case "user":
  42. nouser = false
  43. }
  44. }
  45. maybeSetupPath()
  46. if !*isForeground {
  47. startAsDaemon()
  48. return
  49. }
  50. parts := strings.SplitN(device, "/", 2)
  51. filer, filerPath := parts[0], parts[1]
  52. command.RunMount(
  53. filer, "/"+filerPath, mountPoint, "", "000", "",
  54. 4, !nouser, 0, 1000000)
  55. }
  56. func maybeSetupPath() {
  57. // sudo mount -av may not include PATH in some linux, e.g., Ubuntu
  58. hasPathEnv := false
  59. for _, e := range os.Environ() {
  60. if strings.HasPrefix(e, "PATH=") {
  61. hasPathEnv = true
  62. }
  63. fmt.Println(e)
  64. }
  65. if !hasPathEnv {
  66. os.Setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin")
  67. }
  68. }
  69. func startAsDaemon() {
  70. // adapted from gcsfuse
  71. // Find the executable.
  72. var path string
  73. path, err := osext.Executable()
  74. if err != nil {
  75. glog.Fatalf("osext.Executable: %v", err)
  76. }
  77. // Set up arguments. Be sure to use foreground mode.
  78. args := append([]string{"-foreground"}, os.Args[1:]...)
  79. // Pass along PATH so that the daemon can find fusermount on Linux.
  80. env := []string{
  81. fmt.Sprintf("PATH=%s", os.Getenv("PATH")),
  82. }
  83. err = daemonize.Run(path, args, env, os.Stdout)
  84. if err != nil {
  85. glog.Fatalf("daemonize.Run: %v", err)
  86. }
  87. }