fs.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // +build linux darwin
  2. /*
  3. Copyright 2014 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 fs
  15. import (
  16. "bytes"
  17. "fmt"
  18. "os/exec"
  19. "strings"
  20. "golang.org/x/sys/unix"
  21. "k8s.io/apimachinery/pkg/api/resource"
  22. "k8s.io/kubernetes/pkg/volume/util/fsquota"
  23. )
  24. // FSInfo linux returns (available bytes, byte capacity, byte usage, total inodes, inodes free, inode usage, error)
  25. // for the filesystem that path resides upon.
  26. func FsInfo(path string) (int64, int64, int64, int64, int64, int64, error) {
  27. statfs := &unix.Statfs_t{}
  28. err := unix.Statfs(path, statfs)
  29. if err != nil {
  30. return 0, 0, 0, 0, 0, 0, err
  31. }
  32. // Available is blocks available * fragment size
  33. available := int64(statfs.Bavail) * int64(statfs.Bsize)
  34. // Capacity is total block count * fragment size
  35. capacity := int64(statfs.Blocks) * int64(statfs.Bsize)
  36. // Usage is block being used * fragment size (aka block size).
  37. usage := (int64(statfs.Blocks) - int64(statfs.Bfree)) * int64(statfs.Bsize)
  38. inodes := int64(statfs.Files)
  39. inodesFree := int64(statfs.Ffree)
  40. inodesUsed := inodes - inodesFree
  41. return available, capacity, usage, inodes, inodesFree, inodesUsed, nil
  42. }
  43. // DiskUsage gets disk usage of specified path.
  44. func DiskUsage(path string) (*resource.Quantity, error) {
  45. // First check whether the quota system knows about this directory
  46. // A nil quantity with no error means that the path does not support quotas
  47. // and we should use other mechanisms.
  48. data, err := fsquota.GetConsumption(path)
  49. if data != nil {
  50. return data, nil
  51. } else if err != nil {
  52. return nil, fmt.Errorf("unable to retrieve disk consumption via quota for %s: %v", path, err)
  53. }
  54. // Uses the same niceness level as cadvisor.fs does when running du
  55. // Uses -B 1 to always scale to a blocksize of 1 byte
  56. out, err := exec.Command("nice", "-n", "19", "du", "-x", "-s", "-B", "1", path).CombinedOutput()
  57. if err != nil {
  58. return nil, fmt.Errorf("failed command 'du' ($ nice -n 19 du -x -s -B 1) on path %s with error %v", path, err)
  59. }
  60. used, err := resource.ParseQuantity(strings.Fields(string(out))[0])
  61. if err != nil {
  62. return nil, fmt.Errorf("failed to parse 'du' output %s due to error %v", out, err)
  63. }
  64. used.Format = resource.BinarySI
  65. return &used, nil
  66. }
  67. // Find uses the equivalent of the command `find <path> -dev -printf '.' | wc -c` to count files and directories.
  68. // While this is not an exact measure of inodes used, it is a very good approximation.
  69. func Find(path string) (int64, error) {
  70. if path == "" {
  71. return 0, fmt.Errorf("invalid directory")
  72. }
  73. // First check whether the quota system knows about this directory
  74. // A nil quantity with no error means that the path does not support quotas
  75. // and we should use other mechanisms.
  76. inodes, err := fsquota.GetInodes(path)
  77. if inodes != nil {
  78. return inodes.Value(), nil
  79. } else if err != nil {
  80. return 0, fmt.Errorf("unable to retrieve inode consumption via quota for %s: %v", path, err)
  81. }
  82. var counter byteCounter
  83. var stderr bytes.Buffer
  84. findCmd := exec.Command("find", path, "-xdev", "-printf", ".")
  85. findCmd.Stdout, findCmd.Stderr = &counter, &stderr
  86. if err := findCmd.Start(); err != nil {
  87. return 0, fmt.Errorf("failed to exec cmd %v - %v; stderr: %v", findCmd.Args, err, stderr.String())
  88. }
  89. if err := findCmd.Wait(); err != nil {
  90. return 0, fmt.Errorf("cmd %v failed. stderr: %s; err: %v", findCmd.Args, stderr.String(), err)
  91. }
  92. return counter.bytesWritten, nil
  93. }
  94. // Simple io.Writer implementation that counts how many bytes were written.
  95. type byteCounter struct{ bytesWritten int64 }
  96. func (b *byteCounter) Write(p []byte) (int, error) {
  97. b.bytesWritten += int64(len(p))
  98. return len(p), nil
  99. }