VM Exports in XenServer are a great way to make a backup of an existing virtual machine (VM) as a single file. The file contains your VM settings which includes everything from the name to the amount of memory you have given the VM. It also contains all of the data from the virtual disk drives that you have associated with your VM. One you have exported your VM you can take this file and then import it easily to other XenServer 5 systems. So it’s not only good for backups but you can also move VMs between different environments or even create your own virtual appliances. As space is always a premium here are some tips on minimizing the size of your exported virtual machines.

Don’t Export what you don’t Need

A physical or virtual disk drive is made up of a bunch of blocks. As you save new data on your disk it gets written to these blocks. Brand new blocks are empty but over time as you add and delete files the blocks get filled with data. And in the interest of making file delete operations fast when you delete a file the operating system does not actually clear all the blocks used by the files it just marks the space as free for future use.

Now when XenServer starts exporting the virtual disk it looks and sees data in these unused blocks and in the interest of not tossing anything away on the off chance you need it XenServer exports any blocks with data in them. This can end up making your export files larger than the need to be.

So what you want to do is a process called zeroing out the unused blocks. This will take any space that is no longer used and clear it out. Then when you do your export this data does not have to be exported and you get a smaller file. The process to clear or “zero” out the unused blocks is a little different between Linux and Windows.

NOTE: If you are using VHD backed file systems with XenServer such as NFS storage this has a side effect of maxing out the size of the VHD file on the XenServer box.

Freeing up Windows Blocks

Microsoft has a nifty little utility called SDelete that will run through and find any unused blocks with data and clear them out. You can download SDelete at the link below.

http://technet.microsoft.com/en-us/sysinternals/bb897443.aspx

Once you download the program open up a command prompt and run the command sdelete -c and it will do its magic!

Freeing up Linux Blocks

On Linux you can run a command and get the same result. Open up a shell prompt on your system and then run the following command for each of your file systems:

dd if=/dev/zero of=/deletemesoon; rm /deletemesoon

The first part of this command will fill up any free space (including blocks with unused data) with zeros. It will stop once it runs out of space. Then the second part of the command just deletes the big zero file.

Compress That Exported File

In the interest of not overloading the system that is exporting the virtual machine we don’t compress the data that is exported. So to really save on space after you do an export you can then use your favorite compression program to shrink down the exported file even more. I have been using winrar on Windows since it seems to handle large VM export files well and then using bzip2 or gzip on Linux.