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.

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