volume_linux.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. "k8s.io/kubernetes/pkg/util/chmod"
  19. "k8s.io/kubernetes/pkg/util/chown"
  20. "os"
  21. "github.com/golang/glog"
  22. )
  23. const (
  24. rwMask = os.FileMode(0660)
  25. roMask = os.FileMode(0440)
  26. )
  27. // SetVolumeOwnership modifies the given volume to be owned by
  28. // fsGroup, and sets SetGid so that newly created files are owned by
  29. // fsGroup. If fsGroup is nil nothing is done.
  30. func SetVolumeOwnership(mounter Mounter, fsGroup *int64) error {
  31. if fsGroup == nil {
  32. return nil
  33. }
  34. chownRunner := chown.New()
  35. chmodRunner := chmod.New()
  36. return filepath.Walk(mounter.GetPath(), func(path string, info os.FileInfo, err error) error {
  37. if err != nil {
  38. return err
  39. }
  40. stat, ok := info.Sys().(*syscall.Stat_t)
  41. if !ok {
  42. return nil
  43. }
  44. if stat == nil {
  45. glog.Errorf("Got nil stat_t for path %v while setting ownership of volume", path)
  46. return nil
  47. }
  48. err = chownRunner.Chown(path, int(stat.Uid), int(*fsGroup))
  49. if err != nil {
  50. glog.Errorf("Chown failed on %v: %v", path, err)
  51. }
  52. mask := rwMask
  53. if mounter.GetAttributes().ReadOnly {
  54. mask = roMask
  55. }
  56. err = chmodRunner.Chmod(path, info.Mode()|mask|os.ModeSetgid)
  57. if err != nil {
  58. glog.Errorf("Chmod failed on %v: %v", path, err)
  59. }
  60. return nil
  61. })
  62. }