How To Shrink a VMDK with ESXi5

I’ve had the luxury of working quite a bit with ESXi5 and how it relates to storage. With ESXi5, you can easily expand your virtual disks (VMDK file) for the VM on the fly, however there isn’t a simple way to reduce the size of your drives. As I’ve learned, best practices for VM’s is to always start small, as you can always increase later on with ease. Unfortunately, the previous administrator didn’t honor this, and started everything out with 600gb drives, and only utilizing 25gb. Needless to say, my SAN was getting chewed up by VMDK’s that only using a fraction of the space allocated for it. After some research, this was the method that I use to reduce the size of the VMDK.

I should probably preface all of this that this is a dangerous procedure, and you run the risk of hosing your VM, so proceed at your own risk.

Most of my VM’s are windows machines, so I first need to reduce the windows partition. First I’ll need to defrag the drive. Then, open up the Disk Management via the MMC console snap-in, and right click on the drive, and select Shrink. It will tell you the maximum amount that it can shrink. If you want it to shrink more, it means that certain files are locked towards the end of the disk. In my case, I was able to delete/move the user profiles off of the disk, and that’s what was preventing me from reducing the disk to the size I wanted.

Great, so in this case, I was able to reduce a 600gb disk to 38gb.

Now I need to power down the VM, and SSH into the host and copy the VMDK file to make a backup of it. Hold onto these in case something goes wrong:

cp vmname.vmdk vmname-original.vmdk
cp vmname-flat.vmdk vmname-original-flat.vmdk

Now let’s open the vmdk within vi and modify the expected size. Towards the top, you will see a line that appears similar to the following:

# Extent description
RW 1258291200 VMFS “vmname-flat.vmdk”

The number value will need to be changed to the desired size using the following formula (x = desired size in GB):
vmdk_size = [x * (1024*1024*1024)] / 512

Because I like round numbers, I decided to make my new drive size of 40gb, so my new Extent description was as follows:

# Extent description
RW 83886080 VMFS “vmname-flat.vmdk”

Now I need to clone the drive to get it to the new size:

vmkfstools -i vmname.vmdk vmname-new-size.vmdk

Assuming all goes well, I will new delete the original (because I already made a copy of it just in case), and clone the disk to the original file name:

rm vmname.vmdk
rm vmname-flat.vmdk
vmkfstools -i vmname-new-size.vmdk vmname.vmdk

Now I should be able to start the VM again, and the new disk size will be shown!

This entry was posted in Uncategorized. Bookmark the permalink.

Comments are closed.