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.

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