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.

234 lines
6.4 KiB

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