Mass Import VMs to New ESX Host by .VMX files
One of the advantages of running virtual machines (VMs) on shared storage is that if you need to rebuild your ESX host(s) you won’t lose your VMFS volumes or the VMs. But, what if you can’t VMotion evacuate the VMs to another host with extra capacity first? This scenario means the VMs will no longer be registered on an ESX host once you are finished. Importing VMs with the datastore browser is simple enough, but what if you have hundreds of VMs to import? Manually importing that many VMs will take hours.
Luckily, the built-in ESX console command vmware-cmd with the -s option allows you to mass import VMs via some simple scripting. Ed Haletky pointed this out to me via Twitter, and then Duncan Epping tweeted me a post of his that contained the following batch script he used for mass VM imports in slightly different scenario :
for i in `find /vmfs/volumes/ -name "*.vmx" `
do
echo "Registering VM $i"
vmware-cmd -s register $i
done
To use the above code save it as a text file with a .sh extension. For example “importvms.sh”. Place the file on anywhere on your ESX host and then as root run
#./importvms.sh
updated 3.12.09 – My Softchoice colleague, Michael Cocat out of our Chicago office, modified the original script above to work when spaces exist in the VM (and .vmx) name. Michael also likes to run the script as a single line entry on the ESX Console. Here’s the updated command in single line format:
find /vmfs/volumes -name “*.vmx” | while read LINE; do echo “registering VM $LINE”; vmware-cmd -s register $LINE
updated 3.12.09 – I also want to point out that this script will not place the VMs back in Resource Pools or Folders. The VMs are imported into the root of the object tree because the resource pool and folder information is not stored in the .vmx file. Once you have the VMs back registered with the ESX server you can simply drag and drop them into resource pools and folders again. Thanks to Jason Boche for bringing this up, and pointing out it is not really a fully automated recovery process. It is still a much better alternative to manually importing each VM with the datastore browser which will prompt you for Resource Pool and VM folder placement.
To find out more about the vmware-cmd options and switches check out
http://www.vmware.com/support/esx21/doc/vmware-cmd.html
and
http://www.esxguide.com/esx/content/view/2/25/
Related Posts
-
Virgil
-
http://vmetc.com rbrambley
-
http://vmetc.com rbrambley
-
http://www.krausmueller.de Johannes
-
http://www.krausmueller.de Johannes









