From 0b5ba8a42f41d3f9d6ed2be06938d3de160c1e5d Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Sat, 16 Feb 2019 17:01:48 +0800 Subject: [PATCH] fix set storage name --- pkg/compute/models/storages.go | 2 +- pkg/hostman/hostutils/hostutils.go | 1 + pkg/hostman/storageman/core.go | 4 ++-- .../storageman/remotefile/remotefile.go | 19 +++++++++---------- pkg/hostman/storageman/storagebase.go | 8 ++++++++ pkg/hostman/storageman/storagelocal.go | 11 +++++++++-- 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/pkg/compute/models/storages.go b/pkg/compute/models/storages.go index e8a028f2bc..e8975c67d5 100644 --- a/pkg/compute/models/storages.go +++ b/pkg/compute/models/storages.go @@ -163,7 +163,7 @@ func (manager *SStorageManager) ValidateCreateData(ctx context.Context, userCred storageType, _ := data.GetString("storage_type") mediumType, _ := data.GetString("medium_type") capacity, _ := data.Int("capacity") - if capacity <= 0 { + if capacity < 0 { return nil, httperrors.NewInputParameterError("Invalid capacity") } data.Set("capacity", jsonutils.NewInt(capacity)) diff --git a/pkg/hostman/hostutils/hostutils.go b/pkg/hostman/hostutils/hostutils.go index 3c7b73b8e3..56f5e07157 100644 --- a/pkg/hostman/hostutils/hostutils.go +++ b/pkg/hostman/hostutils/hostutils.go @@ -24,6 +24,7 @@ type IHost interface { GetZone() string GetHostId() string GetMediumType() string + GetMasterIp() string IsKvmSupport() bool IsNestedVirtualization() bool diff --git a/pkg/hostman/storageman/core.go b/pkg/hostman/storageman/core.go index 70c337efbc..387ada91ab 100644 --- a/pkg/hostman/storageman/core.go +++ b/pkg/hostman/storageman/core.go @@ -43,8 +43,8 @@ func NewStorageManager(host hostutils.IHost) (*SStorageManager, error) { allFull = true ) - for _, d := range options.HostOptions.LocalImagePath { - s := NewLocalStorage(ret, d) + for i, d := range options.HostOptions.LocalImagePath { + s := NewLocalStorage(ret, d, i) if s.Accessible() { ret.Storages = append(ret.Storages, s) if allFull && s.GetFreeSizeMb() > MINIMAL_FREE_SPACE { diff --git a/pkg/hostman/storageman/remotefile/remotefile.go b/pkg/hostman/storageman/remotefile/remotefile.go index 276b2ddd09..410aad5ba2 100644 --- a/pkg/hostman/storageman/remotefile/remotefile.go +++ b/pkg/hostman/storageman/remotefile/remotefile.go @@ -129,19 +129,18 @@ func (r *SRemoteFile) fetch(preChksum string) bool { fetchSucc = false } } - - if !fetchSucc { - retryCnt += 1 - } else if r.localPath != r.tmpPath { - if fileutils2.Exists(r.localPath) { - if err := syscall.Unlink(r.localPath); err != nil { - log.Errorln(err) - } - } - if err := syscall.Rename(r.tmpPath, r.localPath); err != nil { + } + if !fetchSucc { + retryCnt += 1 + } else if r.localPath != r.tmpPath { + if fileutils2.Exists(r.localPath) { + if err := syscall.Unlink(r.localPath); err != nil { log.Errorln(err) } } + if err := syscall.Rename(r.tmpPath, r.localPath); err != nil { + log.Errorln(err) + } } } return fetchSucc diff --git a/pkg/hostman/storageman/storagebase.go b/pkg/hostman/storageman/storagebase.go index d96f601ac2..9c185b55bd 100644 --- a/pkg/hostman/storageman/storagebase.go +++ b/pkg/hostman/storageman/storagebase.go @@ -78,6 +78,14 @@ func (s *SBaseStorage) GetId() string { return s.StorageId } +func (s *SBaseStorage) GetName(generateName func() string) string { + if len(s.StorageName) > 0 { + return s.StorageName + } else { + return generateName() + } +} + func (s *SBaseStorage) GetPath() string { return s.Path } diff --git a/pkg/hostman/storageman/storagelocal.go b/pkg/hostman/storageman/storagelocal.go index 90832d7f7d..70bc831817 100644 --- a/pkg/hostman/storageman/storagelocal.go +++ b/pkg/hostman/storageman/storagelocal.go @@ -35,11 +35,14 @@ var ( type SLocalStorage struct { SBaseStorage + + Index int } -func NewLocalStorage(manager *SStorageManager, path string) *SLocalStorage { +func NewLocalStorage(manager *SStorageManager, path string, index int) *SLocalStorage { var ret = new(SLocalStorage) ret.SBaseStorage = *NewBaseStorage(manager, path) + ret.Index = index ret.StartSnapshotRecycle() return ret } @@ -64,9 +67,13 @@ func (s *SLocalStorage) GetSnapshotPathByIds(diskId, snapshotId string) string { return path.Join(s.GetSnapshotDir(), diskId+options.HostOptions.SnapshotDirSuffix, snapshotId) } +func (s *SLocalStorage) GetComposedName() string { + return fmt.Sprintf("host_%s_%s_storage_%d", s.Manager.host.GetMasterIp(), s.StorageType(), s.Index) +} + func (s *SLocalStorage) SyncStorageInfo() (jsonutils.JSONObject, error) { content := jsonutils.NewDict() - content.Set("name", jsonutils.NewString(s.StorageName)) + content.Set("name", jsonutils.NewString(s.GetName(s.GetComposedName))) content.Set("capacity", jsonutils.NewInt(int64(s.GetAvailSizeMb()))) content.Set("storage_type", jsonutils.NewString(s.StorageType())) content.Set("medium_type", jsonutils.NewString(s.GetMediumType()))