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).
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.
Click on Next to Select the VHD.
- Click on Browse.
- Select you Dynamically expanding VHD.
- Click on Next to Continue.
- Select "Compact".
- Click on "Next" to Continue.
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.
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
"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:
famx11.vhd After compaction:
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 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 -z H:
Where "H:" is the drive to zero-fill.
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: