@@ -395,6 +395,38 @@ func SetVolumeOwnership(path, gid, policy string) error {
395395 return volume .SetVolumeOwnership (& VolumeMounter {path : path }, path , & gidInt64 , & fsGroupChangePolicy , nil )
396396}
397397
398+ // SetRootOwnership sets the ownership of the root directory, Setgid bit and permission
399+ func SetRootOwnership (rootDir string , fsgroup string ) error {
400+ gid , err := strconv .Atoi (fsgroup )
401+ if err != nil {
402+ return fmt .Errorf ("convert %s to int failed with %v" , fsgroup , err )
403+ }
404+
405+ if err := os .Lchown (rootDir , - 1 , gid ); err != nil {
406+ return fmt .Errorf ("set root ownership failed with %v" , err )
407+ }
408+
409+ fsInfo , err := os .Stat (rootDir )
410+ if err != nil {
411+ return fmt .Errorf ("failed to get file system info for %s: %v" , rootDir , err )
412+ }
413+
414+ if fsInfo .Mode ()& os .ModeSymlink != 0 {
415+ return nil
416+ }
417+
418+ unixPerms := os .FileMode (0660 )
419+ unixPerms |= os .ModeSetgid
420+ unixPerms |= os .FileMode (0110 )
421+
422+ err = os .Chmod (rootDir , fsInfo .Mode ()| unixPerms )
423+ if err != nil {
424+ klog .ErrorS (err , "chmod failed" , "path" , rootDir )
425+ }
426+
427+ return nil
428+ }
429+
398430// ExecFunc returns a exec function's output and error
399431type ExecFunc func () (err error )
400432
0 commit comments