Virtual Disk Compaction Confusion

You can shrink a fixed size virtual hard disk. Here's how.

Chris: I created several virtual machines with pre-allocated virtual hard disk files. I did this because I had heard that performance was better. However, my system's hard drive is about out of space and I'm losing several gigabytes to pre-allocated space that isn't being used by my VMs. I thought about cloning each VM's disk and then cloning the disk back to a new growable disk, but wanted to know if there's a better way. I'm currently using VMware Server for Windows.
-- Sanjay

You're not the first person to run into this problem, Sanjay, and lucky for you there is a much easier solution. You are correct in that you will get much better performance using VMware's pre-allocated virtual disks. This is because when you create the disk, all of the space assigned to the disk is assigned to the virtual hard disk's associated files. The result of using the pre-allocated disk is that even as you save more data to the VM's virtual hard disk, the actual virtual disk file does not grow. When a virtual disk file grows at will, it likely grows across non-contiguous blocks on its host system's hard drive. This results in a fragmented virtual disk file that ultimately slows VM performance.

By using pre-allocated disks, the virtual disk can sit on a contiguous range of blocks on the host's physical disk and thus not become fragmented as content is added to the virtual disk file. So when you have the available space, or if you plan to run a VM in production, then deploying VMs with pre-allocated virtual hard disks is definitely a best practice.

Please note that Microsoft's products (Virtual PC and Virtual Server) use slightly different terminology. With Virtual PC and Virtual Server, a disk whose entire space is allocated at the time it is created is known as a fixed size virtual hard disk. A disk that uses minimum space and grows as needed over time is known as a dynamically expanding virtual hard disk.

Now that my long winded explanation is out of the way, let's get onto how to solve your problem. While the VMware Server Console is front and center as the VMware Server management tool, there are also several command line tools included in the VMware Server installation folder (default location is C:\Program Files\VMware\VMware Server).

To convert a pre-allocated virtual disk to a growable virtual hard disk, you will need to use the vmware-vdiskmanager.exe tool. Here's the command's syntax:

vmware-vdiskmanager -r -t

For the "new type" parameter, there are four possible values:

  • 0 -- Single growable virtual disk
  • 1 -- Growable virtual disk split into 2 GB files
  • 2 -- Preallocated virtual disk
  • 3 -- Preallocated virtual disk split into 2 GB files

To convert the disk to a growable virtual disk, you'll need to use either 0 or 1 as the type. I prefer to allow growable virtual disks to consume single files that are no larger than 2 GB, so here's an example that uses a type value of 1:

vmware-vdiskmanager -r "E:\VMs\DC1\scsi0-0.vmdk" -t 1 "E:\VMs\Condensed\DC1\scsi0-0.vmdk"

Note that in my example, both my source and target folders are on the same disk. Since you mentioned that you were running out of disk space, you may need to have the new disk saved to another hard disk such as an external USB drive, and then copy the new growable disk back to the source drive after deleting the original virtual disk files.

For the Virtual PC users out there, you can convert a fixed-size virtual hard disk to a dynamically expanding disk by following these steps:

  1. In the Virtual PC Console, click the virtual machine you wish to edit and then click the Settings button.
  2. In the Settings dialog box, click the hard disk to modify and then click the Virtual Disk Wizard button.
  3. In the Welcome to the Virtual Disk Wizard widow, click Next.
  4. Click the Edit an Existing Virtual Disk radio button and click Next.
  5. Click the Browse button to locate and select the virtual hard disk to modify and then click Open. Then click Next.
  6. If the disk you selected is a fixed-size virtual hard disk, click the "Convert this virtual hard disk to a dynamically expanding virtual hard disk" checkbox and click Next.
  7. At this point, you can either replace the existing disk or select the "Saving the file as" radio button and enter the name for a new disk file. Once you make your selection, click Next. I recommend saving the disk to a new file, since this will give you a chance to test the new VHD file before deleting the original file.
  8. Click Finish. One the Virtual Disk Wizard prompts you that the disk was converted successfully, click Close. Note that the converted disk will still have the same size as the original disk. To reduce the disk size, you will need to rerun the Virtual Disk Wizard, select the new disk and then click the "Compact it" option.

When space is a consideration, growable or dynamically expanding disks definitely have their place. While the advice in this column may not be as usable as the Space Bag, which I just received as a holiday gift from my in-laws, hopefully it will get some of you out of a jam some day. As for my Space Bag, I have no idea what to use it for. If you have some ideas for me, please post them as comments to this article.

About the Author

Chris Wolf is VMware's CTO, Global Field and Industry.