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.

130 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. }
  66. if *mountOptions.allowOthers {
  67. options = append(options, fuse.AllowOther())
  68. }
  69. c, err := fuse.Mount(*mountOptions.dir, options...)
  70. if err != nil {
  71. glog.Fatal(err)
  72. return false
  73. }
  74. util.OnInterrupt(func() {
  75. fuse.Unmount(*mountOptions.dir)
  76. c.Close()
  77. })
  78. filerGrpcAddress, err := parseFilerGrpcAddress(*mountOptions.filer)
  79. if err != nil {
  80. glog.Fatal(err)
  81. return false
  82. }
  83. mountRoot := *mountOptions.filerMountRootPath
  84. if mountRoot != "/" && strings.HasSuffix(mountRoot, "/") {
  85. mountRoot = mountRoot[0 : len(mountRoot)-1]
  86. }
  87. err = fs.Serve(c, filesys.NewSeaweedFileSystem(&filesys.Option{
  88. FilerGrpcAddress: filerGrpcAddress,
  89. GrpcDialOption: security.LoadClientTLS(viper.Sub("grpc"), "client"),
  90. FilerMountRootPath: mountRoot,
  91. Collection: *mountOptions.collection,
  92. Replication: *mountOptions.replication,
  93. TtlSec: int32(*mountOptions.ttlSec),
  94. ChunkSizeLimit: int64(*mountOptions.chunkSizeLimitMB) * 1024 * 1024,
  95. DataCenter: *mountOptions.dataCenter,
  96. DirListingLimit: *mountOptions.dirListingLimit,
  97. EntryCacheTtl: 3 * time.Second,
  98. MountUid: uid,
  99. MountGid: gid,
  100. MountMode: mountMode,
  101. }))
  102. if err != nil {
  103. fuse.Unmount(*mountOptions.dir)
  104. }
  105. // check if the mount process has an error to report
  106. <-c.Ready
  107. if err := c.MountError; err != nil {
  108. glog.Fatal(err)
  109. }
  110. return true
  111. }