1
1
const fs = require ( 'fs' )
2
2
const resolve = require ( 'path' ) . resolve
3
3
4
- const each = require ( 'async/each' )
5
- const jocker = require ( 'jocker' )
6
4
const prompt = require ( 'prompt' )
7
- const rimraf = require ( 'rimraf' ) . sync
8
5
const utils = require ( 'nodeos-mount-utils' )
9
6
10
- const jocker_root = require ( './jocker_root ' )
7
+ const prepareSessions = require ( './sessions ' )
11
8
12
9
const flags = utils . flags
13
10
const MS_NODEV = flags . MS_NODEV
14
11
const MS_NOSUID = flags . MS_NOSUID
15
12
16
13
17
- const HOME = '/tmp'
14
+ const MOUNTPOINT = '/tmp'
18
15
19
16
20
17
/**
@@ -28,17 +25,6 @@ function onerror(error)
28
25
utils . startRepl ( 'NodeOS-mount-filesystems' )
29
26
}
30
27
31
- /**
32
- * Filter folders that are valid user `$HOME`
33
- * @access private
34
- * @param {String } user The name of the user
35
- * @return {Boolean } Returns true If the first char is not a dot
36
- * and not `root` and not ´lost+found´
37
- */
38
- function filterUser ( user )
39
- {
40
- return user [ 0 ] !== '.' && user !== 'root' && user !== 'lost+found'
41
- }
42
28
43
29
/**
44
30
* This helper waits with a limit of tries until the path exists
@@ -82,89 +68,10 @@ function askLocation(error)
82
68
} )
83
69
}
84
70
85
- /**
86
- * If the `single` key is set in the cmdline it starts a admin repl
87
- * If not it just overlays the users filesystem
88
- * @access private
89
- * @param {String } usersFolder The path to folder of the users
90
- */
91
- function adminOrUsers ( usersFolder )
92
- {
93
- function done ( error )
94
- {
95
- // Remove the modules from initramfs to free memory
96
- // rimraf('/lib/node_modules')
97
- rimraf ( '/lib/node_modules/jocker' )
98
-
99
- // Make '/usr' a opaque folder (OverlayFS feature)
100
- rimraf ( '/usr' )
101
-
102
- if ( error ) onerror ( error )
103
- }
104
-
105
- // Mount users directories and exec their init files
106
- fs . readdir ( usersFolder , function ( error , users )
107
- {
108
- if ( error ) return done ( error )
109
-
110
- each ( users . filter ( filterUser ) , function ( username , callback )
111
- {
112
- jocker . run ( usersFolder + '/' + username , '/init' , { PATH : '/bin' } , callback )
113
- } ,
114
- done )
115
- } )
116
- }
117
-
118
- /**
119
- * Prepares the session and checks if the users filesystem has a root account,
120
- * if not check if `/proc/cmdline` has the single key
121
- * It deletes the `root`, `rootfstype` and `vga` environment variables
122
- * and adds `NODE_PATH` to it.
123
- * @access private
124
- * @return {Repl } Returns either a repl or a error if the error contains
125
- * a `ENOENT` code
126
- */
127
- function prepareSessions ( )
128
- {
129
- // Update environment variables
130
- var env = process . env
131
-
132
- delete env [ 'root' ]
133
- delete env [ 'rootfstype' ]
134
- delete env [ 'vga' ]
135
-
136
- env [ 'NODE_PATH' ] = '/lib/node_modules'
137
-
138
- const upperdir = HOME + '/root'
139
-
140
- // Check if users filesystem has an administrator account
141
- fs . readdir ( upperdir , function ( error )
142
- {
143
- if ( error )
144
- {
145
- if ( error . code !== 'ENOENT' ) return onerror ( error )
146
-
147
- return adminOrUsers ( HOME )
148
- }
149
71
150
- // There's an administrator account, prepare it first
151
- jocker_root . create ( upperdir , function ( error , newHome )
152
- {
153
- if ( error ) return onerror ( error )
154
-
155
- // Enter administrator mode
156
- if ( exports . single ) return utils . startRepl ( 'Administrator mode' )
157
-
158
- // Execute `root` user init in un-priviledged environment
159
- jocker . exec ( HOME , '/init' , { PATH : '/bin' } , function ( error )
160
- {
161
- if ( error ) console . warn ( error )
162
-
163
- adminOrUsers ( newHome )
164
- } )
165
- } )
166
- } )
167
- }
72
+ //
73
+ // Public API
74
+ //
168
75
169
76
/**
170
77
* This function mounts the userfs
@@ -181,12 +88,22 @@ function prepareSessions()
181
88
*/
182
89
function mountUsersFS ( cmdline )
183
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
+
184
101
// Allow to override or disable `usersDev`
185
- var usersDev = process . env . root
102
+ var usersDev = env [ ' root' ]
186
103
if ( usersDev === undefined ) usersDev = cmdline . root
187
104
188
105
// Running on a container (Docker, vagga), don't mount the users filesystem
189
- if ( usersDev === 'container' ) return prepareSessions ( )
106
+ if ( usersDev === 'container' ) return prepareSessions ( MOUNTPOINT , single , done )
190
107
191
108
// Running on real hardware or virtual machine, mount the users filesystem
192
109
if ( usersDev )
@@ -195,14 +112,17 @@ function mountUsersFS(cmdline)
195
112
if ( error ) return askLocation . call ( cmdline , error )
196
113
197
114
// Mount users filesystem
198
- var type = process . env . rootfstype || cmdline . rootfstype || 'auto'
115
+ var type = env [ ' rootfstype' ] || cmdline . rootfstype || 'auto'
199
116
var extras = { errors : 'remount-ro' , devFile : resolve ( usersDev ) }
200
117
201
- utils . mkdirMount ( HOME , type , MS_NODEV | MS_NOSUID , extras , function ( error )
118
+ utils . mkdirMount ( MOUNTPOINT , type , MS_NODEV | MS_NOSUID , extras , function ( error )
202
119
{
203
120
if ( error ) return onerror ( error )
204
121
205
- prepareSessions ( )
122
+ delete env [ 'root' ]
123
+ delete env [ 'rootfstype' ]
124
+
125
+ prepareSessions ( MOUNTPOINT , single , done )
206
126
} )
207
127
} )
208
128
@@ -218,5 +138,4 @@ function mountUsersFS(cmdline)
218
138
}
219
139
220
140
221
- exports . mountUsersFS = mountUsersFS
222
- exports . single = false
141
+ module . exports = mountUsersFS
0 commit comments