Package: libvirt-bin Version: 0.5.1-2 Severity: wishlist Tags: patch Hi,
I implemented feature to automatically save virtual machines managed by libvirt on host shutdown and restore on host startup. See the attached patch. If you wish, you can use it as is or modify in any way desired. It has only been tested with KVM based VMs. Regards, -- Tuomas Jormola <t...@solitudo.net>
Index: debian/libvirt-domain-state.default =================================================================== --- debian/libvirt-domain-state.default (revision 0) +++ debian/libvirt-domain-state.default (revision 0) @@ -0,0 +1,29 @@ +# Provide support for storing virtual machine state on host shutdown +# and restore on host start-up. In order to support this, please note +# that you must configure each virtual machine so that the libvirt +# autostart feature is disabled. Also if you're using Xen, the built-in +# save/restore on shutdown/start-up mechanism must be disabled. + +# Directory where to save virtual machine state files. This directory +# needs to be created manually. Be sure that there's enough free disk +# space available! You will need at least the combied amount of physical +# memory and swap space allocated to all virtual machines plus some room +# for overhead. For instance, if you are running two virtual machines +# each configured to use 1GB of memory and 2GB of swap, you should +# reserve more than 6GB of space in the file system holding this +# directory. This setting is required. +#vm_state_file_directory=/var/lib/libvirt/domain-state-files + +# libvirt URI of the hypervisor. +# See http://libvirt.org/remote.html#Remote_URI_reference +# This setting is required. +#libvirt_hypervisor_uri=qemu:///system + +# Uncommenting this setting will destroy each virtual machine state file +# after successful restore. Disabled by default, i.e. restore is performed +# but state files are left intact. +#delete_restored_vm_state_files=true + +# Uncomment this to actually enable automatic saving and restoring +# of the virtual machines. +#enabled=true Index: debian/rules =================================================================== --- debian/rules (revision 1604) +++ debian/rules (working copy) @@ -43,3 +43,7 @@ binary-install/libvirt-doc:: cd $(EXAMPLES) && rm -rf .libs *.o info1 suspend ../html/CVS + +binary-post-install/libvirt-bin:: + install -m 755 -o root -g root debian/libvirt-domain-state.init debian/libvirt-bin/etc/init.d/libvirt-domain-state + install -m 644 -o root -g root debian/libvirt-domain-state.default debian/libvirt-bin/etc/default/libvirt-domain-state Index: debian/libvirt-domain-state.init =================================================================== --- debian/libvirt-domain-state.init (revision 0) +++ debian/libvirt-domain-state.init (revision 0) @@ -0,0 +1,120 @@ +#!/bin/sh +# +# This script automatically saves running VMs +# managed by libvirtd on stop and restores VMs on start +# +# (C) 2008 Tuomas Jormola <t...@solitudo.net> +# +### BEGIN INIT INFO +# Required-Start: $network $local_fs +# Required-Stop: +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: save and restore libvirtd managed VMs +### END INIT INFO + +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +enabled=false + +. /lib/lsb/init-functions + +if test -f /etc/default/libvirt-domain-state; then + . /etc/default/libvirt-domain-state +fi + +test "$enabled" = "true" || exit 0 + +if test -z "$vm_state_file_directory"; then + log_failure_msg "Virtual Machine save directory not defined" + exit 1 +fi +if ! test -d "$vm_state_file_directory"; then + log_failure_msg "Virtual Machine save directory not found: $vm_state_file_directory" + exit 1 +fi +if test -z "$libvirt_hypervisor_uri"; then + log_failure_msg "libvirt hypervisor connection URI not defined" + exit 1 +fi +if test "$libvirt_hypervisor_uri" != "`virsh -q -c $libvirt_hypervisor_uri uri 2>/dev/null`"; then + log_failure_msg "Failed to connect to libvirt hypervisor $libvirt_hypervisor_uri" + exit 1 +fi + +set -e + +run_virsh() { + virsh -q -c $libvirt_hypervisor_uri $* +} + +print_vm_status() { + log_action_msg "$vm_name: $vm_status" +} + +save_vm() { + if test "$vm_status" != "running"; then + return + fi + vm_state_file=$vm_state_file_directory/$vm_name + if run_virsh save $vm_id $vm_state_file >/dev/null 2>&1; then + log_action_msg "$vm_name" + else + log_failure_msg "Failed to save $vm_name as $vm_state_file" + fi +} + +iterate_virtual_machines() { + callback=$1 + run_virsh list 2>/dev/null | grep -v ^Connecting | \ + while read vm_id vm_name vm_status; do + eval "$callback" + done +} + +restore_virtual_machines() { + for vm_state_file in $vm_state_file_directory/*; do + test -f $vm_state_file || continue + vm_name=`basename $vm_state_file` + if run_virsh restore $vm_state_file >/dev/null 2>&1; then + if test "$delete_restored_vm_state_files" = "true"; then + rm -f $vm_state_file + fi + log_action_msg "$vm_name" + else + log_failure_msg "Failed to restore $vm_name from $vm_state_file" + fi + done +} + +save_virtual_machines() { + iterate_virtual_machines save_vm +} + +check_status() { + iterate_virtual_machines print_vm_status +} + +case "$1" in + start) + log_action_begin_msg "Restoring virtual machines" + restore_virtual_machines + log_action_end_msg 0 + ;; + stop) + log_action_begin_msg "Saving virtual machines" + save_virtual_machines + log_action_end_msg 0 + ;; + status) + log_action_begin_msg "Checking status of virtual machines" + check_status + log_action_end_msg 0 + ;; + *) + N=/etc/init.d/libvirt-domain-state + echo "Usage: $N {start|stop|status}" >&2 + exit 1 + ;; +esac + +exit 0 Index: debian/libvirt-bin.postinst =================================================================== --- debian/libvirt-bin.postinst (revision 1604) +++ debian/libvirt-bin.postinst (working copy) @@ -34,6 +34,10 @@ ;; esac +if [ -x "/etc/init.d/libvirt-domain-state" ]; then + update-rc.d libvirt-domain-state start 21 2 3 4 5 . stop 19 0 1 6 . >/dev/null +fi + # dh_installdeb will replace this with shell code automatically # generated by other debhelper scripts. Index: debian/libvirt-bin.postrm =================================================================== --- debian/libvirt-bin.postrm (revision 1604) +++ debian/libvirt-bin.postrm (working copy) @@ -24,6 +24,7 @@ if getent group libvirt >/dev/null; then delgroup libvirt || true fi + update-rc.d libvirt-domain-state remove >/dev/null || exit $? ;; remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear) Index: debian/README.Debian =================================================================== --- debian/README.Debian (revision 1604) +++ debian/README.Debian (working copy) @@ -54,4 +54,11 @@ Access to the libvirt socket is controlled by membership in the "libvirt" group. If you want to manage VMs as non root you need to add a user to that group. +Automatic saving and restoring of virtual machines +================================================== +libvirt-bin package ships with an init script that can be used to save running +virtual machines on host shutdown and restore the saved virtual machines on +host start-up automatically. In order to enable this feature, you have to edit +the file /etc/default/libvirt-domain-state. + -- Guido Guenther <a...@sigxcpu.org> Thu, 15 May 2008 14:13:03 +0100