Skip to content

Commit 5535896

Browse files
committed
Split sessions specific code to its own file
1 parent bc30dfa commit 5535896

File tree

3 files changed

+137
-112
lines changed

3 files changed

+137
-112
lines changed

lib/index.js

Lines changed: 24 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
const fs = require('fs')
22
const resolve = require('path').resolve
33

4-
const each = require('async/each')
5-
const jocker = require('jocker')
64
const prompt = require('prompt')
7-
const rimraf = require('rimraf').sync
85
const utils = require('nodeos-mount-utils')
96

10-
const jocker_root = require('./jocker_root')
7+
const prepareSessions = require('./sessions')
118

129
const flags = utils.flags
1310
const MS_NODEV = flags.MS_NODEV
1411
const 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
*/
18289
function 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

lib/sessions.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const fs = require('fs')
2+
3+
const each = require('async/each')
4+
const jocker = require('jocker')
5+
const rimraf = require('rimraf').sync
6+
const startRepl = require('nodeos-mount-utils').startRepl
7+
8+
const jocker_root = require('./jocker_root')
9+
10+
11+
/**
12+
* Filter folders that are valid user `$HOME`
13+
* @access private
14+
* @param {String} user The name of the user
15+
* @return {Boolean} Returns true If the first char is not a dot
16+
* and not `root` and not ´lost+found´
17+
*/
18+
function filterUser(user)
19+
{
20+
return user[0] !== '.' && user !== 'root' && user !== 'lost+found'
21+
}
22+
23+
/**
24+
* Overlays the users filesystem
25+
*
26+
* @param {String} usersFolder The path to folder of the users
27+
* @param {Function} callback
28+
*/
29+
function usersSessions(usersFolder, callback)
30+
{
31+
function done(error)
32+
{
33+
// Remove the modules from initramfs to free memory
34+
// rimraf('/lib/node_modules')
35+
rimraf('/lib/node_modules/jocker')
36+
37+
// Make '/usr' a opaque folder (OverlayFS feature)
38+
rimraf('/usr')
39+
40+
callback(error)
41+
}
42+
43+
// Mount users directories and exec their init files
44+
fs.readdir(usersFolder, function(error, users)
45+
{
46+
if(error) return done(error)
47+
48+
each(users.filter(filterUser), function(username, callback)
49+
{
50+
jocker.run(usersFolder+'/'+username, '/init', {PATH: '/bin'}, callback)
51+
},
52+
done)
53+
})
54+
}
55+
56+
57+
//
58+
// Public API
59+
//
60+
61+
/**
62+
* Prepares the session and checks if the users filesystem has a root account,
63+
* if not check if `/proc/cmdline` has the single key
64+
* It deletes the `root`, `rootfstype` and `vga` environment variables
65+
* and adds `NODE_PATH` to it.
66+
* @access private
67+
* @return {Repl} Returns either a repl or a error if the error contains
68+
* a `ENOENT` code
69+
*/
70+
function prepareSessions(home, single, callback)
71+
{
72+
const upperdir = home+'/root'
73+
74+
// Check if users filesystem has an administrator account
75+
fs.readdir(upperdir, function(error)
76+
{
77+
if(error)
78+
{
79+
if(error.code !== 'ENOENT') return callback(error)
80+
81+
return usersSessions(home, callback)
82+
}
83+
84+
// There's an administrator account, prepare it first
85+
jocker_root.create(upperdir, function(error, newHome)
86+
{
87+
if(error) return callback(error)
88+
89+
// Enter administrator mode
90+
if(single) return startRepl('Administrator mode')
91+
92+
// Execute `root` user init in un-priviledged environment
93+
jocker.exec(home, '/init', {PATH: '/bin'}, function(error)
94+
{
95+
if(error) console.warn(error)
96+
97+
usersSessions(newHome, callback)
98+
})
99+
})
100+
})
101+
}
102+
103+
104+
module.exports = prepareSessions

server.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const eachOf = require('async/eachOf')
66
const mkdirp = require('mkdirp')
77
const rimraf = require('rimraf').sync
88

9-
const nmf = require('.')
9+
const mountUsersFS = require('.')
1010

1111

1212
/**
@@ -65,7 +65,7 @@ rimraf('/sbin')
6565
// Symlinks for config data optained from `procfs`
6666
mkdirp('/etc', '0100', function(error)
6767
{
68-
if(error && error.code != 'EEXIST') throw error
68+
if(error && error.code !== 'EEXIST') throw error
6969

7070
const symlinks =
7171
{
@@ -86,16 +86,18 @@ mkdirp('/etc', '0100', function(error)
8686
{
8787
if(error) throw error
8888

89+
// Update environment variables
90+
var env = process.env
91+
delete env['vga']
92+
env['NODE_PATH'] = '/lib/node_modules'
93+
94+
// Get Linux kernel command line arguments
8995
fs.readFile('/proc/cmdline', 'utf8', function(error, data)
9096
{
9197
if(error) throw error
9298

93-
var cmdline = linuxCmdline(data)
94-
95-
nmf.single = cmdline.single
96-
9799
// Mount users filesystem
98-
nmf.mountUsersFS(cmdline)
100+
mountUsersFS(linuxCmdline(data))
99101
})
100102
})
101103
})

0 commit comments

Comments
 (0)