Skip to content

Commit 4d21071

Browse files
committed
Put basic environment on it's own file
1 parent bd4fa38 commit 4d21071

File tree

4 files changed

+199
-183
lines changed

4 files changed

+199
-183
lines changed

lib/basicEnvironment.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const symlink = require('fs').symlink
2+
3+
const eachOf = require('async/eachOf')
4+
const mkdirp = require('mkdirp')
5+
const rimraf = require('rimraf').sync
6+
7+
8+
function basicEnvironment(callback)
9+
{
10+
// Change umask system wide so new files are accesible ONLY by its owner
11+
process.umask(0066)
12+
13+
// Remove from initramfs the files only needed on boot to free memory
14+
rimraf('/bin/nodeos-mount-filesystems')
15+
rimraf('/init')
16+
rimraf('/lib/node_modules/nodeos-mount-filesystems')
17+
rimraf('/sbin')
18+
19+
// Symlinks for config data optained from `procfs`
20+
mkdirp('/etc', '0100', function(error)
21+
{
22+
if(error && error.code !== 'EEXIST') return callback(error)
23+
24+
const symlinks =
25+
{
26+
'/proc/mounts': '/etc/mtab',
27+
'/proc/net/pnp': '/etc/resolv.conf'
28+
}
29+
30+
eachOf(symlinks, function(dest, src, callback)
31+
{
32+
symlink(src, dest, function(error)
33+
{
34+
if(error && error.code !== 'EEXIST') return callback(error)
35+
36+
callback()
37+
})
38+
},
39+
function(error)
40+
{
41+
if(error) return callback(error)
42+
43+
// Update environment variables
44+
var env = process.env
45+
delete env['vga']
46+
env['NODE_PATH'] = '/lib/node_modules'
47+
48+
callback()
49+
})
50+
})
51+
}
52+
53+
54+
module.exports = basicEnvironment

lib/index.js

Lines changed: 3 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,3 @@
1-
const fs = require('fs')
2-
const resolve = require('path').resolve
3-
4-
const prompt = require('prompt')
5-
const utils = require('nodeos-mount-utils')
6-
7-
const prepareSessions = require('./sessions')
8-
9-
const flags = utils.flags
10-
const MS_NODEV = flags.MS_NODEV
11-
const MS_NOSUID = flags.MS_NOSUID
12-
13-
14-
const MOUNTPOINT = '/tmp'
15-
16-
17-
/**
18-
* This error handler traces the error and starts a node.js repl
19-
* @access private
20-
* @param {Error} error The error that gets traced
21-
*/
22-
function onerror(error)
23-
{
24-
console.trace(error)
25-
utils.startRepl('NodeOS-mount-filesystems')
26-
}
27-
28-
29-
/**
30-
* This helper waits with a limit of tries until the path exists
31-
* @access private
32-
* @param {String} path The path to check for
33-
* @param {Number} tries A limit of tries
34-
* @param {Function} callback The callback function
35-
* @return {Function} Returns the callback with either nothing
36-
* or with a error
37-
*/
38-
function waitUntilExists(path, tries, callback)
39-
{
40-
fs.exists(path, function(exists)
41-
{
42-
if(exists) return callback()
43-
44-
if(tries-- <= 0) return callback(new Error(path+' not exists'))
45-
46-
setTimeout(waitUntilExists, 1000, path, tries, callback)
47-
})
48-
}
49-
50-
/**
51-
* Starts a prompt and asks for the location of the userfs
52-
* @access private
53-
* @param {Error} error The error will be printed in the console
54-
*/
55-
function askLocation(error)
56-
{
57-
console.warn('Could not find userfs:', error)
58-
59-
var self = this
60-
61-
prompt.start()
62-
prompt.get('path to userfs', function(error, result)
63-
{
64-
if(error) console.warn(error)
65-
66-
self.root = result['path to userfs']
67-
return mountUsersFS(self)
68-
})
69-
}
70-
71-
72-
//
73-
// Public API
74-
//
75-
76-
/**
77-
* This function mounts the userfs
78-
* if the root env variable contains `container` it prepares the session
79-
* and if there is no root env var then it awaits the user device and
80-
* then mounts the user device and then prepares the session
81-
* @access private
82-
* @param {Object} cmdline This objects holds key/value pairs from the
83-
* `/proc/cmdline` file
84-
* @return {Prompt|Error} It returns either a prompt if the
85-
* tries has reached its limit or a error
86-
* if the `mkdirMount` fails to create the user
87-
* device
88-
*/
89-
function mountUsersFS(cmdline)
90-
{
91-
function done(error)
92-
{
93-
if(error) onerror(error)
94-
}
95-
96-
97-
const single = cmdline.single
98-
99-
var env = process.env
100-
101-
// Allow to override or disable `usersDev`
102-
var usersDev = env['root']
103-
if(usersDev === undefined) usersDev = cmdline.root
104-
105-
// Running on a container (Docker, vagga), don't mount the users filesystem
106-
if(usersDev === 'container') return prepareSessions(MOUNTPOINT, single, done)
107-
108-
// Running on real hardware or virtual machine, mount the users filesystem
109-
if(usersDev)
110-
waitUntilExists(usersDev, 5, function(error)
111-
{
112-
if(error) return askLocation.call(cmdline, error)
113-
114-
// Mount users filesystem
115-
var type = env['rootfstype'] || cmdline.rootfstype || 'auto'
116-
var extras = {errors: 'remount-ro', devFile: resolve(usersDev)}
117-
118-
utils.mkdirMount(MOUNTPOINT, type, MS_NODEV | MS_NOSUID, extras, function(error)
119-
{
120-
if(error) return onerror(error)
121-
122-
delete env['root']
123-
delete env['rootfstype']
124-
125-
prepareSessions(MOUNTPOINT, single, done)
126-
})
127-
})
128-
129-
// Users filesystem is not defined, launch a Node.js REPL
130-
else
131-
fs.readFile('resources/readonly_warning.txt', 'utf8', function(error, data)
132-
{
133-
if(error) return onerror(error)
134-
135-
console.warn(data)
136-
utils.startRepl('NodeOS-mount-filesystems')
137-
})
138-
}
139-
140-
141-
module.exports = mountUsersFS
1+
exports.basicEnvironment = require('./basicEnvironment')
2+
exports.mountUsersFS = require('./mountUsersFS')
3+
exports.prepareSessions = require('./prepareSessions')

lib/mountUsersFS.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
const fs = require('fs')
2+
const resolve = require('path').resolve
3+
4+
const prompt = require('prompt')
5+
const utils = require('nodeos-mount-utils')
6+
7+
const flags = utils.flags
8+
const MS_NODEV = flags.MS_NODEV
9+
const MS_NOSUID = flags.MS_NOSUID
10+
11+
12+
/**
13+
* This helper waits with a limit of tries until the path exists
14+
* @access private
15+
* @param {String} path The path to check for
16+
* @param {Number} tries A limit of tries
17+
* @param {Function} callback The callback function
18+
* @return {Function} Returns the callback with either nothing
19+
* or with a error
20+
*/
21+
function waitUntilExists(path, tries, callback)
22+
{
23+
fs.exists(path, function(exists)
24+
{
25+
if(exists) return callback()
26+
27+
if(tries-- <= 0) return callback(new Error(path+' not exists'))
28+
29+
setTimeout(waitUntilExists, 1000, path, tries, callback)
30+
})
31+
}
32+
33+
/**
34+
* Starts a prompt and asks for the location of the userfs
35+
* @access private
36+
* @param {Error} error The error will be printed in the console
37+
*/
38+
function askLocation(mountpoint, cmdline, error, callback)
39+
{
40+
console.warn('Could not find userfs:', error)
41+
42+
prompt.start()
43+
prompt.get('path to userfs', function(error, result)
44+
{
45+
if(error) console.warn(error)
46+
47+
cmdline.root = result['path to userfs']
48+
return mountUsersFS(mountpoint, cmdline, callback)
49+
})
50+
}
51+
52+
53+
//
54+
// Public API
55+
//
56+
57+
/**
58+
* This function mounts the userfs
59+
* if the root env variable contains `container` it prepares the session
60+
* and if there is no root env var then it awaits the user device and
61+
* then mounts the user device and then prepares the session
62+
* @access private
63+
* @param {Object} cmdline This objects holds key/value pairs from the
64+
* `/proc/cmdline` file
65+
* @return {Prompt|Error} It returns either a prompt if the
66+
* tries has reached its limit or a error
67+
* if the `mkdirMount` fails to create the user
68+
* device
69+
*/
70+
function mountUsersFS(mountpoint, cmdline, callback)
71+
{
72+
// Get filesystem location & type and clean their environment variables
73+
var env = process.env
74+
75+
var usersDev = env['root']
76+
var type = env['rootfstype'] || cmdline.rootfstype || 'auto'
77+
78+
delete env['root']
79+
delete env['rootfstype']
80+
81+
// Allow to override or disable `usersDev`
82+
if(usersDev === undefined) usersDev = cmdline.root
83+
84+
// Running on a container (Docker, vagga), don't mount the users filesystem
85+
if(usersDev === 'container') return callback()
86+
87+
// Running on real hardware or virtual machine, mount the users filesystem
88+
if(usersDev)
89+
return waitUntilExists(usersDev, 5, function(error)
90+
{
91+
if(error) return askLocation(mountpoint, cmdline, error, callback)
92+
93+
// Mount users filesystem
94+
var extras = {errors: 'remount-ro', devFile: resolve(usersDev)}
95+
96+
utils.mkdirMount(mountpoint, type, MS_NODEV | MS_NOSUID, extras, callback)
97+
})
98+
99+
// Users filesystem is not defined, launch a Node.js REPL
100+
fs.readFile(`${__dirname}/../resources/readonly_warning.txt`, 'utf8',
101+
function(error, data)
102+
{
103+
if(error) return callback(error)
104+
105+
console.warn(data)
106+
utils.startRepl('NodeOS-mount-filesystems')
107+
})
108+
}
109+
110+
111+
module.exports = mountUsersFS

0 commit comments

Comments
 (0)