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.

199 lines
5.3 KiB

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