Skip to content

Commit

Permalink
Fix du command to be portable (kubernetes#95172)
Browse files Browse the repository at this point in the history
The `-B 1` argument is not defiend in POSIX du[1] and does not work with
busybox du. Fix this by using `-k` and append "Ki" for the ParseQuantity
call.

`resource.ParseQuantity()` will set the format to `BinarySI` when there
is a "Ki" suffix so we can remove the explicit set of the format.

[1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/du.html

Signed-off-by: Natanael Copa <ncopa@mirantis.com>
  • Loading branch information
ncopa committed Oct 28, 2020
1 parent 17312ea commit e8970cf
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
5 changes: 2 additions & 3 deletions pkg/volume/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -1427,15 +1427,14 @@ func FindEmptyDirectoryUsageOnTmpfs() (*resource.Quantity, error) {
return nil, err
}
defer os.RemoveAll(tmpDir)
out, err := exec.New().Command("nice", "-n", "19", "du", "-x", "-s", "-B", "1", tmpDir).CombinedOutput()
out, err := exec.New().Command("nice", "-n", "19", "du", "-x", "-s", "-k", tmpDir).CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed command 'du' on %s with error %v", tmpDir, err)
}
used, err := resource.ParseQuantity(strings.Fields(string(out))[0])
used, err := resource.ParseQuantity(strings.Fields(string(out))[0] + "Ki")
if err != nil {
return nil, fmt.Errorf("failed to parse 'du' output %s due to error %v", out, err)
}
used.Format = resource.BinarySI
return &used, nil
}

Expand Down
8 changes: 3 additions & 5 deletions pkg/volume/util/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,14 @@ func DiskUsage(path string) (*resource.Quantity, error) {
return nil, fmt.Errorf("unable to retrieve disk consumption via quota for %s: %v", path, err)
}
// Uses the same niceness level as cadvisor.fs does when running du
// Uses -B 1 to always scale to a blocksize of 1 byte
out, err := exec.Command("nice", "-n", "19", "du", "-x", "-s", "-B", "1", path).CombinedOutput()
out, err := exec.Command("nice", "-n", "19", "du", "-x", "-s", "-k", path).CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed command 'du' ($ nice -n 19 du -x -s -B 1) on path %s with error %v", path, err)
return nil, fmt.Errorf("failed command 'du' ($ nice -n 19 du -x -s -k) on path %s with error %v", path, err)
}
used, err := resource.ParseQuantity(strings.Fields(string(out))[0])
used, err := resource.ParseQuantity(strings.Fields(string(out))[0] + "Ki")
if err != nil {
return nil, fmt.Errorf("failed to parse 'du' output %s due to error %v", out, err)
}
used.Format = resource.BinarySI
return &used, nil
}

Expand Down

0 comments on commit e8970cf

Please sign in to comment.