build_image, image_to_vm: add disk-size assertions

Assert that the image is a multiple of 1 MiB
This commit is contained in:
Alex Crawford 2014-10-07 16:52:50 -07:00
parent c4fb64a948
commit 44520881c7
2 changed files with 26 additions and 0 deletions

View File

@ -163,6 +163,24 @@ package_provided() {
done
}
assert_image_size() {
local disk_img="$1"
local disk_type="$2"
local size
size=$(qemu-img info -f "${disk_type}" --output json "${disk_img}" | \
gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}' ; \
exit ${PIPESTATUS[0]})
if [[ $? -ne 0 ]]; then
die_notrace "assert failed: could not read image size"
fi
MiB=$((1024*1024))
if [[ $(($size % $MiB)) -ne 0 ]]; then
die_notrace "assert failed: image must be a multiple of 1 MiB ($size B)"
fi
}
start_image() {
local image_name="$1"
local disk_layout="$2"
@ -181,6 +199,8 @@ start_image() {
"${BUILD_LIBRARY_DIR}/disk_util" --disk_layout="${disk_layout}" \
format "${disk_img}"
assert_image_size "${disk_img}" raw
"${BUILD_LIBRARY_DIR}/disk_util" --disk_layout="${disk_layout}" \
mount "${disk_img}" "${root_fs_dir}"
trap "cleanup_mounts '${root_fs_dir}' && delete_prompt" EXIT

View File

@ -331,6 +331,8 @@ setup_disk_image() {
update "${VM_TMP_IMG}"
fi
assert_image_size "${VM_TMP_IMG}" raw
info "Mounting image to $(relpath "${VM_TMP_ROOT}")"
"${BUILD_LIBRARY_DIR}/disk_util" --disk_layout="${disk_layout}" \
mount "${VM_TMP_IMG}" "${VM_TMP_ROOT}"
@ -432,18 +434,22 @@ _write_raw_disk() {
_write_qcow2_disk() {
qemu-img convert -f raw "$1" -O qcow2 "$2"
assert_image_size "$2" qcow2
}
_write_vhd_disk() {
qemu-img convert -f raw "$1" -O vpc "$2"
assert_image_size "$2" vpc
}
_write_vmdk_ide_disk() {
qemu-img convert -f raw "$1" -O vmdk -o adapter_type=ide "$2"
assert_image_size "$2" vmdk
}
_write_vmdk_scsi_disk() {
qemu-img convert -f raw "$1" -O vmdk -o adapter_type=lsilogic "$2"
assert_image_size "$2" vmdk
}
_write_cpio_common() {