Skip to content

Commit

Permalink
Fix bug when unpublishing already unmounted file system
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheng Pan committed Nov 28, 2019
1 parent 867eee4 commit 352bbbc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/driver/mounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
type Mounter interface {
mount.Interface
MakeDir(pathname string) error
GetDeviceName(mountPath string) (string, int, error)
}

type NodeMounter struct {
Expand All @@ -44,3 +45,7 @@ func (m *NodeMounter) MakeDir(pathname string) error {
}
return nil
}

func (m *NodeMounter) GetDeviceName(mountPath string) (string, int, error) {
return mount.GetDeviceNameFromMount(m, mountPath)
}
19 changes: 18 additions & 1 deletion pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,25 @@ func (d *Driver) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublish
return nil, status.Error(codes.InvalidArgument, "Target path not provided")
}

// Check if target directory is a mount point. GetDeviceNameFromMount
// given a mnt point, finds the device from /proc/mounts
// returns the device name, reference count, and error code
_, refCount, err := d.mounter.GetDeviceName(target)
if err != nil {
msg := fmt.Sprintf("failed to check if volume is mounted: %v", err)
return nil, status.Error(codes.Internal, msg)
}

// From the spec: If the volume corresponding to the volume_id
// is not staged to the staging_target_path, the Plugin MUST
// reply 0 OK.
if refCount == 0 {
klog.V(5).Infof("NodeUnpublishVolume: %s target not mounted", target)
return &csi.NodeUnpublishVolumeResponse{}, nil
}

klog.V(5).Infof("NodeUnpublishVolume: unmounting %s", target)
err := d.mounter.Unmount(target)
err = d.mounter.Unmount(target)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not unmount %q: %v", target, err)
}
Expand Down

0 comments on commit 352bbbc

Please sign in to comment.