mount_std.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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(false, 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.AllowDev(),
  147. fuse.AllowSUID(),
  148. fuse.DefaultPermissions(),
  149. fuse.MaxReadahead(1024 * 512),
  150. fuse.AsyncRead(),
  151. // fuse.WritebackCache(),
  152. // fuse.MaxBackground(1024),
  153. // fuse.CongestionThreshold(1024),
  154. }
  155. options = append(options, osSpecificMountOptions()...)
  156. if *option.allowOthers {
  157. options = append(options, fuse.AllowOther())
  158. }
  159. if *option.nonempty {
  160. options = append(options, fuse.AllowNonEmptyMount())
  161. }
  162. if *option.readOnly {
  163. options = append(options, fuse.ReadOnly())
  164. }
  165. // find mount point
  166. mountRoot := filerMountRootPath
  167. if mountRoot != "/" && strings.HasSuffix(mountRoot, "/") {
  168. mountRoot = mountRoot[0 : len(mountRoot)-1]
  169. }
  170. diskType := types.ToDiskType(*option.diskType)
  171. seaweedFileSystem := filesys.NewSeaweedFileSystem(&filesys.Option{
  172. MountDirectory: dir,
  173. FilerAddresses: filerAddresses,
  174. GrpcDialOption: grpcDialOption,
  175. FilerMountRootPath: mountRoot,
  176. Collection: *option.collection,
  177. Replication: *option.replication,
  178. TtlSec: int32(*option.ttlSec),
  179. DiskType: diskType,
  180. ChunkSizeLimit: int64(chunkSizeLimitMB) * 1024 * 1024,
  181. ConcurrentWriters: *option.concurrentWriters,
  182. CacheDir: *option.cacheDir,
  183. CacheSizeMB: *option.cacheSizeMB,
  184. DataCenter: *option.dataCenter,
  185. MountUid: uid,
  186. MountGid: gid,
  187. MountMode: mountMode,
  188. MountCtime: fileInfo.ModTime(),
  189. MountMtime: time.Now(),
  190. MountParentInode: parentInode,
  191. Umask: umask,
  192. VolumeServerAccess: *mountOptions.volumeServerAccess,
  193. Cipher: cipher,
  194. UidGidMapper: uidGidMapper,
  195. })
  196. // mount
  197. c, err := fuse.Mount(dir, options...)
  198. if err != nil {
  199. glog.V(0).Infof("mount: %v", err)
  200. return true
  201. }
  202. defer fuse.Unmount(dir)
  203. grace.OnInterrupt(func() {
  204. fuse.Unmount(dir)
  205. c.Close()
  206. })
  207. glog.V(0).Infof("mounted %s%s to %v", *option.filer, mountRoot, dir)
  208. server := fs.New(c, nil)
  209. seaweedFileSystem.Server = server
  210. seaweedFileSystem.StartBackgroundTasks()
  211. err = server.Serve(seaweedFileSystem)
  212. // check if the mount process has an error to report
  213. <-c.Ready
  214. if err := c.MountError; err != nil {
  215. glog.V(0).Infof("mount process: %v", err)
  216. return true
  217. }
  218. return true
  219. }