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.

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