@@ -109,6 +109,29 @@ func (d *nodeService) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
109
109
return nil , status .Error (codes .InvalidArgument , "Staging target not provided" )
110
110
}
111
111
112
+ mounted , err := d .isDirMounted (target )
113
+ needsCreateDir := false
114
+ if mounted {
115
+ // Already mounted
116
+ klog .V (4 ).Infof ("NodeStageVolume succeeded on volume %v to staging target path %s, mount already exists." , volumeID , target )
117
+ return & csi.NodeStageVolumeResponse {}, nil
118
+ }
119
+
120
+ if err != nil {
121
+ if os .IsNotExist (err ) {
122
+ needsCreateDir = true
123
+ } else {
124
+ return nil , err
125
+ }
126
+ }
127
+
128
+ if needsCreateDir {
129
+ klog .V (4 ).Infof ("NodeStageVolume attempting mkdir for path %s" , target )
130
+ if err := os .MkdirAll (target , 0750 ); err != nil {
131
+ return nil , fmt .Errorf ("mkdir failed for path %s (%v)" , target , err )
132
+ }
133
+ }
134
+
112
135
volCap := req .GetVolumeCapability ()
113
136
if volCap == nil {
114
137
return nil , status .Error (codes .InvalidArgument , "Volume capability not provided" )
@@ -153,23 +176,6 @@ func (d *nodeService) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
153
176
154
177
klog .V (4 ).Infof ("NodeStageVolume: find device path for wwn %s -> %s" , wwn , source )
155
178
156
- exists , err := d .mounter .ExistsPath (target )
157
- if err != nil {
158
- msg := fmt .Sprintf ("failed to check if target %q exists: %v" , target , err )
159
- return nil , status .Error (codes .Internal , msg )
160
- }
161
- // When exists is true it means target path was created but device isn't mounted.
162
- // We don't want to do anything in that case and let the operation proceed.
163
- // Otherwise we need to create the target directory.
164
- if ! exists {
165
- // If target path does not exist we need to create the directory where volume will be staged
166
- klog .V (4 ).Infof ("NodeStageVolume: creating target dir %q" , target )
167
- if err = d .mounter .MakeDir (target ); err != nil {
168
- msg := fmt .Sprintf ("could not create target dir %q: %v" , target , err )
169
- return nil , status .Error (codes .Internal , msg )
170
- }
171
- }
172
-
173
179
// Check if a device is mounted in target directory
174
180
device , _ , err := d .mounter .GetDeviceName (target )
175
181
if err != nil {
@@ -521,3 +527,19 @@ func hasMountOption(options []string, opt string) bool {
521
527
}
522
528
return false
523
529
}
530
+
531
+ // isDirMounted checks if the path is already a mount point
532
+ func (d * nodeService ) isDirMounted (target string ) (bool , error ) {
533
+ // Check if mount already exists
534
+ // TODO(msau): check why in-tree uses IsNotMountPoint
535
+ // something related to squash and not having permissions to lstat
536
+ notMnt , err := d .mounter .IsLikelyNotMountPoint (target )
537
+ if err != nil {
538
+ return false , err
539
+ }
540
+ if ! notMnt {
541
+ // Already mounted
542
+ return true , nil
543
+ }
544
+ return false , nil
545
+ }
0 commit comments