Skip to contentSkip to conversation

init scripts for KVM guests UPDATE!

DISCUSSION Saman Behnam 5 participants 6 messages Jul 18, 2009 – Jul 19, 2009 KVM

lore
Share & tools
lore
#0 lore raw

Hi there,

here is an update for the "init scripts for KVM guests"I've wrote for starting and stopping the KVM guest machines either by saving and restoring or by gracefully shutdown and start. My question, is there an option within "libvirt" to make the KVM guest mashines get saved when the KVM host shuts down? I had problems to gracefully shutdown Windows VM's!!!

Right now I had to write these scripts to save and restore the machines upon starting and stopping the KVM host.

Many Thanks for your response

vim /etc/init.d/kvm-win0-rcs

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

#!/bin/sh

### BEGIN INIT INFO

# Provides:          kvm-win0-rcs
# Required-Start:    $kvm $libvirtd
# Required-Stop:     $kvm $libvirtd
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starting/Saving, Stopping/Restoring kvm-win0-rcs
KVM guest.
# Description:       Starting/Saving, Stopping/Restoring KVM guest "win0".
# kvm-win0-rcs KVM guest.
### END INIT INFO#

# Please copy the init scripts of the KVM VM's in to "/etc/init.d" and then run! "for i in cd /etc/init.d && ls kvm-* ; do update-rc.d $i defaults 21 19 ; done"

# U need per VM a separate start-stop-script!
VM_NAME="win0" # Name of ur guest VM
TIMEOUT=300
VM_SAVE_DIR="/kvm/tmp/vm_save_dir" # Directory for storing the guest VM save data..
DEVNULL="/dev/null"
#ACTION="shutdown"
ACTION="save" # ACTION takes one argument, either "save" or "shutdown". In the "save" case it will save the VM and restore it while stopping or starting. In the "shtudown" case it will sutdown then VM and start it while stopping or starting.

vmshutdown () {
if ! virsh list|grep running|grep -v grep|grep "$VM_NAME" >
"$DEVNULL" 2>&1; then
echo "VM "$VM_NAME" is not running!!! Exiting." exit 1
fi
COUNT=0
echo "kvmshutdown \: Shutting down "$VM_NAME" with pid $PID" virsh shutdown "$VM_NAME" &
while [ "$COUNT" -lt "$TIMEOUT" ]; do
if ! virsh list|grep running|grep -v grep|grep
"$VM_NAME" > "$DEVNULL" 2>&1; then

                        exit 0
                fi
                sleep 5
                COUNT=$(($COUNT+5))
        done
        echo "kvmshutdown \: Timeout happend. Destroying VM "$VM_NAME""
        virsh destroy "$VM_NAME" && exit 1
}

vmstart () {
if virsh list|grep running|grep -v grep|grep "$VM_NAME" >
"$DEVNULL" 2>&1; then
echo "VM "$VM_NAME" is allready running!!! Exiting." exit 1
fi
virsh start "$VM_NAME" && exit 0
exit 1
}

vmsave () {
if ! virsh list|grep running|grep -v grep|grep "$VM_NAME" >
"$DEVNULL" 2>&1; then

                echo "VM "$VM_NAME" is not running!!! Exiting."
                exit 1
        fi
        COUNT=0
        echo "vmsave \: Saving VM "$VM_NAME""
        if [ ! -d "$VM_SAVE_DIR" ]; then
                echo "creating  "$VM_SAVE_DIR""
                mkdir -p "$VM_SAVE_DIR"
        fi
        virsh save "$VM_NAME" "$VM_SAVE_DIR/$VM_NAME.kvm.save" &
        while [ "$COUNT" -lt "$TIMEOUT" ]; do
                if ! virsh list|grep running|grep -v grep|grep

"$VM_NAME" > "$DEVNULL" 2>&1; then
exit 0
fi
sleep 5
COUNT=$(($COUNT+5))
done
echo "vmsave \: Timeout happend. Destroying VM "$VM_NAME"" virsh destroy "$VM_NAME"
exit 1
}

vmrestore () {
if virsh list|grep running|grep -v grep|grep "$VM_NAME" >
"$DEVNULL" 2>&1; then
echo "VM "$VM_NAME" is allready running!!! Exiting." exit 1
fi
if ! ls "$VM_SAVE_DIR"/"$VM_NAME".kvm.save > "$DEVNULL" 2>&1 ; then
echo "Restore file "$VM_NAME".kvm.save in "$VM_SAVE_DIR"
not found! Starting VM!!!"

                vmstart
                exit 1
        fi
        cd "$VM_SAVE_DIR"
        echo "vmrestore \: Restoring VM "$VM_NAME""
        virsh restore "$VM_SAVE_DIR"/"$VM_NAME".kvm.save && rm -f

"$VM_SAVE_DIR"/"$VM_NAME".kvm.save
exit 0
}

# Here is the start of the main "program" :-)
case "$1" in

        start)
                case "$ACTION" in
                        save)
                                        vmrestore
                                        ;;
                        shutdown)
                                        vmstart
                                        ;;
                esac
                ;;
        stop)
                case "$ACTION" in
                        save)
                                        vmsave
                                        ;;
                        shutdown)
                                        vmshutdown
                                        ;;
                esac
                ;;

        *)
                N="$0"
                echo "Usage: "$N" {start|stop}" >&2
                exit 1
                ;;

esac
exit 0

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

Please tell also if there is an official script for gracefully stopping / starting KVM VM's???

greets

saman

--

mit freundlichen Grüßen / Kind regards

Saman Behnam

--
opensoft24-Systems
Lena-Christ-Str. 3
80807 München
Tel.: +49 (0) 89 69397631
Fax.: +49 (0) 89 69397631
[email protected]
www.opensoft24.de
www.opensoft24.com

Bankverbindung:
Postbank München
BLZ.: 70010080
Kto.: 279702806
Steuer-Nr.: 135 70442

UST-IdNr.: 76 158 941 031
IBAN.: PBNKDEFF
SWIFT.: DE21700100800279702806

Diese Nachricht und/oder Anhänge sind vertraulich und können der Geheimhaltungspflicht unterliegen. Wenn Sie nicht der vorgesehene Adressat sind, weisen wir Sie hiermit darauf hin, dass jegliche Nutzung, Weiterleitung und Kopien unzulässig sind. Benachrichtigen Sie uns bitte unverzüglich per E-Mail und löschen Sie diese Nachricht und/oder Anhänge aus Ihrem System.

This mail and/or attachments are confidential and may also be legally privileged. If you are not the intended recipient, you are hereby notified, that any review, dissemination, distribution or copying of this email and/or attachments is strictly prohibited. Please notify us immediately by email and delete this message and all its attachments.

#1 lore raw
Quoted 9 lines · Saman Behnam

Hi there,

here is an update for the "init scripts for KVM guests"I've wrote for starting and stopping the KVM guest machines either by saving and restoring or by gracefully shutdown and start. My question, is there an option within "libvirt" to make the KVM guest mashines get saved when the KVM host shuts down? I had problems to gracefully shutdown Windows VM's!!!

By the way, do you realize that libvirt != kvm?

/mjt

#2 lore raw

Hi Michael.

Quoted 8 lines · Michael Tokarev
Quoted 6 lines

here is an update for the "init scripts for KVM guests"I've wrote for starting and stopping the KVM guest machines either by saving and restoring or by gracefully shutdown and start. My question, is there an option within "libvirt" to make the KVM guest mashines get saved when the KVM host shuts down? I had problems to gracefully shutdown Windows VM's!!!

By the way, do you realize that libvirt != kvm?

Is there some way to obtain this without using libvirt? To write a script for starting the virtual machines is not a problem to do doing without libvirt, nevertheless the problem is in how doing shutdown of the VMs when host does it.

Regards,
Daniel

Signature · 2 lines
Fingerprint: BFB3 08D6 B4D1 31B2 72B9  29CE 6696 BF1B 14E6 1D37
Powered by Debian GNU/Linux Squeeze - Linux user #188.598
#3 lore raw
In reply to Saman Behnam

To echo Michael a little here -- posting this on the KVM mailing list implies that it shouldn't have dependencies beyond KVM; as such, the libvirt mailing list would probably be a better place for these scripts.

That said -- no, there isn't an option within libvirt to save or gracefully shut down the guests on daemon (or host) shutdown. This is in part because in newer versions, the libvirt daemon can be shut down or restarted without also shutting down the guests -- presuming that the host is being shut down whenever the guest is terminated would be an unfortunate thing from the perspective of some users, so in my opinion a separate init script for controlling the guests (similar to what you're doing here) is probably the Right Thing.

#4 lore raw
In reply to Saman Behnam
Quoted 6 lines · Saman Behnam

here is an update for the "init scripts for KVM guests"I've wrote for starting and stopping the KVM guest machines either by saving and restoring or by gracefully shutdown and start. My question, is there an option within "libvirt" to make the KVM guest mashines get saved when the KVM host shuts down? I had problems to gracefully shutdown Windows VM's!!!

Hello,

I sent my (and also your) script to libvir-list, but no response yet.
There are also some suggestions, how libvirt can handle graceful shutdown and leave ability to restart libvirt without touching guests.

I use my script to also shutdown windows guests. It's working well, if you
configure to shutdown windows on acpi event (virtual power button pressed). I am not an expert for windows and I can't tell you more, but I am sure, that you can find more with google. :-)

SAL

#5 lore raw
Quoted 8 lines · Ján ONDREJ (SAL)
Quoted 6 lines · Saman Behnam

here is an update for the "init scripts for KVM guests"I've wrote for starting and stopping the KVM guest machines either by saving and restoring or by gracefully shutdown and start. My question, is there an option within "libvirt" to make the KVM guest mashines get saved when the KVM host shuts down? I had problems to gracefully shutdown Windows VM's!!!

Quoted 8 lines

I sent my (and also your) script to libvir-list, but no response yet.
There are also some suggestions, how libvirt can handle graceful shutdown and leave ability to restart libvirt without touching guests.

I use my script to also shutdown windows guests. It's working well, if you
configure to shutdown windows on acpi event (virtual power button pressed). I am not an expert for windows and I can't tell you more, but I am sure, that you can find more with google. :-)

Forgot to attach mail archive URL:
http://www.mail-archive.com/[email protected]/msg14754.html

SAL