Skip to content

Instantly share code, notes, and snippets.

@alunux
Created September 22, 2018 04:08
Show Gist options
  • Save alunux/8eb4b957ea40d7c09ea70f8b96500ac1 to your computer and use it in GitHub Desktop.
Save alunux/8eb4b957ea40d7c09ea70f8b96500ac1 to your computer and use it in GitHub Desktop.
#!/bin/bash
IMGNAME="Fedora-Cloud-Base-28-1.1.x86_64"
VHDIMG="${IMGNAME}.vhd"
IMG="${IMGNAME}.raw"
XZIMG="${IMG}.xz"
XZIMGURL="https://download.fedoraproject.org/pub/fedora/linux/releases/28/Cloud/x86_64/images/${XZIMG}"
IMGSUM="b9b302ab6b7dd7143a15539c2eedff3d8a302f70e619a4e0895857fc9ad7f09d"
wget -nc $XZIMGURL
if [[ $(sha256sum $XZIMG) != $IMGSUM ]]; then
rm -f $XZIMG
wget $XZIMGURL
if [ $? -ne 0 ]; then
rm -f $XZIMG
exit 0
fi
fi
unxz $XZIMG
MB=$((1024*1024))
# Obtain the size of the RAW image
size=$(qemu-img info -f raw --output json $IMG | gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}')
# Calculate the 1MB rounded size for the VHD image
rounded_size=$((($size/$MB + 1)*$MB))
# Resize the RAW Image
qemu-img resize -f raw $IMG $rounded_size
# Convert the RAW image to VHD
qemu-img convert -f raw -o subformat=fixed,force_size -O vpc $IMG $VHDIMG
# cleanup
rm -rf $XZIMG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment