KVM - The Linux Kernel-Based Virtual Machine
News, Blogs and Resources on the Linux (KVM) Kernel-Based Virtual Machine

stop script running vms using virsh

stop script running vms using virsh

Hi there,

don't know if something like this is already out there (hint ?),

wrote my own little script :

#!/bin/sh
TIMEOUT=60

kvmshutdown () {
COUNT=0
PID=$(ps ax|grep $1|grep kvm|cut -c 1-6)

echo kvmshutdown \: Shutting down $1 with pid $PID

virsh shutdown $1

while [ "$COUNT" -lt "$TIMEOUT" ]
do
  ps --pid $PID
#  echo pid $?
  if [ "$?" -eq "1" ]
  then
    return 0
  fi
  sleep 5
  COUNT=$(($COUNT+5))
done

echo kvmshutdown \: Timeout happend. Destroying VM $1

virsh destroy $1

return 1

}

# Here is the start of the main "program" :-)

virsh list|grep running|tr -s \ |cut -f3 -d\  > /tmp/runvm.lst

while read vm
do
  echo  $vm is running
  kvmshutdown $vm
  if [ "$?" -eq "0" ]
  then
    echo VM $vm normally shutdown
  else
    echo VM $vm destroyed !
  fi
done < /tmp/runvm.lst

 If you can improve the script : do it :-)

greetings

     joern