2

I'm currently setting up a dynamic High-Performance-Computing cluster in Azure, and I'm thinking of basing it on Ubuntu.

The Microsoft documentation recommends to base an Ubuntu image on the tested Ubuntu Cloud-Images for Azure (e.g., https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64-azure.vhd.zip)

The issue is, that these images are already 30GB is size, even if only a fraction of the space is actually used. In order to speed-up the deployment time of my cluster nodes in Azure, I would like to have a smaller image (e.g., 10GB).

What would be the most efficient way, to shrink the size of the above mentioned image? Is there any way efficient way, or do I have to create a smaller image from scratch?

I would be totally happy if somebody could help me out with this topic.

1 Answer 1

1

You can't shrink managed disks in Azure directly. However, you can use another VM (or on your computer) to handle the image: download, shrink, upload to your Storage account as Block blob, then create an image.

Using Hyper-V:

  • Create/use a VM with Linux (preferably with GUI)
  • Attach the image you will use
  • Shrink the last partition to lees than the desired size (9 GB for example) (fsck before)
  • Poweroff the VM and detack the disk
  • Shrink the disk (either using qemu-img or the Hyper-V disk utility), ensuring,
    • The resulting image is (much) greater than the end of the last partition)
    • It is allocated to a megabyte(*)
  • Attach the disk image again
  • Boot the VM
  • Rellocate the backup GPT to the end of the disk (using gdisk or sgdisk)
  • Run fsck
  • Poweroff the VM
  • Detach the disk, then, swap it with the disk image used for handling the new disk
  • Ensure the disk image boots on Hyper-V
  • The image is ready to upload to your storage account and to be used to create a managed image
  • Finally, ensure the image disk boots in Azure

(*)Please notice that, in order to import a managed disk from a Block blob (if you want to handle the image from the local computer, then, uploading to your Storage account), the image should be allocated to MB, and the image must be in VHD fixed. Otherwise, the import will fail. Some tools like qemu-img (and even the Hyper-V disk utility) adds extra padding of 512 bytes to the end of the resulted image.

Another (quick-and-dirty) approach is using Azure:

  • Create a VM (preferably Ubuntu 18.04)
    • Attach an empty managed disk of the desired size
  • Download and the image
  • Install qemu-utils
  • Convert the image to RAW
    • qemu-img convert -f vpc -O raw bionic.vhd bionic.img
  • Import the downloaded image as loop device
    • sudo losetup -f bionic.img
  • Shrink the last partition to 9 GiB using parted (fsck before)
  • Detach the image to the loop device
    • sudo losetup -d /dev/loop0
  • Copy the image to the attached managed disk using dd (quick-and-dirty)
    • dd if /dev/loop0 of=/dev/sdb bs=4096
  • Rellocate the backup GPT to the end of the disk (using gdisk or sgdisk)
  • Grow the last partition to the end of the disk
  • Run fsck
  • Stop (dellocate) the VM
  • Detach the recently filled disk
  • Swap the boot disk with the recently created one
  • Start the VM with the recently created disk to ensure it boots
  • Do anything to prepare the image
  • If everything goes well, capture the VM into a managed image

Finally, NEVER, ever truncate an image without shinking the last partition first! Otherwise, the image is lost (or at least hard to recovery; is better to start over).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .