myITforum.com

Welcome to myITforum.com Sign in | Join | Help
in Search

Tech Ramblings from a Higher Ed. Sysadmin

Three methods for shrinking a VHD

A while back we started using the P2V capabilities of SCVMM to virtualize a few of our servers.  Unfortunately, we made two mistakes.  First we have most of our drives set to Dynamic instead of Fixed.  Secondly we have over-allocated our disk space.  I setup down recently and figured out two methods to reduce the size of our virtual disk allocations and convert them to dynamic disk.  I have three methods.  So far the first is the method I am going with as much as possible.  The scond method (using xcopy) seems to be the quickest method for straight up data drives but will not capture a boot sector.  I am holding the third method in reserve for cases where I can't reduce a system drives partition as far as I want to (more on that later).  Server 2003 doesn't support the first method, so you will have to either find a different way to resize the partition, or use method three.

Additional Note: I found out the hard way this weekend that you really should look to see if there are any shapshots for the VM before you try to resize it.  Remove all of the snapshots and merge the VHDs before you start.

Method One using Disk Management and VHDResizer

This method has you first shrinking the disk partition to create unallocated disk space.  You can then use a free tool called VHDResizer to reduce the size of the VHD and convert it to a fixed disk in one fell swoop.  VHDResizer is available from here:  http://vmtoolkit.com/files/folders/converters/entry87.aspx

 On VM:

  • 1. Defrag the hard drive *
  • 2. Go to Disk Managment
  • 3. Right click the volume to shrink and select "Shrink Volume"
  • 4. Choose how small you want to make the volume and click OK
  • 5. Shutdown the VM

On Host:

  • 1. Delete/Merge any Hyper-V snapshots
  • 2. Run VHDResizer ** 
  • 3. Now you should be able to reduce the size of the VHD (Note: a little bug in VHDResizer seems to be you cannot choose the smallest size possible, say 20 GB but instead must add to it - 21 GB works)
  • 4. Change the VM settings and point to new VHD
  • 5. Start VM to validate everything is working
  • 6. Use the expand wizard from disk managment to expand your partition if needed.
  • 7. Start a Consistancy Check of the VM in DPM.
  • 8. Delete old VHD

Notes:

  * Defragment the hard drive. This step is needed if you want to "shrink" the size of the hard drive by more than 50%. Windows won't shrink more than 50% because of unmovable system files placed halfway on Windows volumes. Use a defragmenter that moves system files - the built in Windows defragmenter won't do this. Not all free/cheap defragmenters will work. Perfect Disk Defragmenter is a known solution because it will move Windows system files with its Boot Time defrag option. A free trial version is offered or ~$100 purchase.  Other disk partition solutions are also available.

I have however found this to not be a big issue on our Dynamic VHDs.  Since they are not actually using the entire disk I have been able to use the disk management Shrink capabilities to get my partitions where I want them.  I recommend trying the build in tool first, then consider either a 3rd party disk defrag program or the Alternated Method using ImageX documented below.

** VHDResizer doesn't give real feedback.  Once you click on resize it may sit there without any sign of process for a very long time.  This doesn't mean that it isn't actually creating the VHD in the background.  Even if you click cancle and close the window, it can be working in the background.  You can see it running as a process in the task manager.  YOu can watch the size of the VHD going up if you refresh explorer.  Beware of this.

Alternate Method using XCOPY
 
If you are trying to shrink a data drive (something without an OS on it) this method seems to be the quickest method.
 
On VM Host:

  1. Shutdown VM.
  2. Open computer management on your local computer.
  3. Expand Storage, right click  Disk Management and click on Attach VHD.
  4. Browes to VHD.  This adds the disk to the host system
  5. Create the new VHD.  This can be done while the image is being created.
    1. From Disk Management click Create VHD
    2. Choose a location to put the VHD. and the size you want it to be.
    3. Initialize Disk
    4. Create a New Simple Volume
      1. Use the entire volume
      2. Choose a driver letter and click next (W:\)
      3. Quick Format
  6. From an administrative command prompt run the following xcopy command
    1. XCOPY [Source]:\*.* [New]:\ /c/h/e/k/r
    2. Where you use the Source drive letter and the drive letter for the new drive
  7. Detach VHD
    1. Make sure "Delete the virtual disk..." is NOT checked!
  8. Change the VM Settings and point the hard drives to the new VHDs
  9. Start VM and verify everything is working correctly
  10. Start a Consistancy Check of the VM in DPM.
  11. Delete old VHDs

Alternate Method using ImageX

This method will clone a disk from one VM to anouther.  It does take the longest amount of time because you first have to create an image and then apply the image.  I have however found that this method isn't 100% relyable.  I have sussefully used it to reduce the size of a system drive VHD, but I have also had it fail at other times.

On VM Host:

  1. Open computer management on your local computer.
  2. Expand Storage, right click  Disk Management and click on Attach VHD.
  3. Browes to VHD.  This adds the disk to the host system
  4. Capture an image of the VHD
    1. Run Deployment tools command prompt with administrative rights.
    2. imagex /capture I: C:\NewVHD.wim "disk1" /compress fast /verify /check
  5. Create the new VHD.  This can be done while the image is being created
    1. From Disk Management click Create VHD
    2. Choose a location to put the VHD. and the size you want it to be.
    3. Initialize Disk
    4. Create a New Simple Volume
      1. Use the entire volume
      2. Choose a driver letter and click next (W:\)
      3. Quick Format
  6. Apply captured wim to new vhd
    1. From the Deployment tools command prompt (with admin rights)
    2. imagex /apply c:\newVHD.wim 1 W:\
  7. Detach VHD
    1. Make sure "Delete the virtual disk..." is NOT checked!
  8. Change the VM Settings and point the hard drives to the new VHDs
  9. Start VM and verify everything is working correctly
  10. Start a Consistancy Check of the VM in DPM.
  11. Delete old VHDs

References:

http://blog.coretech.dk/mip/resizing-a-microsoft-virtual-hard-disk-vhd/

http://social.technet.microsoft.com/Forums/en-US/winserverhyperv/thread/9f1290f4-4d91-4050-9afb-378fbf5bbc09/

 

Published Apr 05 2011, 11:13 AM by mclanem
Filed under:

Comments

No Comments

About mclanem

Sr Systems Administrator for the College of LSA @ UofM. Also ... MSI Candidate @ School of Information of UofM.
Powered by Community Server (Commercial Edition), by Telligent Systems