2020-07-09 13:38:04 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2016-2020 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.
|
|
|
|
*/
|
|
|
|
|
2023-09-06 14:39:31 +00:00
|
|
|
import { Args } from '@oclif/core';
|
2021-01-21 19:27:07 +00:00
|
|
|
import type { BlockDevice } from 'etcher-sdk/build/source-destination';
|
2020-07-09 13:38:04 +00:00
|
|
|
import Command from '../../command';
|
|
|
|
import { ExpectedError } from '../../errors';
|
|
|
|
import * as cf from '../../utils/common-flags';
|
2021-08-26 23:49:54 +00:00
|
|
|
import { getChalk, getVisuals, stripIndent } from '../../utils/lazy';
|
2020-07-09 13:38:04 +00:00
|
|
|
|
|
|
|
export default class LocalFlashCmd extends Command {
|
|
|
|
public static description = stripIndent`
|
|
|
|
Flash an image to a drive.
|
|
|
|
|
|
|
|
Flash a balenaOS image to a drive.
|
|
|
|
Image file may be one of: .img|.zip|.gz|.bz2|.xz
|
|
|
|
|
|
|
|
If --drive is not specified, then it will interactively
|
|
|
|
show a list of available drives for selection.
|
|
|
|
`;
|
|
|
|
|
|
|
|
public static examples = [
|
|
|
|
'$ balena local flash path/to/balenaos.img',
|
|
|
|
'$ balena local flash path/to/balenaos.img --drive /dev/disk2',
|
|
|
|
'$ balena local flash path/to/balenaos.img --drive /dev/disk2 --yes',
|
|
|
|
];
|
|
|
|
|
2023-09-06 14:39:31 +00:00
|
|
|
public static args = {
|
|
|
|
image: Args.string({
|
2020-07-09 13:38:04 +00:00
|
|
|
description: 'path to OS image',
|
|
|
|
required: true,
|
2023-09-06 14:39:31 +00:00
|
|
|
}),
|
|
|
|
};
|
2020-07-09 13:38:04 +00:00
|
|
|
|
|
|
|
public static usage = 'local flash <image>';
|
|
|
|
|
2023-09-06 14:39:31 +00:00
|
|
|
public static flags = {
|
2020-07-15 19:13:18 +00:00
|
|
|
drive: cf.drive,
|
2020-07-09 13:38:04 +00:00
|
|
|
yes: cf.yes,
|
|
|
|
help: cf.help,
|
|
|
|
};
|
|
|
|
|
2021-11-12 16:03:29 +00:00
|
|
|
public static offlineCompatible = true;
|
|
|
|
|
2020-07-09 13:38:04 +00:00
|
|
|
public async run() {
|
2023-09-06 14:39:31 +00:00
|
|
|
const { args: params, flags: options } = await this.parse(LocalFlashCmd);
|
2020-07-09 13:38:04 +00:00
|
|
|
|
2021-01-21 19:27:07 +00:00
|
|
|
if (process.platform === 'linux') {
|
|
|
|
const { promisify } = await import('util');
|
|
|
|
const { exec } = await import('child_process');
|
|
|
|
const execAsync = promisify(exec);
|
|
|
|
let distroVersion = '';
|
|
|
|
try {
|
|
|
|
const info = await execAsync('cat /proc/version');
|
|
|
|
distroVersion = info.stdout.toLowerCase();
|
2021-07-20 13:57:00 +00:00
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
2021-01-21 19:27:07 +00:00
|
|
|
if (distroVersion.includes('microsoft')) {
|
|
|
|
throw new ExpectedError(stripIndent`
|
|
|
|
This command is known not to work on WSL. Please use a CLI release
|
|
|
|
for Windows (not WSL), or balenaEtcher.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 13:38:04 +00:00
|
|
|
const drive = await this.getDrive(options);
|
|
|
|
|
2021-08-26 23:49:54 +00:00
|
|
|
const { confirm } = await import('../../utils/patterns');
|
|
|
|
await confirm(
|
|
|
|
options.yes,
|
|
|
|
'This will erase the selected drive. Are you sure?',
|
|
|
|
);
|
2020-07-09 13:38:04 +00:00
|
|
|
|
2021-08-26 23:49:54 +00:00
|
|
|
const { sourceDestination, multiWrite } = await import('etcher-sdk');
|
2021-01-21 19:27:07 +00:00
|
|
|
const file = new sourceDestination.File({
|
|
|
|
path: params.image,
|
|
|
|
});
|
2020-07-09 13:38:04 +00:00
|
|
|
const source = await file.getInnerSource();
|
|
|
|
|
|
|
|
const visuals = getVisuals();
|
|
|
|
const progressBars: { [key: string]: any } = {
|
|
|
|
flashing: new visuals.Progress('Flashing'),
|
|
|
|
verifying: new visuals.Progress('Validating'),
|
|
|
|
};
|
|
|
|
|
2021-01-21 19:27:07 +00:00
|
|
|
await multiWrite.pipeSourceToDestinations({
|
2020-07-09 13:38:04 +00:00
|
|
|
source,
|
2021-01-21 19:27:07 +00:00
|
|
|
destinations: [drive],
|
|
|
|
onFail: (_, error) => {
|
|
|
|
console.error(getChalk().red.bold(error.message));
|
|
|
|
if (error.message.includes('EACCES')) {
|
|
|
|
console.error(
|
|
|
|
getChalk().red.bold(
|
|
|
|
'Try running this command with elevated privileges, with sudo or in a shell running with admininstrator privileges.',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2020-07-09 13:38:04 +00:00
|
|
|
},
|
2021-01-21 19:27:07 +00:00
|
|
|
onProgress: (progress) => {
|
2020-07-09 13:38:04 +00:00
|
|
|
progressBars[progress.type].update(progress);
|
|
|
|
},
|
2021-01-21 19:27:07 +00:00
|
|
|
verify: true,
|
|
|
|
});
|
2020-07-09 13:38:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 19:27:07 +00:00
|
|
|
async getDrive(options: { drive?: string }): Promise<BlockDevice> {
|
2020-07-09 13:38:04 +00:00
|
|
|
const drive = options.drive || (await getVisuals().drive('Select a drive'));
|
|
|
|
|
|
|
|
const sdk = await import('etcher-sdk');
|
|
|
|
|
2021-01-21 19:27:07 +00:00
|
|
|
const adapter = new sdk.scanner.adapters.BlockDeviceAdapter({
|
|
|
|
includeSystemDrives: () => false,
|
|
|
|
unmountOnSuccess: false,
|
|
|
|
write: true,
|
|
|
|
direct: true,
|
|
|
|
});
|
2020-07-09 13:38:04 +00:00
|
|
|
const scanner = new sdk.scanner.Scanner([adapter]);
|
|
|
|
await scanner.start();
|
|
|
|
try {
|
|
|
|
const d = scanner.getBy('device', drive);
|
|
|
|
if (
|
|
|
|
d === undefined ||
|
|
|
|
!(d instanceof sdk.sourceDestination.BlockDevice)
|
|
|
|
) {
|
|
|
|
throw new ExpectedError(`Drive not found: ${options.drive}`);
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
} finally {
|
|
|
|
scanner.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|