4
Feb

Shrinking a Dynamic VirtualBox Disk Image

I hadn’t realized this before, but you can shrink a dynamic VirtualBox disk image. This is incredibly helpful if you’ve uninstalled programs or freed up a bunch of space and you want the .vdi image size to reflect that. Otherwise, the dynamic disk image will stay the same size it was before. The process is simple, but can be a bit involved so I’ll just touch on the basics and then refer you to a few guides that were really helpful when I did this for my Ubuntu 9.10 VirtualBox image. These guides can be applied to other guest images as well.

1.) Install “zerofree” on your virtualbox guest machine.
2.) Boot to safe mode (recovery mode) where you can access your root partition (/dev/sda1).
3.) Mount the root partition as read-only (mount -o ro /dev/sda1 /mnt/tmp)
4.) Run “zerofree /dev/sda1″
5.) Shutdown the virtual machine and run “VBoxManage modifyhd –compact /path/to/virtualboximage.vdi”

In-Depth Guides:
http://maketecheasier.com/shrink-your-virtualbox-vm/2009/04/06 — Keep in mind that zerofree does in fact support ext4 (I think this article is a tiny bit dated, but it’s a great one).

http://www.virtualbox.org/manual/UserManual.html — The all important VirtualBox Users Guide.

http://forums.virtualbox.org/viewtopic.php?p=29272#29272 — Another good reference for VirtualBox

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:


There's 11 Comments So Far

  • Suncat2000
    January 27th, 2012 at 7:11 pm

    These instructions don’t say how to “shink” a virtual disk, they just say how to “compact” – remove unused space and reduce its storage area on the host. They are not the same thing. The virtual disk stays the same size if you only compact it!

  • Kcho
    February 13th, 2012 at 10:36 am

    You can use “sdelete” in Windows partitions.

  • Ryan Murphy
    March 24th, 2012 at 2:46 pm

    Can’t mount root filesystem read only…

    keep getting mount /: device is busy

  • Dave Mista
    June 22nd, 2012 at 1:15 pm

    I had the same problem as Ryan with mounting – mount: / device is busy

    So I resorted to running the Ubuntu LiveCD, I had to install zerofree there, which required enabling the universe repository (checkbox on first tab of “Software Sources” tool from System menu) and running sudo apt-get update.

    The mount command I used was (after running mkdir /mnt/tmp):
    mount -o ro /dev/sda1 /mnt/tmp

    This worked perfectly for me with an ext4 drive, from 125 GB down to 20 – zerofree took at least an hour to run though, so I left it running over night. The compact command took another 90 minutes or so the next morning…

    Here’s a very recent post I found giving me confidence that this works for current versions of VirtualBox (I used 4.1.16) – No need to resort to cloning…
    http://jurajsplayground.com/2012/05/20/shrinking-a-dynamic-virtualbox-hard-disk-on-ubuntu/

  • Paolo
    October 23rd, 2012 at 5:10 am

    Here is what I found out in case the guest filesystem is reiserfs.

    1.) Install “reiserfsprogs” package (at least in a recent Ubuntu release). It contains debugreiserfs tool which can be used in place of zerofree (the latter only works on ext flavors).

    2.) [same as before] Boot to safe mode (recovery mode) where you can access your root partition (/dev/sda1).

    3.) [same as before] Mount the root partition as read-only (mount -o ro /dev/sda1 /mnt/tmp)
    Note: I think the partition doesn’t need to be mounted.

    3b.) [optional but recommended] You may wish to check that the filesystem is sane:
    reiserfsck /dev/sda1

    This is just to reduce the risk of zeroing out relevant data because of filesystem corruption. This is a rather quick step, so I really recommend to run it.

    4.) Run:
    debugreiserfs -U -Z /dev/sda1

    Note: the -U option is VERY important as it instructs the tools to only zero out the Unused blocks. These are undocumented options briefly mentioned in the source code (which has not been easy to retrieve, too):
    http://git.kernel.org/?p=linux/kernel/git/jeffm/reiserfsprogs.git;a=blob;f=debugreiserfs/debugreiserfs.c;h=289aa076dd18c030c08a3d4643c01dc551c17aef;hb=HEAD

    The filesystem bitmap is initially loaded with unused blocks marked to 0 and used blocks marked to 1, then inverted (because of the -U flag). The -Z option will zero out the blocks corresponding to a bit set to 1 in the loaded bitmap.

    This will take several minutes.

    4b.) [optional but recommended] Check the filesystem again:
    reiserfsck /dev/sda1

    5.) [same as before] Shutdown the virtual machine and run “VBoxManage modifyhd –compact /path/to/virtualboximage.vdi”

    This will likely take even more that step 4.

    General notes: using dd as mentioned in various other sites will make your vdi grow till you fill all the guest partition (or the host disk), which may be unfeasible if you’re already running out of space.
    Also, shrinking the reiserfs partition as suggested in another site will make the vdi grow a little bit, so same drawback as above with dd.

    Hope this helps somebody out there, as I couldn’find anything practical for reiserfs until I digged into the debugreiserfs sources :-7

  • Derek@TheDailyLinux
    October 23rd, 2012 at 8:02 am

    Thanks so much for this very detailed information. I’m sure it’ll be useful to many!

  • Tukler
    January 21st, 2013 at 1:41 pm

    For future reference: if you keep getting the “mount /: device is busy” after booting in safe mode, when trying to remount the filesystem in readonly, it means some process is keeping some file in use.

    If you are using Ubuntu (I’m using 12.10, but it should be the same for other versions), it most probably is the syslog daemon.

    So try to issue “service rsyslog stop” and then see if the mount works.

  • Attackwave
    June 30th, 2014 at 4:57 pm

    Hi,

    a long time ago…..

    Use CloneVDI (Windows) to copy and shrink size at one time (after zerofree)

Who Linked To This Post?

  1. Virtualbox ext4 | Jaykaymotors
  2. Tagging the Web Daily 04/09/2013 | PAB Skunkworks Weblog
  3. Shink/Compact VirtualBox vdi or vmdk images to save disk space ← Putztastic Tech

Share your thoughts, leave a comment!