Thursday, November 25, 2010

Shrink a VHD

If you have created a VHD using a large size (maybe using the default of 127GB) because you thought "it's dynamically expanding, so I may as well make it large to leave room for expansion", you may be in for a surprise.

My surprise occurred when I discovered that native booting to a VHD causes that VHD to instantly expand to it's fully allocated size.  This was a problem for me because I had used 150GB as the VHD maximum size, but I only had a 300GB HD.  After a few weeks I managed to fill half of my HD with other files.  I was then unable to boot to my Windows 7 VM. 

This is because my Windows 7 VHD was set for a maximum size of 150GB.  When Windows 7 boots to the VHD in native mode, it instantly expands the VHD to it’s maximum size (150GB in my case).  Even though the Win7.VHD file itself was only 30GB, it still expands that Win7.VHD file to 150GB.  Well, since my HD free space had been reduced to 140GB, I received a BSOD when trying to boot.

I searched the Internet for ways to reduce the size of the VHD file and came across all sorts of elaborate methods.  I stumbled across one that I found to be easier.  If you know why this technet blog has so many more steps, please let me know: http://blogs.technet.com/b/tonyso/archive/2008/10/09/hyper-v-how-to-shrink-a-vhd-file.aspx  Just do me a favor and make a backup copy of your VHD first.

 

1. Attach the VHD

You'll have to boot to Windows 7 or Server 2008r2.  This usually requires transferring your VHD file to another computer.  Open disk management (start > run > diskmgmt.msc).  Click Action > Attach VHD, and browse to your VHD file.

 34

 

2. Shrink the volume that is contained within the VHD.

Locate the volume on the VHD that should now be listed in Disk Management.  Right-Click on it and select Shrink Volume.  Windows will scan the volume.

5

I found the next selection odd, but you need to enter how much space you want removed from the VHD.  Below that it will list the size that the VHD will be after it's shrunk.

6

I was surprised to find that the volume that contained my almost new installation of Windows 7 VHD could only be reduced to 86,223MB.  You see, the VHD file itself was only 35,515MB.  I ran it through a defrag at this point (the VHD file is already mounted as a normal HD) just to see if that would make a difference:
Before Defrag: 86,223MB minimum size of VHD
After Defrag:  86,223MB minimum size of VHD
So apparently I have some "unmovable files" in my VHD that won't let me reduce the size as much as I'd like, but it's better than nothing.

Click the Shrink button and you should see unallocated space now appearing on your VHD:

7

3. Detach your VHD


Go back to Disk Management, right-click on your VHD disk, and select detach. 8

Don't check the box for "delete the virtual hard disk" or it will delete your VHD.

 9

 

4. Resize your VHD


Download and install VHD Resizer:

The rest is straightforward.  Run VHD resizer, select your VHD file, specify where to save the new VHD file, set the new size and click resize:

 

10

That's about it.  It takes a while for resizer to run (about 45 minutes for my scenario).  When you boot to the VHD, it should now show a smaller volume for the HD contained in the VHD.

No comments:

Post a Comment