Categories

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.

VMware ESXi: howto wait for commands to complete (e.g. create snapshot)

In VMware ESXi, you have blocking and non-blocking tasks. When creating scripts to execute non-blocking tasks, it may be useful to wait for non-blocking commands to complete. This can be done by looking to the task list in the command line.

As an example, I used one of the harmless tasks in VMware: creating a snapshot. Here’s a script that will wait for a createSnapshot task until it finishes, showing the final state:

#/bin/ash
#######################################################################################
#
#   Check snapshot completion
#
#   Author:    Marcos Orfila <www.marcosorfila.com>
#   Last modified:
#              2010-10-23   (Marcos Orfila)
#
#######################################################################################

# Delay for the check loop, in seconds
DELAY=5

#######################################################################################

# Check parameters
VM_NAME=$1
if [ -z "$VM_NAME" ]
then
 echo "ERROR: not enough parameters"
 echo
 echo "   Usage:   $0 vmname"
 echo
 exit 1
fi

VM_ID=`vim-cmd vmsvc/getallvms | grep "$VM_NAME/" | awk '{print $1}'`
if [ -z "$VM_ID" ]; then
 echo "ERROR: unknown virtual machine ($VM_NAME)"
 exit 1
fi

TASK_ID=`vim-cmd vimsvc/task_list | grep "$VM_ID-vim.VirtualMachine.createSnapshot" | cut -d':' -f2 | cut -d"'" -f1`
if [ -z "$TASK_ID" ]; then
 echo "ERROR: no createSnapshot tasks found for $VM_NAME"
 exit 1
fi

echo -n "Snapshot for $VM_NAME is being created."
while true; do
 STATE=`vim-cmd vimsvc/task_info $TASK_ID | grep 'state = ' | cut -d'"' -f2`
 case "$STATE" in
 "running")
 echo -n '.'
 sleep $DELAY
 ;;
 "success")
 echo " OK"
 break
 ;;
 *)
 echo " ERROR"
 echo "The snapshot task ended with state $STATE (it should be 'success')"
 exit 1
 ;;
 esac    
done

To adapt this script to other kind of tasks, you might change the line for the “TASK_ID” variable and maybe the values for the acceptable state values in the “case” sentence.

  • Delicious
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • Twitter

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>