Add image size check to hdd-image.sh (#2965)

There is no sanity check when creating OS images, so when some of the
partitions gets too big, part of its data may get overwritten by the
following partition, resulting in corrupted image. Add checks for the
defined partition sizes and bail out if they're too big.
This commit is contained in:
Jan Čermák 2023-12-05 11:38:33 +01:00 committed by GitHub
parent d427248a05
commit 1211b24c7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -43,6 +43,22 @@ function size2sectors() {
} }
function _check_image_size() {
local image="${1}"
local max_size="${2}"
local image_size=$(stat -c %s "${image}")
local image_size_sectors=$((image_size / 512))
local max_size_sectors=$(size2sectors "${max_size}")
if [ "${image_size_sectors}" -gt "${max_size_sectors}" ]; then
echo "Image ${image} is too big: ${image_size_sectors} sectors > ${max_size_sectors} sectors" >&2
exit 1
fi
}
function get_boot_size() { function get_boot_size() {
# shellcheck disable=SC2153 # shellcheck disable=SC2153
echo "${BOOT_SIZE}" echo "${BOOT_SIZE}"
@ -66,6 +82,8 @@ function create_boot_image() {
truncate --size="$(get_boot_size)" "${boot_img}" truncate --size="$(get_boot_size)" "${boot_img}"
mkfs.vfat -n "hassos-boot" "${boot_img}" mkfs.vfat -n "hassos-boot" "${boot_img}"
mcopy -i "${boot_img}" -sv "${boot_data}"/* :: mcopy -i "${boot_img}" -sv "${boot_data}"/* ::
_check_image_size "${boot_img}" "$(get_boot_size)"
} }
@ -75,6 +93,8 @@ function create_overlay_image() {
rm -f "${overlay_img}" rm -f "${overlay_img}"
truncate --size="${OVERLAY_SIZE}" "${overlay_img}" truncate --size="${OVERLAY_SIZE}" "${overlay_img}"
mkfs.ext4 -L "hassos-overlay" -I 256 -E lazy_itable_init=0,lazy_journal_init=0 "${overlay_img}" mkfs.ext4 -L "hassos-overlay" -I 256 -E lazy_itable_init=0,lazy_journal_init=0 "${overlay_img}"
_check_image_size "${overlay_img}" "${OVERLAY_SIZE}"
} }
@ -86,10 +106,15 @@ function create_kernel_image() {
# Make image # Make image
rm -f "${kernel_img}" rm -f "${kernel_img}"
mksquashfs "${kernel}" "${kernel_img}" -comp lzo mksquashfs "${kernel}" "${kernel_img}" -comp lzo
_check_image_size "${kernel_img}" "${KERNEL_SIZE}"
} }
function _prepare_disk_image() { function _prepare_disk_image() {
_check_image_size "$(path_data_img)" "${DATA_SIZE}"
_check_image_size "$(path_rootfs_img)" "${SYSTEM_SIZE}"
create_boot_image create_boot_image
create_overlay_image create_overlay_image
create_kernel_image create_kernel_image