-
Notifications
You must be signed in to change notification settings - Fork 19
/
btrfs_stats.go
79 lines (64 loc) · 2.31 KB
/
btrfs_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package metrix // import "code.cloudfoundry.org/grootfs/store/filesystems/btrfs/drax/metrix"
import (
"bytes"
"os/exec"
"strings"
"code.cloudfoundry.org/commandrunner"
"code.cloudfoundry.org/lager"
errorspkg "github.com/pkg/errors"
)
type BtrfsStats struct {
commandRunner commandrunner.CommandRunner
btrfsBin string
}
func NewBtrfsStats(btrfsBin string, commandRunner commandrunner.CommandRunner) *BtrfsStats {
return &BtrfsStats{
commandRunner: commandRunner,
btrfsBin: btrfsBin,
}
}
func (m *BtrfsStats) VolumeStats(logger lager.Logger, path string, forceSync bool) ([]byte, error) {
logger = logger.Session("btrfs-fetching-volume-stats", lager.Data{"path": path, "forceSync": forceSync})
logger.Info("starting")
defer logger.Info("ending")
if err := m.isSubvolume(logger, path); err != nil {
return nil, err
}
if forceSync {
cmd := exec.Command(m.btrfsBin, "filesystem", "sync", path)
combinedBuffer := bytes.NewBuffer([]byte{})
cmd.Stdout = combinedBuffer
cmd.Stderr = combinedBuffer
if err := m.commandRunner.Run(cmd); err != nil {
logger.Error("command-failed", err)
return nil, errorspkg.Errorf("syncing filesystem: %s", strings.TrimSpace(combinedBuffer.String()))
}
}
cmd := exec.Command(m.btrfsBin, "qgroup", "show", "--raw", "-F", path)
outputBuffer := bytes.NewBuffer([]byte{})
cmd.Stdout = outputBuffer
errorBuffer := bytes.NewBuffer([]byte{})
cmd.Stderr = errorBuffer
if err := m.commandRunner.Run(cmd); err != nil {
logger.Error("command-failed", err)
return nil, errorspkg.Errorf("qgroup usage: %s, %s",
strings.TrimSpace(outputBuffer.String()),
strings.TrimSpace(errorBuffer.String()))
}
logger.Debug("btrfs-output", lager.Data{"output": outputBuffer.String()})
return outputBuffer.Bytes(), nil
}
func (m *BtrfsStats) isSubvolume(logger lager.Logger, path string) error {
cmd := exec.Command(m.btrfsBin, "subvolume", "show", path)
combinedBuffer := bytes.NewBuffer([]byte{})
cmd.Stderr = combinedBuffer
cmd.Stdout = combinedBuffer
if err := m.commandRunner.Run(cmd); err != nil {
if strings.Contains(combinedBuffer.String(), "not a subvolume") {
return errorspkg.Wrapf(err, "`%s` is not a btrfs volume", path)
}
logger.Error("command-failed", err)
return errorspkg.Errorf("checking if volume exists: %s", combinedBuffer.String())
}
return nil
}