# Errors when compacting VirtualBox VHD Image

My host OS is OS X Lion running a Windows XP client with VirtualBox 4.2.0. I'm wanting to make a copy of the image and compact it to share with a colleague. First, I used the VirtualBox command line to clone the image via clonehd.

VBoxManage clonevdi /path/to/image.vhd /new/path/to/image.vhd


Next, I tried compacting the image with the following command:

VBoxManage modifyhd /new/path/to/image.vhd --compact


In which I'm presented with the following error:

Progress state: VBOX_E_FILE_ERROR
VBoxManage: error: Failed to compact hard disk
VBoxManage: error: Could not compact medium '/new/path/to/image.vhd' (VERR_INVALID_STATE)
VBoxManage: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component Medium, interface IMedium
VBoxManage: error: Context: "int handleModifyHardDisk(HandlerArg*)" at line 577 of file VBoxManageDisk.cpp


I've tried my hand at resolving the issue by searching, but I've came up empty handed up to this point. Any help will be greatly appreciated.

-

You can only compact VDI images using the VirtualBox commandline as far as I know. You can use 3rd party tools to compact a VHD - Google is your friend.

If your host is Windows 7 or Windows Server 2008 R2, you should be able to use compact vdisk according to the page at technet.microsoft.com/en-us/library/gg252556.aspx.

In the VirtualBox world, here's how...

VBoxManage modifyhd path\to\disk.VDI --compact


...if you've created a disk in a different format - e.g. VHD, exec the following command first...

VBoxManage clonehd "path\to\vhdformat.vhd" "path\to\vdiformat.vdi" --format vdi


See this example..., as well as this comprehensive example by Matt Wrock on how to optimise a VirtualBox VM, ready for packaging.

-