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.

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