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.

163 lines
3.9 KiB

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