diff --git a/pkg/hostman/storageman/storage_base.go b/pkg/hostman/storageman/storage_base.go index e5523a59f3..e590bcb570 100644 --- a/pkg/hostman/storageman/storage_base.go +++ b/pkg/hostman/storageman/storage_base.go @@ -125,6 +125,7 @@ type IStorage interface { disksBackingFile, srcSnapshots jsonutils.JSONObject, rebaseDisks bool, diskDesc jsonutils.JSONObject) error Accessible() error + Detach() error } type SBaseStorage struct { diff --git a/pkg/hostman/storageman/storage_local.go b/pkg/hostman/storageman/storage_local.go index 4c8a6a863b..40fffc090b 100644 --- a/pkg/hostman/storageman/storage_local.go +++ b/pkg/hostman/storageman/storage_local.go @@ -174,6 +174,10 @@ func (s *SLocalStorage) Accessible() error { } +func (s *SLocalStorage) Detach() error { + return nil +} + func (s *SLocalStorage) DeleteDiskfile(diskpath string) error { log.Infof("Start Delete %s", diskpath) if options.HostOptions.RecycleDiskfile { diff --git a/pkg/hostman/storageman/storage_nfs.go b/pkg/hostman/storageman/storage_nfs.go index 34b9dace42..df4b4041bc 100644 --- a/pkg/hostman/storageman/storage_nfs.go +++ b/pkg/hostman/storageman/storage_nfs.go @@ -17,6 +17,7 @@ package storageman import ( "context" "fmt" + "path" "strings" "time" @@ -129,3 +130,22 @@ func (s *SNFSStorage) checkAndMount() error { } return nil } + +func (s *SNFSStorage) Detach() error { + if !strings.HasPrefix(s.Path, "/opt/cloud") { + tmpPath := path.Join(TempBindMountPath, s.Path) + out, err := procutils.NewCommand("umount", s.Path).Output() + if err != nil { + return errors.Wrapf(err, "1. umount %s failed %s", s.Path, out) + } + out, err = procutils.NewRemoteCommandAsFarAsPossible("umount", tmpPath).Output() + if err != nil { + return errors.Wrapf(err, "2. umount %s failed %s", tmpPath, out) + } + } + out, err := procutils.NewRemoteCommandAsFarAsPossible("umount", s.Path).Output() + if err != nil { + return errors.Wrapf(err, "3. umount %s failed %s", s.Path, out) + } + return nil +} diff --git a/pkg/hostman/storageman/storage_rbd.go b/pkg/hostman/storageman/storage_rbd.go index 564d564451..a8ecb0c9bc 100644 --- a/pkg/hostman/storageman/storage_rbd.go +++ b/pkg/hostman/storageman/storage_rbd.go @@ -634,12 +634,16 @@ func (s *SRbdStorage) Accessible() error { select { case err = <-c: break - case <-time.After(time.Second * 10): + case <-time.After(time.Second * 30): err = ErrStorageTimeout } return err } +func (s *SRbdStorage) Detach() error { + return nil +} + func (s *SRbdStorage) SaveToGlance(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) { data, ok := params.(*jsonutils.JSONDict) if !ok { diff --git a/pkg/hostman/storageman/storagehandler/storagehandler.go b/pkg/hostman/storageman/storagehandler/storagehandler.go index 2fe9e84286..4a19fe223c 100644 --- a/pkg/hostman/storageman/storagehandler/storagehandler.go +++ b/pkg/hostman/storageman/storagehandler/storagehandler.go @@ -136,6 +136,9 @@ func storageDetach(ctx context.Context, body jsonutils.JSONObject) (interface{}, if storage == nil { return nil, httperrors.NewBadRequestError("ShareStorage[%s] Has detach from host ...", name) } + if err := storage.Detach(); err != nil { + log.Errorf("detach storage %s failed: %s", storage.GetPath(), err) + } storageman.GetManager().Remove(storage) return nil, nil }