-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwritable-nix-store.js
executable file
·325 lines (287 loc) · 10.5 KB
/
writable-nix-store.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#! /usr/bin/env node
// writable nix store
// author: milahu
// license: MIT
// date: 2022-05-03
// based on nixos-patch-installer/backend/index.js
// this is a more general version
// to get write-access to all files in /nix/store
// note: all write operations go to the upper dir
// so every new derivation or store-path goes to upper
// and is lost on umount
// global state
let isVerbose = false;
const commandList = ['start', 'stop', 'status'];
function main(argv) {
const scriptName = 'writable-nix-store/index.js';
var args = argv.slice(1); // argv0 is index.js
if (args[0] == '--verbose') {
isVerbose = true;
args.shift();
}
const command = args.shift();
if (!command || !isCommand(command)) return showHelp(scriptName);
const storePath = '/nix/store';
const overlayBase = `/nix/overlay-store`;
if (command == 'start') {
startOverlayfs(storePath, overlayBase);
}
else if (command == 'stop') {
stopOverlayfs(storePath, overlayBase);
}
else if (command == 'status') {
statusOverlayfs(storePath, overlayBase);
}
}
function showHelp(scriptName) {
console.log([
`usage:`,
` sudo ${scriptName} [--verbose] command`,
'',
'commands:',
' start mount the overlay',
' stop unmount the overlay',
].join('\n'));
}
const isCommand = command => new Set(commandList).has(command);
const os = require('os');
const fs = require('fs');
const path = require('path');
const cp = require('child_process');
// UTC timestamp (seconds since 1970-01-01)
//const timestampUtc = () => Math.floor((new Date()).getTime() / 1000);
const timestampUtc = () => (new Date()).getTime() / 1000; // float seconds
//const isLink = path => fs.lstatSync(path).isSymbolicLink();
const basename = path => path.split("/").filter(Boolean).slice(-1)[0];
const mkdirTemp = (base = 'temp') => fs.mkdtempSync(`${os.tmpdir()}/${base.replace(/\//g, '--')}-`);
const getPkgPath = subdirPath => subdirPath.split("/").slice(0, 4).join("/");
// -> /nix/store/hash-name-version
const readdir = path => fs.readdirSync(path);
const realpath = path => fs.realpathSync(path);
const exists = path => fs.existsSync(path);
const rename = (a, b) => {
if (isVerbose) console.log(`rename: ${a} -> ${b}`);
fs.renameSync(a, b);
};
const mkdir = (path, o={}) => {
if (o.canExist && exists(path)) {
if (isVerbose) console.log(`dir exists: ${path}`);
return;
}
if (isVerbose) console.log(`mkdir: ${path}`);
fs.mkdirSync(path, { recursive: true });
};
const rmdir = path => {
if (isVerbose) console.log(`rmdir: ${path}`);
fs.rmdirSync(path);
};
const hardlink = (source, target) => {
if (exists(target)) {
if (isVerbose) console.log(`hardlink target exists: ${target}`)
return;
};
if (isVerbose) console.log(`hardlink: ${source} -> ${target}`)
fs.linkSync(source, target)
};
const symlink = (source, target) => {
if (exists(target)) {
if (isVerbose) console.log(`symlink target exists: ${target}`)
return;
};
if (isVerbose) console.log(`symlink: ${source} -> ${target}`)
fs.symlinkSync(source, target)
};
const copy = (source, target) => {
if (exists(target))
if (isVerbose) console.log(`overwrite: ${source} -> ${target}`)
else
if (isVerbose) console.log(`copy: ${source} -> ${target}`)
fs.copyFileSync(source, target);
};
const exec = cmd => {
if (Array.isArray(cmd)) {
if (isVerbose) console.log(`exec: ${cmd.join(' ')}`); // TODO pretty-print escaped args
return cp.spawnSync(cmd[0], cmd.slice(1), { encoding: 'utf8', maxbuffer: Infinity }).stdout;
}
if (isVerbose) console.log(`exec: ${cmd}`);
return cp.execSync(cmd, { encoding: 'utf8', maxbuffer: Infinity });
};
function getOverlayDirs(lowerDir, overlayBase) {
const sep = (lowerDir == overlayBase) ? '.overlayfs.' : '/';
// note: hide == mount
// hide is shadowed by mount
// -> bind-mount hide to lower
return {
hide: lowerDir,
mount: lowerDir,
//lower: `${overlayBase}${sep}lower`,
lower: path.join('/a', lowerDir), // example: /a/nix/store
//upper: `${overlayBase}${sep}upper`,
upper: path.join('/b', lowerDir), // example: /b/nix/store
work: `${overlayBase}${sep}work`,
};
}
function isMountedOverlay(mountPoint) {
// overlayfs:
// overlay on /nix/store type overlay (...)
// bind-mount:
// /dev/sda1 on /nix/overlay-store/lower type ext4 (ro,noatime,nodiratime,data=ordered)
const mountList = exec('mount').trim().split('\n').map(l => l.split(' ')).filter(l => l[4] == 'overlay');
// TODO use /proc/self/mountinfo?
/*
if (isVerbose) {
console.log(`isMountedOverlay: mountList:`);
console.log(JSON.stringify(mountList, null, 2));
}
*/
return mountList.find(l => l[2] == mountPoint);
};
function isMountedBind(mountPoint) {
// overlayfs:
// overlay on /nix/store type overlay (...)
// bind-mount:
// /dev/sda1 on /nix/overlay-store/lower type ext4 (ro,noatime,nodiratime,data=ordered)
// TODO regex
const mountList = fs.readFileSync('/proc/self/mountinfo', 'utf8').trim().split('\n').map(l => l.split(' '));
/*
if (isVerbose) {
console.log(`isMountedBind: mountList:`);
console.log(JSON.stringify(mountList, null, 2));
}
*/
return mountList.find(l => l[4] == mountPoint);
};
function isOverlayActive(lowerDir, overlayBase) {
if (!exists(lowerDir)) throw `error: lowerDir missing: ${lowerDir}`;
if (!overlayBase) throw `error: overlayBase is required`;
const overlay = getOverlayDirs(lowerDir, overlayBase);
return isMountedOverlay(overlay.mount);
}
function startOverlayfs(lowerDir, overlayBase) {
if (!exists(lowerDir)) throw `error: lowerDir missing: ${lowerDir}`;
if (!overlayBase) throw `error: overlayBase is required`;
const overlay = getOverlayDirs(lowerDir, overlayBase);
mkdir(overlay.upper, { canExist: true });
mkdir(overlay.work, { canExist: true });
mkdir(overlay.lower, { canExist: true });
// TODO print the "before" mounts. mount | grep store
if (isMountedBind(overlay.lower)) {
console.log(`already mounted: ${overlay.lower}`);
}
else {
// https://superuser.com/questions/1314003/how-can-i-access-the-original-files-the-lowerdir-of-an-overlay-mounted-on-the
exec(`mount --bind ${overlay.mount} ${overlay.lower}`);
exec(`mount --make-private ${overlay.lower}`);
}
if (isMountedOverlay(overlay.mount)) {
console.log(`already mounted: ${overlay.mount}`);
}
else {
const mountOptions = [
`lowerdir=${overlay.hide}`,
`upperdir=${overlay.upper}`,
`workdir=${overlay.work}`,
// fix error: stale file handle
// https://bbs.archlinux.org/viewtopic.php?id=265312
'index=off',
'metacopy=off',
].join(',');
exec(`mount -t overlay overlay -o ${mountOptions} ${overlay.mount}`);
//console.log(`success: mounted overlayfs on ${overlay.mount}`);
console.log([
'success: mounted overlayfs:',
` overlay.hide: ${overlay.hide}`,
` overlay.mount: ${overlay.mount}`,
` overlay.work: ${overlay.work}`,
` overlay.lower: ${overlay.lower}`,
` overlay.upper: ${overlay.upper}`,
].join('\n'));
}
// opaque (non-transparent) upper dirs: https://superuser.com/questions/1553610/possible-to-do-overlay-like-fs-where-dirs-in-the-upper-layer-completely-overshad
console.log('disabling nixos-rebuild. stop the overlay to run nixos-rebuild');
if (!fs.existsSync('/run/current-system/sw/bin/.nixos-rebuild--disabled')) {
rename('/run/current-system/sw/bin/nixos-rebuild', '/run/current-system/sw/bin/.nixos-rebuild--disabled');
}
else {
// here we can patch nixos-rebuild, because the overlay is already mounted.
// nixos-rebuild lives in
// /run/current-system/sw -> /nix/store/ *-system-path
// so the patched nixos-rebuild goes to /b/nix/store
const nixosRebuildDummy = [
'#! /usr/bin/env bash',
'',
"cat <<'EOF'",
'nixos-rebuild was disabled by writable-nix-store.js',
'',
'the original file is in /run/current-system/sw/bin/.nixos-rebuild--disabled',
'',
'nixos-rebuild is the one command',
'that you do *not* want to run',
'while the overlay is active',
'',
'if you do run `nixos-rebuild switch`',
'and then stop the overlay, your system is broken,',
'because many symlink targets have moved',
'from /nix/store to /b/nix/store.',
'then you need a hard reboot,',
'and boot a previous generation of your nixos config',
'',
'to run nixos-rebuild, first stop the overlay by running',
`sudo node ${process.argv[1]} stop`,
'',
'to manually stop the overlay, you can run',
`sudo umount --lazy -t overlay ${overlay.mount}`,
`sudo umount --lazy ${overlay.lower}`,
'EOF',
].join('\n') + '\n';
fs.writeFileSync('/run/current-system/sw/bin/nixos-rebuild', nixosRebuildDummy, 'utf8');
fs.chmodSync('/run/current-system/sw/bin/nixos-rebuild', 0o555); // chmod +x
}
}
function stopOverlayfs(lowerDir, overlayBase) {
if (!exists(lowerDir)) throw `error: orig dir missing: ${lowerDir}`;
if (!overlayBase) throw `error: argument overlayBase is required`;
const overlay = getOverlayDirs(lowerDir, overlayBase);
if (isMountedOverlay(overlay.mount)) {
// need "--lazy" to fix "umount: /nix/store: target is busy."
// https://stackoverflow.com/a/19969471/10440128
exec(`umount --lazy -t overlay ${overlay.mount}`);
console.log(`success: stopped overlayfs on ${overlay.mount}`);
}
else {
console.log(`not mounted: ${overlay.mount}`);
}
if (isMountedBind(overlay.lower)) {
// need "--lazy" to fix "umount: /nix/store: target is busy."
// https://stackoverflow.com/a/19969471/10440128
exec(`umount --lazy ${overlay.lower}`);
console.log(`success: unmounted lower from ${overlay.lower}`);
}
else {
console.log(`not mounted: ${overlay.lower}`);
}
console.log(`overlay stopped`);
console.log(``);
console.log(`you may need to run this to fix your store:`);
console.log(`sudo nix-store --verify --repair`);
console.log(``);
console.log(`to remove the patched files:`);
console.log(`sudo rm -rf /a /b`);
/*
not needed. the patched nixos-rebuild is now in /b/nix/store
if (!fs.existsSync('/run/current-system/sw/bin/nixos-rebuild--disabled')) {
console.log('already enabled: nixos-rebuild');
}
else {
console.log('enabling nixos-rebuild');
rename('/run/current-system/sw/bin/nixos-rebuild--disabled', '/run/current-system/sw/bin/nixos-rebuild');
}
*/
}
function statusOverlayfs(lowerDir, overlayBase) {
const overlay = getOverlayDirs(lowerDir, overlayBase);
console.log('overlay ' + (isMountedOverlay(overlay.mount) ? 'on' : 'off'));
console.log('bind ' + (isMountedBind(overlay.lower) ? 'on' : 'off'));
}
// finally!
main(process.argv.slice(1)); // arg0 is node