Skip to content

Instantly share code, notes, and snippets.

@betamax
Created January 13, 2017 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betamax/976b3308601dc806f68a18ad0537fe0e to your computer and use it in GitHub Desktop.
Save betamax/976b3308601dc806f68a18ad0537fe0e to your computer and use it in GitHub Desktop.
#!/bin/bash
# Create an Azure compatible VHD from a VirtualBox VDI
# More info: https://docs.microsoft.com/en-us/azure/virtual-machines/virtual-machines-linux-create-upload-generic#general-linux-installation-notes#resizing-vhds
# And: http://serverfault.com/a/770425
name=$1
# Convert the image to raw
qemu-img convert -f vdi -O raw "$name.vdi" image.raw
MB=$((1024*1024))
# Get the size of the vdi
size=$(qemu-img info -f vdi --output json "$name.vdi" | \
gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}')
# Calculate the size of the image to the nearest even integer because Azure is crazy
mb=$(($size/$MB))
echo "Old size: $mb MB"
if [ $((mb%2)) -eq 0 ]; then increment=2; else increment=1; fi
mbincremented=$(($mb + $increment))
echo "New size: $mbincremented MB"
rounded_size=$((($mbincremented)*$MB))
echo "Rounded Size = $rounded_size"
# Resize the image to the rounded size
qemu-img resize -f raw image.raw $rounded_size
# Convert the raw image to VHD
qemu-img convert -f raw -O vpc -o subformat=fixed,force_size image.raw "$name.vhd"
rm image.raw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment