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.

227 lines
6.2 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
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.ParseFilerGrpcAddress(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. err = pb.WithGrpcFilerClient(filerGrpcAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  54. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  55. if err != nil {
  56. return fmt.Errorf("get filer grpc address %s configuration: %v", filerGrpcAddress, err)
  57. }
  58. cipher = resp.Cipher
  59. return nil
  60. })
  61. if err != nil {
  62. glog.Infof("failed to talk to filer %s: %v", filerGrpcAddress, err)
  63. return true
  64. }
  65. filerMountRootPath := *option.filerMountRootPath
  66. dir := util.ResolvePath(*option.dir)
  67. chunkSizeLimitMB := *mountOptions.chunkSizeLimitMB
  68. fmt.Printf("This is SeaweedFS version %s %s %s\n", util.Version(), runtime.GOOS, runtime.GOARCH)
  69. if dir == "" {
  70. fmt.Printf("Please specify the mount directory via \"-dir\"")
  71. return false
  72. }
  73. if chunkSizeLimitMB <= 0 {
  74. fmt.Printf("Please specify a reasonable buffer size.")
  75. return false
  76. }
  77. fuse.Unmount(dir)
  78. // detect mount folder mode
  79. if *option.dirAutoCreate {
  80. os.MkdirAll(dir, os.FileMode(0777)&^umask)
  81. }
  82. fileInfo, err := os.Stat(dir)
  83. uid, gid := uint32(0), uint32(0)
  84. mountMode := os.ModeDir | 0777
  85. if err == nil {
  86. mountMode = os.ModeDir | os.FileMode(0777)&^umask
  87. uid, gid = util.GetFileUidGid(fileInfo)
  88. fmt.Printf("mount point owner uid=%d gid=%d mode=%s\n", uid, gid, mountMode)
  89. } else {
  90. fmt.Printf("can not stat %s\n", dir)
  91. return false
  92. }
  93. if uid == 0 {
  94. if u, err := user.Current(); err == nil {
  95. if parsedId, pe := strconv.ParseUint(u.Uid, 10, 32); pe == nil {
  96. uid = uint32(parsedId)
  97. }
  98. if parsedId, pe := strconv.ParseUint(u.Gid, 10, 32); pe == nil {
  99. gid = uint32(parsedId)
  100. }
  101. fmt.Printf("current uid=%d gid=%d\n", uid, gid)
  102. }
  103. }
  104. // mapping uid, gid
  105. uidGidMapper, err := meta_cache.NewUidGidMapper(*option.uidMap, *option.gidMap)
  106. if err != nil {
  107. fmt.Printf("failed to parse %s %s: %v\n", *option.uidMap, *option.gidMap, err)
  108. return false
  109. }
  110. // Ensure target mount point availability
  111. if isValid := checkMountPointAvailable(dir); !isValid {
  112. glog.Fatalf("Expected mount to still be active, target mount point: %s, please check!", dir)
  113. return true
  114. }
  115. mountName := path.Base(dir)
  116. options := []fuse.MountOption{
  117. fuse.VolumeName(mountName),
  118. fuse.FSName(filer + ":" + filerMountRootPath),
  119. fuse.Subtype("seaweedfs"),
  120. // fuse.NoAppleDouble(), // include .DS_Store, otherwise can not delete non-empty folders
  121. fuse.NoAppleXattr(),
  122. fuse.NoBrowse(),
  123. fuse.AutoXattr(),
  124. fuse.ExclCreate(),
  125. fuse.DaemonTimeout("3600"),
  126. fuse.AllowSUID(),
  127. fuse.DefaultPermissions(),
  128. fuse.MaxReadahead(1024 * 128),
  129. fuse.AsyncRead(),
  130. fuse.WritebackCache(),
  131. fuse.MaxBackground(128),
  132. fuse.CongestionThreshold(128),
  133. }
  134. options = append(options, osSpecificMountOptions()...)
  135. if *option.allowOthers {
  136. options = append(options, fuse.AllowOther())
  137. }
  138. if *option.nonempty {
  139. options = append(options, fuse.AllowNonEmptyMount())
  140. }
  141. // find mount point
  142. mountRoot := filerMountRootPath
  143. if mountRoot != "/" && strings.HasSuffix(mountRoot, "/") {
  144. mountRoot = mountRoot[0 : len(mountRoot)-1]
  145. }
  146. diskType := types.ToDiskType(*option.diskType)
  147. seaweedFileSystem := filesys.NewSeaweedFileSystem(&filesys.Option{
  148. MountDirectory: dir,
  149. FilerAddress: filer,
  150. FilerGrpcAddress: filerGrpcAddress,
  151. GrpcDialOption: grpcDialOption,
  152. FilerMountRootPath: mountRoot,
  153. Collection: *option.collection,
  154. Replication: *option.replication,
  155. TtlSec: int32(*option.ttlSec),
  156. DiskType: diskType,
  157. ChunkSizeLimit: int64(chunkSizeLimitMB) * 1024 * 1024,
  158. ConcurrentWriters: *option.concurrentWriters,
  159. CacheDir: *option.cacheDir,
  160. CacheSizeMB: *option.cacheSizeMB,
  161. DataCenter: *option.dataCenter,
  162. EntryCacheTtl: 3 * time.Second,
  163. MountUid: uid,
  164. MountGid: gid,
  165. MountMode: mountMode,
  166. MountCtime: fileInfo.ModTime(),
  167. MountMtime: time.Now(),
  168. Umask: umask,
  169. VolumeServerAccess: *mountOptions.volumeServerAccess,
  170. Cipher: cipher,
  171. UidGidMapper: uidGidMapper,
  172. })
  173. // mount
  174. c, err := fuse.Mount(dir, options...)
  175. if err != nil {
  176. glog.V(0).Infof("mount: %v", err)
  177. return true
  178. }
  179. defer fuse.Unmount(dir)
  180. grace.OnInterrupt(func() {
  181. fuse.Unmount(dir)
  182. c.Close()
  183. })
  184. glog.V(0).Infof("mounted %s%s to %s", filer, mountRoot, dir)
  185. server := fs.New(c, nil)
  186. seaweedFileSystem.Server = server
  187. err = server.Serve(seaweedFileSystem)
  188. // check if the mount process has an error to report
  189. <-c.Ready
  190. if err := c.MountError; err != nil {
  191. glog.V(0).Infof("mount process: %v", err)
  192. return true
  193. }
  194. return true
  195. }