Skip to content

Commit

Permalink
Add image size check to hdd-image.sh (#2965)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sairon authored Dec 5, 2023
1 parent d427248 commit 1211b24
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions buildroot-external/scripts/hdd-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
# shellcheck disable=SC2153
echo "${BOOT_SIZE}"
Expand All @@ -66,6 +82,8 @@ function create_boot_image() {
truncate --size="$(get_boot_size)" "${boot_img}"
mkfs.vfat -n "hassos-boot" "${boot_img}"
mcopy -i "${boot_img}" -sv "${boot_data}"/* ::

_check_image_size "${boot_img}" "$(get_boot_size)"
}


Expand All @@ -75,6 +93,8 @@ function create_overlay_image() {
rm -f "${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}"

_check_image_size "${overlay_img}" "${OVERLAY_SIZE}"
}


Expand All @@ -86,10 +106,15 @@ function create_kernel_image() {
# Make image
rm -f "${kernel_img}"
mksquashfs "${kernel}" "${kernel_img}" -comp lzo

_check_image_size "${kernel_img}" "${KERNEL_SIZE}"
}


function _prepare_disk_image() {
_check_image_size "$(path_data_img)" "${DATA_SIZE}"
_check_image_size "$(path_rootfs_img)" "${SYSTEM_SIZE}"

create_boot_image
create_overlay_image
create_kernel_image
Expand Down

0 comments on commit 1211b24

Please sign in to comment.