Tuesday, January 19, 2010

How to Resize a Fixed VHD

I just set up a bootable virtual machine in Windows 7 running Windows Server 2008 R2. I realized after creating the VM that I didn't need as much capacity as I allocated for the VHD file. After several hours of failed attempts I finally came up with a solution and wanted to share.

1) I first attached the VHD using Disk Management in Windows 7.
2) Using DiskPart I shrank the VHD volume down to a manageable size running the following commands. Note I had 100 GB of free space.
-select disk 1 (varies depending on the number assigned to the attached VHD)
-select volume 0 (also varies depending on the number assigned to the VHD volume in the disk)
-shrink ( If you would not like to shrink the volume to the absolute minimum capacity I would recommend using the [desired=] [minimum=] parameters. I repeated this command until I shrank the volume to the desired size. You can use detail disk command to check the actual amount of space needed for the disk).
3) After shrinking the VHD volume down you'll notice in Disk Management that there is a certain amount of unallocated space in the VHD. To remove that unallocated space use VHDResizer to actually resize the VHD to the size of the volume you just shrank.