Enable emulated builds on remote devices running a different OS as the CLI

E.g. "balena build -e -h <IP> -p 2375" with the CLI running on a Mac laptop,
using balenaEngine on an Intel NUC device, building an image for the RPi (ARM
image arch). Previously, QEMU setup by the CLI assumed that docker ran on the
same OS as the CLI (Docker for Mac has built-in binfmt_misc support and does
not require additional setup, but balenaEngine on Linux requires explicit QEMU
setup.)

Change-type: minor
Signed-off-by: Paulo Castro <paulo@balena.io>
This commit is contained in:
Paulo Castro 2019-08-21 20:45:07 +01:00
parent 516fa90a20
commit 19c3178062
5 changed files with 130 additions and 26 deletions

View File

@ -188,7 +188,7 @@ exports.buildProject = (
transpose = require('docker-qemu-transpose')
{ BALENA_ENGINE_TMP_PATH } = require('../config')
{ checkBuildSecretsRequirements, makeBuildTasks } = require('./compose_ts')
qemu = require('./qemu')
qemu = require('./qemu-ts')
{ toPosixPath } = builder.PathUtils
logger.logInfo("Building for #{arch}/#{deviceType}")
@ -204,7 +204,7 @@ exports.buildProject = (
renderer.start()
Promise.resolve(checkBuildSecretsRequirements(docker, projectPath))
.then -> qemu.installQemuIfNeeded(emulated, logger, arch)
.then -> qemu.installQemuIfNeeded(emulated, logger, arch, docker)
.tap (needsQemu) ->
return if not needsQemu
logger.logInfo('Emulation is enabled')

61
lib/utils/qemu-ts.ts Normal file
View File

@ -0,0 +1,61 @@
/**
* @license
* Copyright 2019 Balena Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Dockerode = require('dockerode');
import Logger = require('./logger');
import { getQemuPath, installQemu } from './qemu';
export { QEMU_BIN_NAME } from './qemu';
export * from './qemu';
export async function installQemuIfNeeded(
emulated: boolean,
logger: Logger,
arch: string,
docker: Dockerode,
): Promise<boolean> {
if (!emulated || !(await platformNeedsQemu(docker))) {
return false;
}
const fs = await import('mz/fs');
if (!(await fs.exists(await getQemuPath(arch)))) {
logger.logInfo(`Installing qemu for ${arch} emulation...`);
await installQemu(arch);
}
return true;
}
/**
* Check whether the Docker daemon (including balenaEngine) requires explicit
* QEMU emulation setup. Note that Docker for Mac (macOS), and reportedly also
* Docker for Windows, have built-in support for binfmt_misc, so do not require
* explicity QEMU setup. References:
* - https://en.wikipedia.org/wiki/Binfmt_misc
* - https://docs.docker.com/docker-for-mac/multi-arch/
* - https://www.ecliptik.com/Cross-Building-and-Running-Multi-Arch-Docker-Images/
* - https://stackoverflow.com/questions/55388725/run-linux-arm-container-via-qemu-binfmt-misc-on-docker-lcow
*
* @param docker Dockerode instance
*/
async function platformNeedsQemu(docker: Dockerode): Promise<boolean> {
const dockerInfo = await docker.info();
// Docker for Mac reports dockerInfo.OSType as 'linux'
// https://stackoverflow.com/questions/38223965/how-can-i-detect-if-docker-for-mac-is-installed
const isDockerForMac = /Docker for Mac/i.test(dockerInfo.OperatingSystem);
return dockerInfo.OSType === 'linux' && !isDockerForMac;
}

View File

@ -1,18 +1,25 @@
###*
# @license
# Copyright 2017-2019 Balena Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###
Promise = require('bluebird')
exports.QEMU_VERSION = QEMU_VERSION = 'v4.0.0-balena'
exports.QEMU_BIN_NAME = QEMU_BIN_NAME = 'qemu-execve'
exports.installQemuIfNeeded = Promise.method (emulated, logger, arch) ->
return false if not (emulated and platformNeedsQemu())
hasQemu(arch)
.then (present) ->
if !present
logger.logInfo("Installing qemu for #{arch} emulation...")
installQemu(arch)
.return(true)
exports.qemuPathInContext = (context) ->
path = require('path')
binDir = path.join(context, '.balena')
@ -45,15 +52,7 @@ exports.copyQemu = (context, arch) ->
.then ->
path.relative(context, binPath)
hasQemu = (arch) ->
fs = require('mz/fs')
getQemuPath(arch)
.then(fs.stat)
.return(true)
.catchReturn(false)
getQemuPath = (arch) ->
exports.getQemuPath = getQemuPath = (arch) ->
balena = require('balena-sdk').fromSharedOptions()
path = require('path')
fs = require('mz/fs')
@ -65,11 +64,7 @@ getQemuPath = (arch) ->
.then ->
path.join(binDir, "#{QEMU_BIN_NAME}-#{arch}-#{QEMU_VERSION}")
platformNeedsQemu = ->
os = require('os')
os.platform() == 'linux'
installQemu = (arch) ->
exports.installQemu = (arch) ->
request = require('request')
fs = require('fs')
zlib = require('zlib')

22
lib/utils/qemu.d.ts vendored Normal file
View File

@ -0,0 +1,22 @@
/**
* @license
* Copyright 2019 Balena Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export const QEMU_VERSION: string;
export const QEMU_BIN_NAME: string;
export async function getQemuPath(arch: string): Promise<string>;
export async function installQemu(arch: string): Promise<void>;

26
tests/utils/qemu.spec.ts Normal file
View File

@ -0,0 +1,26 @@
/**
* @license
* Copyright 2019 Balena Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { expect } from 'chai';
describe('resin-multibuild consistency', function() {
it('should use the same values for selected constants', async () => {
const { QEMU_BIN_NAME: MQEMU_BIN_NAME } = await import('resin-multibuild');
const { QEMU_BIN_NAME } = await import('../../build/utils/qemu-ts');
expect(QEMU_BIN_NAME).to.equal(MQEMU_BIN_NAME);
});
});