Table des matières

FIXME: cette page est obsolète, et n'a été utilisé que pour la création du paquet virt-stack

Contenue du paquet:

Script d'hibernation/réveil des VM lors de l'arrête/démarrage de l'hôte

Ce script ne devrait plus être nécessaire à partit de libvirt-0.8.0 (qui intègre de façon native une fonction “managed save” qui permet de réveiller une VM depuis un fichier state, simplement en démarrant le domaine)

#!/bin/sh
 
# the following is chkconfig init header
#
# virt-helper:   save and restore VM state
#
# chkconfig: 345 99 01
# description:  Libvirt helper script to save and load VM
#               state when the host shutdown and start
#               ensure also correct permissions and SELinux
#
 
# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sysconfig/virt-addons
 
VIRSH=$(which virsh)
VIRSH=$VIRSH" -c "$LIBVIRT_URI
 
RETVAL=0
ERROR=0
 
hibernate(){
    echo -n $"Saving Virtual Machines states: "
 
    # For each running VM (or paused ones), save the state in $STATE_DIR
    for VM in $(LANG=C $VIRSH list | egrep '(running|paused)' | awk {'print $2'}); do
        echo -n $"Saving $VM state: "
        $VIRSH save $VM $STATE_DIR/$VM.state 2>&1 > /dev/null
        if [ $? = 0 ]; then
            success; echo
        else
            ERROR=1
            error; echo
        fi
    done
}
 
wakup(){
    ERROR=0
 
    # For each state file found in $STATE_DIR, try to restore it
    # and remove the file if we succed, else report an error
    for VM in $(ls $STATE_DIR/*.state 2>/dev/null); do
        echo -n $"Restoring $VM state: "
        $VIRSH restore $VM 2>&1 > /dev/null
        if [ $? = 0 ]; then
            sucess; echo
            rm -f $VM
        else
            error; echo
            ERROR=1
        fi
    done
}
 
set_perms(){
    for DIR in /var/lib/libvirt/qemu /var/run/libvirt/qemu /var/cache/libvirt/qemu; do
        [ -d $DIR ] || mkdir -p $DIR
        chown $KVMUSER:$KVMGROUP $DIR || ERROR=1
    done
}
 
set_selinux(){
    # Fix SELinux context for all storage support used by a VM
    echo -n $"Fixing SELinux contexts: "
    VMS=$(LANG=C virsh list --all | egrep '(running|shut off|paused)' | awk {'print $2'})
    VOLS=$(for VM in $VMS;do virsh dumpxml $VM | xmlstarlet sel -t -m \
        "/domain/devices/disk/source" -v @dev -v @file -n; done | grep '/')
    for VOL in $VOLS;do 
        chcon -t virt_image_t $VOL || ERROR=1
    done
    if [ $(grep -c virt-addons $(semodule -l)) = 0]; then
        semodule -i /usr/share/selinux/targeted/virt-addons.pp || ERROR=1
    fi
    if [ $ERROR = 0 ]; then
        sucess; echo
        rm -f $VM
    else
        error; echo
        ERROR=1
    fi
}
 
start_ksm(){
    modprobe ksm 2>&1 > /dev/null
    chown :$KVMUSER /dev/ksm
    chmod 660 /dev/ksm
    ksmctl start $NBPAGES $SLEEP 2>&1 > /dev/null
}
 
stop_ksm(){
    ksmctl stop 2&1 > /dev/null
}
 
start(){
    [ $STATE ] && wakup
    [ $SET_PERM ] && set_perms
    [ $SET_SELINUX ] && set_selinux
    [ $KSM ] && start_ksm
    if [ $ERROR = 1 ]; then
        RETVAL=1
    fi
}
 
stop(){
    [ $STATE ] && hibernate
    [ $KSM ] && stop_ksm
    if [ $ERROR = 1 ]; then
        RETVAL=1
    fi
}
 
restart(){
    stop && start
}
 
case "$1" in
    start|stop|restart)
        $1
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac
exit $RETVAL

/etc/sysconfig/virt-addons:

# Should VM states should be saved when host shutdown and restored when started
# This acts as an auto hibernate function for VM, and make rebooting guests
# independantly from the host
STATE=true
 
# Should permissions should be set on some directory
SET_PERM=true
 
# Should SELinux functions (fixing context on devices, and allowing save/restore on
# systems using SELinux enforcing
SET_SELINUX=true
 
# Enable KSM
KSM=true
 
# Default URI for libvirt
LIBVIRT_URI="qemu:///system"
 
# User and group kvm runs as (as configured in /etc/libvirt/qemu.conf)
KVMUSER="qemu"
KVMGROUP="qemu"

Script de configuration des permissions qui vont bien

Hook script pour qemu (/etc/libvirt/hook/qemu)

#!/bin/bash
 
VM=shift
OP=shift
SUBOP=shift
 
VOLS=(cat /dev/stdin | xmlstarlet sel -t -m \
    "/domain/devices/disk/source" -v @dev -v @file -n; done | grep '/')
 
if [ $OP == "start" ]; then
    for VOL in $VOLS;do 
        chcon -t virt_image_t $VOL
    done
fi
 
exit 0

Hook script pour le démon (/etc/libvirt/hook/daemon)

#!/bin/bash
 
OBJ=shift
OP=shift
 
if [ $OP == "start" ]; then
    for DIR in /var/lib/libvirt/qemu /var/run/libvirt/qemu /var/cache/libvirt/qemu; do
        [ -d $DIR ] || mkdir -p $DIR
        chown qemu:qemu $DIR
    done
fi
 
exit 0

Un chcon tout les supports utilisés par une VM:

# Fix SELinux context for all storage support used by a VM
VMS=$(LANG=C virsh list --all | egrep '(running|shut off|paused)' | awk {'print $2'})
VOLS=$(for VM in $VMS;do virsh dumpxml $VM | xmlstarlet sel -t -m \
    "/domain/devices/disk/source" -v @dev -v @file -n; done | grep '/')
for VOL in $VOLS;do 
    chcon -t virt_image_t $VOL
done
 
# And fix permissions on some directories
for DIR in /var/lib/libvirt/qemu /var/run/libvirt/qemu /var/cache/libvirt/qemu; do
        [ -d $DIR ] || mkdir -p $DIR
        chown $KVMUSER:$KVMGROUP $DIR
done

FIXME:

Lancement de KSM

modprobe ksm
chown :$KVMUSER /dev/ksm
ksmctl start $NBPAGES $SLEEP 2>&1 > /dev/null

FIXME: