Shrinking an ext3fs virtual disk in VMWare Fusion
Mac OS X — 28 Jul 2007 11:42 — 955 days ago

This is information from a tip/question I posted to the VMWare forums.

I downloaded a pre-built Fedora 7 Linux virtual machine and set up the guest OS installation according to my requirements. Among other things this meant ripping out tons of unused packages including everything graphical (gnome/X11) to save space (the yum front end to RPM was very helpful, it offers to remove all dependencies along with the requested package). This Linux VM will only be used in the background to relay some network traffic so I want it to use very little memory and disk space.

The disk image was about 5.4GB and after my yum frenzy, the installation used only a fraction of that and I wanted to shrink the virtual harddisk files accordingly.

Shrinking is not possible on a mounted file system so I booted the Linux VM from an attached ISO file of the Fedora 7 live cd (Fedora-7-Live-i686.torrent from http://torrent.fedoraproject.org/). BTW, this live cd is awesome, it has all the tools I needed, including the Logical Volume Manager LVM. I will be using it for all my future rescue operations...

Using the live cd, I successfully shrunk the ext2 root file system (resize2fs), then the LVM logical volume on which that file system resided (lvresize), then the LVM physical volume below the logical volume (pvresize) and finally, by deleting and recreating it with less cylinders in fdisk, the /dev/sda2 partition containing the LVM physical volume. I also dropped and recreated (lvremove/lvcreate) a second logical volume used for swap. I had to do this before the pvresize step because the second volume’s extents were located at the opposite end of the partition, all the empty space was in between the two logical volumes and I found no way to move extents (Is there really no way to do this? I was lucky that it was just a swap partition).

After all this, the guest OS booted sucessfully. The /dev/sda2 partition used only about 2GB and I wanted to reclaim the remaining 3GB. Since there is No X11 in the guest OS anymore, I guessed that I could not use the (GUI-based) vmware-tools. Are there any command-line vmware tools that I can use inside the guest?

In the host Mac OS X, I found the /Applications/VMware Fusion.app/Contents/MacOS/diskTool utility with its shrink (-k) option and tried this:

./diskTool -k 0 /path/to/f7.vmdk Shrink : 100% (9060864/9060864) done.

This took a while and I noticed no change in size.

I read somewhere that the utility needs a huge zero-filled file to mark the unused space. Back in the guest OS, I created a new partition /dev/sda3 with all the remaining space, formatted it as ext3 and put the zero-filled file in there:

cat /dev/zero > zero.fill; sync; sleep 1; sync; rm -f zero.fill

Running diskTool again did now indeed shrink the size of the files.

Thinking about it now, I guess diskTool’s shrink operation is just doing its compression thing on sparse disk images. I could probably have saved myself the trouble of all the resizing and just zero the unused data in the original root file system and then run diskTool.

As a last operation I wanted to merge the multiple virtual disk image files into one. I found vmware-vdiskmanager in /Library/Application Support/VMware Fusion:
$ ./vmware-vdiskmanager -r /path/to/f7.vmdk -t 0 ~/Desktop/f7.vmdk
Post a comment














Answer 6 * 2 =

Note: Comments take a while to appear. Please don't submit twice.

Live Comment Preview
 
Powered By blojsom