volume_linux.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. klog.Warningf("Setting volume ownership for %s and fsGroup set. If the volume has a lot of files then setting volume ownership could be slow, see https://github.com/kubernetes/kubernetes/issues/69699", mounter.GetPath())
  34. return filepath.Walk(mounter.GetPath(), func(path string, info os.FileInfo, err error) error {
  35. if err != nil {
  36. return err
  37. }
  38. // chown and chmod pass through to the underlying file for symlinks.
  39. // Symlinks have a mode of 777 but this really doesn't mean anything.
  40. // The permissions of the underlying file are what matter.
  41. // However, if one reads the mode of a symlink then chmods the symlink
  42. // with that mode, it changes the mode of the underlying file, overridden
  43. // the defaultMode and permissions initialized by the volume plugin, which
  44. // is not what we want; thus, we skip chown/chmod for symlinks.
  45. if info.Mode()&os.ModeSymlink != 0 {
  46. return nil
  47. }
  48. stat, ok := info.Sys().(*syscall.Stat_t)
  49. if !ok {
  50. return nil
  51. }
  52. if stat == nil {
  53. klog.Errorf("Got nil stat_t for path %v while setting ownership of volume", path)
  54. return nil
  55. }
  56. err = os.Chown(path, int(stat.Uid), int(*fsGroup))
  57. if err != nil {
  58. klog.Errorf("Chown failed on %v: %v", path, err)
  59. }
  60. mask := rwMask
  61. if mounter.GetAttributes().ReadOnly {
  62. mask = roMask
  63. }
  64. if info.IsDir() {
  65. mask |= os.ModeSetgid
  66. mask |= execMask
  67. }
  68. err = os.Chmod(path, info.Mode()|mask)
  69. if err != nil {
  70. klog.Errorf("Chmod failed on %v: %v", path, err)
  71. }
  72. return nil
  73. })
  74. }