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.

217 lines
6.0 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/filer"
  7. "github.com/chrislusf/seaweedfs/weed/filesys/meta_cache"
  8. "os"
  9. "os/user"
  10. "path"
  11. "runtime"
  12. "strconv"
  13. "strings"
  14. "time"
  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. filer.ReadWaitTime = *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. // 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. util.LoadConfiguration("security", false)
  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 | fileInfo.Mode()
  87. uid, gid = util.GetFileUidGid(fileInfo)
  88. fmt.Printf("mount point owner uid=%d gid=%d mode=%s\n", uid, gid, fileInfo.Mode())
  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. }
  132. options = append(options, osSpecificMountOptions()...)
  133. if *option.allowOthers {
  134. options = append(options, fuse.AllowOther())
  135. }
  136. if *option.nonempty {
  137. options = append(options, fuse.AllowNonEmptyMount())
  138. }
  139. // find mount point
  140. mountRoot := filerMountRootPath
  141. if mountRoot != "/" && strings.HasSuffix(mountRoot, "/") {
  142. mountRoot = mountRoot[0 : len(mountRoot)-1]
  143. }
  144. seaweedFileSystem := filesys.NewSeaweedFileSystem(&filesys.Option{
  145. FilerGrpcAddress: filerGrpcAddress,
  146. GrpcDialOption: grpcDialOption,
  147. FilerMountRootPath: mountRoot,
  148. Collection: *option.collection,
  149. Replication: *option.replication,
  150. TtlSec: int32(*option.ttlSec),
  151. ChunkSizeLimit: int64(chunkSizeLimitMB) * 1024 * 1024,
  152. CacheDir: *option.cacheDir,
  153. CacheSizeMB: *option.cacheSizeMB,
  154. DataCenter: *option.dataCenter,
  155. EntryCacheTtl: 3 * time.Second,
  156. MountUid: uid,
  157. MountGid: gid,
  158. MountMode: mountMode,
  159. MountCtime: fileInfo.ModTime(),
  160. MountMtime: time.Now(),
  161. Umask: umask,
  162. OutsideContainerClusterMode: *mountOptions.outsideContainerClusterMode,
  163. Cipher: cipher,
  164. UidGidMapper: uidGidMapper,
  165. })
  166. // mount
  167. c, err := fuse.Mount(dir, options...)
  168. if err != nil {
  169. glog.V(0).Infof("mount: %v", err)
  170. return true
  171. }
  172. defer fuse.Unmount(dir)
  173. grace.OnInterrupt(func() {
  174. fuse.Unmount(dir)
  175. c.Close()
  176. })
  177. glog.V(0).Infof("mounted %s%s to %s", filer, mountRoot, dir)
  178. err = fs.Serve(c, seaweedFileSystem)
  179. // check if the mount process has an error to report
  180. <-c.Ready
  181. if err := c.MountError; err != nil {
  182. glog.V(0).Infof("mount process: %v", err)
  183. return true
  184. }
  185. return true
  186. }