Skip to content

Commit

Permalink
Unified execution code for test and start scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
piranna committed Jun 11, 2017
1 parent 8ac3de1 commit 6749a66
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 201 deletions.
127 changes: 127 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* NodeOS
*
* @copyright 2013-2017 Jacob Groundwater, Jesús Leganés-Combarro 'piranna'
* and other contributors
*
* @license MIT
*/

const fs = require('fs')
const join = require('path').join

const processArguments = require('nodeos-barebones/scripts/processArguments')


function prepareCommandLine(argv, output)
{
const args = processArguments(argv)
const link = fs.readlinkSync('out/latest').split('/') || []

const cpu_family = args.cpu_family || link[link.length-3]
const machine = args.machine || link[link.length-2]
const platform = args.platform || link[link.length-1]

const cpu = args.cpu

let command = 'qemu-system-'+cpu_family

argv =
[
'-machine', machine,
'-m', '256M',
'-vga', 'std',
'-net', 'nic',
'-net', 'user,id=eth0,hostfwd=tcp::50080-:80',
'-net', 'user,id=eth1,hostfwd=tcp::50443-:443'
]

switch(output)
{
case 'curses' : argv.push('-curses') ; break
case 'nographic': argv.push('-nographic'); break
}

// check if kvm is supported
let contents = ""

try
{
contents = fs.readFileSync('/proc/cpuinfo');
} catch(e){
if(e.code !== 'ENOENT') throw(e)
}

if(/(vmx|svm)/.test(contents) !== false) argv.push('-enable-kvm')

// CWD
const cwd = join('out', cpu_family, machine, platform)

switch(platform)
{
case 'disk':
argv.push('-hda', 'disk.img')
break

case 'docker':
command = 'docker'
argv =
[
'run', '-it',
'--cap-add', 'SYS_ADMIN',
'--security-opt=apparmor:unconfined',
'--device', '/dev/fuse',
'nodeos/nodeos',
// '-v', 'usersfs:/tmp'
]
break

case 'img':
argv.push('-hda', 'bootfs.img',
'-hdb', 'usersfs.img')
break

case 'iso':
argv.push('-cdrom', 'bootfs.iso',
'-hda' , 'usersfs.img')
break

case 'qemu':
const append =
[
'root=/dev/sda',
'ip=dhcp'
]

switch(output)
{
case 'nographic':
append.push('console=ttyS0') // redirect to terminal
break

case 'curses':
append.push('vga=extended') // 80x50
break

default:
append.push('vga=0x344') // 1024x768x32
}

argv.push('--kernel', 'kernel',
'--initrd', 'initramfs.cpio.gz',
'-drive', `file=usersfs.img,format=raw,index=0`,
'-append', append.join(' '))
break

case 'tar':
case 'vagga': break

default:
throw 'Unknown platform "'+platform+'"'
}

return {command, argv, cwd, platform}
}


module.exports = prepareCommandLine
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "NodeOS",
"version": "1.0.0-RC3",
"description": "Lightweight operating system using Node.js as userspace",
"main": "lib",
"scripts": {
"BigRedButton": "scripts/BigRedButton",
"docker": "scripts/docker",
"dockerBuild": "scripts/dockerBuild",
"dockerClean": "docker volume rm $(docker volume ls -qf dangling=true) || exit 0",
"build": "scripts/build",
Expand Down
15 changes: 0 additions & 15 deletions scripts/docker

This file was deleted.

103 changes: 4 additions & 99 deletions scripts/start
Original file line number Diff line number Diff line change
Expand Up @@ -9,111 +9,16 @@
* @license MIT
*/

var fs = require('fs')
var join = require('path').join
var spawn = require('child_process').spawn
const spawn = require('child_process').spawn

const prepareCommandLine = require('..')

var link = fs.readlinkSync('out/latest').split('/');

var cpu_family = link[link.length-3]
var machine = link[link.length-2]
var platform = link[link.length-1]


var command = 'qemu-system-'+cpu_family

var args =
[
'-machine', machine,
'-enable-kvm',
'-vga', 'std',
'-m', '256M',
'-net', 'nic',
'-net', 'user,id=eth0,hostfwd=tcp::50080-:80',
'-net', 'user,id=eth1,hostfwd=tcp::50443-:443'
]

switch(process.argv[2])
{
case 'curses' : args.push('-curses') ; break
case 'nographic': args.push('-nographic'); break
}

const outDir = join('out', cpu_family, machine, platform)
switch(platform)
{
case 'disk':
args.push('-hda', join(outDir, 'disk.img'))
break

case 'docker':
command = 'scripts/docker'
args = []
break

case 'img':
args.push('-hda', join(outDir, 'bootfs.img'),
'-hdb', join(outDir, 'usersfs.img'))
break

case 'iso':
args.push('-cdrom', join(outDir, 'bootfs.iso'),
'-hda' , join(outDir, 'usersfs.img'))
break

case 'qemu':
var append =
[
'root=/dev/sda',
'ip=dhcp'
]

switch(process.argv[2])
{
case 'nographic':
append.push('console=ttyS0') // redirect to terminal
break

case 'curses':
append.push('vga=extended') // 80x50
break

default:
append.push('vga=0x344') // 1024x768x32
}

args.push('--kernel', join(outDir, 'kernel'),
'--initrd', join(outDir, 'initramfs.cpio.gz'),
'-drive', `file=${join(outDir, 'usersfs.img')},format=raw,index=0`,
'-append', append.join(' '))
break

default:
throw 'Unknown platform "'+platform+'"'
}

// check if kvm is supported
var kvm = /(vmx|svm)/;
var contents = "";

try
{
contents = fs.readFileSync('/proc/cpuinfo');
} catch(e){
throw(e);
}

if(kvm.test(contents) === false)
{
// disable kvm
console.log("Your computer does not support kvm. NodeOS will be slower without it.");
args.shift();
}
const {command, argv, cwd} = prepareCommandLine(process.argv.slice(2))


// Exec NodeOS
spawn(command, args, {stdio: 'inherit'})
spawn(command, argv, {stdio: 'inherit', cwd})
.on('error', console.trace.bind(console))
.on('exit', function(code, signal)
{
Expand Down
Loading

0 comments on commit 6749a66

Please sign in to comment.