Files
slacker-bot/storage.go
T
2026-11-01 11:47:41 -05:00

208 lines
5.4 KiB
Go

package main
import (
"errors"
"fmt"
"math"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"syscall"
)
const btrfsMagic = 0x9123683E
var (
ErrStorageFull = errors.New("not enough storage left on the drives")
ErrStorageTooBig = errors.New("above the storage limit")
ErrMinOverMax = errors.New("the guaranteed amount can't be more than the max")
ErrBelowUsage = errors.New("below what's already in use")
ErrStorageTooSmall = errors.New("too small")
)
const MinStorageMB = 512
type StorageConfig struct {
Min string `toml:"min"`
Max string `toml:"max"`
Limit string `toml:"limit"`
MinMB, MaxMB, LimitMB int64 `toml:"-"`
}
type DriveConfig struct {
Path string `toml:"path"`
Reserve string `toml:"reserve"`
ReserveMB int64 `toml:"-"`
}
var storageToken = regexp.MustCompile(`^([+-]?)(\d+(?:\.\d+)?)(tib|tb|t|gib|gb|g|mib|mb|m)$`)
func parseSizeMB(s string) (int64, error) {
t := strings.ToLower(strings.ReplaceAll(strings.TrimSpace(s), " ", ""))
p := storageToken.FindStringSubmatch(t)
if p == nil || p[1] != "" {
return 0, fmt.Errorf("%q isn't a size like 5G or 500M", s)
}
return toMB(p[2], p[3]), nil
}
func toMB(num, unit string) int64 {
n, _ := strconv.ParseFloat(num, 64)
switch unit[0] {
case 't':
n *= 1024 * 1024
case 'g':
n *= 1024
}
return int64(math.Round(n))
}
func formatMB(mb int64) string {
trim := func(f float64) string { return strconv.FormatFloat(math.Round(f*10)/10, 'f', -1, 64) }
switch {
case mb >= 1024*1024:
return trim(float64(mb)/(1024*1024)) + " TB"
case mb >= 1024:
return trim(float64(mb)/1024) + " GB"
}
return fmt.Sprintf("%d MB", mb)
}
func parseStorage(args string, curMin, curMax int64) (min, max int64, err error) {
args = unitSpace.ReplaceAllString(strings.ToLower(args), "$1$2")
words := strings.Fields(args)
min, max = curMin, curMax
var minDone, maxDone bool
for i := 0; i < len(words); i++ {
target := "max"
switch words[i] {
case "min", "guarantee", "guaranteed", "reserve":
target = "min"
i++
case "max", "cap", "limit":
i++
}
if i >= len(words) {
return curMin, curMax, fmt.Errorf("%s needs a size after it, like `%s 10G`", target, target)
}
p := storageToken.FindStringSubmatch(words[i])
if p == nil {
if i == 0 {
return curMin, curMax, errNotCommand
}
return curMin, curMax, fmt.Errorf("I didn't understand `%s`", words[i])
}
n := toMB(p[2], p[3])
if target == "min" {
if minDone {
return curMin, curMax, errors.New("min was given twice")
}
minDone = true
min = int64(apply(p[1], float64(curMin), float64(n)))
} else {
if maxDone {
return curMin, curMax, errors.New("max was given twice")
}
maxDone = true
max = int64(apply(p[1], float64(curMax), float64(n)))
}
}
return min, max, nil
}
type DriveReport struct {
Path string
UsableMB int64
CommittedMB int64
BookedMB int64
Homes int
}
func (d DriveReport) HeadroomMB() int64 { return d.UsableMB - d.CommittedMB }
func (d DriveReport) Overbooked() bool { return d.BookedMB > d.UsableMB }
func committed(minMB, usedMB int64) int64 { return max(minMB, usedMB) }
func pickDrive(reports []DriveReport, needMB int64) (string, error) {
best := -1
for i, r := range reports {
if r.HeadroomMB() >= needMB && (best < 0 || r.HeadroomMB() > reports[best].HeadroomMB()) {
best = i
}
}
if best < 0 {
return "", ErrStorageFull
}
return reports[best].Path, nil
}
func prepareDrive(path string) error {
if err := os.MkdirAll(path, 0o700); err != nil {
return err
}
var st syscall.Statfs_t
if err := syscall.Statfs(path, &st); err != nil {
return err
}
if uint32(st.Type) != btrfsMagic {
return fmt.Errorf("%s is not on a btrfs filesystem", path)
}
if out, err := exec.Command("btrfs", "quota", "enable", path).CombinedOutput(); err != nil {
return fmt.Errorf("turning on btrfs quotas for %s: %v: %s", path, err, strings.TrimSpace(string(out)))
}
return nil
}
func freeMB(path string) (int64, error) {
var st syscall.Statfs_t
if err := syscall.Statfs(path, &st); err != nil {
return 0, err
}
return int64(st.Bavail) * int64(st.Bsize) / (1 << 20), nil
}
func createSubvolume(path string) error {
if out, err := exec.Command("btrfs", "subvolume", "create", path).CombinedOutput(); err != nil {
return fmt.Errorf("creating %s: %v: %s", path, err, strings.TrimSpace(string(out)))
}
return nil
}
func setQuota(path string, mb int64) error {
bytes := strconv.FormatInt(mb<<20, 10)
if out, err := exec.Command("btrfs", "qgroup", "limit", bytes, path).CombinedOutput(); err != nil {
return fmt.Errorf("setting quota on %s: %v: %s", path, err, strings.TrimSpace(string(out)))
}
return nil
}
// Quota numbers can lag a few seconds behind writes.
func usedMB(path string) (int64, error) {
out, err := exec.Command("btrfs", "qgroup", "show", "-f", "--raw", path).CombinedOutput()
if err != nil {
return 0, fmt.Errorf("reading usage of %s: %v: %s", path, err, strings.TrimSpace(string(out)))
}
return parseQgroupShow(string(out))
}
// Parses `btrfs qgroup show --raw`: "0/257 <referenced bytes> <exclusive bytes> ...".
func parseQgroupShow(out string) (int64, error) {
for _, line := range strings.Split(out, "\n") {
f := strings.Fields(line)
if len(f) >= 2 && strings.HasPrefix(f[0], "0/") {
b, err := strconv.ParseInt(f[1], 10, 64)
if err != nil {
return 0, err
}
return b >> 20, nil
}
}
return 0, errors.New("no quota group found")
}