-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
executable file
·115 lines (88 loc) · 3.04 KB
/
index.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
#!/usr/bin/env node
var chroot = require('posix').chroot;
var rimraf = require('rimraf').sync;
var mount = require('nodeos-mount');
var utils = require('nodeos-mount-utils');
const pathRootfs = '/.rootfs';
const pathOverlay = '/.overlay';
function onerror(error)
{
// Error mounting the root filesystem or executing init, enable REPL
console.trace(error)
utils.startRepl('NodeOS-mount-rootfs')
}
function onerror_nodev(error)
{
if(error) console.warn(error);
}
function overlayfsroot(envDev)
{
var flags = mount.MS_NODEV | mount.MS_NOSUID;
// Mount root filesystem
var type = process.env.ROOTFSTYPE || 'auto';
var extras = {errors: 'remount-ro'};
utils.mountfs(envDev, pathRootfs, type, flags, extras, function(error)
{
if(error) return onerror(error)
// Craft overlayed filesystem
var type = 'overlay';
// var extras = {lowerdir: pathRootfs};
var extras =
{
lowerdir: '/',
upperdir: pathRootfs+'/rootfs',
workdir : pathRootfs+'/workdir'
};
utils.mkdirMount('', pathOverlay, type, extras, function(error)
// utils.mkdirMount('', pathOverlay, type, mount.MS_RDONLY, extras, function(error)
{
if(error) return onerror(error)
var path = '/';
// Re-mount initram as read-only
var flags = mount.MS_REMOUNT | mount.MS_RDONLY;
mount.mount('', path, flags, function(error)
{
if(error)
{
console.error('Error re-mounting '+path+' as read-only')
// return onerror(error)
}
// Move kernel filesystems to overlayed filesystem
mount.mountSync('/dev' , pathOverlay+'/dev' , mount.MS_MOVE);
mount.mountSync('/proc', pathOverlay+'/proc', mount.MS_MOVE);
// mount.mountSync('/sys' , pathOverlay+'/sys' , mount.MS_MOVE);
mount.mountSync('/tmp' , pathOverlay+'/tmp' , mount.MS_MOVE);
// Move overlayed filesytem
process.chdir(pathOverlay)
mount.mount('.', path, mount.MS_MOVE, function(error)
{
if(error)
{
console.error('Error moving overlayed filesystem to '+path)
return onerror(error)
}
chroot('.')
// Execute init
utils.execInit('/root', process.argv.slice(2), onerror)
// utils.execInit(path, process.argv.slice(2), onerror)
});
});
});
});
}
// Change umask system wide so new files are accesible ONLY by its owner
process.umask(0066);
// Remove from rootfs the files only needed on boot to free memory
rimraf('/bin/century')
rimraf('/bin/nodeos-mount-rootfs')
rimraf('/init')
rimraf('/lib/node_modules')
rimraf('/sbin')
// Mount kernel filesystems
var flags = mount.MS_NODEV | mount.MS_NOEXEC | mount.MS_NOSUID
utils.mkdirMount('udev' , '/dev' , 'devtmpfs', {mode: 0755}, onerror_nodev)
utils.mkdirMount('proc' , '/proc', 'proc' , flags, onerror_nodev)
// utils.mkdirMount('sysfs', '/sys', 'sysfs', flags, onerror_nodev)
utils.mkdirMount('tmpfs', '/tmp' , 'tmpfs' , flags, {mode: 1777}, onerror_nodev)
// Mount root filesystem
overlayfsroot('ROOT')