From 3fd4c0167f1aacbf3944363b23b51b7d31767129 Mon Sep 17 00:00:00 2001 From: Dean Roehrich Date: Fri, 12 Apr 2024 12:09:01 -0500 Subject: [PATCH] Verify that the volume is not yet mounted on the target. (#71) Signed-off-by: Dean Roehrich --- pkg/lustre-driver/service/node.go | 38 ++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/pkg/lustre-driver/service/node.go b/pkg/lustre-driver/service/node.go index ca150d1..49032a3 100644 --- a/pkg/lustre-driver/service/node.go +++ b/pkg/lustre-driver/service/node.go @@ -1,5 +1,5 @@ /* - * Copyright 2021, 2022 Hewlett Packard Enterprise Development LP + * Copyright 2021, 2022, 2024 Hewlett Packard Enterprise Development LP * Other additional copyright holders may be indicated within. * * The entirety of this work is licensed under the Apache License, @@ -73,18 +73,34 @@ func (s *service) NodePublishVolume( return nil, status.Errorf(codes.Internal, "NodePublishVolume - Mountpoint mkdir Failed: Error %v", err) } - // 2. Perform the mount + // 2. Verify that it's not yet mounted. mounter := mount.New("") - err = mounter.Mount( - req.GetVolumeId(), - req.GetTargetPath(), - req.GetVolumeCapability().GetMount().GetFsType(), - req.GetVolumeCapability().GetMount().GetMountFlags()) - + isMounted := false + mountpoints, err := mounter.List() if err != nil { - return nil, status.Errorf(codes.Internal, "NodePublishVolume - Mount Failed: Error %v", err) - } else { - log.WithField("source", req.GetVolumeId()).WithField("target", req.GetTargetPath()).Info("Mounted") + return nil, status.Errorf(codes.Internal, "NodePublishVolume - List mounts failed: Error %v", err) + } + for idx := range mountpoints { + if mountpoints[idx].Path == req.GetTargetPath() && mountpoints[idx].Device == req.GetVolumeId() { + log.WithField("source", req.GetVolumeId()).WithField("target", req.GetTargetPath()).Info("Already mounted") + isMounted = true + break + } + } + + // 3. Perform the mount. + if !isMounted { + err := mounter.Mount( + req.GetVolumeId(), + req.GetTargetPath(), + req.GetVolumeCapability().GetMount().GetFsType(), + req.GetVolumeCapability().GetMount().GetMountFlags()) + + if err != nil { + return nil, status.Errorf(codes.Internal, "NodePublishVolume - Mount Failed: Error %v", err) + } else { + log.WithField("source", req.GetVolumeId()).WithField("target", req.GetTargetPath()).Info("Mounted") + } } return &csi.NodePublishVolumeResponse{}, nil