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.

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