From 2457d41401275bdd1a63c2e65dd8df872ff4d4fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sima=CC=83o=20Reis?= Date: Mon, 21 Jan 2019 11:27:42 -0100 Subject: [PATCH] Check if target path is mounted before unmounting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Simão Reis --- pkg/driver/node.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/driver/node.go b/pkg/driver/node.go index b1efbfd895..f1036a91ed 100644 --- a/pkg/driver/node.go +++ b/pkg/driver/node.go @@ -149,8 +149,29 @@ func (d *Driver) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolu return nil, status.Error(codes.InvalidArgument, "Staging target 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 + dev, refCount, err := mount.GetDeviceNameFromMount(d.mounter, 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("NodeUnstageVolume: %s target not mounted", target) + return &csi.NodeUnstageVolumeResponse{}, nil + } + + if refCount > 1 { + klog.Warningf("NodeUnstageVolume: found %d references to device %s mounted at target path %s", refCount, dev, target) + } + klog.V(5).Infof("NodeUnstageVolume: unmounting %s", target) - err := d.mounter.Interface.Unmount(target) + err = d.mounter.Interface.Unmount(target) if err != nil { return nil, status.Errorf(codes.Internal, "Could not unmount target %q: %v", target, err) }