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.

261 lines
7.0 KiB

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