Compacting Hyper-V VHDX Files of Ubuntu Linux VMs

Note: if your guest OS supports the trim command you can use the faster method described in this article.

If you configured your Hyper-V virtual machines with dynamically expanding virtual disks you will find that the VHDX files backing the virtual disks always grow in size, they never shrink. Eventually this becomes a problem either directly because of increased storage requirements or indirectly because backups take longer. Compacting VHDX files is possible but more complicated than I feel it should be. I have described the process for Windows VMs here. This article shows how to do it for Ubuntu Linux VMs.

Assessment

I am describing the process of reducing virtual hard disk size using our Ubuntu web server VM as an example. I distinctly remember that the VHDX size was approximately 24 GB when I installed the machine about 1.5 years ago. Let’s take a look at the current size:

Wow, that’s a whopping 60 GB! The VHDX’ size more than doubled! Let’s see what we can do about that.

Of course, you can get the same information from Hyper-V Manager’s GUI, too, by inspecting the virtual hard drive:

(Above screenshot was taken at a later time, thus the different size)

Preparation

The key to success is to zero all deleted blocks on the disk. For performance reasons the OS does not do that but simply marks those blocks as deleted, keeping the actual data as it is. But we can politely convince Linux to fill all deleted blocks with nulls using the following commands: