|
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') |
0 commit comments