Shrinking VHD Files for Xenserver and Citrix PVS

A question that often comes up when working with Dynamic vDisk when using Citrix Provisioning Server is does PVS automatically shrink the vDisk or is there a built in method to compact them. Citrix PVS does not have a way to do this automatically but with a few steps this can save some disk space on your storage.

** Update: 9/23 You can find the PowerShell script to Shrink VHD files here

A good use case is lets say you clean up your images and remove old programs, installation files and things you no longer need, you will notice the file size does not go down. In order to maximize your storage investment, you want to keep these images as small as possible so you can do this on a regular basis.

This method can also be used to shrink any VHD files which are used by Hyper-V, Citrix PVS, and you can even configure Windows 2008 and Windows 7 to use VHD files.

Basic steps

  1. Make a copy of the VHD file that you have to compact 
  2. Open Server Manager
  3. Expand Storage
  4. Right-click on Disk Management on the server.
  5. Select Attach VHD.
  6. Select the required VHD File (The VHD appears as a volume on the server.)
  7. Defragment the drive for performance and storage optimization. (After defragmentation and optimization, the data on the VHD is now all at the beginning of the disk and defragmented.)
  8. Detach the VHD from within Disk Management. (Ensure that you do not delete the VHD when you are detaching the VHD.)
  9. Run the DISKPART command from a command Window.
  10. Run the following commands, substituting the path to your VHD file
    1. select vdisk file =”c:pathvdisk.vhd”
    2. attach vdisk readonly
    3. compact vdisk
    4. detach vdisk 
    5. exit

Coming soon I will write a script that will execute against a folder and compact all VHD files in the folder. Keep an eye out here for that script 🙂 

One Response to Shrinking VHD Files for Xenserver and Citrix PVS

  1. attenpting this with pVS 6.1 on Win2k8, errors as cannot be performed on a virtual disk of this type.