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.

197 lines
5.1 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. "context"
  5. "fmt"
  6. "os"
  7. "os/user"
  8. "path"
  9. "runtime"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/jacobsa/daemonize"
  14. "github.com/chrislusf/seaweedfs/weed/filesys"
  15. "github.com/chrislusf/seaweedfs/weed/glog"
  16. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  17. "github.com/chrislusf/seaweedfs/weed/security"
  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. umask, umaskErr := strconv.ParseUint(*mountOptions.umaskString, 8, 64)
  25. if umaskErr != nil {
  26. fmt.Printf("can not parse umask %s", *mountOptions.umaskString)
  27. return false
  28. }
  29. return RunMount(
  30. *mountOptions.filer,
  31. *mountOptions.filerMountRootPath,
  32. *mountOptions.dir,
  33. *mountOptions.collection,
  34. *mountOptions.replication,
  35. *mountOptions.dataCenter,
  36. *mountOptions.chunkSizeLimitMB,
  37. *mountOptions.allowOthers,
  38. *mountOptions.ttlSec,
  39. *mountOptions.dirListCacheLimit,
  40. os.FileMode(umask),
  41. )
  42. }
  43. func RunMount(filer, filerMountRootPath, dir, collection, replication, dataCenter string, chunkSizeLimitMB int,
  44. allowOthers bool, ttlSec int, dirListCacheLimit int64, umask os.FileMode) bool {
  45. util.LoadConfiguration("security", false)
  46. fmt.Printf("This is SeaweedFS version %s %s %s\n", util.VERSION, runtime.GOOS, runtime.GOARCH)
  47. if dir == "" {
  48. fmt.Printf("Please specify the mount directory via \"-dir\"")
  49. return false
  50. }
  51. if chunkSizeLimitMB <= 0 {
  52. fmt.Printf("Please specify a reasonable buffer size.")
  53. return false
  54. }
  55. fuse.Unmount(dir)
  56. uid, gid := uint32(0), uint32(0)
  57. // detect mount folder mode
  58. mountMode := os.ModeDir | 0755
  59. fileInfo, err := os.Stat(dir)
  60. if err == nil {
  61. mountMode = os.ModeDir | fileInfo.Mode()
  62. uid, gid = util.GetFileUidGid(fileInfo)
  63. fmt.Printf("mount point owner uid=%d gid=%d mode=%s\n", uid, gid, fileInfo.Mode())
  64. }
  65. if uid == 0 {
  66. if u, err := user.Current(); err == nil {
  67. if parsedId, pe := strconv.ParseUint(u.Uid, 10, 32); pe == nil {
  68. uid = uint32(parsedId)
  69. }
  70. if parsedId, pe := strconv.ParseUint(u.Gid, 10, 32); pe == nil {
  71. gid = uint32(parsedId)
  72. }
  73. fmt.Printf("current uid=%d gid=%d\n", uid, gid)
  74. }
  75. }
  76. // Ensure target mount point availability
  77. if isValid := checkMountPointAvailable(dir); !isValid {
  78. glog.Fatalf("Expected mount to still be active, target mount point: %s, please check!", dir)
  79. return false
  80. }
  81. mountName := path.Base(dir)
  82. options := []fuse.MountOption{
  83. fuse.VolumeName(mountName),
  84. fuse.FSName(filer + ":" + filerMountRootPath),
  85. fuse.Subtype("seaweedfs"),
  86. // fuse.NoAppleDouble(), // include .DS_Store, otherwise can not delete non-empty folders
  87. fuse.NoAppleXattr(),
  88. fuse.NoBrowse(),
  89. fuse.AutoXattr(),
  90. fuse.ExclCreate(),
  91. fuse.DaemonTimeout("3600"),
  92. fuse.AllowSUID(),
  93. fuse.DefaultPermissions(),
  94. fuse.MaxReadahead(1024 * 128),
  95. fuse.AsyncRead(),
  96. fuse.WritebackCache(),
  97. fuse.AllowNonEmptyMount(),
  98. }
  99. options = append(options, osSpecificMountOptions()...)
  100. if allowOthers {
  101. options = append(options, fuse.AllowOther())
  102. }
  103. c, err := fuse.Mount(dir, options...)
  104. if err != nil {
  105. glog.V(0).Infof("mount: %v", err)
  106. daemonize.SignalOutcome(err)
  107. return true
  108. }
  109. util.OnInterrupt(func() {
  110. fuse.Unmount(dir)
  111. c.Close()
  112. })
  113. // parse filer grpc address
  114. filerGrpcAddress, err := parseFilerGrpcAddress(filer)
  115. if err != nil {
  116. glog.V(0).Infof("parseFilerGrpcAddress: %v", err)
  117. daemonize.SignalOutcome(err)
  118. return true
  119. }
  120. // try to connect to filer, filerBucketsPath may be useful later
  121. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  122. err = withFilerClient(filerGrpcAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  123. _, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  124. if err != nil {
  125. return fmt.Errorf("get filer %s configuration: %v", filerGrpcAddress, err)
  126. }
  127. return nil
  128. })
  129. if err != nil {
  130. glog.Fatal(err)
  131. return false
  132. }
  133. // find mount point
  134. mountRoot := filerMountRootPath
  135. if mountRoot != "/" && strings.HasSuffix(mountRoot, "/") {
  136. mountRoot = mountRoot[0 : len(mountRoot)-1]
  137. }
  138. daemonize.SignalOutcome(nil)
  139. err = fs.Serve(c, filesys.NewSeaweedFileSystem(&filesys.Option{
  140. FilerGrpcAddress: filerGrpcAddress,
  141. GrpcDialOption: grpcDialOption,
  142. FilerMountRootPath: mountRoot,
  143. Collection: collection,
  144. Replication: replication,
  145. TtlSec: int32(ttlSec),
  146. ChunkSizeLimit: int64(chunkSizeLimitMB) * 1024 * 1024,
  147. DataCenter: dataCenter,
  148. DirListCacheLimit: dirListCacheLimit,
  149. EntryCacheTtl: 3 * time.Second,
  150. MountUid: uid,
  151. MountGid: gid,
  152. MountMode: mountMode,
  153. MountCtime: fileInfo.ModTime(),
  154. MountMtime: time.Now(),
  155. Umask: umask,
  156. }))
  157. if err != nil {
  158. fuse.Unmount(dir)
  159. }
  160. // check if the mount process has an error to report
  161. <-c.Ready
  162. if err := c.MountError; err != nil {
  163. glog.V(0).Infof("mount process: %v", err)
  164. daemonize.SignalOutcome(err)
  165. return true
  166. }
  167. return true
  168. }