|
| 1 | +From 83eb7d75444ae74ca79e49569d3d13d54a93623e Mon Sep 17 00:00:00 2001 |
| 2 | +From: Pavel Karpov < [email protected]> |
| 3 | +Date: Sun, 27 Jul 2025 22:17:41 +0300 |
| 4 | +Subject: [PATCH] add mountPermissions to snapshot |
| 5 | + |
| 6 | +Signed-off-by: Pavel Karpov < [email protected]> |
| 7 | +--- |
| 8 | + pkg/nfs/controllerserver.go | 33 +++++++++++++++++++++++++++++++-- |
| 9 | + pkg/nfs/nodeserver.go | 9 +++++---- |
| 10 | + pkg/nfs/utils.go | 3 ++- |
| 11 | + 3 files changed, 38 insertions(+), 7 deletions(-) |
| 12 | + |
| 13 | +diff --git a/pkg/nfs/controllerserver.go b/pkg/nfs/controllerserver.go |
| 14 | +index 3b7b21a4..348086e2 100644 |
| 15 | +--- a/pkg/nfs/controllerserver.go |
| 16 | ++++ b/pkg/nfs/controllerserver.go |
| 17 | +@@ -185,8 +185,8 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol |
| 18 | + |
| 19 | + if mountPermissions > 0 { |
| 20 | + // Reset directory permissions because of umask problems |
| 21 | +- if err = os.Chmod(internalVolumePath, os.FileMode(mountPermissions)); err != nil { |
| 22 | +- klog.Warningf("failed to chmod subdirectory: %v", err) |
| 23 | ++ if err := chmodIfPermissionMismatch(internalVolumePath, os.FileMode(mountPermissions)); err != nil { |
| 24 | ++ return nil, status.Error(codes.Internal, err.Error()) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | +@@ -404,6 +404,17 @@ func (cs *ControllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS |
| 29 | + if err := validateSnapshot(snapInternalVolPath, snapshot); err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | ++ mountPermissionsForSnapInternalVolPath, err := getMountPermissionsForSnapInternalVolPath(req.GetParameters()) |
| 33 | ++ if err != nil { |
| 34 | ++ return nil, status.Errorf(codes.NotFound, "failed to get mountPermissionsForSnapInternalVolPath: %v", err) |
| 35 | ++ } else { |
| 36 | ++ if mountPermissionsForSnapInternalVolPath > 0 { |
| 37 | ++ if err := chmodIfPermissionMismatch(snapInternalVolPath, os.FileMode(mountPermissionsForSnapInternalVolPath)); err != nil { |
| 38 | ++ return nil, status.Error(codes.Internal, err.Error()) |
| 39 | ++ } |
| 40 | ++ } |
| 41 | ++ |
| 42 | ++ } |
| 43 | + |
| 44 | + if err = cs.internalMount(ctx, srcVol, req.GetParameters(), nil); err != nil { |
| 45 | + return nil, status.Errorf(codes.Internal, "failed to mount src nfs server: %v", err) |
| 46 | +@@ -680,6 +691,8 @@ func newNFSSnapshot(name string, params map[string]string, vol *nfsVolume) (*nfs |
| 47 | + baseDir = v |
| 48 | + case mountOptionsField: |
| 49 | + // no op |
| 50 | ++ case mountPermissionsField: |
| 51 | ++ // no op |
| 52 | + default: |
| 53 | + return nil, status.Errorf(codes.InvalidArgument, "invalid parameter %q in snapshot storage class", k) |
| 54 | + } |
| 55 | +@@ -914,3 +927,19 @@ func volumeFromSnapshot(snap *nfsSnapshot) *nfsVolume { |
| 56 | + uuid: snap.uuid, |
| 57 | + } |
| 58 | + } |
| 59 | ++ |
| 60 | ++// getMountPermissionsForSnapInternalVolPath Convert VolumeSnapshot parameters to a snapInternalVolPath |
| 61 | ++func getMountPermissionsForSnapInternalVolPath(params map[string]string) (uint64, error) { |
| 62 | ++ mountPermissions := uint64(0) |
| 63 | ++ for k, v := range params { |
| 64 | ++ if strings.ToLower(k) == mountPermissionsField { |
| 65 | ++ if v != "" { |
| 66 | ++ var err error |
| 67 | ++ if mountPermissions, err = strconv.ParseUint(v, 8, 32); err != nil { |
| 68 | ++ return 0, status.Errorf(codes.InvalidArgument, "invalid mountPermissions %s in storage class", v) |
| 69 | ++ } |
| 70 | ++ } |
| 71 | ++ } |
| 72 | ++ } |
| 73 | ++ return mountPermissions, nil |
| 74 | ++} |
| 75 | +diff --git a/pkg/nfs/nodeserver.go b/pkg/nfs/nodeserver.go |
| 76 | +index ac28dd22..ec2ccbcc 100644 |
| 77 | +--- a/pkg/nfs/nodeserver.go |
| 78 | ++++ b/pkg/nfs/nodeserver.go |
| 79 | +@@ -118,7 +118,7 @@ func (ns *NodeServer) NodePublishVolume(_ context.Context, req *csi.NodePublishV |
| 80 | + notMnt, err := ns.mounter.IsLikelyNotMountPoint(targetPath) |
| 81 | + if err != nil { |
| 82 | + if os.IsNotExist(err) { |
| 83 | +- if err := os.MkdirAll(targetPath, os.FileMode(mountPermissions)); err != nil { |
| 84 | ++ if err := os.MkdirAll(targetPath, 0777); err != nil { |
| 85 | + return nil, status.Error(codes.Internal, err.Error()) |
| 86 | + } |
| 87 | + notMnt = true |
| 88 | +@@ -146,9 +146,10 @@ func (ns *NodeServer) NodePublishVolume(_ context.Context, req *csi.NodePublishV |
| 89 | + } |
| 90 | + |
| 91 | + if mountPermissions > 0 { |
| 92 | +- if err := chmodIfPermissionMismatch(targetPath, os.FileMode(mountPermissions)); err != nil { |
| 93 | +- return nil, status.Error(codes.Internal, err.Error()) |
| 94 | +- } |
| 95 | ++ klog.V(2).Infof("skip chmod on targetPath(%s), as there is no need to change the root directory of the nfs server", targetPath) |
| 96 | ++ //if err := chmodIfPermissionMismatch(targetPath, os.FileMode(mountPermissions)); err != nil { |
| 97 | ++ // return nil, status.Error(codes.Internal, err.Error()) |
| 98 | ++ //} |
| 99 | + } else { |
| 100 | + klog.V(2).Infof("skip chmod on targetPath(%s) since mountPermissions is set as 0", targetPath) |
| 101 | + } |
| 102 | +diff --git a/pkg/nfs/utils.go b/pkg/nfs/utils.go |
| 103 | +index 53737cb2..8f20549e 100644 |
| 104 | +--- a/pkg/nfs/utils.go |
| 105 | ++++ b/pkg/nfs/utils.go |
| 106 | +@@ -22,6 +22,7 @@ import ( |
| 107 | + "path/filepath" |
| 108 | + "strings" |
| 109 | + "sync" |
| 110 | ++ "syscall" |
| 111 | + "time" |
| 112 | + |
| 113 | + "github.com/container-storage-interface/spec/lib/go/csi" |
| 114 | +@@ -167,7 +168,7 @@ func chmodIfPermissionMismatch(targetPath string, mode os.FileMode) error { |
| 115 | + perm := info.Mode() & os.ModePerm |
| 116 | + if perm != mode { |
| 117 | + klog.V(2).Infof("chmod targetPath(%s, mode:0%o) with permissions(0%o)", targetPath, info.Mode(), mode) |
| 118 | +- if err := os.Chmod(targetPath, mode); err != nil { |
| 119 | ++ if err := syscall.Chmod(targetPath, uint32(mode)); err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + } else { |
| 123 | +-- |
| 124 | +2.43.0 |
| 125 | + |
0 commit comments