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.

185 lines
5.1 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
7 years ago
  1. // +build linux darwin freebsd
  2. package command
  3. import (
  4. "context"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/filesys/meta_cache"
  7. "os"
  8. "path"
  9. "runtime"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/seaweedfs/fuse"
  14. "github.com/seaweedfs/fuse/fs"
  15. "github.com/chrislusf/seaweedfs/weed/filesys"
  16. "github.com/chrislusf/seaweedfs/weed/glog"
  17. "github.com/chrislusf/seaweedfs/weed/pb"
  18. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  19. "github.com/chrislusf/seaweedfs/weed/security"
  20. "github.com/chrislusf/seaweedfs/weed/util"
  21. "github.com/chrislusf/seaweedfs/weed/util/grace"
  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 := util.ResolvePath(*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. // detect mount folder mode
  73. if *option.dirAutoCreate {
  74. os.MkdirAll(dir, os.FileMode(0777)&^umask)
  75. }
  76. fileInfo, err := os.Stat(dir)
  77. // mapping uid, gid
  78. uidGidMapper, err := meta_cache.NewUidGidMapper(*option.uidMap, *option.gidMap)
  79. if err != nil {
  80. fmt.Printf("failed to parse %s %s: %v\n", *option.uidMap, *option.gidMap, err)
  81. return false
  82. }
  83. // Ensure target mount point availability
  84. if isValid := checkMountPointAvailable(dir); !isValid {
  85. glog.Fatalf("Expected mount to still be active, target mount point: %s, please check!", dir)
  86. return true
  87. }
  88. mountName := path.Base(dir)
  89. options := []fuse.MountOption{
  90. fuse.VolumeName(mountName),
  91. fuse.FSName(filer + ":" + filerMountRootPath),
  92. fuse.Subtype("seaweedfs"),
  93. // fuse.NoAppleDouble(), // include .DS_Store, otherwise can not delete non-empty folders
  94. fuse.NoAppleXattr(),
  95. fuse.NoBrowse(),
  96. fuse.AutoXattr(),
  97. fuse.ExclCreate(),
  98. fuse.DaemonTimeout("3600"),
  99. fuse.AllowSUID(),
  100. fuse.DefaultPermissions(),
  101. fuse.MaxReadahead(1024 * 128),
  102. fuse.AsyncRead(),
  103. fuse.WritebackCache(),
  104. }
  105. options = append(options, osSpecificMountOptions()...)
  106. if *option.allowOthers {
  107. options = append(options, fuse.AllowOther())
  108. }
  109. if *option.nonempty {
  110. options = append(options, fuse.AllowNonEmptyMount())
  111. }
  112. // find mount point
  113. mountRoot := filerMountRootPath
  114. if mountRoot != "/" && strings.HasSuffix(mountRoot, "/") {
  115. mountRoot = mountRoot[0 : len(mountRoot)-1]
  116. }
  117. seaweedFileSystem := filesys.NewSeaweedFileSystem(&filesys.Option{
  118. FilerGrpcAddress: filerGrpcAddress,
  119. GrpcDialOption: grpcDialOption,
  120. FilerMountRootPath: mountRoot,
  121. Collection: *option.collection,
  122. Replication: *option.replication,
  123. TtlSec: int32(*option.ttlSec),
  124. ChunkSizeLimit: int64(chunkSizeLimitMB) * 1024 * 1024,
  125. CacheDir: *option.cacheDir,
  126. CacheSizeMB: *option.cacheSizeMB,
  127. DataCenter: *option.dataCenter,
  128. EntryCacheTtl: 3 * time.Second,
  129. MountCtime: fileInfo.ModTime(),
  130. MountMtime: time.Now(),
  131. Umask: umask,
  132. OutsideContainerClusterMode: *mountOptions.outsideContainerClusterMode,
  133. Cipher: cipher,
  134. UidGidMapper: uidGidMapper,
  135. })
  136. // mount
  137. c, err := fuse.Mount(dir, options...)
  138. if err != nil {
  139. glog.V(0).Infof("mount: %v", err)
  140. return true
  141. }
  142. defer fuse.Unmount(dir)
  143. grace.OnInterrupt(func() {
  144. fuse.Unmount(dir)
  145. c.Close()
  146. })
  147. glog.V(0).Infof("mounted %s%s to %s", filer, mountRoot, dir)
  148. err = fs.Serve(c, seaweedFileSystem)
  149. // check if the mount process has an error to report
  150. <-c.Ready
  151. if err := c.MountError; err != nil {
  152. glog.V(0).Infof("mount process: %v", err)
  153. return true
  154. }
  155. return true
  156. }