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.
Note: Before proceeding ensure no snapshots are present on the VM being migrated.
1. Log into the ESXi host as ‘root’ via SSH.
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
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:
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:
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:
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
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
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)