main.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "os"
  17. "syscall"
  18. "bazil.org/fuse"
  19. "bazil.org/fuse/fs"
  20. "github.com/sevlyar/go-daemon"
  21. "yunion.io/x/log"
  22. "yunion.io/x/log/hooks"
  23. "yunion.io/x/pkg/util/signalutils"
  24. )
  25. func main() {
  26. if !opt.Foreground {
  27. cntxt := &daemon.Context{
  28. WorkDir: "./",
  29. Umask: 027,
  30. }
  31. d, err := cntxt.Reborn()
  32. if err != nil {
  33. log.Fatalf("Unable to run: %s", err)
  34. }
  35. if d != nil {
  36. return
  37. }
  38. defer cntxt.Release()
  39. }
  40. if opt.Debug {
  41. logFileHook := hooks.LogFileRotateHook{
  42. LogFileHook: hooks.LogFileHook{
  43. FileDir: "/tmp",
  44. FileName: "fetcherfs.log",
  45. },
  46. RotateNum: 10,
  47. RotateSize: 4096,
  48. }
  49. logFileHook.Init()
  50. defer logFileHook.DeInit()
  51. log.Logger().AddHook(&logFileHook)
  52. }
  53. fetcherFs, err := initFetcherFs()
  54. if err != nil {
  55. log.Fatalln(err)
  56. }
  57. defer destoryInitFetcherFs()
  58. c, err := fuse.Mount(
  59. opt.MountPoint,
  60. fuse.FSName("fetcherfs"),
  61. fuse.Subtype("fetcher"),
  62. // https://github.com/bazil/fuse/issues/175
  63. // fuse.MaxReadahead(128*1024),
  64. fuse.MaxReadahead(uint32(opt.Blocksize*1024*1024)),
  65. )
  66. if err != nil {
  67. log.Fatalln(err)
  68. }
  69. defer c.Close()
  70. err = fs.Serve(c, *fetcherFs)
  71. if err != nil {
  72. log.Errorf("serve failed %s", err)
  73. }
  74. }
  75. func init() {
  76. signalutils.RegisterSignal(func() {
  77. destoryInitFetcherFs()
  78. os.Exit(1)
  79. }, syscall.SIGTERM, syscall.SIGINT)
  80. signalutils.StartTrap()
  81. }