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.

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