mount_std.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //go:build linux || darwin || freebsd
  2. // +build linux darwin freebsd
  3. package command
  4. import (
  5. "context"
  6. "fmt"
  7. "net/http"
  8. "os"
  9. "os/user"
  10. "path"
  11. "path/filepath"
  12. "runtime"
  13. "strconv"
  14. "strings"
  15. "syscall"
  16. "time"
  17. "github.com/chrislusf/seaweedfs/weed/storage/types"
  18. "github.com/chrislusf/seaweedfs/weed/filesys/meta_cache"
  19. "github.com/seaweedfs/fuse"
  20. "github.com/seaweedfs/fuse/fs"
  21. "github.com/chrislusf/seaweedfs/weed/filesys"
  22. "github.com/chrislusf/seaweedfs/weed/glog"
  23. "github.com/chrislusf/seaweedfs/weed/pb"
  24. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  25. "github.com/chrislusf/seaweedfs/weed/security"
  26. "github.com/chrislusf/seaweedfs/weed/util"
  27. "github.com/chrislusf/seaweedfs/weed/util/grace"
  28. )
  29. func runMount(cmd *Command, args []string) bool {
  30. if *mountOptions.debug {
  31. go http.ListenAndServe(fmt.Sprintf(":%d", *mountOptions.debugPort), nil)
  32. }
  33. grace.SetupProfiling(*mountCpuProfile, *mountMemProfile)
  34. if *mountReadRetryTime < time.Second {
  35. *mountReadRetryTime = time.Second
  36. }
  37. util.RetryWaitTime = *mountReadRetryTime
  38. umask, umaskErr := strconv.ParseUint(*mountOptions.umaskString, 8, 64)
  39. if umaskErr != nil {
  40. fmt.Printf("can not parse umask %s", *mountOptions.umaskString)
  41. return false
  42. }
  43. if len(args) > 0 {
  44. return false
  45. }
  46. return RunMount(&mountOptions, os.FileMode(umask))
  47. }
  48. func getParentInode(mountDir string) (uint64, error) {
  49. parentDir := filepath.Clean(filepath.Join(mountDir, ".."))
  50. fi, err := os.Stat(parentDir)
  51. if err != nil {
  52. return 0, err
  53. }
  54. stat, ok := fi.Sys().(*syscall.Stat_t)
  55. if !ok {
  56. return 0, nil
  57. }
  58. return stat.Ino, nil
  59. }
  60. func RunMount(option *MountOptions, umask os.FileMode) bool {
  61. filerAddresses := pb.ServerAddresses(*option.filer).ToAddresses()
  62. util.LoadConfiguration("security", false)
  63. // try to connect to filer, filerBucketsPath may be useful later
  64. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  65. var cipher bool
  66. var err error
  67. for i := 0; i < 10; i++ {
  68. err = pb.WithOneOfGrpcFilerClients(false, filerAddresses, 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", filerAddresses, 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", filerAddresses, 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", filerAddresses, 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.AllowDev(),
  151. fuse.AllowSUID(),
  152. fuse.DefaultPermissions(),
  153. fuse.MaxReadahead(1024 * 512),
  154. fuse.AsyncRead(),
  155. // fuse.WritebackCache(),
  156. // fuse.MaxBackground(1024),
  157. // fuse.CongestionThreshold(1024),
  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: filerAddresses,
  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. }