mount_std.go 4.4 KB

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