From 02f7c5ac353fe7abd007054224c121585b2adc33 Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Sun, 27 Sep 2020 18:30:17 +0800 Subject: [PATCH] umount nfs on detach storage --- pkg/hostman/storageman/storage_base.go | 1 + pkg/hostman/storageman/storage_local.go | 4 ++++ pkg/hostman/storageman/storage_nfs.go | 20 +++++++++++++++++++ pkg/hostman/storageman/storage_rbd.go | 4 ++++ .../storagehandler/storagehandler.go | 3 +++ 5 files changed, 32 insertions(+) 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..28405ea174 100644 --- a/pkg/hostman/storageman/storage_rbd.go +++ b/pkg/hostman/storageman/storage_rbd.go @@ -640,6 +640,10 @@ func (s *SRbdStorage) Accessible() error { 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 }