VMWare ESXi 5.1–convert virtual disk (vmdk) from thick to thin provision


To convert a virtual disk from thick to thin, easy but slow way is to use VMWare Converter to actually convert the VM to local PC, and then re-upload.  I found one blog with instruction here

If you have two datastores, you can do the conversion by using “Migrate Virtual Machine” function at vSphere client.  You can follow the instruction at VMWare Knowledge Base here.

If you have only one datastores, or you want to make the process faster, you can follow my steps to do it purely on ESXi console:
  1. Stop the VM
  2. Make sure the VM has no outstanding snapshots.
  3. SSH to ESXi host.  If SSH is not enabled, please enable sshd on ESXi via vSphere Client by following steps here.
  4. Check the actual size of the vmdk: du –h *.vmdk
    /vmfs/volumes/<GUID>/VM01 # du –h *.vmdk
    60.0G   VM01-flat.vmdk
    0       VM01.vmdk

    Since it is in think provision, no matter what the actual size is, it sill uses 60G of data store.
  5. Make sure there is enough free space.  There is no direct conversion. We are just to clone the vmdk to a new file of a new format (thin provision).
    df –h
    /vmfs/volumes/<GUID>/VM01 # df -h
    Filesystem   Size   Used Available Use% Mounted on
    VMFS-5     833.2G 498.3G    334.9G  60% /vmfs/volumes/MyDataStore
    vfat         4.0G  31.8M      4.0G   1% /vmfs/volumes/<GUID>
    vfat       249.7M 145.8M    103.9M  58% /vmfs/volumes/<GUID>
    vfat       249.7M   8.0K    249.7M   0% /vmfs/volumes/<GUID>
    vfat       285.8M 201.9M     83.9M  71% /vmfs/volumes/<GUID>
  6. Clone the vmdk into thin provision
    vmkfstools -i VM01.vmdk  VM01-thin.vmdk -d thin
    Destination disk format: VMFS thin-provisioned
    Cloning disk 'VM01.vmdk'...
    Clone: 100% done.
  7. Check the provisioned and actual disk usage again:

    /vmfs/volumes/<GUID>/VM01 #ls -lh *.vmdk
    -rw-------    1 root   root  60.0G Dec 29 08:37 VM01-flat.vmdk
    -rw-------    1 root   root  60.0G Mar  6 08:05 VM01-thin-flat.vmdk
    -rw-------    1 root   root    524 Mar  6 08:06 VM01-thin.vmdk
    -rw-------    1 root   root    470 Nov 23 04:09 VM01.vmdk

    /vmfs/volumes/<GUID>/VM01 # du -h *.vmdk
    60.0G   VM01-flat.vmdk
    2.8G    VM01-thin-flat.vmdk
    0       VM01-thin.vmdk
    0       VM01.vmdk


    The actual usage is just 2.8G.  57.2G disk space is wasted.
  8. Rename the original file (VM01.vmdk) to VM01-thick.vmdk, and rename the new file (VM01-thin.vmdk) to the file name of the orginial file.  Never use move (mv) command.  We must use vmkfstools to rename the file:

    /vmfs/volumes/<GUID>/VM01 # vmkfstools -E VM01.vmdk VM01-thick.vmdk
    /vmfs/volumes/<GUID>/VM01 # vmkfstools -E VM01-thin.vmdk VM01.vmdk
    /vmfs/volumes/<GUID>/VM01 # ls -lh *.vmdk
    -rw-------    1 root     root       60.0G Mar  6 08:05 VM01-flat.vmdk
    -rw-------    1 root     root       60.0G Dec 29 08:37 VM01-thick-flat.vmdk
    -rw-------    1 root     root         476 Mar  6 08:23 VM01-thick.vmdk
    -rw-------    1 root     root         519 Mar  6 08:23 VM01.vmdk
    /vmfs/volumes/<GUID>/VM01 # du -h *.vmdk
    2.8G    VM01-flat.vmdk
    60.0G   VM01-thick-flat.vmdk
    0       VM01-thick.vmdk
    0       VM01.vmdk
  9. Turn on the VM to test if the new file works.
  10. Delete the old vmdk to free up the disk space:
    /vmfs/volumes/<GUID>/VM01 # vmkfstools -U VM01-thick.vmdk
  11. By checking disk free space with df command, we can see we have free up additonal disk space.

    /vmfs/volumes/<GUID>/SAS VM01 # df -h
    Filesystem   Size   Used Available Use% Mounted on
    VMFS-5     833.2G 438.3G    394.9G  53% /vmfs/volumes/myDataStore
    vfat         4.0G  32.8M      4.0G   1% /vmfs/volumes/<GUID>
    vfat       249.7M 145.8M    103.9M  58% /vmfs/volumes/<GUID>
    vfat       249.7M   8.0K    249.7M   0% /vmfs/volumes/<GUID>
    vfat       285.8M 201.9M     83.9M  71% /vmfs/volumes/<GUID>
Optional:
If you are using Windows, you can zero fill the free space before converting from think to thin provision.  Then more free space can be released.
  1. Download sDelete from Microsoft/sysinternal
  2. run sdelete -z
For Linux, 
  1. Estimate how many space wasted/need to reclaim
    1. use df -h command inside the VM to estimate how many real data are (e.g. 7G)
    2. Under ESXi, use du -h xyz.vmdk command to know how much space actually consumed the datastore (e.g. 255GB)
    3. Then we need to reclaim 248GB free space
  2. Use dd command to fill zero to space
    1. dd if=/dev/zero of=BIGFILE bs=2G ; rm -f BIGFILE”.
  3. Follow the above instruction to clone the disk again to reclaim free space from vmdk to the datastore.



Comments

Popular Posts