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.

180 lines
4.4 KiB

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/jacobsa/daemonize"
  13. "github.com/spf13/viper"
  14. "github.com/chrislusf/seaweedfs/weed/filesys"
  15. "github.com/chrislusf/seaweedfs/weed/glog"
  16. "github.com/chrislusf/seaweedfs/weed/security"
  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.dirListCacheLimit,
  39. os.FileMode(umask),
  40. )
  41. }
  42. func RunMount(filer, filerMountRootPath, dir, collection, replication, dataCenter string, chunkSizeLimitMB int,
  43. allowOthers bool, ttlSec int, dirListCacheLimit int64, 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. // Ensure target mount point availability
  76. if isValid := checkMountPointAvailable(dir); !isValid {
  77. glog.Fatalf("Expected mount to still be active, target mount point: %s, please check!", dir)
  78. return false
  79. }
  80. mountName := path.Base(dir)
  81. options := []fuse.MountOption{
  82. fuse.VolumeName(mountName),
  83. fuse.FSName(filer + ":" + filerMountRootPath),
  84. fuse.Subtype("seaweedfs"),
  85. fuse.NoAppleDouble(),
  86. fuse.NoAppleXattr(),
  87. fuse.NoBrowse(),
  88. fuse.AutoXattr(),
  89. fuse.ExclCreate(),
  90. fuse.DaemonTimeout("3600"),
  91. fuse.AllowSUID(),
  92. fuse.DefaultPermissions(),
  93. fuse.MaxReadahead(1024 * 128),
  94. fuse.AsyncRead(),
  95. fuse.WritebackCache(),
  96. fuse.AllowNonEmptyMount(),
  97. }
  98. options = append(options, osSpecificMountOptions()...)
  99. if allowOthers {
  100. options = append(options, fuse.AllowOther())
  101. }
  102. c, err := fuse.Mount(dir, options...)
  103. if err != nil {
  104. glog.V(0).Infof("mount: %v", err)
  105. daemonize.SignalOutcome(err)
  106. return true
  107. }
  108. util.OnInterrupt(func() {
  109. fuse.Unmount(dir)
  110. c.Close()
  111. })
  112. filerGrpcAddress, err := parseFilerGrpcAddress(filer)
  113. if err != nil {
  114. glog.V(0).Infof("parseFilerGrpcAddress: %v", err)
  115. daemonize.SignalOutcome(err)
  116. return true
  117. }
  118. mountRoot := filerMountRootPath
  119. if mountRoot != "/" && strings.HasSuffix(mountRoot, "/") {
  120. mountRoot = mountRoot[0 : len(mountRoot)-1]
  121. }
  122. daemonize.SignalOutcome(nil)
  123. err = fs.Serve(c, filesys.NewSeaweedFileSystem(&filesys.Option{
  124. FilerGrpcAddress: filerGrpcAddress,
  125. GrpcDialOption: security.LoadClientTLS(viper.Sub("grpc"), "client"),
  126. FilerMountRootPath: mountRoot,
  127. Collection: collection,
  128. Replication: replication,
  129. TtlSec: int32(ttlSec),
  130. ChunkSizeLimit: int64(chunkSizeLimitMB) * 1024 * 1024,
  131. DataCenter: dataCenter,
  132. DirListCacheLimit: dirListCacheLimit,
  133. EntryCacheTtl: 3 * time.Second,
  134. MountUid: uid,
  135. MountGid: gid,
  136. MountMode: mountMode,
  137. MountCtime: fileInfo.ModTime(),
  138. MountMtime: time.Now(),
  139. Umask: umask,
  140. }))
  141. if err != nil {
  142. fuse.Unmount(dir)
  143. }
  144. // check if the mount process has an error to report
  145. <-c.Ready
  146. if err := c.MountError; err != nil {
  147. glog.V(0).Infof("mount process: %v", err)
  148. daemonize.SignalOutcome(err)
  149. return true
  150. }
  151. return true
  152. }