Ask Your Question
3

Make libvirt to shutdown my guests (not suspend)

asked 2011-11-24 06:56:04 -0500

lzap gravatar image

updated 2011-11-25 22:16:46 -0500

mether gravatar image

Hi,

libvirt suspends my guests by default when I poweroff/restart my host. I want them to be gracefully shutdowned. What's the trick? ;-)

edit retag flag offensive close merge delete

5 Answers

Sort by ยป oldest newest most voted
5

answered 2011-11-24 09:53:00 -0500

lzap gravatar image

Solved, thanks all.

vim /etc/init.d/libvirt-guests
...
ON_SHUTDOWN=shutdown
SHUTDOWN_TIMEOUT=10
...
:wq

Done.

edit flag offensive delete link more

Comments

Don't know if it's applicable to fedora but in CentOS you must also enable libvirt-guests (systemctl enable libvirt-guests)

ndemou gravatar imagendemou ( 2016-04-21 12:16:07 -0500 )edit
1

answered 2011-11-24 07:54:27 -0500

SoumyaC gravatar image

Is ACPI and ACPID running on the VM ? Please check this link to libvirt wiki.

I just browsed and came across this solution for a graceful shutdown using virsh shutdown command

Hope it helps you.

edit flag offensive delete link more

Comments

Yeah I have it running. I know how virrsh shutdown works, I just want to shutdown them while my HOST is being shutdown. They are suspended to disc (or paused if you want) currently.

lzap gravatar imagelzap ( 2011-11-24 08:29:28 -0500 )edit
1

answered 2011-11-24 08:51:08 -0500

hhlp gravatar image

updated 2011-11-24 09:02:11 -0500

To shut down a guest VM use the virsh command form shell prompt Code:

virsh list
virsh shutdown domain-id

To list the virtual machines currently running:

su -c "virsh list"

To list all virtual machines, running or not:

su -c "virsh list --all"

To gracefully power off a guest:

su -c "virsh shutdown <virtual machine (name | id | uuid)>"

To non gracefully power off a guest:

su -c "virsh destroy <virtual machine (name | id | uuid)>"

But always there is a solution ... You can use this script and adapt to your needs.. ->

#!/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

You can place the script in /lib/systemd/system-shutdown and set the permissions so it's executable.

basicaly the script try to shutdown the guests if isn't possible suspend the guest...

edit flag offensive delete link more

Comments

Cool, but the service itself starts pausing them before this script gets executed :-(

lzap gravatar imagelzap ( 2011-11-24 09:36:33 -0500 )edit
1

answered 2013-12-24 04:51:35 -0500

Stijn Hoop gravatar image

The accepted answer works, but breaks updates as the init script is managed by RPM. Even better is:

vim /etc/sysconfig/libvirt-guests
...
ON_SHUTDOWN=shutdown
SHUTDOWN_TIMEOUT=10
...
:wq

Note the difference between /etc/init.d and /etc/sysconfig

edit flag offensive delete link more

Comments

Modifying /etc/init.d/libvirt-guests is generally not good form because as already mentioned, it's packaged into an RPM. This is the most correct answer, as it overrides the defaults set in the init script, allowing system specific changes without having to repackage an RPM for every system that you install this on.

willo gravatar imagewillo ( 2015-02-09 20:57:03 -0500 )edit
0

answered 2013-12-06 02:56:23 -0500

banjo67xxx gravatar image

updated 2013-12-06 02:57:01 -0500

Alternative answer for systemd managed systems:-

vim /usr/libexec/libvirt-guests.sh
...
ON_SHUTDOWN=shutdown
SHUTDOWN_TIMEOUT=10
...
:wq

And don't forget to enable libvirt-guests too

systemctl enable libvirt-guests.service
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-11-24 06:56:04 -0500

Seen: 31,060 times

Last updated: Dec 24 '13