How to make Azure Ubuntu Linux image less than 32G in size

Lev Petrushchak
2 min readSep 22, 2021

--

Photo by Markus Spiske from Pexels

The smallest image of Ubuntu Linux you can get in Azure is 32 GB. Although it’s not a big difference in price between 32G and 16G volumes, it can still make a quite big difference if you are using hundreds or thousands of instances. Luckily there is workaround — make your own custom image.

Worth mentioning that Azure Managed Disks are billed by specific size, e.g least possible is 4 GB, then 8 GB, 16 GB and so on, but volume size can be set to anything you like. What does it mean? For example, disk size can be set to 35 GB, but in fact, it will be treated as the next available standard disk size which is 64 GB, and charged accordingly. It’s something important to keep in mind.

First of all, let’s download the image from the official Ubuntu repository and unzip it (just make sure you have enough of free space)

$ curl https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64-azure.vhd.zip -o focal-server-cloudimg-amd64-azure.vhd.zip
$ unzip focal-server-cloudimg-amd64-azure.vhd.zip

Azure supports VHD images — Virtual Hard Disk. From Wiki documentation, we can see that VHD is a publicly available file (or image) format specification that allows encapsulation of the hard disk into an individual file. Ubuntu image in Azure is VPC type (VirtualPC compatible image). In this format, we can’t make any changes so it needs to be converted to RAW.

This can be done with the help of Qemu tool. In Ubuntu, it can be installed like this:

$ apt-get install qemu-tools

let’s get some info

$ qemu-img info livecd.ubuntu-cpc.azure.vhdimage: livecd.ubuntu-cpc.azure.vhd
file format: raw
virtual size: 30 GiB (32213303808 bytes)
disk size: 30 GiB

and convert it to the RAW image format

$ qemu-img convert -f vpc -O raw livecd.ubuntu-cpc.azure.vhd livecd.ubuntu-cpc.azure.img

and check info again

$ qemu-img info livecd.ubuntu-cpc.azure.imgimage: livecd.ubuntu-cpc.azure.img
file format: raw
virtual size: 30 GiB (32213303296 bytes)
disk size: 1.39 GiB

As you can see, in fact just 1.39 GB of space is used, so we can easily create 8 GB or even 4 GB partition for the appropriate volume

$ qemu-img resize -f raw --shrink livecd.ubuntu-cpc.azure.img 8G

and what do we have

$ qemu-img info livecd.ubuntu-cpc.azure.imgimage: livecd.ubuntu-cpc.azure.img
file format: raw
virtual size: 8 GiB (8589934592 bytes)
disk size: 1.39 GiB

and finally — convert it back to VPC type

$ qemu-img convert -o subformat=fixed,force_size -f raw livecd.ubuntu-cpc.azure.img -O vpc livecd.ubuntu-cpc.azure.vhd

Now livecd.ubuntu-cpc.azure.vhd can be uploaded to Azure blob storage and used to create a custom image and set up Ubuntu servers with small root volumes. See more about custom images in this documentation and FAQ about managed disk pricing

--

--

Lev Petrushchak
Lev Petrushchak

Written by Lev Petrushchak

Senior Site Reliability Engineer at Quiq

Responses (1)