Virtual PC Guy's Blog

-- Ben Armstrong, Virtualization Program Manager

Talking about core virtualization at Microsoft (Hyper-V, Virtual PC and Virtual Server).

Compacting the virtual hard disk of a Linux virtual machine

Compacting the virtual hard disk of a Linux virtual machine

  • Comments 11

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

Leave a Comment
  • Please add 4 and 4 and type the answer here:
  • Post
  • You can also use the && operator (bash shell)

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

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

  • But in this case the first command is expected to finish with a "disk full" error, right?
  • 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.
  • 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.
  • Here's your Linux-Windows rosetta stone (scroll down).

    http://linuxshop.ru/linuxbegin/win-lin-soft-en/table.shtml
  • 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.
  • 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.
  • 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
  • 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.
  • 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.
  • This didn't have quite the desired effect for me either. Linux VM says it's using 40G, VHD was 72G, compacting had no effect. Running the suggested command increased the VHD to 101G, compacting it reduced it to 93G :(

Page 1 of 1 (11 items)