fuseutils.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 fuseutils
  15. import (
  16. "os"
  17. "path"
  18. "strconv"
  19. "strings"
  20. "time"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/errors"
  23. "yunion.io/x/onecloud/pkg/apis"
  24. "yunion.io/x/onecloud/pkg/util/fileutils2"
  25. "yunion.io/x/onecloud/pkg/util/procutils"
  26. )
  27. func MountFusefs(
  28. fetcherfsPath, url, tmpdir, token, mntpath string,
  29. blocksize int, encryptInfo *apis.SEncryptInfo,
  30. ) error {
  31. var metaPath = path.Join(mntpath, "meta")
  32. if f, err := os.OpenFile(metaPath, os.O_RDONLY, 0644); err == nil {
  33. f.Close()
  34. log.Infof("Already mounted: %s, skip...", mntpath)
  35. return nil
  36. }
  37. // is mounted
  38. if err := procutils.NewCommand("mountpoint", mntpath).Run(); err == nil {
  39. out, err := procutils.NewCommand("umount", mntpath).Output()
  40. if err != nil {
  41. return errors.Wrapf(err, "umount %s failed: %s", mntpath, out)
  42. }
  43. }
  44. if !fileutils2.Exists(tmpdir) {
  45. if out, err := procutils.NewCommand("mkdir", "-p", tmpdir).Output(); err != nil {
  46. return errors.Wrapf(err, "mkdir %s failed: %s", tmpdir, out)
  47. }
  48. }
  49. if !fileutils2.Exists(mntpath) {
  50. if out, err := procutils.NewCommand("mkdir", "-p", mntpath).Output(); err != nil {
  51. return errors.Wrapf(err, "mkdir %s failed: %s", mntpath, out)
  52. }
  53. }
  54. var cmd = []string{
  55. fetcherfsPath,
  56. "--url", url,
  57. "--token", token,
  58. "--tmpdir", tmpdir,
  59. "--blocksize", strconv.Itoa(blocksize),
  60. "--mount-point", mntpath,
  61. }
  62. if encryptInfo != nil && len(encryptInfo.Key) > 0 {
  63. // TODO: base64 encrypt key
  64. cmd = append(cmd, "--encrypt-key", encryptInfo.Key)
  65. }
  66. if encryptInfo != nil && len(encryptInfo.Alg) > 0 {
  67. cmd = append(cmd, "--encrypt-alg", string(encryptInfo.Alg))
  68. }
  69. log.Infof("%s", strings.Join(cmd, " "))
  70. out, err := procutils.NewRemoteCommandAsFarAsPossible(cmd[0], cmd[1:]...).Output()
  71. if err != nil {
  72. log.Errorf("%v Mount fetcherfs filed: %s %s", cmd, err, out)
  73. out2, err2 := procutils.NewCommand("umount", mntpath).Output()
  74. if err2 != nil {
  75. log.Errorf("umount fetcherfs failed %s %s", err2, out2)
  76. }
  77. return errors.Wrapf(err, "mount fetcherfs failed: %s", out)
  78. }
  79. var mounted = false
  80. for i := 0; i < 3; i++ {
  81. time.Sleep(1 * time.Second)
  82. if f, err := os.OpenFile(metaPath, os.O_RDONLY, 0644); err == nil {
  83. f.Close()
  84. mounted = true
  85. break
  86. } else {
  87. log.Warningf("failed open metaPath %s: %s", metaPath, err)
  88. }
  89. }
  90. if !mounted {
  91. out2, err2 := procutils.NewCommand("umount", mntpath).Output()
  92. if err2 != nil {
  93. log.Errorf("umount fetcherfs failed %s %s", err2, out2)
  94. }
  95. return errors.Error("failed open metaPath")
  96. } else {
  97. return nil
  98. }
  99. }