storage_nfs.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 storageman
  15. import (
  16. "context"
  17. "fmt"
  18. "path"
  19. "strings"
  20. "time"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/log"
  23. "yunion.io/x/pkg/errors"
  24. api "yunion.io/x/onecloud/pkg/apis/compute"
  25. "yunion.io/x/onecloud/pkg/hostman/hostutils"
  26. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  27. "yunion.io/x/onecloud/pkg/util/fileutils2"
  28. "yunion.io/x/onecloud/pkg/util/procutils"
  29. )
  30. func init() {
  31. registerStorageFactory(&SNFSStorageFactory{})
  32. }
  33. type SNFSStorageFactory struct {
  34. }
  35. func (factory *SNFSStorageFactory) NewStorage(manager *SStorageManager, mountPoint string) IStorage {
  36. return NewNFSStorage(manager, mountPoint)
  37. }
  38. func (factory *SNFSStorageFactory) StorageType() string {
  39. return api.STORAGE_NFS
  40. }
  41. type SNFSStorage struct {
  42. SNasStorage
  43. }
  44. func NewNFSStorage(manager *SStorageManager, path string) *SNFSStorage {
  45. ret := &SNFSStorage{}
  46. ret.SNasStorage = *NewNasStorage(manager, path, ret)
  47. if !fileutils2.Exists(path) {
  48. procutils.NewCommand("mkdir", "-p", path).Run()
  49. }
  50. return ret
  51. }
  52. func (s *SNFSStorage) newDisk(diskId string) IDisk {
  53. return NewNFSDisk(s, diskId)
  54. }
  55. func (s *SNFSStorage) StorageType() string {
  56. return api.STORAGE_NFS
  57. }
  58. func (s *SNFSStorage) IsLocal() bool {
  59. return false
  60. }
  61. func (s *SNFSStorage) SyncStorageInfo() (jsonutils.JSONObject, error) {
  62. if len(s.StorageId) == 0 {
  63. return nil, fmt.Errorf("Sync nfs storage without storage id")
  64. }
  65. content := jsonutils.NewDict()
  66. content.Set("capacity", jsonutils.NewInt(int64(s.GetAvailSizeMb())))
  67. content.Set("storage_type", jsonutils.NewString(s.StorageType()))
  68. content.Set("status", jsonutils.NewString(api.STORAGE_ONLINE))
  69. content.Set("zone", jsonutils.NewString(s.GetZoneId()))
  70. log.Infof("Sync storage info %s", s.StorageId)
  71. res, err := modules.Storages.Put(
  72. hostutils.GetComputeSession(context.Background()),
  73. s.StorageId, content)
  74. if err != nil {
  75. log.Errorf("SyncStorageInfo Failed: %s: %s", content, err)
  76. }
  77. return res, err
  78. }
  79. func (s *SNFSStorage) SetStorageInfo(storageId, storageName string, conf jsonutils.JSONObject) error {
  80. s.StorageId = storageId
  81. s.StorageName = storageName
  82. if dconf, ok := conf.(*jsonutils.JSONDict); ok {
  83. s.StorageConf = dconf
  84. }
  85. if err := s.checkAndMount(); err != nil {
  86. return errors.Errorf("Fail to mount storage to mountpoint: %s, %s", s.Path, err)
  87. }
  88. return s.BindMountStoragePath(s.Path)
  89. }
  90. func (s *SNFSStorage) checkAndMount() error {
  91. if err := procutils.NewRemoteCommandAsFarAsPossible("mountpoint", s.Path).Run(); err == nil {
  92. return nil
  93. }
  94. if s.StorageConf == nil {
  95. return fmt.Errorf("Storage conf is nil")
  96. }
  97. host, err := s.StorageConf.GetString("nfs_host")
  98. if err != nil {
  99. return fmt.Errorf("Storage conf missing nfs_host ")
  100. }
  101. sharedDir, err := s.StorageConf.GetString("nfs_shared_dir")
  102. if err != nil {
  103. return fmt.Errorf("Storage conf missing nfs_shared_dir")
  104. }
  105. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  106. defer cancel()
  107. err = procutils.NewRemoteCommandContextAsFarAsPossible(ctx,
  108. "mount", "-t", "nfs", fmt.Sprintf("%s:%s", host, sharedDir), s.Path).Run()
  109. if err != nil {
  110. return err
  111. }
  112. return nil
  113. }
  114. func (s *SNFSStorage) Detach() error {
  115. if !strings.HasPrefix(s.Path, "/opt/cloud") {
  116. tmpPath := path.Join(TempBindMountPath, s.Path)
  117. out, err := procutils.NewCommand("umount", s.Path).Output()
  118. if err != nil {
  119. return errors.Wrapf(err, "1. umount %s failed %s", s.Path, out)
  120. }
  121. out, err = procutils.NewRemoteCommandAsFarAsPossible("umount", tmpPath).Output()
  122. if err != nil {
  123. return errors.Wrapf(err, "2. umount %s failed %s", tmpPath, out)
  124. }
  125. }
  126. out, err := procutils.NewRemoteCommandAsFarAsPossible("umount", s.Path).Output()
  127. if err != nil {
  128. return errors.Wrapf(err, "3. umount %s failed %s", s.Path, out)
  129. }
  130. return nil
  131. }