Some final tweaks to the sd card image script

This commit is contained in:
David Carley
2022-09-09 18:54:34 +00:00
parent 0bcd784bb2
commit 49f00be43c
2 changed files with 64 additions and 46 deletions

View File

@@ -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,19 +140,21 @@ 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...");
let loopback;
await doFinally(
async () => {
const start = meta.partitions[partition].start; const start = meta.partitions[partition].start;
const loopback = runCommand(`losetup -f --show -o "${start}" "${meta.imageFilePath}"`); loopback = runCommand(`losetup -f --show -o "${start}" "${meta.imageFilePath}"`);
const finallyHandler = () => runCommand(`losetup -d ${loopback}`);
const unregister = registerSignalHandler(finallyHandler);
try {
await cb(loopback); await cb(loopback);
} finally { },
finallyHandler(); () => {
unregister(); if (loopback) {
runCommand(`losetup -d ${loopback}`);
} }
} }
);
}
function prepareImage() { function prepareImage() {
const imageFilePath = createImageFileCopy(); const imageFilePath = createImageFileCopy();
@@ -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) {

View File

@@ -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();
}
}