#!/bin/bash # # /etc/init.d/xendomains # Starts/stops domains automatically when domain0 boots/shuts down. # # chkconfig: 345 99 00 # description: Start / stop Xen domains. # # This script offers fairly basic functionality. It should work on Redhat # but also on LSB-compliant SuSE releases and on Debian with the LSB package # installed. (LSB is the Linux Standard Base) # # Based on the example in the "Designing High Quality Integrated Linux # Applications HOWTO" by Avi Alkalay # # ### BEGIN INIT INFO # Provides: xendomains # Required-Start: $syslog $remote_fs xend # Should-Start: # Required-Stop: $syslog $remote_fs xend # Should-Stop: # Default-Start: 3 4 5 # Default-Stop: 0 1 2 6 # Default-Enabled: yes # Short-Description: Start/stop secondary xen domains # Description: Start / stop domains automatically when domain 0 # boots / shuts down. ### END INIT INFO XEN_HOME="/etc/xen/auto" WAIT_TIME="15" export WAIT_TIME wait_time () { for i in $(seq 0 ${WAIT_TIME}) do echo -n "." sleep 1 done echo "" } # this function sort the order that machines # will be start, following the boot scripts sort # exemplo: # 01machine-foo will start before 05machine-bar machines_sort () { MACHINES_SORT=$(ls -1 "$XEN_HOME") for machine in ${MACHINES_SORT} do if [ -f "/etc/xen/auto/${machine}" ]; then MACHINES="${MACHINES} ${machine}" fi done echo "${MACHINES}" } # this function check if the machine # passed as parameter is running and # return 1 if it is true machines_check () { machine="$(echo ${1} | cut -c 3-)" xm list | grep "${machine} " > /dev/null if [ "${?}" = "1" ]; then echo "0" else echo "1" fi } # this function return a list # of all machines running # even the Zombies machines_running () { machines="$(xm list | sed '1,2d;s/ .*//')" if [ -z "${machines}" ]; then echo "NULL" else for machine in ${machines} do MACHINES="${machine} ${MACHINES}" done echo "${MACHINES}" fi } # this function start all # machines in order to the # sort function xen_start () { m_sort="$(machines_sort)" for xen_machine in $m_sort do m_running="$(machines_check ${xen_machine})" if [ "${m_running}" = "0" ]; then xm create ${XEN_HOME}/${xen_machine} > /dev/null 2>&1 if [ "${?}" = "0" ]; then echo -n "Starting ${xen_machine} "; wait_time else echo "Unable to start ${xen_machine}" fi else echo "${xen_machine} already running" fi done } # this function stop # all machines that are # running xen_stop () { m_running="$(machines_running)" if [ "${m_running}" = "NULL" ]; then echo "No machine running" else for xen_machine in ${m_running} do xm shutdown ${xen_machine} echo -n "Halting $xen_machine "; wait_time done fi } case "${1}" in 'start') xen_start ;; 'stop') xen_stop ;; *) echo "Use ${0} [start/stop] and wait" ;; esac