libvirt によるKVMのホストOSのシャットダウン時のオートシャットダウン(auto suspend)

ubuntu 9.04のKVM libvirtが思ったより良く出来ていていろいろ試してみた。
ホストOSを普通にシャットダウンすると、自動でゲストOS側もシャットダウンしてくれるんだろうな
と思い込んでいたが、違った。ゲストOS側はブッツリ切られてしまうようだ。

自動シャットダウン的なもの無いかと探したがなかなか見つからなくて
やっと見つけたのがこれだった。
http://www.vogelweith.com/debian_server/14_kvm.php#x1-170004

/usr/share/doc/libvirt-bin/examples/libvirt-suspendonreboot
こんなものが用意されてました。
中身は、↓の感じ。

#! /bin/bash

# (c) Andi Barth <aba@not.so.argh.org> 2008
# Distributable under the terms of the GNU GPL version 2.
#
# copy to /etc/init.d/libvirt-suspendonreboot and use
# update-rc.d libvirt-suspendonreboot defaults 21 19
# to enable

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

suspenddir=/var/lib/libvirt/autosuspend

case "$1" in
  start)
    for domain in ${suspenddir}/*dump; do
        if [ -f $domain ]; then
            domain=$(basename $domain .dump)
            echo "resuming $domain ..."
            virsh restore ${suspenddir}/${domain}.dump && rm ${suspenddir}/${domain}.dump
        fi
    done
    ;;
  stop)
    for domain in /etc/libvirt/qemu/*xml; do
        domain=$(basename $domain .xml)
        state=$(virsh domstate $domain)
        if [ "$state" == "running" ]; then
            echo "suspending $domain ..."
            virsh save ${domain} ${suspenddir}/${domain}.dump
        fi
    done
    ;;
  reload|force-reload|restart)
    # No action, nothing to reload
    ;;
  *)
        echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
        exit 1
esac

使い方は、以下のようにしろと書いてます

 cp /usr/share/doc/libvirt-bin/examples/libvirt-suspendonreboot /etc/init.d/ 
 chmod 755 /etc/init.d/libvirt-suspendonreboot 
 mkdir -p /var/lib/libvirt/autosuspend/ 
 update-rc.d libvirt-suspendonreboot defaults 21 19

まだ試して無いですが・・・多分動くでしょう。 動きました。