-
Notifications
You must be signed in to change notification settings - Fork 1
/
tftpmain.js
186 lines (164 loc) · 4.08 KB
/
tftpmain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env node
/* vim: set ts=8 sts=8 sw=8 noet: */
var mod_tftp = require('tftp');
var mod_fs = require('fs');
var mod_path = require('path');
var TFTP;
/*
* This is where we will get the platforms, overlay files, grub, etc:
*/
var ROOT = mod_path.join(__dirname, 'data');
function
log(msg)
{
var dts = (new Date()).toLocaleTimeString();
console.error('[' + dts + '] ' + msg);
}
function
walk_overlay_dir(root, dir)
{
var out = [];
var ents = mod_fs.readdirSync(mod_path.join(root, dir));
for (var i = 0; i < ents.length; i++) {
var ent = ents[i];
var st = mod_fs.statSync(mod_path.join(root, dir,
ent));
if (st.isFile()) {
out.push(mod_path.join(dir, ent));
} else if (st.isDirectory()) {
out = out.concat(walk_overlay_dir(root,
mod_path.join(dir, ent)));
}
}
return (out);
}
function
build_menu_lst()
{
var out = [];
var plats = [];
var ents = mod_fs.readdirSync(ROOT);
for (var i = 0; i < ents.length; i++) {
var m = ents[i].match(/^platform-(.*)$/);
if (m) {
plats.push(m[1]);
}
}
plats.sort().reverse();
var overlays = walk_overlay_dir(mod_path.join(ROOT, 'overlay'), '');
/*
* Hash of the password 'root':
*/
var root_shadow = '$5$hEQ0l8d5$s0Jwt.oif76hVoQpzsgH2XVKhS' +
'8uCXnMlQhXltYgvaB';
var emit_plat = function (name, extra_string, opts) {
out.push('title SmartOS (' + name + ') ' + extra_string);
/*
* Add the Kernel for this platform:
*/
out.push(' kernel$ /os/' + name +
'/platform/i86pc/kernel/amd64/unix' +
' -B console=${os_console},' +
'${os_console}-mode="115200,8,n,1,-",' +
opts.join(','));
/*
* Add the Boot RAM Disk:
*/
out.push(' module$ /os/' + name +
'/platform/i86pc/amd64/boot_archive' +
' type=rootfs name=ramdisk');
out.push(' module$ /os/' + name +
'/platform/i86pc/amd64/boot_archive.hash' +
' type=hash name=ramdisk');
/*
* Add each of the overlay files:
*/
for (var j = 0; j < overlays.length; j++) {
out.push(' module$ /overlay/' + overlays[j] +
' type=file name=' + overlays[j]);
}
out.push('');
};
out.push('default 0');
out.push('timeout 15');
out.push('serial --speed=115200 --unit=1 --word=8 --parity=no ' +
'--stop=1');
out.push('terminal composite');
out.push('variable os_console vga');
out.push('');
for (var i = 0; i < plats.length; i++) {
emit_plat(plats[i], '', [
'smartos=true',
'root_shadow=' + root_shadow
]);
emit_plat(plats[i], 'noinstall', [
'noimport=true',
'root_shadow=' + root_shadow
]);
}
return (out.join('\n') + '\n\n');
}
function
make_path(path)
{
return (mod_path.join(ROOT, path));
}
function
file_size(path)
{
try {
var st = mod_fs.statSync(path);
return (st.size);
} catch (ex) {
log('ERROR: file_size: ' + ex.stack);
return (false);
}
}
var PLATFILE_RE = new RegExp('^/?os/([^/]+)/platform/(.*)$');
var OVERLAY_RE = new RegExp('^/?overlay/(.*)$');
(function
main(argv)
{
log('smartos-pxe starting up');
TFTP = mod_tftp.createServer({
host: '0.0.0.0',
/*port: 69,*/
denyPUT: true
}, function (req, res) {
log(req.method + ' ' + req.file + ' (' +
req.stats.remoteAddress + ')');
req.on('error', function (err) {
//log('ERROR: aborted req: ' + err.message);
});
var m;
if (req.file === 'bootfile') {
var path = make_path('grub/pxegrub');
var size = file_size(path);
res.setSize(size);
mod_fs.createReadStream(path).pipe(res);
} else if (req.file.match(/^menu.lst.*/)) {
var buf = new Buffer(build_menu_lst());
res.setSize(buf.length);
res.write(buf);
res.end();
} else if (m = OVERLAY_RE.exec(req.file)) {
var path = make_path('overlay/' + m[1]);
var size = file_size(path);
res.setSize(size);
mod_fs.createReadStream(path).pipe(res);
return;
} else if (m = PLATFILE_RE.exec(req.file)) {
var path = make_path('platform-' + m[1] + '/' +
m[2]);
var size = file_size(path);
res.setSize(size);
mod_fs.createReadStream(path).pipe(res);
return;
} else {
log('WARN: invalid file: ' + req.file);
req.abort();
return;
}
});
TFTP.listen();
})(process.argv.slice(2));