empty_dir_linux.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // +build linux
  2. /*
  3. Copyright 2015 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 emptydir
  15. import (
  16. "fmt"
  17. "golang.org/x/sys/unix"
  18. "k8s.io/klog"
  19. "k8s.io/utils/mount"
  20. v1 "k8s.io/api/core/v1"
  21. )
  22. // Defined by Linux - the type number for tmpfs mounts.
  23. const (
  24. linuxTmpfsMagic = 0x01021994
  25. linuxHugetlbfsMagic = 0x958458f6
  26. )
  27. // realMountDetector implements mountDetector in terms of syscalls.
  28. type realMountDetector struct {
  29. mounter mount.Interface
  30. }
  31. func (m *realMountDetector) GetMountMedium(path string) (v1.StorageMedium, bool, error) {
  32. klog.V(5).Infof("Determining mount medium of %v", path)
  33. notMnt, err := m.mounter.IsLikelyNotMountPoint(path)
  34. if err != nil {
  35. return v1.StorageMediumDefault, false, fmt.Errorf("IsLikelyNotMountPoint(%q): %v", path, err)
  36. }
  37. buf := unix.Statfs_t{}
  38. if err := unix.Statfs(path, &buf); err != nil {
  39. return v1.StorageMediumDefault, false, fmt.Errorf("statfs(%q): %v", path, err)
  40. }
  41. klog.V(5).Infof("Statfs_t of %v: %+v", path, buf)
  42. if buf.Type == linuxTmpfsMagic {
  43. return v1.StorageMediumMemory, !notMnt, nil
  44. } else if int64(buf.Type) == linuxHugetlbfsMagic {
  45. return v1.StorageMediumHugePages, !notMnt, nil
  46. }
  47. return v1.StorageMediumDefault, !notMnt, nil
  48. }