Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ?

share|improve this question
According to the link there, zerofree works on ext4. – psusi Apr 3 '12 at 13:51
I'm not sure if it works at your time or not, but the manpage said that it supports ext4 – Lưu Vĩnh Phúc Aug 4 '15 at 11:39
up vote 2 down vote accepted

One way to go is to create a second disk image, add it to your guest OS and copy files from one to the other. Make the second disk bootable and remove the first one.

Another way is to

  1. resize your filesystem with resize2fs to its minimum size possible
  2. then resize the partition with parted resize to the same or a bit larger size
  3. create a new partition in the new unallocated space and zero it out with dd if=/dev/zero of=/dev/sdXY
  4. use VBoxManage modifyhd <uuid>|<filename> --compact to shrink the image.
  5. resize the partition and then the filesystem to their original size.
share|improve this answer
Thanks, the second solution better fits my case as the virtual disk size is greater than the actual hdd. – rajaganesh87 Apr 14 '11 at 8:34
@Mr.Confused.A.Lot, in this case you might not be able to zero the whole disk at once. You may try doing it in small chuncks, e.g. create small partitions, write zero to one of them, compact vdi, repeat. – forcefsck Apr 14 '11 at 9:36
The secure-delete toolkit includes a tool that supposedly overwrites blocks that are marked as unused. That followed by the VBoxManage compact command might be safer than messing with the partitions. – Perkins Feb 24 '15 at 20:04

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.

share|improve this answer
Thanks for your reply. The manpage of zerofree did not specify ext4, thats why i needed an alternative. – rajaganesh87 Apr 14 '11 at 8:37
The alternative proposed (dd if=/dev/zero of=test.file) worked for me! Much simpler than other solutions. – Pisu Nov 5 '11 at 14:58
This should be the accepted answer. Works beautifully. – Chris Pratt Jun 6 '12 at 18:19
Do a sync after the dd. – ignis Jan 28 '13 at 9:47
dd idea worked for me too. Nicely done! – Stefan Apr 15 '14 at 14:53

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

share|improve this answer
Thanks for ur reply, but I don't want to shrink the partition but the filesystem image(vdi) of the guest. – rajaganesh87 Apr 12 '11 at 3:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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