forked from ottomatica/slim
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
100 lines (79 loc) · 3.13 KB
/
build.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
const child = require('child_process');
const Docker = require('dockerode');
const fs = require('fs-extra');
const path = require('path');
const tar = require('tar');
const { info, ok } = require('./logger');
const env = require('./env');
const { slimdir, scriptdir } = env.vars();
const docker = new Docker();
async function build(context) {
let { buildPath, outputDir, dockerOpts } = context;
let exportDir = path.join(slimdir, 'slim-vm');
await fs.emptyDir(exportDir);
info('building docker image');
await buildImage(buildPath, dockerOpts);
info('exporting docker filesystem');
await exportImage('slim-vm', exportDir);
info('creating initrd');
await fs.move(path.join(exportDir, 'vmlinuz'), path.join(slimdir, 'vmlinuz'), { overwrite: true });
child.execSync(`find . | cpio -o -H newc 2>/dev/null | gzip > ${path.join(slimdir, 'initrd')}`, {cwd: exportDir, stdio: 'inherit'});
info('copying initrd and vmlinuz');
await fs.copy(path.join(slimdir, 'initrd'), path.join(outputDir, 'initrd'));
await fs.copy(path.join(slimdir, 'vmlinuz'), path.join(outputDir, 'vmlinuz'));
info('creating microkernel');
await buildIso(path.join(outputDir, 'slim.iso'));
ok('success!');
}
async function buildIso(outputPath) {
let isoDir = path.join(slimdir, 'slim-iso')
let bootDir = path.join(isoDir, 'boot');
let isolinuxDir = path.join(isoDir, 'isolinux');
await Promise.all([
fs.emptyDir(isoDir),
fs.emptyDir(bootDir),
fs.emptyDir(isolinuxDir)
]);
await fs.copy(path.join(scriptdir, 'scripts', 'syslinux'), isolinuxDir);
await fs.copy(path.join(slimdir, 'vmlinuz'), path.join(bootDir, 'vmlinuz'));
await fs.copy(path.join(slimdir, 'initrd'), path.join(bootDir, 'initrd'));
child.execSync(`
mkisofs -o ${outputPath} \
-b isolinux/isolinux.bin \
-c isolinux/boot.cat \
-no-emul-boot -boot-load-size 4 -boot-info-table \
-V slim -J -R ${isoDir}`, {stdio: 'inherit'});
child.execSync(`ls -la ${outputPath}`, {stdio: 'inherit'});
}
async function buildImage(dockerfilePath, dockerOpts) {
if (!fs.existsSync(path.join(dockerfilePath, 'Dockerfile'))) throw new Error(`Expected Dockerfile in ${dockerfilePath}`);
const image = await docker.buildImage({ context: dockerfilePath }, {
t: 'slim-vm',
...dockerOpts
});
await new Promise((resolve, reject) => {
docker.modem.followProgress(
image,
(err, res) => err ? reject(err) : resolve(res),
ev => process.stdout.write(ev.stream)
);
});
}
async function exportImage(image, outdir) {
const container = await docker.createContainer({ Image: image, Cmd: ['sh'] });
const contents = await container.export();
try {
await new Promise((resolve, reject) => {
contents.pipe(
tar.x({ C: outdir })
.on('close', resolve)
.on('error', err => reject(err))
);
});
} catch (e) {
throw e;
} finally {
container.remove().catch(() => undefined);
}
}
module.exports = build;