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
    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
    Aug 4, 2015 at 11:39

2 Answers 2

23

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. 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
    Nov 5, 2011 at 14:58
  • 3
    This should be the accepted answer. Works beautifully. Jun 6, 2012 at 18:19
  • Do a sync after the dd.
    – ignis
    Jan 28, 2013 at 9:47
  • dd idea worked for me too. Nicely done!
    – Stefan
    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. Apr 12, 2011 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.