From b1ca25b5a6b4b5c3f43f1f26f84c2942483caa0b Mon Sep 17 00:00:00 2001 From: Meinhard Zhou Date: Mon, 22 May 2023 16:19:05 +0800 Subject: [PATCH] for test Signed-off-by: Meinhard Zhou --- pkg/nvmf/nodeserver.go | 2 +- pkg/nvmf/nvmf.go | 45 ++++++++++++++++++++++-------------------- pkg/nvmf/nvmf_utils.go | 4 +++- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/pkg/nvmf/nodeserver.go b/pkg/nvmf/nodeserver.go index ea3966c..7c1bcb4 100644 --- a/pkg/nvmf/nodeserver.go +++ b/pkg/nvmf/nodeserver.go @@ -128,7 +128,7 @@ func (n *NodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpub // Detach disk targetPath := req.GetTargetPath() - err := DetachDisk(req.VolumeId, targetPath) + err := DetachDisk(targetPath) if err != nil { klog.Errorf("VolumeID: %s detachDisk err: %v", req.VolumeId, err) return nil, err diff --git a/pkg/nvmf/nvmf.go b/pkg/nvmf/nvmf.go index 46a3329..efc26df 100644 --- a/pkg/nvmf/nvmf.go +++ b/pkg/nvmf/nvmf.go @@ -18,6 +18,8 @@ package nvmf import ( "fmt" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" "os" "github.com/container-storage-interface/spec/lib/go/csi" @@ -67,15 +69,19 @@ func AttachDisk(req *csi.NodePublishVolumeRequest, devicePath string) error { if req.GetVolumeCapability().GetBlock() != nil { _, err := os.Lstat(targetPath) if os.IsNotExist(err) { + klog.Errorf("AttachDisk: Block VolumeID %s, Path %s is not exist, so create one.", req.GetVolumeId(), req.GetTargetPath()) if err = makeFile(targetPath); err != nil { - return fmt.Errorf("failed to create target path, err: %s", err.Error()) + if removeErr := os.Remove(targetPath); removeErr != nil { + return status.Errorf(codes.Internal, "Could not remove mount target %q: %v", targetPath, removeErr) + } + return status.Errorf(codes.Internal, "Could not create file %q: %v", targetPath, err) } } if err != nil { return fmt.Errorf("failed to check if the target block file exist, err: %s", err.Error()) } - notMounted, err := mounter.IsLikelyNotMountPoint(targetPath) + notMounted, err := mount.IsNotMountPoint(mounter, targetPath) if err != nil { if !os.IsNotExist(err) { return fmt.Errorf("error checking path %s for mount: %w", targetPath, err) @@ -88,7 +94,7 @@ func AttachDisk(req *csi.NodePublishVolumeRequest, devicePath string) error { return nil } - options := []string{""} + options := []string{"bind"} if err = mounter.Mount(devicePath, targetPath, "", options); err != nil { klog.Errorf("AttachDisk: failed to mount Device %s to %s with options: %v", devicePath, targetPath, options) return fmt.Errorf("failed to mount Device %s to %s with options: %v", devicePath, targetPath, options) @@ -132,27 +138,24 @@ func AttachDisk(req *csi.NodePublishVolumeRequest, devicePath string) error { return nil } -func DetachDisk(volumeID string, targetPath string) error { +func DetachDisk(targetPath string) (err error) { mounter := mount.New("") - _, cnt, err := mount.GetDeviceNameFromMount(mounter, targetPath) - if err != nil { - klog.Errorf("nvmf detach disk: failed to get device from mnt: %s\nError: %v", targetPath, err) - return err + if notMnt, err := mount.IsNotMountPoint(mounter, targetPath); err != nil { + if !os.IsNotExist(err) { + return fmt.Errorf("Check target path error: %w.", err) + } + } else if !notMnt { + if err = mounter.Unmount(targetPath); err != nil { + klog.Errorf("nvmf detach disk: failed to unmount: %s\nError: %v", targetPath, err) + return err + } } - if pathExists, pathErr := mount.PathExists(targetPath); pathErr != nil { - return fmt.Errorf("Error checking if path exists: %v", pathErr) - } else if !pathExists { - klog.Warningf("Warning: Unmount skipped because path does not exist: %v", targetPath) - return nil - } - if err = mounter.Unmount(targetPath); err != nil { - klog.Errorf("iscsi detach disk: failed to unmount: %s\nError: %v", targetPath, err) - return err - } - cnt-- - if cnt != 0 { - return nil + + // Delete the mount point + + if err = os.RemoveAll(targetPath); err != nil { + return fmt.Errorf("remove target path: %w", err) } return nil diff --git a/pkg/nvmf/nvmf_utils.go b/pkg/nvmf/nvmf_utils.go index 71f419b..7481b0f 100644 --- a/pkg/nvmf/nvmf_utils.go +++ b/pkg/nvmf/nvmf_utils.go @@ -106,11 +106,13 @@ func Rollback(err error, fc func()) { func makeFile(pathname string) error { f, err := os.OpenFile(pathname, os.O_CREATE, os.FileMode(0644)) - defer f.Close() if err != nil { if !os.IsExist(err) { return err } } + if err = f.Close(); err != nil { + return err + } return nil }