mount_std.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // +build linux darwin freebsd
  2. package command
  3. import (
  4. "context"
  5. "fmt"
  6. "os"
  7. "os/user"
  8. "path"
  9. "runtime"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/jacobsa/daemonize"
  14. "github.com/chrislusf/seaweedfs/weed/filesys"
  15. "github.com/chrislusf/seaweedfs/weed/glog"
  16. "github.com/chrislusf/seaweedfs/weed/pb"
  17. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  18. "github.com/chrislusf/seaweedfs/weed/security"
  19. "github.com/chrislusf/seaweedfs/weed/util"
  20. "github.com/seaweedfs/fuse"
  21. "github.com/seaweedfs/fuse/fs"
  22. )
  23. func runMount(cmd *Command, args []string) bool {
  24. util.SetupProfiling(*mountCpuProfile, *mountMemProfile)
  25. umask, umaskErr := strconv.ParseUint(*mountOptions.umaskString, 8, 64)
  26. if umaskErr != nil {
  27. fmt.Printf("can not parse umask %s", *mountOptions.umaskString)
  28. return false
  29. }
  30. return RunMount(
  31. *mountOptions.filer,
  32. *mountOptions.filerMountRootPath,
  33. *mountOptions.dir,
  34. *mountOptions.collection,
  35. *mountOptions.replication,
  36. *mountOptions.dataCenter,
  37. *mountOptions.chunkSizeLimitMB,
  38. *mountOptions.allowOthers,
  39. *mountOptions.ttlSec,
  40. *mountOptions.dirListCacheLimit,
  41. os.FileMode(umask),
  42. *mountOptions.outsideContainerClusterMode,
  43. )
  44. }
  45. func RunMount(filer, filerMountRootPath, dir, collection, replication, dataCenter string, chunkSizeLimitMB int,
  46. allowOthers bool, ttlSec int, dirListCacheLimit int64, umask os.FileMode, outsideContainerClusterMode bool) bool {
  47. util.LoadConfiguration("security", false)
  48. fmt.Printf("This is SeaweedFS version %s %s %s\n", util.VERSION, runtime.GOOS, runtime.GOARCH)
  49. if dir == "" {
  50. fmt.Printf("Please specify the mount directory via \"-dir\"")
  51. return false
  52. }
  53. if chunkSizeLimitMB <= 0 {
  54. fmt.Printf("Please specify a reasonable buffer size.")
  55. return false
  56. }
  57. fuse.Unmount(dir)
  58. uid, gid := uint32(0), uint32(0)
  59. // detect mount folder mode
  60. mountMode := os.ModeDir | 0755
  61. fileInfo, err := os.Stat(dir)
  62. if err == nil {
  63. mountMode = os.ModeDir | fileInfo.Mode()
  64. uid, gid = util.GetFileUidGid(fileInfo)
  65. fmt.Printf("mount point owner uid=%d gid=%d mode=%s\n", uid, gid, fileInfo.Mode())
  66. }
  67. if uid == 0 {
  68. if u, err := user.Current(); err == nil {
  69. if parsedId, pe := strconv.ParseUint(u.Uid, 10, 32); pe == nil {
  70. uid = uint32(parsedId)
  71. }
  72. if parsedId, pe := strconv.ParseUint(u.Gid, 10, 32); pe == nil {
  73. gid = uint32(parsedId)
  74. }
  75. fmt.Printf("current uid=%d gid=%d\n", uid, gid)
  76. }
  77. }
  78. // Ensure target mount point availability
  79. if isValid := checkMountPointAvailable(dir); !isValid {
  80. glog.Fatalf("Expected mount to still be active, target mount point: %s, please check!", dir)
  81. return false
  82. }
  83. mountName := path.Base(dir)
  84. options := []fuse.MountOption{
  85. fuse.VolumeName(mountName),
  86. fuse.FSName(filer + ":" + filerMountRootPath),
  87. fuse.Subtype("seaweedfs"),
  88. // fuse.NoAppleDouble(), // include .DS_Store, otherwise can not delete non-empty folders
  89. fuse.NoAppleXattr(),
  90. fuse.NoBrowse(),
  91. fuse.AutoXattr(),
  92. fuse.ExclCreate(),
  93. fuse.DaemonTimeout("3600"),
  94. fuse.AllowSUID(),
  95. fuse.DefaultPermissions(),
  96. fuse.MaxReadahead(1024 * 128),
  97. fuse.AsyncRead(),
  98. fuse.WritebackCache(),
  99. fuse.AllowNonEmptyMount(),
  100. }
  101. options = append(options, osSpecificMountOptions()...)
  102. if allowOthers {
  103. options = append(options, fuse.AllowOther())
  104. }
  105. c, err := fuse.Mount(dir, options...)
  106. if err != nil {
  107. glog.V(0).Infof("mount: %v", err)
  108. daemonize.SignalOutcome(err)
  109. return true
  110. }
  111. util.OnInterrupt(func() {
  112. fuse.Unmount(dir)
  113. c.Close()
  114. })
  115. // parse filer grpc address
  116. filerGrpcAddress, err := pb.ParseFilerGrpcAddress(filer)
  117. if err != nil {
  118. glog.V(0).Infof("ParseFilerGrpcAddress: %v", err)
  119. daemonize.SignalOutcome(err)
  120. return true
  121. }
  122. // try to connect to filer, filerBucketsPath may be useful later
  123. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  124. var cipher bool
  125. err = pb.WithGrpcFilerClient(filerGrpcAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  126. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  127. if err != nil {
  128. return fmt.Errorf("get filer %s configuration: %v", filerGrpcAddress, err)
  129. }
  130. cipher = resp.Cipher
  131. return nil
  132. })
  133. if err != nil {
  134. glog.Fatal(err)
  135. return false
  136. }
  137. // find mount point
  138. mountRoot := filerMountRootPath
  139. if mountRoot != "/" && strings.HasSuffix(mountRoot, "/") {
  140. mountRoot = mountRoot[0 : len(mountRoot)-1]
  141. }
  142. daemonize.SignalOutcome(nil)
  143. err = fs.Serve(c, filesys.NewSeaweedFileSystem(&filesys.Option{
  144. FilerGrpcAddress: filerGrpcAddress,
  145. GrpcDialOption: grpcDialOption,
  146. FilerMountRootPath: mountRoot,
  147. Collection: collection,
  148. Replication: replication,
  149. TtlSec: int32(ttlSec),
  150. ChunkSizeLimit: int64(chunkSizeLimitMB) * 1024 * 1024,
  151. DataCenter: dataCenter,
  152. DirListCacheLimit: dirListCacheLimit,
  153. EntryCacheTtl: 3 * time.Second,
  154. MountUid: uid,
  155. MountGid: gid,
  156. MountMode: mountMode,
  157. MountCtime: fileInfo.ModTime(),
  158. MountMtime: time.Now(),
  159. Umask: umask,
  160. OutsideContainerClusterMode: outsideContainerClusterMode,
  161. Cipher: cipher,
  162. }))
  163. if err != nil {
  164. fuse.Unmount(dir)
  165. }
  166. // check if the mount process has an error to report
  167. <-c.Ready
  168. if err := c.MountError; err != nil {
  169. glog.V(0).Infof("mount process: %v", err)
  170. daemonize.SignalOutcome(err)
  171. return true
  172. }
  173. return true
  174. }