[RPI] Refactor Image Generation Script for Clarity and Modularization

- Reorganized variable definitions for clarity and consistency.
- Encapsulated SWU file creation logic into 'create_swu_file' function for better modularity and readability.
- Implemented 'cleanup' function to handle temporary file cleanup, enhancing script robustness.
- Utilized bash array for 'SWUPDATE_FILES' to improve handling of multiple file entries.
- Simplified error handling with 'set -e' and streamlined script exit using 'trap' for cleanup operations.
- Cleaned up directory changing commands by redirecting 'pushd' and 'popd' output to '/dev/null'.
This commit is contained in:
Bohdan Buinich 2024-01-08 00:50:44 +02:00
parent 40f43862ba
commit 73b139c4c0
1 changed files with 51 additions and 21 deletions

View File

@ -2,33 +2,63 @@
set -e set -e
BOARD_DIR="$(dirname $0)" # Define board directory and related variables
BOARD_NAME="$(basename ${BOARD_DIR})" BOARD_DIR="$(dirname "$0")"
BOARD_NAME="$(basename "${BOARD_DIR}")"
GENIMAGE_CFG="${BOARD_DIR}/genimage-${BOARD_NAME}.cfg" GENIMAGE_CFG="${BOARD_DIR}/genimage-${BOARD_NAME}.cfg"
GENIMAGE_TMP="${BUILD_DIR}/genimage.tmp" GENIMAGE_TMP="${BUILD_DIR}/genimage.tmp"
SWUPDATE_FILES="sw-description rootfs.squashfs"
# Pass an empty rootpath. genimage makes a full copy of the given rootpath to # Define files for SWUPDATE
# ${GENIMAGE_TMP}/root so passing TARGET_DIR would be a waste of time and disk SWUPDATE_FILES=("sw-description" "rootfs.squashfs")
# space. We don't rely on genimage to build the rootfs image, just to insert a
# pre-built one in the disk image.
trap 'rm -rf "${ROOTPATH_TMP}"' EXIT # Check if necessary files and directories exist
if [ ! -d "${BUILD_DIR}" ] || [ ! -f "${GENIMAGE_CFG}" ]; then
echo "Required directories or config files are missing."
exit 1
fi
# Function to create SWU file
create_swu_file() {
local binaries_dir=$1
shift
local files=("$@")
pushd "${binaries_dir}" > /dev/null
printf '%s\n' "${files[@]}" | cpio -ov -H crc > rootfs.swu
if [ $? -ne 0 ]; then
echo "Error creating SWU file."
exit 1
fi
popd > /dev/null
}
# Clean up function for EXIT trap
cleanup() {
echo "Cleaning up temporary files."
rm -rf "${ROOTPATH_TMP}"
rm -rf "${GENIMAGE_TMP}"
}
# Setting up trap for cleanup on script exit
trap cleanup EXIT
# Create temporary root path
ROOTPATH_TMP="$(mktemp -d)" ROOTPATH_TMP="$(mktemp -d)"
rm -rf "${GENIMAGE_TMP}" # Generate image using genimage
echo "Generating image with genimage..."
genimage \ genimage \
--rootpath "${ROOTPATH_TMP}" \ --rootpath "${ROOTPATH_TMP}" \
--tmppath "${GENIMAGE_TMP}" \ --tmppath "${GENIMAGE_TMP}" \
--inputpath "${BINARIES_DIR}" \ --inputpath "${BINARIES_DIR}" \
--outputpath "${BINARIES_DIR}" \ --outputpath "${BINARIES_DIR}" \
--config "${GENIMAGE_CFG}" --config "${GENIMAGE_CFG}"
pushd ${BINARIES_DIR} if [ $? -ne 0 ]; then
for f in ${SWUPDATE_FILES} ; do echo "Error during image generation."
echo ${f} exit 1
done | cpio -ov -H crc > rootfs.swu fi
popd
exit $? # Create SWU file
echo "Creating SWU file..."
create_swu_file "${BINARIES_DIR}" "${SWUPDATE_FILES[@]}"