Month: March 2014

Shrink a VMDK with VMKFSTOOLS

Contents

Purpose

This article gives the steps to shrink a Vmware VMDK virtual disk file from command-line with the Vmware utility vmkfstools. As far as it’s known this is an unsupported method and can defiantly corrupt or break you virtual disk. So, make a backup first and try at your own risk. It worked great for me after I figured it all out. I only had to restore from my backup clone once or twice until I got the steps worked out.

This was performed on ESXi 5.1 and a CentOS 6.3 VM root partition. The VM had three virtual drives. Each dedicated to the three primary volumes (root,boot,swap). Not using LVM. Meaning a separate virtual drive for all three volumes.

 

Example Configuration

  • ESXi 5.1.0-838463 Host
  • CentOS 6.3 x64 Guest
  • Root partition is 100GB
    • /dev/sdb1
  • 29GB of space is used

 

Example Goal

  • Reduce root partition to 50GB

 

STEP 1

Remove All Snapshots on Guest

 

Backup

  1. Power off the VM Guest
  2. Create a clone of the root virtual drive
    1. SSH to ESX Host
    2. Browse to VM folder
      /vmfs/volumes/datastorename/vmfolder
    vmkfstools -i hostname.vmdk -d thin bak-hostname.vmdk

Icon-Tip-Square-Green.pngIf all the Snapshots are removed general there shouldn’t be any files with 00001 in the name. If there is, most likely something like a backup program has gone wrong and you’ll need to repair the virtual disk/s first.

 

Shrink Partition

  1. Boot guest off of GParted Live ISO
  2. Select Root drive
  3. Re-size (Shrink) partition to 49GB = 1024 * 49 = 50176
  4. Apply changes
  5. Power Off the VM Guest

 

Edit VMDK Descriptor

Gnome-sticky-notes-applet If there are snapshots or possibly orphaned snapshots: the VMDK file will appear empty even if you run cat against it. Obviously that will need to be fixed before attempting this unsupported shrink method.

Math

Formula [X * (1024 * 1024 * 1024)] / 512
X = Resulting Size in GB

Example
[50 * (1024 * 1024 * 1024)] / 512 = 104857600

From ESX Host CLI

  1. vi hostname.vmdk
    RW 209715200 VMFS "hostname-flat.vmdk"
    TO
    RW 104857600 VMFS "hostname-flat.vmdk"
  2. Save and Quit
    :wq

Icon-Tip-Square-Green.png In this example we’re cutting the drive in half… so the easy math is 209715200 / 2

 

Clone Edited VMDK

Make a clone of the VMDK that is set to only Read and Write to a different size.

  1. vmkfstools -i hostname.vmdk -d thin shrunk-hostname.vmdk

Gnome-sticky-notes-applet In this example when cloning it will stop at 50% during the clone because our new VMDK is half the size of the original.

 

Swap VMDK Files

Replace new shrunk VMDK for the original one.

  1. vmkfstools -E hostname.vmdk original-hostname.vmdk
  2. vmkfstools -E shrunk-hostname.vmdk hostname.vmdk

 

Extent Partition to End

  1. Boot guest off of GParted Live ISO
  2. Select Root drive
  3. Re-size (Grow) partition to entire 50GB virtual drive
    1. Absorb 1GB unallocated space at end
  4. Apply changes
  5. Exit Gparted | Eject ISO | Reboot

 

Test

  • Boot OS and test
  • Remove back and original cloned VMDK files
    • vmkfstools -U bak-hostname.vmdk
    • vmkfstools -U original-hostname.vmdk

 

Remove and Re-Add to Inventory

Lastly if all is well then remove the virtual machine from the ESX host inventory and add it back to correct the GUI readout of the size. Plus, fix it so if you wanted to use the GUI later to grow it.

  1. Power Off VM
  2. Login to with ViClient
  3. Right-Click on Guest
  4. Select Remove from Inventory
  5. Browse Datastore to virtual machine folder
  6. Right-Click on hostname.vmx
  7. Select Add to Inventory
  8. Follow prompts

 

Windows

The same steps can be used to shrink a Windows drive. With Vista/2008 and newer the disk manager can be used to change the size of the partitions instead of Gparted or Gparted can be used as well.

 

Related Articles

(HowTo) Grow a VMDK with VMKFSTOOLS
(HowTo) Convert a VMDK from Thick to Thin on ESX\

Many thanks to the person who wrote this article