11const fs = require ( 'fs' )
22const resolve = require ( 'path' ) . resolve
33
4- const each = require ( 'async/each' )
5- const jocker = require ( 'jocker' )
64const prompt = require ( 'prompt' )
7- const rimraf = require ( 'rimraf' ) . sync
85const utils = require ( 'nodeos-mount-utils' )
96
10- const jocker_root = require ( './jocker_root ' )
7+ const prepareSessions = require ( './sessions ' )
118
129const flags = utils . flags
1310const MS_NODEV = flags . MS_NODEV
1411const MS_NOSUID = flags . MS_NOSUID
1512
1613
17- const HOME = '/tmp'
14+ const MOUNTPOINT = '/tmp'
1815
1916
2017/**
@@ -28,17 +25,6 @@ function onerror(error)
2825 utils . startRepl ( 'NodeOS-mount-filesystems' )
2926}
3027
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- }
4228
4329/**
4430 * This helper waits with a limit of tries until the path exists
@@ -82,89 +68,10 @@ function askLocation(error)
8268 } )
8369}
8470
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- }
14971
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+ //
16875
16976/**
17077 * This function mounts the userfs
@@ -181,12 +88,22 @@ function prepareSessions()
18188 */
18289function mountUsersFS ( cmdline )
18390{
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+
184101 // Allow to override or disable `usersDev`
185- var usersDev = process . env . root
102+ var usersDev = env [ ' root' ]
186103 if ( usersDev === undefined ) usersDev = cmdline . root
187104
188105 // 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 )
190107
191108 // Running on real hardware or virtual machine, mount the users filesystem
192109 if ( usersDev )
@@ -195,14 +112,17 @@ function mountUsersFS(cmdline)
195112 if ( error ) return askLocation . call ( cmdline , error )
196113
197114 // Mount users filesystem
198- var type = process . env . rootfstype || cmdline . rootfstype || 'auto'
115+ var type = env [ ' rootfstype' ] || cmdline . rootfstype || 'auto'
199116 var extras = { errors : 'remount-ro' , devFile : resolve ( usersDev ) }
200117
201- utils . mkdirMount ( HOME , type , MS_NODEV | MS_NOSUID , extras , function ( error )
118+ utils . mkdirMount ( MOUNTPOINT , type , MS_NODEV | MS_NOSUID , extras , function ( error )
202119 {
203120 if ( error ) return onerror ( error )
204121
205- prepareSessions ( )
122+ delete env [ 'root' ]
123+ delete env [ 'rootfstype' ]
124+
125+ prepareSessions ( MOUNTPOINT , single , done )
206126 } )
207127 } )
208128
@@ -218,5 +138,4 @@ function mountUsersFS(cmdline)
218138}
219139
220140
221- exports . mountUsersFS = mountUsersFS
222- exports . single = false
141+ module . exports = mountUsersFS
0 commit comments