I recently was looking to free up some unused resources on our VMWare ESXi 5.1 environment. This is a good thing to do once and awhile to make sure you’re not over-allocating resources to certain VMs that don’t need it. In looking through the different servers, I found two old Windows Server 2003 VMs that had 100GB more disk space than they would ever need. I wanted to take that 200GB and add it to another server who’s data drive was filling up.
Shrinking the disks
Before you work with any disks in VMWare, it’s important to do a few things. First things first, backup your data. Secondly, I like to clean up the disk a bit – defrag and run chkdsk. I originally did this in order to use gparted (see What Didn’t Work below), but I noticed that the disks seemed to copy much faster once I did this. I had a 6 hour estimated time at first, but after cleaning it up it went down to 2 hours. Finally, make sure you don’t have any snapshots on the VM!
The best way I found to shrink the disks was to use the free VMWare provided utility called VMWare vCenter Converter Standalone. You may have used this before to do a Physical to Virtual migration, to convert Hyper-V to VMWare, etc. It’s a pretty handy tool. In this case, I’m using it to copy a VM to a new VM in the same VMWare environment. The part of this tool we’re most interested in is configuring the hardware on the new copy. Through the wizard, there’s a point to edit the settings of the new machine – here you can shrink the disk. It won’t let you go lower than the used space – in my case 30GB. I took 100GB off to make the disk 100GB.
I powered off the machine, let the converter run, and it finished in a couple of hours. I powered on the new machine, and voilà, I had a 70GB drive! After extensive testing of the new copy, it was time to get rid of the old machine.
Running into problems
Confident in my newly shrunk VM, and confident in my backups, it was time to delete the old machine and reclaim my disk space. Removing the machine failed. I went into the Datastore Browse. I could delete most files, but the .vmdk file – the one that was taking up all my space – would not delete. It seemed like a lock was on it, but there were no other VMs using this disk. I was even able to rename it with on trouble.
I then SSHed into one of my hosts. I browsed the datastore (typically /vmfs/volumes/datastorename/vmname) and typed rm machine-name.vmdk. It failed with the error: rm: cannot remove ‘.vmdk’: Resource temporarily unavailable.
What was locking my file? I triple checked each VM and nothing was mounting this drive. This data was orphaned and there was no reason it shouldn’t be deleted. I decided to check the logs. I typed: tail /var/log/vmkernel.log
This is what I got:
2014-05-30T00:46:50.853Z cpu1:40376)[type 10c00002 offset 1199107429376 v 34, hb offset 3809280
gen 63, mode 1, owner 5273d58c-7b5992ec-7389-90b11c0431da mtime 30161
num 0 gblnum 0 gblgen 0 gblbrk 0]
Expanding the disk
Luckily this part is much easier. Especially since the VM I was adding the space to was running Windows Server 2008. All you have to do is go into the Properties of the VM, and add space onto your disk. Then open up Disk Management in Windows, run “Rescan Disks” and expand your partition. Best part, you can do this while the machine is live.
What didn’t work
Originally I thought I could use the gparted live CD to resize the partition. I had recently done this in Hyper-V with no issues. While I was able to resize it, The disk still showed it’s full size in VMWare (which makes sense.) I then tried to create a second disk on the machine at the smaller size and use Clonezilla to clone the first disk to this second one. Since Clonezilla doesn’t support (to my knowledge) cloning a large disk to a smaller one, I tried just cloning the partition. As expected, the second drive did not boot. This may still be viable by tweaking the Clonezilla settings, using linux utils or a cloning tool like Acronis. By this point though, I went with the VMWare Converter which was much easier and smoother.