VMware Cloud Community
dswart_1
Contributor
Contributor

Register Multiple VM's from a flexclone datastore with a script

I am using Netapp Flexclone and want to register my VM's with a script.

Have a look at http://www.youtube.com/watch?v=7Miv0PiJFzM

I got this script from Netapp , but have no idea ?

Have anybody tried this before ? and if so , how ?

Thanks

#!/usr/bin/bash

#

  1. Example code to register and start all VMs

#

  1. This sample code is provided AS IS, with no support or warranties of any

  2. kind, including but not limited to warranties of merchantability or

  3. or fitness of any kind, expressed or implied.

#

  1. Network Appliance

#

  1. --------------------------------------------------------------------------

VERBOSE=0

LOGFILE=/dev/null

RSH=/usr/bin/ssh

RSH_USER=root

RSH_CMD="$ -l $

  1. Name: write2log()
  2. Func: Write information out to the log file.
" #
  1. Name: check_server()
  2. Func: Determine whether ping and rsh/ssh access to the server are working properly.
  3. Check ping
# write2log() { TEXT=$1 echo "`date '+%D %T'` \c" >> ${LOGFILE} 2>&1 echo ": $" >> $ 2>&1 if ; then echo $1 else RES=`echo $ | egrep '(WARNING|ERROR)'` if [ "$" ] ; then echo $1 fi fi } # # check_server() { SERVER=$1 write2log "check_server(): Checking network access to server ${SERVER} ..." result=`/usr/bin/ping $ -n 3 > /dev/null 2>&1` if ; then write2log "check_server(): ERROR: $ is not available on the network" exit 1 else write2log "check_server(): Server $
  1. Check rsh/ssh
is on the network." fi write2log "check_server(): Checking remote access to server $
..." result=`$ $ "id"` tr=`echo "$result" | grep $`

if [ "$" = "" ] ; then

write2log "check_server(): ERROR: $ is not accessible with ($)!" exit 1 fi write2log "check_server(): access with ($) to $ successful."

}

#

  1. Name: run_vmhost

  2. Func: Run the operations for each VMWare host specified on the command line.

#

run_vmhost()

{

  1. assign the server

SERVER=$1

write2log "run_vmhost(): Starting VMs on server ${SERVER} ..."

  1. first make sure the host specified is up

check_server $

  1. get the find output
write2log "run_vmhost(): Running $ $ find /vmfs/volumes -mindepth 3 -maxdepth 3 -name '*.vmx' -print ..."

VMXES=`$ $ "find /vmfs/volumes -mindepth 3 -maxdepth 3 -name '*.vmx' -print"` if ; then write2log "run_vmhost(): ERROR: Execution of $ failed!"

exit 1

fi

if [ "$

  1. We have a set of VMFS volumes -- let's perform operations on them
" != "" ] ; then for VMX in $ ; do

  1. register the VM

write2log "run_vmhost(): Running $ $ /usr/bin/vmware-cmd -s register $ ..." $ $ "/usr/bin/vmware-cmd -s register $" if ; then write2log "run_vmhost(): ERROR: Execution of $

  1. start the VM
failed!" exit 1 fi write2log "run_vmhost(): Running $ $ /usr/bin/vmware-cmd $ start ..." 18 $ $ "/usr/bin/vmware-cmd $ start"

if ; then

write2log "run_vmhost(): ERROR: Execution of $ failed!"

exit 1

else

write2log "run_vmhost(): VM $ started."

fi

done

else

write2log "run_vmhost(): WARNING: Server $ did not have any VMFS volumes active!" fi write2log "run_vmhost(): Configuration of server $ completed."

}

#

  1. MAIN SCRIPT

#

ARGS=$#

while ; do

case "$1" in

-v) VERBOSE=1 ; ARGS=`expr $ - 1` ; shift ;; *) HOST=$1 ; shift ; ARGS=`expr $ - 1` ; run_vmhost $HOST ;;

esac

done

write2log "Script completed."

Tags (2)
0 Kudos
6 Replies
kjb007
Immortal
Immortal

The script does a lot of tasks to include checking hosts, finding vmfs volumes, and registering and starting vm's.

If all you want to do is register vm's, then you can use 'vmware-cmd -s register /path/to/vmx/file.vmx' on an esx host.

-KjB

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB
0 Kudos
dswart_1
Contributor
Contributor

Thanks , I know but I have 100 VM's in each datastore and the vmware-cmd register only does one at a time.

Can I use a variable in the $whatever.vmx to register everything in the datastore ?

Tx

0 Kudos
kjb007
Immortal
Immortal

Yes, do this for a quick and dirty method below. For any outliers, you'll have to go back and add manually.

Log into your service console.

Cd to your /vmfs/volumes folder

cd

repeat steps

-KjB

Message was edited by: kjb007 : fixed loop

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB
0 Kudos
dswart_1
Contributor
Contributor

Thanks for your help , but I get the following error .

# vmware-cmd -s register 'pwd'/$vm/$vm.vmx

VMControl error -999: Unknown error: SoapError: ServerFaultCode(0) : (Invalid datastore format '[]pwd//.vmx'.)

Any ideas ?

Thanks

0 Kudos
kjb007
Immortal
Immortal

Sorry, those are backticks, not single quotes, the one next to the 1 on your keyboard. Otherwise, just change the `pwd`/$vm/$vm.vmx to /vmfs/volume/datastore_name/$vm/$vm.vmx

-KjB

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB
0 Kudos
NathanEly
Contributor
Contributor

This looks very helpful.

What if you have VMs with multiple disks, and some of those disks are located on other datastores? Will it still register properly?

Thanks

-N

0 Kudos