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.

173 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 freebsd
  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. options = append(options, osSpecificMountOptions()...)
  94. if allowOthers {
  95. options = append(options, fuse.AllowOther())
  96. }
  97. c, err := fuse.Mount(dir, options...)
  98. if err != nil {
  99. glog.Fatal(err)
  100. daemonize.SignalOutcome(err)
  101. return false
  102. }
  103. util.OnInterrupt(func() {
  104. fuse.Unmount(dir)
  105. c.Close()
  106. })
  107. filerGrpcAddress, err := parseFilerGrpcAddress(filer)
  108. if err != nil {
  109. glog.Fatal(err)
  110. daemonize.SignalOutcome(err)
  111. return false
  112. }
  113. mountRoot := filerMountRootPath
  114. if mountRoot != "/" && strings.HasSuffix(mountRoot, "/") {
  115. mountRoot = mountRoot[0 : len(mountRoot)-1]
  116. }
  117. daemonize.SignalOutcome(nil)
  118. err = fs.Serve(c, filesys.NewSeaweedFileSystem(&filesys.Option{
  119. FilerGrpcAddress: filerGrpcAddress,
  120. GrpcDialOption: security.LoadClientTLS(viper.Sub("grpc"), "client"),
  121. FilerMountRootPath: mountRoot,
  122. Collection: collection,
  123. Replication: replication,
  124. TtlSec: int32(ttlSec),
  125. ChunkSizeLimit: int64(chunkSizeLimitMB) * 1024 * 1024,
  126. DataCenter: dataCenter,
  127. DirListingLimit: dirListingLimit,
  128. EntryCacheTtl: 3 * time.Second,
  129. MountUid: uid,
  130. MountGid: gid,
  131. MountMode: mountMode,
  132. MountCtime: fileInfo.ModTime(),
  133. MountMtime: time.Now(),
  134. Umask: umask,
  135. }))
  136. if err != nil {
  137. fuse.Unmount(dir)
  138. }
  139. // check if the mount process has an error to report
  140. <-c.Ready
  141. if err := c.MountError; err != nil {
  142. glog.Fatal(err)
  143. daemonize.SignalOutcome(err)
  144. }
  145. return true
  146. }