Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/file-server/btrfs/subvolume-quota.ts
1539 views
1
import { type Subvolume } from "./subvolume";
2
import { btrfs } from "./util";
3
import getLogger from "@cocalc/backend/logger";
4
5
const logger = getLogger("file-server:btrfs:subvolume-quota");
6
7
export class SubvolumeQuota {
8
constructor(public subvolume: Subvolume) {}
9
10
private qgroup = async () => {
11
const { stdout } = await btrfs({
12
verbose: false,
13
args: ["--format=json", "qgroup", "show", "-reF", this.subvolume.path],
14
});
15
const x = JSON.parse(stdout);
16
return x["qgroup-show"][0];
17
};
18
19
get = async (): Promise<{
20
size: number;
21
used: number;
22
}> => {
23
let { max_referenced: size, referenced: used } = await this.qgroup();
24
if (size == "none") {
25
size = null;
26
}
27
return {
28
used,
29
size,
30
};
31
};
32
33
set = async (size: string | number) => {
34
if (!size) {
35
throw Error("size must be specified");
36
}
37
logger.debug("setQuota ", this.subvolume.path, size);
38
await btrfs({
39
args: ["qgroup", "limit", `${size}`, this.subvolume.path],
40
});
41
};
42
43
du = async () => {
44
return await btrfs({
45
args: ["filesystem", "du", "-s", this.subvolume.path],
46
});
47
};
48
49
usage = async (): Promise<{
50
// used and free in bytes
51
used: number;
52
free: number;
53
size: number;
54
}> => {
55
const { stdout } = await btrfs({
56
args: ["filesystem", "usage", "-b", this.subvolume.path],
57
});
58
let used: number = -1;
59
let free: number = -1;
60
let size: number = -1;
61
for (const x of stdout.split("\n")) {
62
if (used == -1) {
63
const i = x.indexOf("Used:");
64
if (i != -1) {
65
used = parseInt(x.split(":")[1].trim());
66
continue;
67
}
68
}
69
if (free == -1) {
70
const i = x.indexOf("Free (statfs, df):");
71
if (i != -1) {
72
free = parseInt(x.split(":")[1].trim());
73
continue;
74
}
75
}
76
if (size == -1) {
77
const i = x.indexOf("Device size:");
78
if (i != -1) {
79
size = parseInt(x.split(":")[1].trim());
80
continue;
81
}
82
}
83
}
84
return { used, free, size };
85
};
86
}
87
88