'

2 ways to compact your Dynamically expanding VHDs.

March 15th, 2014 by Andrea Matesi 43572 Views

 

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:

  1. With the "Edit Disk..."-wizard (from Hyper-V Manager).
  2. With diskpart (on an Admin CMD).

Your choice.

 

Compact VHD Requirements.

To successfully compact VHDs, there are 2 important requirements.

  1. You'll have to delete all the Snapshots.
  2. 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

Edit Virtual Hard Disk Wizard

Click on Next to Select the VHD.

 

 

Edit Virtual Hard Disk Wizard - Locate Disk

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

Edit Virtual Hard Disk WIzard - Choose Action

  • Select "Compact".
  • Click on "Next" to Continue.

 

 

Edit Virtual Hard Disk WIzard - Summary

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

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:

select vdisk FILE=C:\HyperV\Virtual Hard Disks\vm01.vhdx
compact vdisk

"FILE=" points to your VM's VHD Location while "compact vdisk" starts the compaction process.

 

Conclusion & Proof.

As I said at the beginning, you might end-up saving up-to ~50GBs!

Proof - after running diskpart on one of my mistreated VMs ("famx11.vhd"):

famx11.vhd Before compaction:

02. compact-VHD-before

 

famx11.vhd After compaction:

03. compact-VHD-AFTER

 

BONUS: Sdelete Vs Precompact.

Now, if you don't get the desired results, that means hidden data (somehow) is still on your drive...

That is especially true when the steps described above give you risible results.

Not everything is lost!

Compaction alone might not always work because the data might still be within your dynamic VHD(X) in the form of hidden (ignored) data.

To get rid of this hidden data, you might wish to zero-fill all the extra (free but ignored) space.

I don't want to get into a discussion of what tool is "best".

'Though, in my experience, sdelete and precompact have worked quite well and are my best favourites zero-fill free space Windows tools.

They both have a very simple syntax and are pretty easy to use.

 

sdelete.

sdelete is another iteration of Mark Russinovich's excellent tools.

Among other things it makes coffee it zero-fills your dynamically expanding VHD(X)s.

Open a cmd prompt as admin and type:

Sdelete Example And Command Switches

Sdelete Example And Command Switches

sdelete -z H:

Where "H:" is the drive to zero-fill.

 

Precompact.

Precompact is a quite "old" MS-Virtual PC remnant (perhaps inherited by the glorious Connectix!).

Precompact does just that - prepares you dynamically expanding VHD(X) for compaction.

The tool simply cleans all your deleted data off your VHD(X) and that's all there it is to it.

  • You can simply Double Click on precompact.exe and it will automatically compact ALL you disks (usually you don't want that unless you have plenty of time...).

Here's a screenshot of precompact doing its sh1t in action on a Windows Server 2012 Virtual Machine:

Microsoft Virtual Disk Pre-Compactor Example and precompact parameters

Microsoft Virtual Disk Pre-Compactor Example and precompact parameters

Posted in Tips and Tricks., Virtualization | 5 Comments »

5 responses to “2 ways to compact your Dynamically expanding VHDs.”

  1. KyleNo Gravatar says:

    Hello,

    Thanks for the info. I have a couple of questions if you don't mind answering:

    1. The method using diskpart: is it the same as the optimize command that is run when using the GUI?

    2. Are the precompact.exe and sdelete utilities run inside the virtual machine, or on the host? Sorry if this is a dumb question... then i am assuming i run the compact utility again?

  2. Hi Kyle and thanks for your comment.

    1. I guess it's a yes (even 'though IDK for sure) - the result is the same.

    2. Yes, run precompact.exe and/or sdelete within the vm then compact the VHDX.

    Rgds,
    A.

  3. Andre BoomNo Gravatar says:

    Thanks for the great write up, much appreciated. Just to be sure, the "best" way is to use sdelete and then compact data and not first compact and then sdelete, am I correct? Thanks in advance.

  4. Andre BoomNo Gravatar says:

    Sorry,
    Missed your previous comment on the post.

  5. Jason SmithNo Gravatar says:

    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.

    Precompact will not expand your VHD.