mirror of
https://github.com/kubernetes-csi/csi-driver-nvmf.git
synced 2025-06-24 21:41:34 +00:00
for test
Signed-off-by: Meinhard Zhou <zhouenhua@bytedance.com>
This commit is contained in:
parent
9c91f4a572
commit
b1ca25b5a6
@ -128,7 +128,7 @@ func (n *NodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpub
|
|||||||
|
|
||||||
// Detach disk
|
// Detach disk
|
||||||
targetPath := req.GetTargetPath()
|
targetPath := req.GetTargetPath()
|
||||||
err := DetachDisk(req.VolumeId, targetPath)
|
err := DetachDisk(targetPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("VolumeID: %s detachDisk err: %v", req.VolumeId, err)
|
klog.Errorf("VolumeID: %s detachDisk err: %v", req.VolumeId, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -18,6 +18,8 @@ package nvmf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/container-storage-interface/spec/lib/go/csi"
|
"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 {
|
if req.GetVolumeCapability().GetBlock() != nil {
|
||||||
_, err := os.Lstat(targetPath)
|
_, err := os.Lstat(targetPath)
|
||||||
if os.IsNotExist(err) {
|
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 {
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to check if the target block file exist, err: %s", err.Error())
|
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 err != nil {
|
||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
return fmt.Errorf("error checking path %s for mount: %w", targetPath, 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
options := []string{""}
|
options := []string{"bind"}
|
||||||
if err = mounter.Mount(devicePath, targetPath, "", options); err != nil {
|
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)
|
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)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DetachDisk(volumeID string, targetPath string) error {
|
func DetachDisk(targetPath string) (err error) {
|
||||||
mounter := mount.New("")
|
mounter := mount.New("")
|
||||||
|
|
||||||
_, cnt, err := mount.GetDeviceNameFromMount(mounter, targetPath)
|
if notMnt, err := mount.IsNotMountPoint(mounter, targetPath); err != nil {
|
||||||
if err != nil {
|
if !os.IsNotExist(err) {
|
||||||
klog.Errorf("nvmf detach disk: failed to get device from mnt: %s\nError: %v", targetPath, err)
|
return fmt.Errorf("Check target path error: %w.", err)
|
||||||
return 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)
|
// Delete the mount point
|
||||||
} else if !pathExists {
|
|
||||||
klog.Warningf("Warning: Unmount skipped because path does not exist: %v", targetPath)
|
if err = os.RemoveAll(targetPath); err != nil {
|
||||||
return nil
|
return fmt.Errorf("remove target path: %w", err)
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -106,11 +106,13 @@ func Rollback(err error, fc func()) {
|
|||||||
|
|
||||||
func makeFile(pathname string) error {
|
func makeFile(pathname string) error {
|
||||||
f, err := os.OpenFile(pathname, os.O_CREATE, os.FileMode(0644))
|
f, err := os.OpenFile(pathname, os.O_CREATE, os.FileMode(0644))
|
||||||
defer f.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !os.IsExist(err) {
|
if !os.IsExist(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if err = f.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user