Compacting the virtual hard disk of a Linux virtual machine

  • Comments 12

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.


Leave a Comment
  • Please add 8 and 1 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:

    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).
  • 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?

  • 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 :(

  • EmailOcean is a brand new platform to cater to your email marketing needs.

    It has been launched under the patronage of KOONK TECHNOLOGIES PVT. LTD. and is one of a kind platform for email marketing.

    There is not point in harping on about the advantages of email marketing,as at this point in time,email marketing is the sole way of marketing or promoting your product,service or message in an efficient and quick manner.

    EmailOcean provides you with the following advantages:

    Highly Economical: It provides promotional emailing at an amazingly low rate of $0.10 per 1000 emails. The customer only pay for what is utilised. This is the lowest price available for mass mailing.

    Web App to create and track campaign: The brilliance of EmailOcean lies in it making the process easier and faster for the customer,as compared to Amazon and Sendgrid, EmailOcean has a beautiful Web-app which makes creating and sending the campaign a lot more easier. Using this web-app one can also send and track their campaigns and get detailed analytics.You can see the users who have opened, clicked, unsubscribed and bounced.

    Inbox Deliveries: Their inbox delivery rate is phenomenal.

    Excellent Support: They also provide a support staff that is available at your beck and call 24*7.

    As of now,the registrations are open by invitation but register yourself with the website and keep self updated as to when they are open to all or for your surprise invitation code.

Page 1 of 1 (12 items)

Compacting the virtual hard disk of a Linux virtual machine