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.

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