mount_std.go 6.4 KB

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