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.

198 lines
5.5 KiB

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