Thursday, October 18, 2012

Convert vmdk disk from Auto-growth to Preallocated

I made the unfortunate decision not to preallocate my 100GB C: on my SharePoint development machine. Since it was set to auto grow, only after a few months I had no less than 9 vmdk files, and the drive had ballooned out to over 200GB. With SSD space precious I had to try and compact this VM. Using the "Compact" tool within the VM settings didn't seem to make much of a difference at all. What I needed to do, so I thought, was "shrink" the vmdks. After reading a lot of very out of date help/forums from VMWare, it looked like I needed to fire up the vmware-vdiskmanager.exe and shrink the vmdk files via that. All good so far. Instructions lead me to believe I had to mount the disks first and prepare them. Since I am running Workstation 8, I think these instructions were quite out-of-dated. If you are interested you can mount/map a vmdk files to your host and browse it's contents (without the VM started!) - pretty cool! But this was not going to help my issue. Turns out I could skip the "mount and prepare" steps and lunch straight into the "shrink".

I located the vmware-vdiskmanager.exe at C:\Program Files (x86)\VMware\VMware Workstation and ran the command (-k is for shrinking a vmdk):

vmware-vdiskmanager.exe -k d:\MyVM\MyVeryLargeDisk.vmdk

Shrinking began, and I thought I was in the home straight now. On the first of the 9 vmdk files, the shrinkage was extremely poor (your mileage may vary). It only shrink about 2%. I tried this on the remaining vmdk files and the result was the same. Shrinking had not solved my problem, and in my case the overall VM reduced from 240BG to 235GB. I have no idea why shrinkage was so ineffective, especially as I knew I had a good 30% of free space within the VMs themselves. Plan B.

Since my D drive was preallocated at 50GB, there was 1 nice file of size 52,428,800kBs (50 x 1024 x1024). I need to convert my ballooned C drive into a prealloacted drive. I tried the follow command:

vmware-vdiskmanager.exe -r d:\MyVM\MyVeryLargeDisk.vmdk -t 2 d:\MyVM\newsmallerdisk.vmdk

The console app started ...
..."Creating disk 'd:\MyVM\newsmallerdisk.vmdk'" ...

And eventually (after about 3 hours on a SATA drive)
  Convert: 100% done.
  Virtual disk conversion successful.

Last steps were to :

  1. go into VMware Workstation
  2. open the VM (not power on)
  3. remove the non-preallocated hard drive
  4. add the new pre-allocated hard drive (newsmallerdisk.vmdk)
  5. Save and Start.
Done. I could then backup and delete those remaining old 9 vmdk files and happy days. Moved everything back to my modest SSD.

PS - some of those options like "-t 2" ... related to the disk type:
  Disk types:
      0                   : single growable virtual disk
      1                   : growable virtual disk split in 2GB files
      2                   : preallocated virtual disk
      3                   : preallocated virtual disk split in 2GB files
      4                   : preallocated ESX-type virtual disk
      5                   : compressed disk optimized for streaming
      6                   : thin provisioned virtual disk - ESX 3.x and above

1 comment:

  1. Before shrinking disk with vdiskmanager you have to mount it and wipe free space. vdiskmanager is a universal tool good for any file system. It uses OS routines to see if space is free. Otherwise it believes that only zeros can fill free space. It looks like this in windows:
    1. Crete batch file with such lines:
    vmware-vdiskmanager -d %1
    vmware-mount Q: %1
    vmware-vdiskmanager -p Q:
    vmware-mount -d Q:
    vmware-vdiskmanager -k %1
    2. Run that file using batch_file.cmd your_disk.vmdk
    If you modify script a little you could shutdown your vm, shrink it and start again with one click.