Welcome to MSDN Blogs Sign in | Join | Help

Compacting the virtual hard disk of a Linux virtual machine

If you have installed Linux (or a *BSD variant) inside of a virtual machine under Virtual PC or Virtual Server - you will probably want to compress the virtual hard disk after a while, to reclaim any unused space on the virtual hard disk.  In order to do this you will need to 'zero-out' the virtual hard disk first.  Now - we provide a handy tool to do this under Windows - but under Linux all you need to do is to run the following command:

cat /dev/zero > zero.dat ; sync ; sleep 1 ; sync ; rm zero.dat

This will create a very large temporary file that has nothing but binary zeros in it, synchronize the hard disk and delete the temporary.  Once this has completed you can shutdown the virtual machine and compact the virtual hard disk by editing it with the virtual hard disk wizard.  Using this command I was able to reduce my SuSE virtual machine from 6.9GB to 4.12GB in size.

Cheers,
Ben

Comments

Wednesday, August 24, 2005 1:38 AM by eric

# re: Compacting the virtual hard disk of a Linux virtual machine

You can also use the && operator (bash shell)

command1 && command2 && command3 ....etc.

Each extra command will not start until
previous command finishes without error,

Wednesday, August 24, 2005 4:24 AM by John Elliott

# re: Compacting the virtual hard disk of a Linux virtual machine

But in this case the first command is expected to finish with a "disk full" error, right?
Wednesday, August 24, 2005 5:38 AM by Ian Brown

# re: Compacting the virtual hard disk of a Linux virtual machine

I also used wipe2fs:

http://www.cs.pdx.edu/~cklin/wipe2fs/

The results are still not as good as with the windows compacter though given a similar amount of data.
Wednesday, August 24, 2005 8:16 AM by Jonathan

# re: Compacting the virtual hard disk of a Linux virtual machine

Don't know about Linux, but with Windows guest OSs, I create a new VHD, attach it to the VM, boot from a (virtual) DOS diskette with Ghost8, and do a disk-to-disk copy, and then chuck the old VHD. It does all phases (defrag guest, zero-write guest, compact VHD) in one go, and usually achieves better results (mostly because of the defragger).

I'd imagine Linux would have some Ghost-equivalent util that could boot from (virtual) diskette or CD.
Wednesday, August 24, 2005 11:06 PM by eric

# re: Ghost for Linux, g4u

Here's your Linux-Windows rosetta stone (scroll down).

http://linuxshop.ru/linuxbegin/win-lin-soft-en/table.shtml
Thursday, August 25, 2005 5:29 AM by Mr. Ed

# re: Compacting the virtual hard disk of a Linux virtual machine

Better make that:

cat /dev/zero > zero.dat ; sync ; sleep 1 ; sync ; rm -f zero.dat

Just left my VM with no space for a few hours, because Linux wanted to be doubly sure that I wanted to remove zero.dat.
Thursday, August 25, 2005 1:50 PM by Mr. Ed

# re: Compacting the virtual hard disk of a Linux virtual machine

I had a 10 GB VHD, default type, Fedora Core 3. I zero'd out the disk, as above, so it grew to 16 GB. Then I compacted it. Now it's 13 GB. I had a net _increase_ of 3 GB. Not good.
Friday, August 26, 2005 6:34 PM by Virtual PC Guy

# re: Compacting the virtual hard disk of a Linux virtual machine

Hi Mr. Ed,

Zeroing out a drive should never cause it to grow in size. Do you know what file system you are using?

Cheers,
Ben
Friday, September 09, 2005 4:36 PM by Jacek

# re: Compacting the virtual hard disk of a Linux virtual machine

I have same situation. I think reason is virtual drive is growing up tu 16GB - dynamic size. My "dat" file was 11,7 GB Big, "shrinking" vhd file from 3,5 to 7GB.
Friday, September 09, 2005 8:04 PM by Chris

# re: Compacting the virtual hard disk of a Linux virtual machine

To wipe swap partitions, try this, substituting your swap partition for /dev/hda2:

swapoff -a
dd of=/dev/hda2 if=/dev/zero
mkswap /dev/hda2
swapon /dev/hda2

Then, of course, you'll want to shutdown the machine and compact it with the Virtual Disk Wizard.
New Comments to this post are disabled
 
Page view tracker