clean up of the codebase
This commit is contained in:
1 parent
7822529a38
commit
0ac61b4ece
52 files changed
+2545
-2274
No files matched your search
@@ -0,0 +1,119 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"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")
|
||||
)
|
||||
|
||||
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 DeleteSubvolume(path string) error {
|
||||
if out, err := exec.Command("btrfs", "subvolume", "delete", path).CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("deleting %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")
|
||||
}
|
||||
Reference in new issue
Block a user