vSphere – Migrating A VM To A New VMFS Datastore (CLI: VMKFSTOOLS)

You may encounter a scenario where a vCenter server is not part of a solution and SVMotion is not an option to migrate a VM from one VMFS datastore to another. In this case you may use the vSphere VI Client datastore browser and copy/move the VM data files from one datastore to another or in the case outlined here you may use the CLI approach to migrate a specific VM to another datastore.

In this example a second RAID1 Mirror has been added to a standalone DELL Server and a new VMFS datastore has been created labelled ‘datastore2’:

Note: Before proceeding ensure no snapshots are present on the VM being migrated.

1. Log into the ESXi host as ‘root’ via SSH.

2. List all VMs present on the ESXi host:
vim-cmd vmsvc/getallvms
You can also use: esxcli vm process list

List the inventory ID of the virtual machine ‘MartinWIN7’ which is being migrated to the new datastore:
vim-cmd vmsvc/getallvms |grep MartinWIN7
We can see from the output that the inventory ID for this VM is ‘9’
Check if VMID ‘9’ has a snapshot: vim-cmd vmsvc/get.snapshot 9
If necessary remove the snapshot: vim-cmd vmsvc/snapshot.remove 9

3. Shutdown the virtual machine ‘MartinWIN7’ VMID ‘9’:
Check the power state of the virtual machine with the following command:
vim-cmd vmsvc/power.getstate 9
Shutdown the virtual machine with the command:
vim-cmd vmsvc/power.shutdown 9

Alternative Power-off cmds:
vim-cmd vmsvc/power.off vmid
Or using esxcli: esxcli vm process kill –w world_id
How to gather the world_id: esxcli vm process list

4. Unregister the VM from the ESXi host:
vim-cmd vmsvc/unregister 9

5. List the VMFS Volumes available on the ESXi host:
cd /vmfs/volumes/
ls -l


6. Change directory to the VMFS volume where the VM currently resides (‘datastore1’) and gather the required information such as vmdk and vmx file names:
cd /vmfs/volumes/datastore1/
ls -l

You can view the actual amount of space used by the vmdk files on ‘datastore1’ by using the cmd: du -ah
If the size displayed by a *.vmdk was zero this would imply the vmdk was a Thin disk and the *-flat.vmdk would display the actual used space of the Thinly provisioned vmdk, something similar to the following:

cd /vmfs/volumes/datastore1/MartinWIN7/
ls -l

As you can see below each VM Disk has a flat file and a descriptor file, for example the virtual machine ‘MartinWIN7’ has a disk named MartinWIN7.vmdk and a corresponding MartinWIN7-flat.vmdk file.

7. Change directory to the new VMFS volume where the VM will be migrated to and create a new folder for the VM files:

cd /vmfs/volumes/datastore2/
mkdir MartinWIN7
ls -l


8.Using the ‘vmkfstools’ command to clone the VM to ‘datastore2’, once the cloning process has completed successfully then we can delete the original VM on ‘datastore1’. The ‘-i’ option used with vmkfstools creates a copy of a virtual disk, using the following syntax:
vmkfstools -i src dst
Where src is the current vmdk location (‘datastore1’) and dst is the destination (‘datastore2’) where you would like the vmdk file copied to.

You can chose the Disk format by using the -d –diskformat suboption. The 3 choices of disk format are:
zeroedthick (default) – all space is allocated during creation but only zeroed on first write, referred to a lazy zeroed.
eagerzeroedthick – all space is allocated and fully zeroed during creation.
thin – only the required space is allocated the remainder is allocated and zeroed over time on demand.

vmkfstools -i src dst -d –diskformat [zeroedthick|thin|eagerzeroedthick] -a –adaptertype [buslogic|lsilogic|ide]

Checking the Disk format using ‘vmkfstools -t0’ before cloning ‘MartinWIN7.vmdk’:
vmkfstools -t0 /vmfs/volumes/datastore1/MartinWIN7/MartinWIN7.vmdk
Example Output, the ‘VMFS Z‘ indicates that it is lazy zeroed (zeroedthick):
You may also use ‘vmkfstools -D’ to check the Disk Format:
vmkfstools -D /vmfs/volumes/datastore1/MartinWIN7/MartinWIN7_1.vmdk
Example Output, the ‘tbz 0‘ indicates that it is eagerzeroedthick:

The following example illustrates cloning the contents of the virtual disk ‘MartinWIN7.vmdk’ from /datastore1/MartinWIN7 to a virtual disk file with the same name on the /datastore2/MartinWIN7 file system:
vmkfstools -i “/vmfs/volumes/datastore1/MartinWIN7/MartinWIN7.vmdk” “/vmfs/volumes/datastore1/MartinWIN7/MartinWIN7.vmdk” -d zeroedthick -a LSILogic


Cloning the second disk ‘MartinWIN7_1.vmdk’:
vmkfstools -i “/vmfs/volumes/datastore1/MartinWIN7/MartinWIN7_1.vmdk” “/vmfs/volumes/datastore2/MartinWIN7/MartinWIN7_1.vmdk” -d zeroedthick -a LSILogic

Monitoring progress:

9. To copy (cp) the virtual machine configuration ( .vmx) file to the new folder, run the command:

cp “/vmfs/volumes/datastore1/MartinWIN7/MartinWIN7.vmx” “/vmfs/volumes/datastore2/MartinWIN7/MartinWIN7.vmx”

10. Next register the newly cloned virtual machine on ‘datastore2’ named ‘MartinWIN7’ on the ESXi host using the vim-cmd solo/registervm cmd:
vim-cmd solo/registervm /vmfs/volumes/datastore2/MartinWIN7/MartinWIN7.vmx MartinWIN7

List the inventory ID of the new virtual machine with the command:
vim-cmd vmsvc/getallvms |grep MartinWIN7

Power-on the virtual machine with VMID 10:
vim-cmd vmsvc/power.on 10


You will receive a prompt on the vi client, chose the option ‘I copied it‘:

Displaying the VM IP address (if you wish to RDP and confirm VM status):
vim-cmd vmsvc/get.guest 10 |grep -m 1 “ipAddress = \””

11. If all looks well and you are happy the VM is operating normally then you may delete the old VM directory. Delete Directory and all contents:
cd /vmfs/volumes/datastore1/
rm -r MartinWIN7OLD/


Useful VMware KB’s:
Cloning and converting virtual machine disks with vmkfstools (1028042)
Cloning individual virtual machine disks via the ESX/ESXi host terminal (1027876)
Determining if a VMDK is zeroedthick or eagerzeroedthick (1011170)
Performing common virtual machine-related tasks with command-line utilities (2012964)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s