diff --git a/pkg/hostman/guestman/guesthandlers/guesthandler.go b/pkg/hostman/guestman/guesthandlers/guesthandler.go index b8bb50e238..4e08ec3199 100644 --- a/pkg/hostman/guestman/guesthandlers/guesthandler.go +++ b/pkg/hostman/guestman/guesthandlers/guesthandler.go @@ -21,6 +21,7 @@ import ( "strings" "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" "yunion.io/x/onecloud/pkg/appsrv" "yunion.io/x/onecloud/pkg/hostman/guestman" @@ -460,7 +461,7 @@ func guestReloadDiskSnapshot(ctx context.Context, sid string, body jsonutils.JSO id, _ := d.GetString("disk_id") if diskId == id { diskPath, _ := d.GetString("path") - disk = storageman.GetManager().GetDiskByPath(diskPath) + disk, _ = storageman.GetManager().GetDiskByPath(diskPath) break } } @@ -495,7 +496,10 @@ func guestSnapshot(ctx context.Context, sid string, body jsonutils.JSONObject) ( id, _ := d.GetString("disk_id") if diskId == id { diskPath, _ := d.GetString("path") - disk = storageman.GetManager().GetDiskByPath(diskPath) + disk, err = storageman.GetManager().GetDiskByPath(diskPath) + if err != nil { + return nil, errors.Wrapf(err, "GetDiskByPath(%s)", diskPath) + } break } } @@ -531,7 +535,10 @@ func guestDeleteSnapshot(ctx context.Context, sid string, body jsonutils.JSONObj id, _ := d.GetString("disk_id") if diskId == id { diskPath, _ := d.GetString("path") - disk = storageman.GetManager().GetDiskByPath(diskPath) + disk, err = storageman.GetManager().GetDiskByPath(diskPath) + if err != nil { + return nil, errors.Wrapf(err, "GetDiskByPath(%s)", diskPath) + } break } } diff --git a/pkg/hostman/guestman/guesttasks.go b/pkg/hostman/guestman/guesttasks.go index c3f8105e87..50f96ac8c8 100644 --- a/pkg/hostman/guestman/guesttasks.go +++ b/pkg/hostman/guestman/guesttasks.go @@ -273,7 +273,7 @@ func (d *SGuestDiskSyncTask) addDisk(disk jsonutils.JSONObject) { func (d *SGuestDiskSyncTask) startAddDisk(disk jsonutils.JSONObject) { diskPath, _ := disk.GetString("path") - iDisk := storageman.GetManager().GetDiskByPath(diskPath) + iDisk, _ := storageman.GetManager().GetDiskByPath(diskPath) if iDisk == nil { d.syncDisksConf() return diff --git a/pkg/hostman/guestman/qemu-arm.go b/pkg/hostman/guestman/qemu-arm.go index 4021014c94..0af07afe1c 100644 --- a/pkg/hostman/guestman/qemu-arm.go +++ b/pkg/hostman/guestman/qemu-arm.go @@ -18,6 +18,7 @@ import ( "fmt" "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" "yunion.io/x/pkg/utils" options "yunion.io/x/onecloud/pkg/hostman/options" @@ -101,9 +102,9 @@ func (s *SKVMGuestInstance) generateArmStartScript(data *jsonutils.JSONDict) (st for _, disk := range disks { diskPath, _ := disk.GetString("path") - d := storageman.GetManager().GetDiskByPath(diskPath) - if d == nil { - return "", fmt.Errorf("get disk %s by storage error", diskPath) + d, err := storageman.GetManager().GetDiskByPath(diskPath) + if err != nil { + return "", errors.Wrapf(err, "GetDiskByPath(%s)", diskPath) } diskIndex, _ := disk.Int("index") diff --git a/pkg/hostman/guestman/qemu-kvm.go b/pkg/hostman/guestman/qemu-kvm.go index 078ac15ba7..67f4bba51b 100644 --- a/pkg/hostman/guestman/qemu-kvm.go +++ b/pkg/hostman/guestman/qemu-kvm.go @@ -844,9 +844,9 @@ func (s *SKVMGuestInstance) DeployFs(deployInfo *deployapi.DeployInfo) (jsonutil disks, _ := s.Desc.GetArray("disks") if len(disks) > 0 { diskPath, _ := disks[0].GetString("path") - disk := storageman.GetManager().GetDiskByPath(diskPath) - if disk == nil { - return nil, fmt.Errorf("Cannot find disk %s index 0", diskPath) + disk, err := storageman.GetManager().GetDiskByPath(diskPath) + if err != nil { + return nil, errors.Wrapf(err, "GetDiskByPath(%s)", diskPath) } return disk.DeployGuestFs(disk.GetPath(), s.Desc, deployInfo) } else { @@ -923,7 +923,7 @@ func (s *SKVMGuestInstance) delTmpDisks(ctx context.Context, migrated bool) erro for _, disk := range disks { if disk.Contains("path") { diskPath, _ := disk.GetString("path") - d := storageman.GetManager().GetDiskByPath(diskPath) + d, _ := storageman.GetManager().GetDiskByPath(diskPath) if d != nil && d.GetType() == compute.STORAGE_LOCAL && migrated { if err := d.DeleteAllSnapshot(); err != nil { log.Errorln(err) @@ -1338,7 +1338,7 @@ func (s *SKVMGuestInstance) streamDisksComplete(ctx context.Context) { disks, _ := s.Desc.GetArray("disks") for i, disk := range disks { diskpath, _ := disk.GetString("path") - d := storageman.GetManager().GetDiskByPath(diskpath) + d, _ := storageman.GetManager().GetDiskByPath(diskpath) if d != nil { log.Infof("Disk %s do post create from fuse", d.GetId()) d.PostCreateFromImageFuse() @@ -1592,7 +1592,10 @@ func (s *SKVMGuestInstance) PrepareMigrate(liveMigrage bool) (*jsonutils.JSONDic for _, disk := range disks { if disk.Contains("path") { diskPath, _ := disk.GetString("path") - d := storageman.GetManager().GetDiskByPath(diskPath) + d, err := storageman.GetManager().GetDiskByPath(diskPath) + if err != nil { + return nil, errors.Wrapf(err, "GetDiskByPath(%s)", diskPath) + } if d.GetType() == compute.STORAGE_LOCAL { back, err := d.PrepareMigrate(liveMigrage) if err != nil { @@ -1622,8 +1625,8 @@ func (s *SKVMGuestInstance) IsSharedStorage() bool { disks, _ := s.Desc.GetArray("disks") for i := 0; i < len(disks); i++ { diskPath, _ := disks[i].GetString("path") - disk := storageman.GetManager().GetDiskByPath(diskPath) - if disk == nil { + disk, err := storageman.GetManager().GetDiskByPath(diskPath) + if err != nil { log.Errorf("failed find disk by path %s", diskPath) return false } diff --git a/pkg/hostman/guestman/qemu-kvmhelper.go b/pkg/hostman/guestman/qemu-kvmhelper.go index 88f77246f2..cd32bbf562 100644 --- a/pkg/hostman/guestman/qemu-kvmhelper.go +++ b/pkg/hostman/guestman/qemu-kvmhelper.go @@ -26,6 +26,7 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/pkg/errors" "yunion.io/x/pkg/utils" "yunion.io/x/onecloud/pkg/hostman/options" @@ -401,9 +402,9 @@ func (s *SKVMGuestInstance) _generateStartScript(data *jsonutils.JSONDict) (stri for _, disk := range disks { diskPath, _ := disk.GetString("path") - d := storageman.GetManager().GetDiskByPath(diskPath) - if d == nil { - return "", fmt.Errorf("get disk %s by storage error", diskPath) + d, err := storageman.GetManager().GetDiskByPath(diskPath) + if err != nil { + return "", errors.Wrapf(err, "GetDiskByPath(%s)", diskPath) } diskIndex, _ := disk.Int("index") diff --git a/pkg/hostman/hostinfo/hostinfo.go b/pkg/hostman/hostinfo/hostinfo.go index eb9028dab1..4253132bb0 100644 --- a/pkg/hostman/hostinfo/hostinfo.go +++ b/pkg/hostman/hostinfo/hostinfo.go @@ -1427,7 +1427,7 @@ func (h *SHostInfo) onGetStorageInfoSucc(hoststorages []jsonutils.JSONObject) { } } else { // Storage type local - storage := storageManager.GetStorageByPath(mountPoint) + storage, _ := storageManager.GetStorageByPath(mountPoint) if storage != nil { storage.SetStoragecacheId(storagecacheId) if IsRootPartition(mountPoint) { diff --git a/pkg/hostman/storageman/core.go b/pkg/hostman/storageman/core.go index db829b1132..a35cf4ceb9 100644 --- a/pkg/hostman/storageman/core.go +++ b/pkg/hostman/storageman/core.go @@ -23,11 +23,13 @@ import ( "time" "yunion.io/x/log" + "yunion.io/x/pkg/errors" "yunion.io/x/pkg/util/timeutils" "yunion.io/x/pkg/utils" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman" + "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/hostman/hostutils" "yunion.io/x/onecloud/pkg/hostman/hostutils/kubelet" "yunion.io/x/onecloud/pkg/hostman/options" @@ -199,16 +201,16 @@ func (s *SStorageManager) GetStorageDisk(storageId, diskId string) IDisk { return nil } -func (s *SStorageManager) GetStorageByPath(sPath string) IStorage { +func (s *SStorageManager) GetStorageByPath(sPath string) (IStorage, error) { for _, storage := range s.Storages { if storage.GetPath() == sPath { - return storage + return storage, nil } } - return nil + return nil, errors.Wrapf(cloudprovider.ErrNotFound, sPath) } -func (s *SStorageManager) GetDiskByPath(diskPath string) IDisk { +func (s *SStorageManager) GetDiskByPath(diskPath string) (IDisk, error) { pos := strings.LastIndex(diskPath, "/") sPath := diskPath[:pos] diskId := diskPath[pos+1:] @@ -216,12 +218,11 @@ func (s *SStorageManager) GetDiskByPath(diskPath string) IDisk { if pos > 0 { diskId = diskId[:pos] } - storage := s.GetStorageByPath(sPath) - if storage != nil { - disk, _ := storage.GetDiskById(diskId) - return disk + storage, err := s.GetStorageByPath(sPath) + if err != nil { + return nil, errors.Wrapf(err, "GetStorageByPath") } - return nil + return storage.GetDiskById(diskId) } func (s *SStorageManager) GetTotalCapacity() int { diff --git a/pkg/hostman/storageman/storagehandler/storagehandler.go b/pkg/hostman/storageman/storagehandler/storagehandler.go index 4a19fe223c..4baffec18b 100644 --- a/pkg/hostman/storageman/storagehandler/storagehandler.go +++ b/pkg/hostman/storageman/storagehandler/storagehandler.go @@ -19,10 +19,12 @@ import ( "fmt" "net/http" + "github.com/pkg/errors" "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/onecloud/pkg/appsrv" + "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/hostman/hostutils" "yunion.io/x/onecloud/pkg/hostman/storageman" "yunion.io/x/onecloud/pkg/httperrors" @@ -130,12 +132,14 @@ func storageDetach(ctx context.Context, body jsonutils.JSONObject) (interface{}, if err != nil { return nil, httperrors.NewMissingParameterError("mount_point") } - storage := storageman.GetManager().GetStorageByPath(mountPoint) - - name, _ := body.GetString("name") - if storage == nil { - return nil, httperrors.NewBadRequestError("ShareStorage[%s] Has detach from host ...", name) + storage, err := storageman.GetManager().GetStorageByPath(mountPoint) + if err != nil { + if errors.Cause(err) == cloudprovider.ErrNotFound { + return nil, nil + } + return nil, errors.Wrapf(err, "GetStorageByPath(%s)", mountPoint) } + if err := storage.Detach(); err != nil { log.Errorf("detach storage %s failed: %s", storage.GetPath(), err) }