Hardcore Linux

Anything about Ubuntu, Centos, openSuSe and Fedora

Automatically shutdown KVM Virtual Machines

Currently, there’s no built-in tool to properly shutdown VMs  when the KVM host does. So after couple of hour “googling” it I got some idea from this link, made some changes to make simplier. Here’s my version:


#!/bin/bash

LIST_VM=`virsh list | grep running | awk '{print $2}'`
TIMEOUT=90
DATE=`date -R`
LOGFILE="/var/log/shutdownkvm.log"

if [ "x$activevm" =  "x" ]
then
 exit 0
fi

for activevm in $LIST_VM
do
 PIDNO=`ps ax | grep $activevm | grep kvm | cut -c 1-6 | head -n1`
 echo "$DATE : Shutdown : $activevm : $PIDNO" >> $LOGFILE
 virsh shutdown $activevm > /dev/null
 COUNT=0
 while [ "$COUNT" -lt "$TIMEOUT" ]
 do
ps --pid $PIDNO > /dev/null
if [ "$?" -eq "1" ]
then
COUNT=110
else
sleep 5
COUNT=$(($COUNT+5))
fi
done
if [ $COUNT -lt 110 ]
then
echo "$DATE : $activevm not successful force shutdown" >> $LOGFILE
virsh destroy $activevm > /dev/null
fi
done
  1. Save the code in /etc/init.d/shutdownvm
  2. Then make it an executable file
    chmod 755 /etc/init.d/shutdownvm
  3. Create links to both rc0.d and rc6.d
    cd /etc/rc0.d ln -s ../init.d/shutdownvm K18shutdownvm cd /etc/rc6.d ln -s ../init.d/shutdownvm K18shutdownvm
  4. Done.

5 responses to “Automatically shutdown KVM Virtual Machines

  1. hardc0l2e November 4, 2010 at 6:29 am

    Update:

    On Ubuntu 10.04, adding shutdown init for shutdownvm.sh is not working anymore. You should add a pre-stop script in libvirt-bin and add shutdown init for it.

    1. Add the following in /etc/init/libvirt-bin.conf:

    pre-stop script
    /etc/init.d/shutdownvm.sh
    end script

    2. Then enlist libvirt-bin in the shutdown and reboot process.

    update-rc.d libvirt-bin stop 10 0 6 .

    3. Done.

  2. Mike April 29, 2011 at 7:29 pm

    Howdy, thanks for sharing your script.

    I think this line:

    if [ "x$activevm" = "x" ]

    Should be

    if [ "x$LIST_VM" = "x" ]

    since $activevm will always be empty at that point in the script

  3. Mike April 29, 2011 at 7:48 pm

    Also, in CentOS 5.6 (not sure if this script was in 5.5) there is a script that suspends the KVM guests on host shutdown:

    /etc/init.d/libvirt-guests

    Here’s the description

    # This is a script for suspending active libvirt guests \
    # on shutdown and resuming them on next boot

  4. homer June 28, 2011 at 6:27 am

    I´ve Ubuntu 10.04.2 LTS,

    and take the script from top. I make my links on rc0.d and rc6.d, too.

    cd /etc/rc0.d
    ln -s ../init.d/shutdownvm S00shutdownvm
    cd /etc/rc6.d
    ln -s ../init.d/shutdownvm S00shutdownvm

    I add not a pre-stop script in ‘libvirt-bin.conf’ AND ‘qemu-kvm.conf’.
    But in both files I add the numbers of the ‘stop on runlevel’.

    for example:
    start on runlevel [2345]
    stop on runlevel [!023456]

    That´s all of my configure.

    P.S. I´ve add ‘….| head -n1’ in line 15 of the script. Because I had problems with
    similar names of my VMs.

    for example:
    VM1: Maschine
    VM2: Maschine-xy

  5. Stuart January 1, 2015 at 7:26 am

    A better way to exit early if no vm’s are running:

    if [ -z “$LIST_VM” ]; then
    exit 0
    fi

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: