Hi there,

I've wrote a script for starting and stopping the KVM guest machines
either by saving and restoring or by graceful shutdown and start. My
question, is there an option within "libvirt" to make the KVM guest
machines 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 in the XML config file located in
"/etc/libvirt/qemu/".
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 "shutdown" case it will shutdown 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
        PID=$(ps ax|grep $VM_NAME|grep kvm|cut -c 1-6)
        COUNT=0
        echo "kvmshutdown \: Shutting down "$VM_NAME" with pid $PID"
        virsh shutdown "$VM_NAME" && exit 0
        while [ "$COUNT" -lt "$TIMEOUT" ]; do
                ps --pid $PID > "$DEVNULL" 2>&1
                if [ "$?" -eq "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
        PID=$(ps ax|grep $VM_NAME|grep kvm|cut -c 1-6)
        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" && exit 0
        while [ "$COUNT" -lt "$TIMEOUT" ]; do
                ps --pid $PID > "$DEVNULL" 2>&1
                if [ "$?" -eq "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 > /dev/null 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


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

 -- 

 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
 i...@opensoft24.de
 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.

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to