Merge pull request #7008 from wanyaoqi/automated-cherry-pick-of-#7006-upstream-release-3.2

Automated cherry pick of #7006: bind mount nfs storage path
This commit is contained in:
Zexi Li
2020-07-02 17:35:30 +08:00
committed by GitHub
5 changed files with 78 additions and 49 deletions
-31
View File
@@ -16,9 +16,7 @@ package hostman
import (
"io/ioutil"
"path"
"path/filepath"
"strings"
execlient "yunion.io/x/executor/client"
"yunion.io/x/log"
@@ -47,8 +45,6 @@ import (
"yunion.io/x/onecloud/pkg/util/sysutils"
)
const TempBindMountPath = "/opt/cloud/workspace/temp-bind"
type SHostService struct {
*service.SServiceBase
}
@@ -72,38 +68,11 @@ func (host *SHostService) InitService() {
if options.HostOptions.EnableRemoteExecutor {
execlient.Init(options.HostOptions.ExecutorSocketPath)
procutils.SetRemoteExecutor()
host.mountLocalImagePath()
}
system_service.Init()
}
func (host *SHostService) mountLocalImagePath() {
for i := 0; i < len(options.HostOptions.LocalImagePath); i++ {
if !strings.HasPrefix(options.HostOptions.LocalImagePath[i], "/opt/cloud") {
tempPath := path.Join(TempBindMountPath, options.HostOptions.LocalImagePath[i])
out, err := procutils.NewCommand("mkdir", "-p", tempPath).Output()
if err != nil {
log.Fatalf("mkdir temp mount path %s failed %s", tempPath, out)
}
out, err = procutils.NewCommand("mkdir", "-p", options.HostOptions.LocalImagePath[i]).Output()
if err != nil {
log.Fatalf("mkdir mount path %s failed %s", options.HostOptions.LocalImagePath[i], out)
}
if procutils.NewCommand("mountpoint", tempPath).Run() != nil {
out, err = procutils.NewRemoteCommandAsFarAsPossible("mount", "--bind", options.HostOptions.LocalImagePath[i], tempPath).Output()
if err != nil {
log.Fatalf("bind mount to temp path failed %s", out)
}
}
out, err = procutils.NewCommand("mount", "--bind", tempPath, options.HostOptions.LocalImagePath[i]).Output()
if err != nil {
log.Fatalf("bind mount temp path to local image path failed %s", out)
}
}
}
}
func (host *SHostService) OnExitService() {}
func (host *SHostService) RunService() {
+9 -3
View File
@@ -1259,7 +1259,9 @@ func (h *SHostInfo) onGetStorageInfoSucc(hoststorages []jsonutils.JSONObject) {
storage := storageManager.NewSharedStorageInstance(mountPoint, storagetype)
if storage != nil {
storage.SetStoragecacheId(storagecacheId)
storage.SetStorageInfo(storageId, storageName, storageConf)
if err := storage.SetStorageInfo(storageId, storageName, storageConf); err != nil {
h.onFail(err)
}
storageManager.Storages = append(storageManager.Storages, storage)
if err := storage.Accessible(); err != nil {
h.onFail(err)
@@ -1272,7 +1274,9 @@ func (h *SHostInfo) onGetStorageInfoSucc(hoststorages []jsonutils.JSONObject) {
storage := storageManager.GetStorageByPath(mountPoint)
if storage != nil {
storage.SetStoragecacheId(storagecacheId)
storage.SetStorageInfo(storageId, storageName, storageConf)
if err := storage.SetStorageInfo(storageId, storageName, storageConf); err != nil {
h.onFail(err)
}
} else {
// XXX hack: storage type baremetal is a converted host,reserve storage
if storagetype != api.STORAGE_BAREMETAL {
@@ -1306,7 +1310,9 @@ func (h *SHostInfo) onSyncStorageInfoSucc(storage storageman.IStorage, storageIn
id, _ := storageInfo.GetString("id")
name, _ := storageInfo.GetString("name")
storageConf, _ := storageInfo.Get("storage_conf")
storage.SetStorageInfo(id, name, storageConf)
if err := storage.SetStorageInfo(id, name, storageConf); err != nil {
h.onFail(err)
}
h.attachStorage(storage)
}
}
+44 -8
View File
@@ -28,6 +28,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/cloudcommon/cronman"
"yunion.io/x/onecloud/pkg/hostman/hostutils"
@@ -45,6 +46,7 @@ const (
_SNAPSHOT_PATH_ = "snapshots"
ErrStorageTimeout = constError("storage accessible check timeout")
TempBindMountPath = "/opt/cloud/workspace/temp-bind"
)
type constError string
@@ -82,7 +84,7 @@ type IStorage interface {
GetStorageName() string
GetZoneName() string
SetStorageInfo(storageId, storageName string, conf jsonutils.JSONObject)
SetStorageInfo(storageId, storageName string, conf jsonutils.JSONObject) error
SyncStorageInfo() (jsonutils.JSONObject, error)
StorageType() string
GetStorageConf() *jsonutils.JSONDict
@@ -126,12 +128,13 @@ type IStorage interface {
}
type SBaseStorage struct {
Manager *SStorageManager
StorageId string
Path string
StorageName string
StorageConf *jsonutils.JSONDict
StoragecacheId string
Manager *SStorageManager
StorageId string
Path string
StorageName string
StorageConf *jsonutils.JSONDict
StoragecacheId string
isSetStorageInfo bool
Disks []IDisk
DiskLock *sync.Mutex
@@ -218,12 +221,45 @@ func (s *SBaseStorage) GetTotalSizeMb() int {
return int(stat.Blocks * uint64(stat.Bsize) / 1024 / 1024)
}
func (s *SBaseStorage) SetStorageInfo(storageId, storageName string, conf jsonutils.JSONObject) {
func (s *SBaseStorage) SetStorageInfo(storageId, storageName string, conf jsonutils.JSONObject) error {
s.StorageId = storageId
s.StorageName = storageName
if dconf, ok := conf.(*jsonutils.JSONDict); ok {
s.StorageConf = dconf
}
if !s.isSetStorageInfo && options.HostOptions.EnableRemoteExecutor {
err := s.bindMountTo(s.Path)
if err == nil {
s.isSetStorageInfo = true
}
return err
}
return nil
}
func (s *SBaseStorage) bindMountTo(sPath string) error {
tempPath := path.Join(TempBindMountPath, sPath)
out, err := procutils.NewCommand("mkdir", "-p", tempPath).Output()
if err != nil {
return errors.Errorf("mkdir temp mount path %s failed %s", tempPath, out)
}
out, err = procutils.NewCommand("mkdir", "-p", sPath).Output()
if err != nil {
errors.Errorf("mkdir mount path %s failed %s", sPath, out)
}
if procutils.NewCommand("mountpoint", tempPath).Run() != nil {
out, err = procutils.NewRemoteCommandAsFarAsPossible("mount", "--bind", sPath, tempPath).Output()
if err != nil {
errors.Errorf("bind mount to temp path failed %s", out)
}
}
if procutils.NewCommand("mountpoint", sPath).Run() != nil {
out, err = procutils.NewCommand("mount", "--bind", tempPath, sPath).Output()
if err != nil {
errors.Errorf("bind mount temp path to local image path failed %s", out)
}
}
return nil
}
func (s *SBaseStorage) RemoveDisk(d IDisk) {
+19 -5
View File
@@ -17,10 +17,12 @@ package storageman
import (
"context"
"fmt"
"strings"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/hostman/hostutils"
@@ -84,15 +86,27 @@ func (s *SNFSStorage) SyncStorageInfo() (jsonutils.JSONObject, error) {
return res, err
}
func (s *SNFSStorage) SetStorageInfo(storageId, storageName string, conf jsonutils.JSONObject) {
s.SLocalStorage.SetStorageInfo(storageId, storageName, conf)
if err := s.checkAndMount(); err != nil {
log.Errorf("Fail to mount storage to mountpoint: %s, %s", s.Path, err)
func (s *SNFSStorage) SetStorageInfo(storageId, storageName string, conf jsonutils.JSONObject) error {
s.StorageId = storageId
s.StorageName = storageName
if dconf, ok := conf.(*jsonutils.JSONDict); ok {
s.StorageConf = dconf
}
if err := s.checkAndMount(); err != nil {
return errors.Errorf("Fail to mount storage to mountpoint: %s, %s", s.Path, err)
}
if !s.isSetStorageInfo && !strings.HasPrefix(s.Path, "/opt/cloud") {
err := s.bindMountTo(s.Path)
if err != nil {
return err
}
s.isSetStorageInfo = true
}
return nil
}
func (s *SNFSStorage) checkAndMount() error {
if err := procutils.NewCommand("mountpoint", s.Path).Run(); err == nil {
if err := procutils.NewRemoteCommandAsFarAsPossible("mountpoint", s.Path).Run(); err == nil {
return nil
}
if s.StorageConf == nil {
@@ -113,7 +113,9 @@ func storageAttach(ctx context.Context, body jsonutils.JSONObject) (interface{},
storageName, _ := body.GetString("name")
storageConf, _ := body.Get("storage_conf")
storage.SetStoragecacheId(storagecacheId)
storage.SetStorageInfo(storageId, storageName, storageConf)
if err := storage.SetStorageInfo(storageId, storageName, storageConf); err != nil {
return nil, err
}
resp, err := storage.SyncStorageInfo()
if err != nil {
return nil, err
@@ -160,7 +162,9 @@ func storageUpdate(ctx context.Context, body jsonutils.JSONObject) (interface{},
return nil, httperrors.NewNotFoundError("Storage %s not found", storageId)
}
storageName, _ := ret.GetString("storage")
storage.SetStorageInfo(storageId, storageName, storageConf)
if err := storage.SetStorageInfo(storageId, storageName, storageConf); err != nil {
return nil, err
}
mountPoint, _ := ret.GetString("mount_point")
storage.SetPath(mountPoint)
return nil, nil