From mboxrd@z Thu Jan 1 00:00:00 1970 From: Saman Behnam Subject: init scripts for KVM guests Date: Thu, 16 Jul 2009 17:01:29 +0200 Message-ID: <4A5F40C9.1090306@opensoft24.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: QUOTED-PRINTABLE To: kvm@vger.kernel.org Return-path: Received: from mailout04.t-online.de ([194.25.134.18]:52971 "EHLO mailout04.t-online.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754272AbZGPPCa (ORCPT ); Thu, 16 Jul 2009 11:02:30 -0400 Sender: kvm-owner@vger.kernel.org List-ID: 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 "win= 0". # 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=3D"win0" # Name of ur guest VM in the XML config file located i= n "/etc/libvirt/qemu/". TIMEOUT=3D300 VM_SAVE_DIR=3D"/kvm/tmp/vm_save_dir" # Directory for storing the guest = VM save data. DEVNULL=3D"/dev/null" #ACTION=3D"shutdown" ACTION=3D"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=3D$(ps ax|grep $VM_NAME|grep kvm|cut -c 1-6) COUNT=3D0 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=3D$(($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=3D$(ps ax|grep $VM_NAME|grep kvm|cut -c 1-6) COUNT=3D0 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=3D$(($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 ; t= hen 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=3D"$0" echo "Usage: "$N" {start|stop}" >&2 exit 1 ;; esac exit 0 #######################################################################= ############################ --=20 mit freundlichen Gr=FC=DFen / Kind regards Saman Behnam -- opensoft24-Systems Lena-Christ-Str. 3 80807 M=FCnchen Tel.: +49 (0) 89 69397631 Fax.: +49 (0) 89 69397631 info@opensoft24.de www.opensoft24.de www.opensoft24.com Bankverbindung: Postbank M=FCnchen BLZ.: 70010080 Kto.: 279702806 Steuer-Nr.: 135 70442 UST-IdNr.: 76 158 941 031 IBAN.: PBNKDEFF SWIFT.: DE21700100800279702806 Diese Nachricht und/oder Anh=E4nge sind vertraulich und k=F6nnen der Ge= heimhaltungspflicht unterliegen. Wenn Sie nicht der vorgesehene Adressa= t sind, weisen wir Sie hiermit darauf hin, dass jegliche Nutzung, Weite= rleitung und Kopien unzul=E4ssig sind. Benachrichtigen Sie uns bitte un= verz=FCglich per E-Mail und l=F6schen Sie diese Nachricht und/oder Anh=E4= nge aus Ihrem System. This mail and/or attachments are confidential and may also be legally p= rivileged. If you are not the intended recipient, you are hereby notifi= ed, that any review, dissemination, distribution or copying of this ema= il and/or attachments is strictly prohibited. Please notify us immediat= ely by email and delete this message and all its attachments.