» tech notes for power users.

Primary Menu

2 ways to compact your Dynamically expanding VHDs.

When you play with Hyper-V 2012 Virtual Machines on your lab, it is a best practice to use Dynamically expanding Virtual Hard Disks (VHDs) for your VMs.

The difference in performance is simply unjustifiable.

'Though, the moment you start Installing/Uninstalling Applications & Adding/Removing Data on your VMs (based off dynamic VHDs), those VHDs start growing indefinitely in size (ie. even if you uninstalled/deleted data from the VM).

For example, you could end-up with a dynamic VHD which:

On your Hyper-V Host uses ~70GBs of physical hdd space.

Inside your VM the used space is reported as ~20GBs.

Now that's a ~50GB difference!

I'm sure you might want to allocate those 50GBs to more pr0n ^..^ "other" projects.

That is especially important when those 50GBs are hosted on your expensive SSDs.

2 ways to compact your differential VHDs.

The 2 ways I'm referring to will provide you the same result.

One is GUI-based while the second is CLI-based:

With the "Edit Disk..."-wizard (from Hyper-V Manager).

With diskpart (on an Admin CMD).

Your choice.

Compact VHD Requirements.

To successfully compact VHDs, there are 2 important requirements.

You'll have to delete all the Snapshots.

You'll have to Shutdown the VMs (not "Turn Off").

Dynamically expanding VHDs are an excellent choice for your lab.

In production environments, Dynamically expanding VHDs are more expensive to maintain.

...And Snapshots are NOT an excuse to not perform Backups!!!

1. Compact VHD with "Edit Disk..."-wizard (from Hyper-V Manager).

Open Hyper-V Manager.

Make sure you've satisfied the "Compact VHD Requirements".

Click on "Edit Disk..." (on the right).

The Edit Virtual Disk Wizard will start.

Edit Virtual Hard Disk Wizard

Click on Next to Select the VHD.

Edit Virtual Hard Disk Wizard - Locate Disk

Click on Browse.

Select you Dynamically expanding VHD.

Click on Next to Continue.

Edit Virtual Hard Disk WIzard - Choose Action

Select "Compact".

Click on "Next" to Continue.

Edit Virtual Hard Disk WIzard - Summary

The last part of the wizard will show you a Summary of your selections.

Click on the "Finish"-Button to Compact your Dynamically expanding VHD.

2. Compact VHD with diskpart (on an Admin CMD).

With diskpart the process is easier to document.

Make sure your VM has been Shutdown and there are no Snapshots.

Run a Command Prompt as Administrator.

Type "diskpart" to launch MS' CLI Disk partitioning super-tool.

diskpart while it compacts a vhd.

After you run diskpart, the prompt will change to "DISKPART>".

To compact your Dynamically expanding VHD, type the following commands at the diskpart prompt:

Be aware that sdelete will expand the VHD to it's max size. So if your very low on space on your hyper-v host storage and don't have enough room to hold the maximum size of the VHD you want to compact, don't use sdelete.