Script to gracefully power off a VM guest


You can use vmware-cmd to shut down a VMware guest.  The syntax is


vmware-cmd /vmfs/volumes/datastore1/guest1/guest1.vmx stop

Powering off a VM guest is often part of a larger script, so I need to know when the guest is really down before executing the remainder of my script.  The following code checks the guest’s state using getstate.  If the state=1, which means it’s still running, the script waits for the shutdown to complete.

# gently power off VM
vmware-cmd /vmfs/volumes/datastore1/guest1/guest1.vmx stop

# check VM state 
# If not powered off (state=0), sleep, and recheck VM state in 15 seconds
while [ ` vmware-cmd /vmfs/volumes/datastore1/guest1/guest1.vmx getstate|grep on|wc -l` -eq 1 ]
do
echo "Waiting for VM guest to shutdown...."
sleep 15
let COUNT=COUNT+1

# check VM guest state 8 times
if [ $COUNT -eq 8 ]
then
echo "VM guest shutdown is taking too long. Cannot shut down guest VM."
skip="1"
fi

done

Replace datastore1 with the name of and path to your datastore.  Replace guest1 with the name of your VMware guest.  Tested on Vmware ESX 3.0.2.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: