Page 1 of 1

Tip: (Better) Shrinking of Ext(2/3) partitions

Posted: 11. Aug 2007, 13:09
by the_camel
Hi,

Well I had the problem that I needed to shrink some VDIs, but the partition they were on had insufficient space to use the "dd -i /dev/null" trick.

I went to look for a solution and found this page Keeping filesystem images sparse whose author had the same problem, but solved it in a way which works equally well and does not blow up the partition to its maximum size:

He wrote a small programm zerofree.c which uses the ext2fs library to get a list of all the blocks, checks if the current block is free but non-zero (i.e. has been used at some point) and fills it with zeroes again. This allows the vboxmanage tool to compact the VDI again.

I used one little patch on the code:

Code: Select all

--- zerofree.c.orig·    2007-08-11 12:47:50.000000000 +0200
+++ zerofree.c· 2007-07-22 16:41:23.000000000 +0200
@@ -58,8 +58,8 @@ int main(int argc, char **argv)
 ·      ·       return 1 ;
 ·      }
·
-·      if ( flags & EXT2_MF_MOUNTED ) {
-·      ·       fprintf(stderr, "%s: filesystem %s is mounted\n",
+·      if ( ( flags & EXT2_MF_MOUNTED ) && ( ! (flags & EXT2_MF_READONLY) ) ) {
+·      ·       fprintf(stderr, "%s: filesystem %s is mounted rw\n",
 ·      ·       ·       ·       ·       argv[0], argv[optind]) ;
 ·      ·       return 1 ;
 ·      }
This patch allows you to shrink a filesystem while it is mounted, but only if it is mounted read only. (Which helps, since you can keep the zerofree binary on the to-be-compacted partition, and do not have to move it around.)

Another Tip: You might want to copy your VDI as a back-up :-)

To use it (in your VM)
1) Compile the (patched or unpachted) zerofree.c
2) Go to single user mode (i.e. "init 1")
3) Remount all your partitions read only (i.e. "mount -n -o remount,ro -t ext2 /dev/XXX /MOUNTPOINT")
3.1)The "-n" is so that mount does not try to update "/etc/mtab" which might be on the now read only disk
3.2)The "-t ext2" is because you really do not need journaling for that :-)
4)Make sure the filesystem is ok now (i.e. "fsck.ext2 -f /dev/XXX")
5)Call zerofree (i.e. "zerofree /dev/XXX")
6)Check if we did not break anything ("fsck.ext2 -f /dev/XXX")
7)Remount all your partitions back to normal (i.e. "mount -t ext3 -o remount,rw /dev/XXX /MOUNTPOINT")
8)Shutdown the VM

And last but not least, call vboxmanage to compact the VDI

Enjoy ;-)

- Camel

P.S. Dear Virtualbox Admins:
I tried to register once before, but the mailaccount I created was not ready by the time the confirmation mail was sent. I tried to contact you to activate the account, but never received an answer. Please delete the account "camel", since it is (as you can see) not longer needed.

New Version of Zerofree

Posted: 12. Aug 2007, 12:19
by the_camel
Ron Yorston, the author of zerofree posted a new version on his website, including the above patch and a makefile :-)

It can be found at: http://intgat.tigress.co.uk/rmy/uml/zerofree-1.0.1.tgz