This repository has been archived by the owner on Nov 10, 2020. It is now read-only.
/
btrfs.js
90 lines (72 loc) · 2.16 KB
/
btrfs.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict';
var Promise = require('bluebird');
var _ = require('lodash');
var fs = require('fs-extra');
var sbutil = require('./util');
// btrfs
function btrfs() {
return sbutil.execFilePromise('btrfs', _.toArray(arguments));
}
function isBtrfs(path) {
return btrfs('filesystem', 'label', path)
.catch(sbutil.rethrow('\'%s\' does not appear to be a btrfs filesystem', path))
.return();
}
function enableQuota(path) {
return btrfs('quota', 'enable', path)
.catch(sbutil.rethrow('Failed to enable quota for \'%s\'', path));
}
function setQuota(path, size) {
return btrfs('qgroup', 'limit', '-e', size, path)
.catch(sbutil.rethrow('Failed to set quota for \'%s\'', path));
}
function destroyQuota(path) {
return btrfs('qgroup', 'show', '-F', path)
.spread(function (stdout) {
var line = stdout.split('\n')[2];
if (line) {
var matches = line.match(/^(\d+\/\d+) /);
if (matches) {
return btrfs('qgroup', 'destroy', matches[1], path);
}
}
var err = new Error('Unable to find qgroup id in \'btrfs qgroup show\' output');
err.stdout = stdout;
throw err;
})
.catch(sbutil.rethrow('Failed to destroy quota for \'%s\'', path));
}
exports.isBtrfs = isBtrfs;
exports.enableQuota = enableQuota;
exports.setQuota = setQuota;
exports.destroyQuota = destroyQuota;
// loop mounts
function mountLoopFile(file, dir) {
return sbutil.execFilePromise('mount', [
'-o', 'loop',
file, dir
]);
}
function createLoopMount(file, dir, size) {
return Promise.using(sbutil.withOpen(file, 'wx'), function (fd) {
return fs.ftruncateAsync(fd, size);
}).then(function () {
return sbutil.execFilePromise('mkfs.btrfs', [file])
.then(function () {
return exports.mountLoopFile(file, dir);
})
.catch(function (err) {
return fs.unlinkAsync(file)
.throw(err);
});
});
}
function destroyLoopMount(dir) {
return sbutil.execFilePromise('umount', [dir])
.then(function () {
return fs.removeAsync(dir);
});
}
exports.mountLoopFile = mountLoopFile;
exports.createLoopMount = createLoopMount;
exports.destroyLoopMount = destroyLoopMount;