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.

260 lines
7.0 KiB

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