diff --git a/lib/utils/compose.coffee b/lib/utils/compose.coffee index d3ae9853..912c7f00 100644 --- a/lib/utils/compose.coffee +++ b/lib/utils/compose.coffee @@ -166,7 +166,7 @@ exports.buildProject = ( renderer = new BuildProgressUI(tty, imageDescriptors) renderer.start() - qemu.installQemuIfNeeded(emulated, logger) + qemu.installQemuIfNeeded(emulated, logger, arch) .tap (needsQemu) -> return if not needsQemu logger.logInfo('Emulation is enabled') diff --git a/lib/utils/qemu.coffee b/lib/utils/qemu.coffee index 74c2ba27..f23270d1 100644 --- a/lib/utils/qemu.coffee +++ b/lib/utils/qemu.coffee @@ -1,16 +1,16 @@ Promise = require('bluebird') -exports.QEMU_VERSION = QEMU_VERSION = 'v2.5.50-resin-execve' +exports.QEMU_VERSION = QEMU_VERSION = 'v3.0.0+resin' exports.QEMU_BIN_NAME = QEMU_BIN_NAME = 'qemu-execve' -exports.installQemuIfNeeded = Promise.method (emulated, logger) -> +exports.installQemuIfNeeded = Promise.method (emulated, logger, arch) -> return false if not (emulated and platformNeedsQemu()) hasQemu() .then (present) -> if !present - logger.logInfo('Installing qemu for ARM emulation...') - installQemu() + logger.logInfo("Installing qemu for #{arch} emulation...") + installQemu(arch) .return(true) exports.qemuPathInContext = (context) -> @@ -67,20 +67,43 @@ platformNeedsQemu = -> os = require('os') os.platform() == 'linux' -installQemu = -> +installQemu = (arch) -> request = require('request') fs = require('fs') zlib = require('zlib') + tar = require('tar') + os = require('os') + path = require('path') + rimraf = require('rimraf') getQemuPath() .then (qemuPath) -> new Promise (resolve, reject) -> - installStream = fs.createWriteStream(qemuPath) - qemuUrl = "https://github.com/balena-io/qemu/releases/download/#{QEMU_VERSION}/#{QEMU_BIN_NAME}.gz" - request(qemuUrl) - .on('error', reject) - .pipe(zlib.createGunzip()) - .on('error', reject) - .pipe(installStream) - .on('error', reject) - .on('finish', resolve) + 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() + ) + ) + ) + ) diff --git a/package.json b/package.json index 8e2fb267..9c324447 100644 --- a/package.json +++ b/package.json @@ -168,6 +168,7 @@ "split": "^1.0.1", "string-width": "^2.1.1", "strip-ansi-stream": "^1.0.0", + "tar": "^4.4.6", "tar-stream": "^1.5.5", "through2": "^2.0.3", "tmp": "0.0.31",