ホストのシャットダウン時のオートサスペンド

CentOS 6 なら libvirt-guests で停止→開始できるのでこのようなものは不要です。

.

.

ゲストが起動したままホストをシャットダウンすると、ゲストはプッツリ切られてしまうようなので、なんとかできないかと探してみたところ下記を発見。

http://d.hatena.ne.jp/desutai/20090502/1241248197

そのままコピペして使ってみたが・・・上手く動かなかったのでCentOS用に少し改変してみた。

改変したスクリプトの内容

#!/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

#
# chkconfig: 345 98 02
# description: libvirt-suspendonreboot
#

# Source function library.
. /etc/rc.d/init.d/functions

LANG=C

suspenddir=/var/lib/libvirt/autosuspend

function log
{
  logger -s -p daemon.info -t libvirt-suspendonreboot -- "$@"
}

case "$1" in
  start)
    touch /var/lock/subsys/libvirt-suspendonreboot
    
    log "begin resuming"
    sleep 3
    
    for domain in ${suspenddir}/*dump; do
        log "find dumpfile $domain"
        if [ -f $domain ]; then
            domain=$(basename $domain .dump)
            log "resuming $domain ..."
            virsh restore ${suspenddir}/${domain}.dump && rm ${suspenddir}/${domain}.dump
        fi
    done
    ;;
  stop)
    [ -f /var/lock/subsys/libvirt-suspendonreboot ] && \
      rm -f /var/lock/subsys/libvirt-suspendonreboot
    
    log "begin suspending"
    
    for domain in /etc/libvirt/qemu/*xml; do
        domain=$(basename $domain .xml)
        state=$(virsh domstate $domain)
        log "find domain $domain ($state)"
        if [ "$state" == "running" ]; then
            log "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

スクリプトの改変内容

・chkconfigで登録できるように・・・(起動順はlibvirtdの後じゃないとダメなので98に)

#
# chkconfig: 345 98 02
# description: libvirt-suspendonreboot
#

・「virsh domstate 」の結果が "running" じゃないとダメなので・・・

LANG=C

・シャットダウン時に「/var/lock/subsys/」が無いと実行されないので・・・

  start)
    touch /var/lock/subsys/libvirt-suspendonreboot
    
    :
  stop)
    [ -f /var/lock/subsys/libvirt-suspendonreboot ] && \
      rm -f /var/lock/subsys/libvirt-suspendonreboot

・libvirtdの起動直後だとvirshが失敗したので・・・

  start)
  
    :
    
    sleep 3

・その他適当にログ書くように変更

スクリプトの設置方法

vi /etc/init.d/libvirt-suspendonreboot
chmod 755 /etc/init.d/libvirt-suspendonreboot
mkdir -p /var/lib/libvirt/autosuspend/
chkconfig --add libvirt-suspendonreboot
/etc/init.d/libvirt-suspendonreboot start

最後に一回 start しているのは /var/lock/subsys/libvirt-suspendonreboot を作るため、
これが無いとシャットダウン時にスクリプトが実行されない(これ気付くのにかなり時間かかりました・・・)。

課題とか?

autostartされるようにしていると上手く動作しない気がする・・・
sleep 3 が適当すぎなのでホスト起動時にリジュームされないことがあるかも・・・