10

I am using Linux as guest OS in VirtualBox. I deleted huge number of files from its filesystem. Now i want to shrink the filesystem image file (vdi). The shrinking works by compressing filesystem image wherever it has "null" value in disk.

It seems an application called zerofree can write "null" into free space of filesystem in such a way that it becomes sparse. But the instructions say it works only on ext2/ext3. I have ext4 on my guest OS.

  1. Why won't it work on ext 4 (reason cited is "extents", but can someone shed more light on it) ?

  2. Will it work, If i mount the ext 4 as ext 3 and then remount as ext 4 ?

  3. Any other tools that can do similiar thing as zerofree on ext ?

2
  • 2
    According to the link there, zerofree works on ext4.
    – psusi
    Commented Apr 3, 2012 at 13:51
  • I'm not sure if it works at your time or not, but the manpage said that it supports ext4
    – phuclv
    Commented Aug 4, 2015 at 11:39

2 Answers 2

24

The page you reference (http://intgat.tigress.co.uk/rmy/uml/index.html) states:

The utility also works on ext3 or ext4 filesystems.

So I'm not sure where you're getting that it doesn't work on ext4 filesystems.

Note that the zerofree utility is different from the zerofree kernel patch that is mentioned on the same page (which indeed does not seem to have a version for ext4).

Update: At least in the case of VirtualBox, I don't think you need this utility at all. In my testing, on a stock Ubuntu 10.04 install on ext4, you can just zero out the filesystem like so:

$ dd if=/dev/zero of=test.file

...wait for the virtual disk to fill, then

$ rm test.file

and shut the VM down. Then on your VirtualBox host do:

$ VBoxManage modifyhd --compact yourImage.vdi

and you'll recover all the unused space.

15
  • Thanks for your reply. The manpage of zerofree did not specify ext4, thats why i needed an alternative. Commented Apr 14, 2011 at 8:37
  • 1
    The alternative proposed (dd if=/dev/zero of=test.file) worked for me! Much simpler than other solutions.
    – Stefano
    Commented Nov 5, 2011 at 14:58
  • 3
    This should be the accepted answer. Works beautifully. Commented Jun 6, 2012 at 18:19
  • Do a sync after the dd.
    – ignis
    Commented Jan 28, 2013 at 9:47
  • dd idea worked for me too. Nicely done!
    – Stefan
    Commented Apr 15, 2014 at 14:53
1

You could use e2fsprogs, there is a version that is usable with ext4.

Make yourself superuser:

# su

Unmount your drive:

# umount /dev/sda1

Remove the journal of your drive:

# tune2fs -O ^has_journal /dev/sda1

And then resize your file system:

# resize2fs /dev/sda1 30G

1
  • Thanks for ur reply, but I don't want to shrink the partition but the filesystem image(vdi) of the guest. Commented Apr 12, 2011 at 3:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .