Présentation

KVM ou Kernel Virtual Machine permet de virtualiser des environnements matériel, au même titre que VirtualBox.

Script init

Ce script lance des machines virtuelles KVM d’un dossier donné.

Merci à Lord sous irc.evolu.net pour l’avoir partagé.

#! /bin/sh
### BEGIN INIT INFO
# Provides:          rd-kvm
# Required-Start:    
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start enabled virtual machines
# Description:       This script start the enabled virtual machines
### END INIT INFO

. /lib/lsb/init-functions

sleep_time=5
RUN_PATH=/var/run/kvm

case "$1" in
  start)
        for i in /etc/kvm/enabled/* ; do
                if su - kvm -c $i
                then
                        log_success_msg "$i started"
                else
                        log_failure_msg "Could not start $i"
                fi
        done

        ;;
  stop)
        for i in $RUN_PATH/*.pid; do
                PID=`cat $i`
                MONITOR=`echo $i | sed -e 's/pid/monitor/'`
                NAME=`echo $i | sed -e 's/pid//'`
                echo "Shutting down $NAME"
                echo "system_powerdown" | socat - UNIX-CONNECT:$MONITOR
                while [ -d /proc/$PID ]; do
                        echo -n "."
                        sleep 1
                done
                echo "Done."
                # kill -TERM $PID
                rm `echo $i`
        done
        
        ;;
  force-reload|restart)
        $0 stop
        $0 start
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|force-reload}" >&2
        exit 3
        ;;
esac