Shrinking a VMDK

Resizing a disk in VMWare vSphere (or ESX 4i, for that matter) is a piece of cake - at least, when it comes to increasing diskspace for a VM. All you do is change the VM's settings, run a tool like Diskpart (if you're using Windows in your VM) - and you're set.

But - what about shrinking it? Now that's a whole different ballgame. Especially if the VM is running Windows Server 2008 R2 - and you want to resize its boot disk... with it's "interesting" disk layout...

Here's the cookbook:

  • For starters, I made a clone of the VM in question. Always a good idea to not play with live data...
  • Next, I grabbed myself a copy of the (free) Ultimate Boot CD (http://www.ultimatebootcd.com/download.html) and put it in a Datastore which is reachable by the virtual machine.
  • Then, I added a second disk (of the desired size) to the virtual machine and set the Ultimate Bood CD's ISO file as source for the CD drive (just don't forget to tick the "Conenct at Startup"checkbox...). While in the settings I also told VMWare to boot the VM into BIOS on the next boot.
  • After turning on the VM, the BIOS screen came up. Here I verified that the machine would use the CD as first boot device.
  • Rebooting the machine brought up the Ultimate Boot CD's menu. I opted for running Parted Magic
  • I fired up one of the included partition managers to see what my disks were showing up as. It came as little surprise that my "large" disk was /dev/sda and that my new, smaller disk was being seen as /dev/sdb
  • Then I used the same partition manager to reduce the size of the partition on the original disk to just a bit above that of the used space on the disk (and smaller than the target disk, of course)
  • The next step might look hard - but it's not. I opened a console window and ran the following command: dd if=/dev/sda of=/dev/sdb
  • Since this is doing a bit-by-bit copy, things took a while (be patient - there's no progress bar or the like). After about 15 minutes dd quit with an error saying that the target was full - which is obvious, since the disk is smaller than the source. That's no problem, though.
  • Next up was to shut down the VM and adjust its settings: The 'original' (large) disk was removed, the new disk got a new SCSI id - that of the old disk, to be exact (SCSI 0:0, in my case). I also turned the option to connect the CD on startup back off.
  • On booting the machine, Windows complained - obviously. I let it run CHKDSK - and it fixed whatever it wanted to fix. After that it rebooted - and Windows came up nice and shiny.
  • Once in Windows, it was time for the final step: expanding the partition to fill the entire space of the new disk. To do so I opened a cmd command-box and typed diskpart. With the command "list volume" I got to see the volumes on the disk. In my case, the volume which I wanted to use the entire disk was volume 2 - so I typed¬† "select volume 2". After that thre was one thing to do: to type "expand".

Et voila - a disk, smaller than the original, filled to the brim with its own partitions.