«

»

Oct 06 2013

Print this Post

Seriously, how to shrink a VHD!

I have been working lately on a case of over blown dynamic virtual hard disks (VHDs) eating up the space available on a cluster shared volume (CSV). The VHDs where expanded much more than the actual size of the data. So much that one VHD was resting on 136.7 GB of space while the data size were about 35 GB! The reason as you might guess, is Physical to Virtual (P2V) of several servers where the VHDs were set to use the same capacity of the physical disks and dynamic expansion.

This called for an immediate action, so we decided to convert all VHDs to fixed and of course, change the capacity of VHDs to have only 5 to 15 GB of free space depending on the type of data.

Here we get another block on the road. How do we shrink a VHD? I couldn’t find an all-in-one instructions on how to do it so decided to combine the knowledge I learned from many resources into this blog, the method I used have some technically not required steps, but I voted to doing them as they can save time and give some extra performance boost.

BACKUP YOUR VHDS

Yes, I meant those CAPS, after shutting down the VM take a copy of your VHDs and put them somewhere safe! Just in case things go nasty you can always revert to the backup. Better safe than sorry.

Defragment the partition(s) on the VHD

Shrinking a volume means that you will need to move the data living at the sectors near the end of the volume to somewhat in the beginning in order to mark the sectors as empty, otherwise data is lost. Windows will NOT allow you to shrink beyond the last written sector. Other tools (mentioned later) will copy the sectors to new locations so shrinking can continue.

I used Auslogics Disk Defrag Free to defrag and optimize the data on the volumes (optimization moves the system files to the beginning of the disk, giving an extra boost). I did this on two passes, one before shutting down the virtual machine (VM) and just after it by attaching the VHD to the host, this is to minimize the downtime required for offline defragmentation.

Defragmenting will also move most of the data (except those nasty unmovable files) to the beginning of the volume, minimizing the time the next step needs.

Shrink the volume on the VHD

You can use the Computer Management console to shrink the volumes, but it will not shrink beyond the last written sector, which is usually not enough. Therefore, I rely on MiniTool Partition Wizard 8 for the shrinking task, unlike what the name may hint, the software is nowhere near a “mini tool”; it can do just about anything you may dream to do on a HDD.

So again, I attach the VHD to the host and let MiniTool do its magic. This is done on two steps, first you right click the volume and select move/resize where I enter the desired size. Second you click Apply in the upper left area. This is a neat way of doing this delicate task allowing you to fully visualize how the disks will look like upon applying.

In most cases, there is only one volume in a VHD, except for system boot drives of newer operating systems that have a small partition in before the C drive that can be neglected. If you have more than one partition on the VHD, you may need to resize them and align them next to each other; otherwise, the next step might not be very efficient.

At this step, I would do another defragmentation pass on the shrinked volume; it will take less than a minute, but call me a perfectionist.

Resize the VHD

Now that you have plenty of unallocated space at the end of the VHD, you can resize it. For this step, I used VMToolKit’s VHD Resizer. It allows you to assign a new capacity to a VHD by copying the data sector by sector. So you cannot resize beyond the last sector allocated for a partition, hence all the shrinking we’ve been doing.

Reconfigure the VM to use the new VHD

Well that is a no brainer, but just a reminder to reconfigure the VM to use the new VHD instead of the old one. Which you can delete along with the backup created earlier after confirming that everything is working properly.

Next steps: Defrag the CSV

There is all sorts of debate around whether defragging the CSV is required or not. In my humble opinion, I am a fan of multi-level defrag; That is use fixed disks, defrag the CSV and defrag the VHDs from the guest machines. However defragging a CSV can be time consuming and requires some downtime for all the VMs on the cluster, so do your planning. A good article about this task can be found here.

That’s all, hope you find the information useful.

About the author

Walid AlMoselhy

Permanent link to this article: http://almoselhy.azurewebsites.net/2013/10/seriously-how-to-shrink-a-vhd/

6 comments

Skip to comment form

  1. Ahmed

    Thank you, very helpful detailed guide

    1. Walid AlMoselhy

      You are welcome!

  2. Girts

    Compact
    Detailed
    Helpful

    Thanks!

  3. Pat

    Some good tips there thanks, you can also shrink the virtual machine disk online in 2012 R2 after first decreasing the volume within the OS, I followed the steps here and it worked well http://www.rootusers.com/decreasing-disk-space-in-windows-server-2012-r2/ all with no third party tools needed.

  4. Peter

    Thank you very much, this helped a lot!

  5. Oswald

    Thank you! This saved me. Very simple, clear and informative post.

Leave a Reply

Your email address will not be published. Required fields are marked *