Badges

gestaltitbadge

follow-me-twitter

Subscribe to me on FriendFeed

Comments / DISQUS
Feedjit.com

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

    The concept works great, but that actually vmware-cmd register line will fail on an VMs with spaces in their names….
    You need to take into account shell expansion etc.

  • http://vmetc.com rbrambley

    Virgil,

    Hmmm. Have not tested with .vmx files that have spaces in names. I strongly suggest to anyone who listens to never use spaces in Linux file names for reasons just like this. In ESX, change the VM name in the properties after it's already created, but be sure not to you spaces while building a new VM. I try to use the FQDN as my standard. If I have to account for a space I use the underscore (“_”). Having to work with spaces in file names at the console is always a problem or extra aggravation.

  • http://vmetc.com rbrambley

    Virgil,

    One of my colleagues updated the script to work with spaces in the VM and file names. See the updated command in the body of the post.

  • http://www.krausmueller.de Johannes

    Another solution for VMs with spaces in their names might be the use of IFS (Internal Field Separator). The script then looks something like this:

    IFS=' n'
    for i in `find /vmfs/volumes/ -name “*.vmx” `
    do
    echo “Registering VM $i”
    vmware-cmd -s register $i
    done

    I used IFS in a script to suspend all VMs at night for backup (they also had spaces in their names), but it should also work here (although not tested).

  • http://www.krausmueller.de Johannes

    Another solution for VMs with spaces in their names might be the use of IFS (Internal Field Separator). The script then looks something like this:

    IFS=' n'
    for i in `find /vmfs/volumes/ -name “*.vmx” `
    do
    echo “Registering VM $i”
    vmware-cmd -s register $i
    done

    I used IFS in a script to suspend all VMs at night for backup (they also had spaces in their names), but it should also work here (although not tested).

Get My Podcast On iTunes!
Support VM /ETC
Support VMETC.com

Support VMETC.com

Free Business and Tech Magazines and eBooks
@rbrambley tweets
VMTN Roundtable Podcasts
Subscribe



Add to Google Reader or Homepage
Subscribe in NewsGator Online
Add to netvibes
Add to Plusmo