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.

197 lines
5.3 KiB

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