Skip to content

Commit 46ad996

Browse files
committed
btrfs cleanup code
1 parent f22c75f commit 46ad996

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

src/packages/file-server/btrfs/filesystem.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ a = require('@cocalc/file-server/btrfs'); fs = await a.filesystem({device:'/tmp/
1212
*/
1313

1414
import refCache from "@cocalc/util/refcache";
15-
import { exists, mkdirp, btrfs, sudo } from "./util";
15+
import { mkdirp, btrfs, sudo } from "./util";
1616
import { join } from "path";
1717
import { Subvolumes } from "./subvolumes";
18+
import { mkdir } from "fs/promises";
19+
import { exists } from "@cocalc/backend/misc/async-utils-node";
20+
import { executeCode } from "@cocalc/backend/execute-code";
1821

1922
// default size of btrfs filesystem if creating an image file.
2023
const DEFAULT_FILESYSTEM_SIZE = "10G";
@@ -45,7 +48,6 @@ export interface Options {
4548
export class Filesystem {
4649
public readonly opts: Options;
4750
public readonly bup: string;
48-
public readonly streams: string;
4951
public readonly subvolumes: Subvolumes;
5052

5153
constructor(opts: Options) {
@@ -56,26 +58,30 @@ export class Filesystem {
5658
};
5759
this.opts = opts;
5860
this.bup = join(this.opts.mount, "bup");
59-
this.streams = join(this.opts.mount, "streams");
6061
this.subvolumes = new Subvolumes(this);
6162
}
6263

6364
init = async () => {
64-
await mkdirp(
65-
[this.opts.mount, this.streams, this.bup].filter((x) => x) as string[],
66-
);
65+
await mkdirp([this.opts.mount]);
6766
await this.initDevice();
6867
await this.mountFilesystem();
6968
await sudo({ command: "chmod", args: ["a+rx", this.opts.mount] });
7069
await btrfs({
7170
args: ["quota", "enable", "--simple", this.opts.mount],
7271
});
72+
await this.initBup();
73+
};
74+
75+
unmount = async () => {
7376
await sudo({
74-
bash: true,
75-
command: `BUP_DIR=${this.bup} bup init`,
77+
command: "umount",
78+
args: [this.opts.mount],
79+
err_on_exit: true,
7680
});
7781
};
7882

83+
close = () => {};
84+
7985
private initDevice = async () => {
8086
if (!isImageFile(this.opts.device)) {
8187
// raw block device -- nothing to do
@@ -125,6 +131,10 @@ export class Filesystem {
125131
}
126132
};
127133

134+
private formatDevice = async () => {
135+
await sudo({ command: "mkfs.btrfs", args: [this.opts.device] });
136+
};
137+
128138
private _mountFilesystem = async () => {
129139
const args: string[] = isImageFile(this.opts.device) ? ["-o", "loop"] : [];
130140
args.push(
@@ -162,19 +172,16 @@ export class Filesystem {
162172
return { stderr, exit_code };
163173
};
164174

165-
unmount = async () => {
166-
await sudo({
167-
command: "umount",
168-
args: [this.opts.mount],
169-
err_on_exit: true,
175+
private initBup = async () => {
176+
if (!(await exists(this.bup))) {
177+
await mkdir(this.bup);
178+
}
179+
await executeCode({
180+
command: "bup",
181+
args: ["init"],
182+
env: { BUP_DIR: this.bup },
170183
});
171184
};
172-
173-
private formatDevice = async () => {
174-
await sudo({ command: "mkfs.btrfs", args: [this.opts.device] });
175-
};
176-
177-
close = () => {};
178185
}
179186

180187
function isImageFile(name: string) {

src/packages/file-server/btrfs/subvolumes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { btrfs } from "./util";
88
import { rename, readdir, rm, stat } from "fs/promises";
99
import { executeCode } from "@cocalc/backend/execute-code";
1010

11-
const RESERVED = new Set(["bup", "recv", "streams", SNAPSHOTS]);
11+
const RESERVED = new Set(["bup", SNAPSHOTS]);
1212

1313
const logger = getLogger("file-server:btrfs:subvolumes");
1414

0 commit comments

Comments
 (0)