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.

191 lines
5.4 KiB

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