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.

127 lines
3.1 KiB

7 years ago
6 years ago
7 years ago
  1. // +build linux darwin
  2. package command
  3. import (
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/security"
  6. "github.com/chrislusf/seaweedfs/weed/server"
  7. "github.com/spf13/viper"
  8. "os"
  9. "os/user"
  10. "runtime"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/chrislusf/seaweedfs/weed/filesys"
  15. "github.com/chrislusf/seaweedfs/weed/glog"
  16. "github.com/chrislusf/seaweedfs/weed/util"
  17. "github.com/seaweedfs/fuse"
  18. "github.com/seaweedfs/fuse/fs"
  19. )
  20. func runMount(cmd *Command, args []string) bool {
  21. weed_server.LoadConfiguration("security", false)
  22. fmt.Printf("This is SeaweedFS version %s %s %s\n", util.VERSION, runtime.GOOS, runtime.GOARCH)
  23. if *mountOptions.dir == "" {
  24. fmt.Printf("Please specify the mount directory via \"-dir\"")
  25. return false
  26. }
  27. if *mountOptions.chunkSizeLimitMB <= 0 {
  28. fmt.Printf("Please specify a reasonable buffer size.")
  29. return false
  30. }
  31. fuse.Unmount(*mountOptions.dir)
  32. // detect mount folder mode
  33. mountMode := os.ModeDir | 0755
  34. if fileInfo, err := os.Stat(*mountOptions.dir); err == nil {
  35. mountMode = os.ModeDir | fileInfo.Mode()
  36. }
  37. // detect current user
  38. uid, gid := uint32(0), uint32(0)
  39. if u, err := user.Current(); err == nil {
  40. if parsedId, pe := strconv.ParseUint(u.Uid, 10, 32); pe == nil {
  41. uid = uint32(parsedId)
  42. }
  43. if parsedId, pe := strconv.ParseUint(u.Gid, 10, 32); pe == nil {
  44. gid = uint32(parsedId)
  45. }
  46. }
  47. util.SetupProfiling(*mountCpuProfile, *mountMemProfile)
  48. options := []fuse.MountOption{
  49. fuse.VolumeName("SeaweedFS"),
  50. fuse.FSName("SeaweedFS"),
  51. fuse.Subtype("SeaweedFS"),
  52. fuse.NoAppleDouble(),
  53. fuse.NoAppleXattr(),
  54. fuse.NoBrowse(),
  55. fuse.AutoXattr(),
  56. fuse.ExclCreate(),
  57. fuse.DaemonTimeout("3600"),
  58. fuse.AllowSUID(),
  59. fuse.DefaultPermissions(),
  60. fuse.MaxReadahead(1024 * 128),
  61. fuse.AsyncRead(),
  62. fuse.WritebackCache(),
  63. }
  64. if *mountOptions.allowOthers {
  65. options = append(options, fuse.AllowOther())
  66. }
  67. c, err := fuse.Mount(*mountOptions.dir, options...)
  68. if err != nil {
  69. glog.Fatal(err)
  70. return false
  71. }
  72. util.OnInterrupt(func() {
  73. fuse.Unmount(*mountOptions.dir)
  74. c.Close()
  75. })
  76. filerGrpcAddress, err := parseFilerGrpcAddress(*mountOptions.filer)
  77. if err != nil {
  78. glog.Fatal(err)
  79. return false
  80. }
  81. mountRoot := *mountOptions.filerMountRootPath
  82. if mountRoot != "/" && strings.HasSuffix(mountRoot, "/") {
  83. mountRoot = mountRoot[0 : len(mountRoot)-1]
  84. }
  85. err = fs.Serve(c, filesys.NewSeaweedFileSystem(&filesys.Option{
  86. FilerGrpcAddress: filerGrpcAddress,
  87. GrpcDialOption: security.LoadClientTLS(viper.Sub("grpc"), "client"),
  88. FilerMountRootPath: mountRoot,
  89. Collection: *mountOptions.collection,
  90. Replication: *mountOptions.replication,
  91. TtlSec: int32(*mountOptions.ttlSec),
  92. ChunkSizeLimit: int64(*mountOptions.chunkSizeLimitMB) * 1024 * 1024,
  93. DataCenter: *mountOptions.dataCenter,
  94. DirListingLimit: *mountOptions.dirListingLimit,
  95. EntryCacheTtl: 3 * time.Second,
  96. MountUid: uid,
  97. MountGid: gid,
  98. MountMode: mountMode,
  99. }))
  100. if err != nil {
  101. fuse.Unmount(*mountOptions.dir)
  102. }
  103. // check if the mount process has an error to report
  104. <-c.Ready
  105. if err := c.MountError; err != nil {
  106. glog.Fatal(err)
  107. }
  108. return true
  109. }