volume_linux.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // +build linux
  2. /*
  3. Copyright 2016 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package volume
  15. import (
  16. "path/filepath"
  17. "syscall"
  18. "os"
  19. "k8s.io/klog"
  20. )
  21. const (
  22. rwMask = os.FileMode(0660)
  23. roMask = os.FileMode(0440)
  24. execMask = os.FileMode(0110)
  25. )
  26. // SetVolumeOwnership modifies the given volume to be owned by
  27. // fsGroup, and sets SetGid so that newly created files are owned by
  28. // fsGroup. If fsGroup is nil nothing is done.
  29. func SetVolumeOwnership(mounter Mounter, fsGroup *int64) error {
  30. if fsGroup == nil {
  31. return nil
  32. }
  33. return filepath.Walk(mounter.GetPath(), func(path string, info os.FileInfo, err error) error {
  34. if err != nil {
  35. return err
  36. }
  37. // chown and chmod pass through to the underlying file for symlinks.
  38. // Symlinks have a mode of 777 but this really doesn't mean anything.
  39. // The permissions of the underlying file are what matter.
  40. // However, if one reads the mode of a symlink then chmods the symlink
  41. // with that mode, it changes the mode of the underlying file, overridden
  42. // the defaultMode and permissions initialized by the volume plugin, which
  43. // is not what we want; thus, we skip chown/chmod for symlinks.
  44. if info.Mode()&os.ModeSymlink != 0 {
  45. return nil
  46. }
  47. stat, ok := info.Sys().(*syscall.Stat_t)
  48. if !ok {
  49. return nil
  50. }
  51. if stat == nil {
  52. klog.Errorf("Got nil stat_t for path %v while setting ownership of volume", path)
  53. return nil
  54. }
  55. err = os.Chown(path, int(stat.Uid), int(*fsGroup))
  56. if err != nil {
  57. klog.Errorf("Chown failed on %v: %v", path, err)
  58. }
  59. mask := rwMask
  60. if mounter.GetAttributes().ReadOnly {
  61. mask = roMask
  62. }
  63. if info.IsDir() {
  64. mask |= os.ModeSetgid
  65. mask |= execMask
  66. }
  67. err = os.Chmod(path, info.Mode()|mask)
  68. if err != nil {
  69. klog.Errorf("Chmod failed on %v: %v", path, err)
  70. }
  71. return nil
  72. })
  73. }