2020-04-23 12:40:26 +00:00
|
|
|
/**
|
|
|
|
* @license
|
2021-03-06 15:06:17 +00:00
|
|
|
* Copyright 2017-2021 Balena Ltd.
|
2020-04-23 12:40:26 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-02-27 12:38:50 +00:00
|
|
|
import type * as Dockerode from 'dockerode';
|
2021-03-06 15:06:17 +00:00
|
|
|
|
|
|
|
import { ExpectedError } from '../errors';
|
2020-05-04 14:57:23 +00:00
|
|
|
import { getBalenaSdk, stripIndent } from './lazy';
|
2020-04-30 20:25:54 +00:00
|
|
|
import Logger = require('./logger');
|
|
|
|
|
2022-06-06 18:55:11 +00:00
|
|
|
export const QEMU_VERSION = 'v7.0.0+balena1';
|
2020-04-23 12:40:26 +00:00
|
|
|
export const QEMU_BIN_NAME = 'qemu-execve';
|
|
|
|
|
2020-04-30 20:22:45 +00:00
|
|
|
export function qemuPathInContext(context: string) {
|
|
|
|
const path = require('path') as typeof import('path');
|
2020-04-23 12:40:26 +00:00
|
|
|
const binDir = path.join(context, '.balena');
|
|
|
|
const binPath = path.join(binDir, QEMU_BIN_NAME);
|
|
|
|
return path.relative(context, binPath);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:22:45 +00:00
|
|
|
export function copyQemu(context: string, arch: string) {
|
|
|
|
const path = require('path') as typeof import('path');
|
2020-06-23 10:57:57 +00:00
|
|
|
const fs = require('fs') as typeof import('fs');
|
2020-04-23 12:40:26 +00:00
|
|
|
// Create a hidden directory in the build context, containing qemu
|
|
|
|
const binDir = path.join(context, '.balena');
|
|
|
|
const binPath = path.join(binDir, QEMU_BIN_NAME);
|
|
|
|
|
2020-07-01 14:26:40 +00:00
|
|
|
return fs.promises
|
|
|
|
.mkdir(binDir)
|
|
|
|
.catch(function (err) {
|
|
|
|
if (err.code === 'EEXIST') {
|
|
|
|
// ignore
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw err;
|
2020-04-23 12:40:26 +00:00
|
|
|
})
|
|
|
|
.then(() => getQemuPath(arch))
|
|
|
|
.then(
|
2020-06-15 22:53:07 +00:00
|
|
|
(qemu) =>
|
2020-06-30 20:24:35 +00:00
|
|
|
new Promise(function (resolve, reject) {
|
2020-04-23 12:40:26 +00:00
|
|
|
const read = fs.createReadStream(qemu);
|
|
|
|
const write = fs.createWriteStream(binPath);
|
|
|
|
|
|
|
|
read
|
|
|
|
.on('error', reject)
|
|
|
|
.pipe(write)
|
|
|
|
.on('error', reject)
|
|
|
|
.on('finish', resolve);
|
|
|
|
}),
|
|
|
|
)
|
2020-06-23 10:57:57 +00:00
|
|
|
.then(() => fs.promises.chmod(binPath, '755'))
|
2020-04-23 12:40:26 +00:00
|
|
|
.then(() => path.relative(context, binPath));
|
|
|
|
}
|
|
|
|
|
2021-03-06 15:06:17 +00:00
|
|
|
export const getQemuPath = function (balenaArch: string) {
|
|
|
|
const qemuArch = balenaArchToQemuArch(balenaArch);
|
2020-04-23 12:40:26 +00:00
|
|
|
const balena = getBalenaSdk();
|
2020-04-30 20:22:45 +00:00
|
|
|
const path = require('path') as typeof import('path');
|
2020-06-23 10:57:57 +00:00
|
|
|
const { promises: fs } = require('fs') as typeof import('fs');
|
2020-04-23 12:40:26 +00:00
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
return balena.settings.get('binDirectory').then((binDir) =>
|
2020-07-01 14:26:40 +00:00
|
|
|
fs
|
|
|
|
.mkdir(binDir)
|
|
|
|
.catch(function (err) {
|
|
|
|
if (err.code === 'EEXIST') {
|
|
|
|
// ignore
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw err;
|
2020-04-23 12:40:26 +00:00
|
|
|
})
|
|
|
|
.then(() =>
|
2021-03-06 15:06:17 +00:00
|
|
|
path.join(binDir, `${QEMU_BIN_NAME}-${qemuArch}-${QEMU_VERSION}`),
|
2020-04-23 12:40:26 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-03-06 15:06:17 +00:00
|
|
|
async function installQemu(arch: string, qemuPath: string) {
|
|
|
|
const qemuArch = balenaArchToQemuArch(arch);
|
|
|
|
const fileVersion = QEMU_VERSION.replace('v', '').replace('+', '.');
|
|
|
|
const urlFile = encodeURIComponent(`qemu-${fileVersion}-${qemuArch}.tar.gz`);
|
|
|
|
const urlVersion = encodeURIComponent(QEMU_VERSION);
|
|
|
|
const qemuUrl = `https://github.com/balena-io/qemu/releases/download/${urlVersion}/${urlFile}`;
|
|
|
|
|
|
|
|
const request = await import('request');
|
|
|
|
const fs = await import('fs');
|
|
|
|
const zlib = await import('zlib');
|
|
|
|
const tar = await import('tar-stream');
|
|
|
|
|
|
|
|
// createWriteStream creates a zero-length file on disk that
|
|
|
|
// needs to be deleted if the download fails
|
|
|
|
const installStream = fs.createWriteStream(qemuPath);
|
|
|
|
try {
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
|
|
const extract = tar.extract();
|
|
|
|
extract.on('entry', function (header, stream, next) {
|
|
|
|
try {
|
2020-04-23 12:40:26 +00:00
|
|
|
stream.on('end', next);
|
|
|
|
if (header.name.includes(`qemu-${qemuArch}-static`)) {
|
|
|
|
stream.pipe(installStream);
|
|
|
|
} else {
|
|
|
|
stream.resume();
|
|
|
|
}
|
2021-03-06 15:06:17 +00:00
|
|
|
} catch (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
request(qemuUrl)
|
|
|
|
.on('error', reject)
|
|
|
|
.pipe(zlib.createGunzip())
|
|
|
|
.on('error', reject)
|
|
|
|
.pipe(extract)
|
|
|
|
.on('error', reject)
|
|
|
|
.on('finish', function () {
|
|
|
|
fs.chmodSync(qemuPath, '755');
|
|
|
|
resolve();
|
2020-04-23 12:40:26 +00:00
|
|
|
});
|
2021-03-06 15:06:17 +00:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
try {
|
|
|
|
await fs.promises.unlink(qemuPath);
|
|
|
|
} catch {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
2020-04-23 12:40:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
const balenaArchToQemuArch = function (arch: string) {
|
2020-04-23 12:40:26 +00:00
|
|
|
switch (arch) {
|
|
|
|
case 'rpi':
|
2021-03-06 15:06:17 +00:00
|
|
|
case 'arm':
|
2020-04-23 12:40:26 +00:00
|
|
|
case 'armhf':
|
2021-03-06 15:06:17 +00:00
|
|
|
case 'armv7hf':
|
2020-04-23 12:40:26 +00:00
|
|
|
return 'arm';
|
2021-03-06 15:06:17 +00:00
|
|
|
case 'arm64':
|
2020-04-23 12:40:26 +00:00
|
|
|
case 'aarch64':
|
|
|
|
return 'aarch64';
|
|
|
|
default:
|
2021-03-06 15:06:17 +00:00
|
|
|
throw new ExpectedError(stripIndent`
|
|
|
|
Unknown ARM architecture identifier "${arch}".
|
|
|
|
Known ARM identifiers: rpi arm armhf armv7hf arm64 aarch64`);
|
2020-04-23 12:40:26 +00:00
|
|
|
}
|
|
|
|
};
|
2020-04-30 20:25:54 +00:00
|
|
|
|
|
|
|
export async function installQemuIfNeeded(
|
|
|
|
emulated: boolean,
|
|
|
|
logger: Logger,
|
|
|
|
arch: string,
|
|
|
|
docker: Dockerode,
|
|
|
|
): Promise<boolean> {
|
|
|
|
// call platformNeedsQemu() regardless of whether emulation is required,
|
|
|
|
// because it logs useful information
|
|
|
|
const needsQemu = await platformNeedsQemu(docker, logger);
|
|
|
|
if (!emulated || !needsQemu) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-23 10:57:57 +00:00
|
|
|
const { promises: fs } = await import('fs');
|
|
|
|
const qemuPath = await getQemuPath(arch);
|
|
|
|
try {
|
2021-03-06 15:06:17 +00:00
|
|
|
const stats = await fs.stat(qemuPath);
|
|
|
|
// Earlier versions of the CLI with broken error handling would leave
|
|
|
|
// behind files with size 0. If such a file is found, delete it.
|
|
|
|
if (stats.size === 0) {
|
|
|
|
await fs.unlink(qemuPath);
|
|
|
|
}
|
2020-06-23 10:57:57 +00:00
|
|
|
await fs.access(qemuPath);
|
|
|
|
} catch {
|
2021-03-06 15:06:17 +00:00
|
|
|
// QEMU not found in cache folder (~/.balena/bin/), so install it
|
2020-04-30 20:25:54 +00:00
|
|
|
logger.logInfo(`Installing qemu for ${arch} emulation...`);
|
2021-03-06 15:06:17 +00:00
|
|
|
await installQemu(arch, qemuPath);
|
2020-04-30 20:25:54 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the Docker daemon (including balenaEngine) requires explicit
|
|
|
|
* QEMU emulation setup. Note that Docker Desktop (Windows and Mac), and also
|
|
|
|
* the older Docker for Mac, have built-in support for binfmt_misc, so they
|
|
|
|
* do not require explicit 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,
|
|
|
|
logger: Logger,
|
|
|
|
): Promise<boolean> {
|
|
|
|
const dockerInfo = await docker.info();
|
|
|
|
// Docker Desktop (Windows and Mac) with Docker Engine 19.03 reports:
|
|
|
|
// OperatingSystem: Docker Desktop
|
|
|
|
// OSType: linux
|
|
|
|
// Docker for Mac with Docker Engine 18.06 reports:
|
|
|
|
// OperatingSystem: Docker for Mac
|
|
|
|
// OSType: linux
|
|
|
|
// On Ubuntu (standard Docker installation):
|
|
|
|
// OperatingSystem: Ubuntu 18.04.2 LTS (containerized)
|
|
|
|
// OSType: linux
|
|
|
|
// https://stackoverflow.com/questions/38223965/how-can-i-detect-if-docker-for-mac-is-installed
|
|
|
|
const isDockerDesktop = /(?:Docker Desktop)|(?:Docker for Mac)/i.test(
|
|
|
|
dockerInfo.OperatingSystem,
|
|
|
|
);
|
|
|
|
if (isDockerDesktop) {
|
|
|
|
logger.logInfo(stripIndent`
|
|
|
|
Docker Desktop detected (daemon architecture: "${dockerInfo.Architecture}")
|
|
|
|
Docker itself will determine and enable architecture emulation if required,
|
|
|
|
without balena-cli intervention and regardless of the --emulated option.`);
|
|
|
|
}
|
|
|
|
return !isDockerDesktop;
|
|
|
|
}
|