2017-12-11 22:37:56 +00:00
|
|
|
Promise = require('bluebird')
|
|
|
|
|
2018-10-18 17:54:26 +00:00
|
|
|
exports.QEMU_VERSION = QEMU_VERSION = 'v3.0.0+resin'
|
2017-12-11 22:37:56 +00:00
|
|
|
exports.QEMU_BIN_NAME = QEMU_BIN_NAME = 'qemu-execve'
|
|
|
|
|
2018-10-18 17:54:26 +00:00
|
|
|
exports.installQemuIfNeeded = Promise.method (emulated, logger, arch) ->
|
2017-12-11 22:37:56 +00:00
|
|
|
return false if not (emulated and platformNeedsQemu())
|
|
|
|
|
|
|
|
hasQemu()
|
|
|
|
.then (present) ->
|
|
|
|
if !present
|
2018-10-18 17:54:26 +00:00
|
|
|
logger.logInfo("Installing qemu for #{arch} emulation...")
|
|
|
|
installQemu(arch)
|
2017-12-11 22:37:56 +00:00
|
|
|
.return(true)
|
|
|
|
|
|
|
|
exports.qemuPathInContext = (context) ->
|
|
|
|
path = require('path')
|
2018-10-19 14:38:50 +00:00
|
|
|
binDir = path.join(context, '.balena')
|
2017-12-11 22:37:56 +00:00
|
|
|
binPath = path.join(binDir, QEMU_BIN_NAME)
|
|
|
|
path.relative(context, binPath)
|
|
|
|
|
|
|
|
exports.copyQemu = (context) ->
|
|
|
|
path = require('path')
|
|
|
|
fs = require('mz/fs')
|
|
|
|
# Create a hidden directory in the build context, containing qemu
|
2018-10-19 14:38:50 +00:00
|
|
|
binDir = path.join(context, '.balena')
|
2017-12-11 22:37:56 +00:00
|
|
|
binPath = path.join(binDir, QEMU_BIN_NAME)
|
|
|
|
|
|
|
|
Promise.resolve(fs.mkdir(binDir))
|
|
|
|
.catch(code: 'EEXIST', ->)
|
|
|
|
.then ->
|
|
|
|
getQemuPath()
|
|
|
|
.then (qemu) ->
|
|
|
|
new Promise (resolve, reject) ->
|
|
|
|
read = fs.createReadStream(qemu)
|
|
|
|
write = fs.createWriteStream(binPath)
|
|
|
|
read
|
|
|
|
.pipe(write)
|
|
|
|
.on('error', reject)
|
|
|
|
.on('finish', resolve)
|
|
|
|
.then ->
|
|
|
|
fs.chmod(binPath, '755')
|
|
|
|
.then ->
|
|
|
|
path.relative(context, binPath)
|
|
|
|
|
|
|
|
hasQemu = ->
|
|
|
|
fs = require('mz/fs')
|
|
|
|
|
|
|
|
getQemuPath()
|
|
|
|
.then(fs.stat)
|
|
|
|
.return(true)
|
|
|
|
.catchReturn(false)
|
|
|
|
|
|
|
|
getQemuPath = ->
|
2018-10-19 14:38:50 +00:00
|
|
|
balena = require('balena-sdk').fromSharedOptions()
|
2017-12-11 22:37:56 +00:00
|
|
|
path = require('path')
|
|
|
|
fs = require('mz/fs')
|
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
balena.settings.get('binDirectory')
|
2017-12-11 22:37:56 +00:00
|
|
|
.then (binDir) ->
|
|
|
|
Promise.resolve(fs.mkdir(binDir))
|
|
|
|
.catch(code: 'EEXIST', ->)
|
|
|
|
.then ->
|
|
|
|
path.join(binDir, QEMU_BIN_NAME)
|
|
|
|
|
|
|
|
platformNeedsQemu = ->
|
|
|
|
os = require('os')
|
|
|
|
os.platform() == 'linux'
|
|
|
|
|
2018-10-18 17:54:26 +00:00
|
|
|
installQemu = (arch) ->
|
2017-12-11 22:37:56 +00:00
|
|
|
request = require('request')
|
|
|
|
fs = require('fs')
|
|
|
|
zlib = require('zlib')
|
2018-10-18 17:54:26 +00:00
|
|
|
tar = require('tar')
|
|
|
|
os = require('os')
|
|
|
|
path = require('path')
|
|
|
|
rimraf = require('rimraf')
|
2017-12-11 22:37:56 +00:00
|
|
|
|
|
|
|
getQemuPath()
|
|
|
|
.then (qemuPath) ->
|
|
|
|
new Promise (resolve, reject) ->
|
2018-10-18 17:54:26 +00:00
|
|
|
downloadArchiveName = "qemu-3.0.0+resin-#{arch}.tar.gz"
|
|
|
|
fs.mkdtemp(path.join(os.tmpdir(), 'balenaqemu-'), (err, folder) ->
|
|
|
|
if err
|
|
|
|
reject(err)
|
|
|
|
qemuUrl = "https://github.com/balena-io/qemu/releases/download/#{QEMU_VERSION}/#{downloadArchiveName}"
|
|
|
|
request(qemuUrl)
|
|
|
|
.on('error', reject)
|
|
|
|
.pipe(zlib.createGunzip())
|
|
|
|
.on('error', reject)
|
|
|
|
.pipe(tar.extract({
|
|
|
|
C: folder,
|
|
|
|
filter: (path, entry) -> return path.includes("qemu-#{arch}-static"),
|
|
|
|
strip: 1
|
|
|
|
})).on('finish', ->
|
|
|
|
# copy qemu binary to intended location then clean up the temp directory
|
|
|
|
tempFile = path.join(folder, "qemu-#{arch}-static")
|
|
|
|
fs.copyFile(tempFile, qemuPath, (err) ->
|
|
|
|
if err
|
|
|
|
reject(err)
|
|
|
|
# File moved successfully. Remove temp dir.
|
|
|
|
rimraf(folder, (err) ->
|
|
|
|
if err
|
|
|
|
reject(err)
|
|
|
|
resolve()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|