mount_std.go 5.5 KB

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