-
Notifications
You must be signed in to change notification settings - Fork 19
/
btrfs.go
71 lines (56 loc) · 2.06 KB
/
btrfs.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
package limiter // import "code.cloudfoundry.org/grootfs/store/filesystems/btrfs/drax/limiter"
import (
"bytes"
"fmt"
"os/exec"
"strconv"
"strings"
"code.cloudfoundry.org/commandrunner"
"code.cloudfoundry.org/lager"
)
type BtrfsLimiter struct {
commandRunner commandrunner.CommandRunner
btrfsBin string
}
func NewBtrfsLimiter(btrfsBin string, commandRunner commandrunner.CommandRunner) *BtrfsLimiter {
return &BtrfsLimiter{
commandRunner: commandRunner,
btrfsBin: btrfsBin,
}
}
func (i *BtrfsLimiter) ApplyDiskLimit(logger lager.Logger, path string, diskLimit int64, exclusiveLimit bool) error {
logger = logger.Session("btrfs-applying-quotas", lager.Data{"path": path, "diskLimit": diskLimit, "exclusiveLimit": exclusiveLimit})
logger.Info("start")
defer logger.Info("end")
cmd := exec.Command(i.btrfsBin, i.argsForLimit(path, strconv.FormatInt(diskLimit, 10), exclusiveLimit)...)
combinedBuffer := bytes.NewBuffer([]byte{})
cmd.Stdout = combinedBuffer
cmd.Stderr = combinedBuffer
logger.Debug("starting-btrfs-command", lager.Data{"cmd": cmd.Path, "args": cmd.Args})
if err := i.commandRunner.Run(cmd); err != nil {
logger.Error("command-failed", err, lager.Data{"commandOutput": combinedBuffer.String()})
return fmt.Errorf(strings.TrimSpace(combinedBuffer.String()))
}
return nil
}
func (i *BtrfsLimiter) DestroyQuotaGroup(logger lager.Logger, path string) error {
logger = logger.Session("btrfs-destroying-qgroup", lager.Data{"path": path})
logger.Info("start")
defer logger.Info("end")
cmd := exec.Command(i.btrfsBin, "qgroup", "destroy", path, path)
combinedBuffer := bytes.NewBuffer([]byte{})
cmd.Stdout = combinedBuffer
cmd.Stderr = combinedBuffer
if err := i.commandRunner.Run(cmd); err != nil {
logger.Error("command-failed", err)
return fmt.Errorf(strings.TrimSpace(combinedBuffer.String()))
}
return nil
}
func (i *BtrfsLimiter) argsForLimit(path, diskLimit string, exclusiveLimit bool) []string {
args := []string{"qgroup", "limit"}
if exclusiveLimit {
args = append(args, "-e")
}
return append(args, diskLimit, path)
}