mount_std.go 6.0 KB

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