Some final tweaks to the sd card image script
This commit is contained in:
@@ -3,12 +3,30 @@
|
|||||||
const inquirer = require("inquirer");
|
const inquirer = require("inquirer");
|
||||||
const merge = require("lodash.merge");
|
const merge = require("lodash.merge");
|
||||||
const { resolve } = require("path");
|
const { resolve } = require("path");
|
||||||
const { statSync, rmdirSync, copyFileSync, writeFileSync, readFileSync, existsSync, rmSync } = require("fs");
|
const {
|
||||||
|
statSync,
|
||||||
|
rmdirSync,
|
||||||
|
copyFileSync,
|
||||||
|
writeFileSync,
|
||||||
|
readFileSync,
|
||||||
|
existsSync,
|
||||||
|
rmSync
|
||||||
|
} = require("fs");
|
||||||
const { exit } = require("process");
|
const { exit } = require("process");
|
||||||
const { glob } = require("glob");
|
const { glob } = require("glob");
|
||||||
const packageJSON = require("../package.json");
|
const packageJSON = require("../package.json");
|
||||||
const config_defaults = require("../src/resources/onefinity_defaults.json");
|
const config_defaults = require("../src/resources/onefinity_defaults.json");
|
||||||
const { info, runCommand, logErrorAndExit, assertOS, assertEffectiveRoot, assertFileExists, assertInstalled, initSignalHandlers, registerSignalHandler } = require("./util");
|
const {
|
||||||
|
info,
|
||||||
|
runCommand,
|
||||||
|
logErrorAndExit,
|
||||||
|
assertOS,
|
||||||
|
assertEffectiveRoot,
|
||||||
|
assertFileExists,
|
||||||
|
assertInstalled,
|
||||||
|
initSignalHandlers,
|
||||||
|
doFinally
|
||||||
|
} = require("./util");
|
||||||
|
|
||||||
const variant_defaults = {
|
const variant_defaults = {
|
||||||
machinist_x35: require("../src/resources/onefinity_machinist_x35_defaults.json"),
|
machinist_x35: require("../src/resources/onefinity_machinist_x35_defaults.json"),
|
||||||
@@ -67,20 +85,7 @@ main();
|
|||||||
async function main() {
|
async function main() {
|
||||||
let meta;
|
let meta;
|
||||||
|
|
||||||
const finallyHandler = () => {
|
await doFinally(async () => {
|
||||||
if (meta) {
|
|
||||||
if (existsSync(meta.imageFilePath)) {
|
|
||||||
rmSync(meta.imageFilePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (existsSync(meta.compressedImageFilePath)) {
|
|
||||||
rmSync(meta.compressedImageFilePath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const unregister = registerSignalHandler(finallyHandler);
|
|
||||||
|
|
||||||
try {
|
|
||||||
assertOS();
|
assertOS();
|
||||||
assertEffectiveRoot();
|
assertEffectiveRoot();
|
||||||
assertFileExists(ORIGINAL_IMAGE_FILENAME);
|
assertFileExists(ORIGINAL_IMAGE_FILENAME);
|
||||||
@@ -104,12 +109,17 @@ async function main() {
|
|||||||
await configureAutoExpand(meta);
|
await configureAutoExpand(meta);
|
||||||
compress(meta);
|
compress(meta);
|
||||||
moveImageFiles(meta);
|
moveImageFiles(meta);
|
||||||
} catch (error) {
|
}, () => {
|
||||||
finallyHandler();
|
if (meta) {
|
||||||
unregister();
|
if (existsSync(meta.imageFilePath)) {
|
||||||
|
rmSync(meta.imageFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
console.error(error);
|
if (existsSync(meta.compressedImageFilePath)) {
|
||||||
}
|
rmSync(meta.compressedImageFilePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function createImageFileCopy() {
|
function createImageFileCopy() {
|
||||||
@@ -130,18 +140,20 @@ function createImageFileCopy() {
|
|||||||
async function attachToLoopback(meta, partition, cb) {
|
async function attachToLoopback(meta, partition, cb) {
|
||||||
info("Attaching the image to a loopback device...");
|
info("Attaching the image to a loopback device...");
|
||||||
|
|
||||||
const start = meta.partitions[partition].start;
|
let loopback;
|
||||||
const loopback = runCommand(`losetup -f --show -o "${start}" "${meta.imageFilePath}"`);
|
await doFinally(
|
||||||
|
async () => {
|
||||||
|
const start = meta.partitions[partition].start;
|
||||||
|
loopback = runCommand(`losetup -f --show -o "${start}" "${meta.imageFilePath}"`);
|
||||||
|
|
||||||
const finallyHandler = () => runCommand(`losetup -d ${loopback}`);
|
await cb(loopback);
|
||||||
const unregister = registerSignalHandler(finallyHandler);
|
},
|
||||||
|
() => {
|
||||||
try {
|
if (loopback) {
|
||||||
await cb(loopback);
|
runCommand(`losetup -d ${loopback}`);
|
||||||
} finally {
|
}
|
||||||
finallyHandler();
|
}
|
||||||
unregister();
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function prepareImage() {
|
function prepareImage() {
|
||||||
@@ -417,23 +429,17 @@ function getCompressedFilename(target) {
|
|||||||
async function mountLoopback(loopback, cb) {
|
async function mountLoopback(loopback, cb) {
|
||||||
let mountpoint;
|
let mountpoint;
|
||||||
|
|
||||||
const finallyHandler = () => {
|
await doFinally(async () => {
|
||||||
if (mountpoint) {
|
|
||||||
runCommand(`umount "${mountpoint}"`);
|
|
||||||
rmdirSync(mountpoint);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const unregister = registerSignalHandler(finallyHandler);
|
|
||||||
|
|
||||||
try {
|
|
||||||
mountpoint = runCommand("mktemp --tmpdir -d onefinity-raspi-root-XXXXXXXXXX");
|
mountpoint = runCommand("mktemp --tmpdir -d onefinity-raspi-root-XXXXXXXXXX");
|
||||||
|
|
||||||
runCommand(`mount ${loopback} ${mountpoint}`);
|
runCommand(`mount ${loopback} ${mountpoint}`);
|
||||||
await cb(mountpoint);
|
await cb(mountpoint);
|
||||||
} finally {
|
}, () => {
|
||||||
finallyHandler();
|
if (mountpoint) {
|
||||||
unregister();
|
runCommand(`umount "${mountpoint}"`);
|
||||||
}
|
rmdirSync(mountpoint);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function scrubFiles(mountpoint, patterns) {
|
function scrubFiles(mountpoint, patterns) {
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ module.exports = {
|
|||||||
assertOS,
|
assertOS,
|
||||||
assertEffectiveRoot,
|
assertEffectiveRoot,
|
||||||
assertFileExists,
|
assertFileExists,
|
||||||
assertInstalled
|
assertInstalled,
|
||||||
|
doFinally
|
||||||
};
|
};
|
||||||
|
|
||||||
let signalHandlers = [];
|
let signalHandlers = [];
|
||||||
@@ -147,3 +148,14 @@ function assertInstalled(tools) {
|
|||||||
`);
|
`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function doFinally(doHandler, finallyHandler) {
|
||||||
|
const unregister = registerSignalHandler(finallyHandler);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await doHandler();
|
||||||
|
} finally {
|
||||||
|
unregister();
|
||||||
|
await finallyHandler();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user