#!/bin/sh ############################################################################### # # # rc.vms R00-02.00 # # # # =========================================================================== # # # # Purpose: # # # # This script is used to start and stop Virtual Machines. # # # # =========================================================================== # # # # Arguments: # # # # # # =========================================================================== # # # # Programming Notes: # # # # This script expects that the configuration contains at least a -qmp # # option on the command line that started the Virtual Machine session. # # # # =========================================================================== # # # # Revision: # # # # Revision Who When Why # # ========= =========== =========== ====================================== # # R00-00.00 DRLJr/I-AS 09/May/2013 Script created # # R00-01.00 DRLJr/I-AS 10/May/2013 Reworked to use ps and look at the # # command line of the process for a -qmp # # option to do a controlled power based # # shutdown. Added use of start-up # # configuration file to start the VMs # # via /etc/virtual_machines so the # # script does not have to change. # # R00-02.00 DRLJr/I-AS 02/May/2014 Echo command being run, except for the # # sleep commands (sleep, /bin/sleep, # # or /usr/bin/sleep). # # # ############################################################################### #=============================================================================# # # #=============================================================================# get_field() { OFF=$1 if [ ${OFF} -le $# ] ; then shift ${OFF} ANS="$1" else ANS="" fi echo "${ANS}" return 0 } #=============================================================================# # Insure all leading blanks are eliminated # #=============================================================================# strip_blanks() { CMD="$*" echo "${CMD}" return 0 } #=============================================================================# # Extract the Associated QEMU Pipe Name itself # #=============================================================================# vms_extract_field() { echo "$2" return 0 } #=============================================================================# # Extract the Process Ids # #=============================================================================# vms_extract_pids() { WPIDS="" # for pid in $* ; do while ( true ) do if [ "$1" = "" ] ; then break fi WPIDS="${WPIDS} $1" shift 2 done echo "${WPIDS}" return 0 } #=============================================================================# # Read a Configuration File # #=============================================================================# start_vms_config_file() { while ( true ) do read COMMAND EOF=$? if [ ${EOF} -ne 0 ] ; then break fi COMMAND="`strip_blanks ${COMMAND}`" COMMENT="${COMMAND:0:1}" if [ "${COMMENT}" != "#" -a "${COMMENT}" != "" ] ; then # Not a Command echo "Executing Command: ${COMMAND}" ${COMMAND} fi done return 0 } #=============================================================================# # Start a Virtual Machine Session # #=============================================================================# start_vms() { echo "Removing any left-over Virtual Machine access point files." echo " " rm -f /tmp/.qemu_control_* CFG_FILES="`ls ${VM_CDIR}/*.conf`" for file in ${CFG_FILES} ; do echo " " echo "Processing ${file}" echo " " start_vms_config_file < ${file} done return 0 } #=============================================================================# # Extract the Associated QEMU Pipe (-qmp argument) # #=============================================================================# vms_extract_qemu_pipe() { PIPENAME="none" STATUS=1 while ( true ) do if [ "$1" = "" ] ; then break fi if [ "$1" = "-qmp" ] ; then PIPENAME="$2" IFSSV="${IFS}" IFS=":," PIPENAME=`vms_extract_field $2` IFS="${IFSSV}" STATUS=0 break fi shift 1 done echo "${PIPENAME}" return ${STATUS} } #=============================================================================# # $1=PIPE $2=QPID [ $3=QFNAM ] # #=============================================================================# stop_one_qemu_vm() { echo "Shutting down QEMU Virtual Machine $2 (Access Pipe $1)" # echo "QEMU PIPE = $1" # echo "QEMU PID = $2" # echo "QEMU FNAM = $3" if [ "$1" = "none" ] ; then echo " Killing Virtual Machine $2 with PWR (no -qmp control socket)" kill -PWR $2 STATUS=2 else echo "system_powerdown" | /usr/local/sbin/QMP/qmp-shell $1 > /dev/null EOT=$? if [ ${EOT} != 0 ] ; then echo " Unable to communicate with QEMU Virtual Machine $2 to do shutdown." echo " Killing Virtual Machine $2 with PWR (no -qmp control socket)" kill -PWR $2 STATUS=1 fi fi for try in 10 10 20 20 30 30 30 30 30 30 30 30 30 kill ; do RPID=`ps -e -o pid | grep "^ *$2"` RPID="${RPID/ /}" if [ "${RPID}" != "" -a "${try}" != "kill" ] ; then echo " Waiting for ${try} seconds for Virtual Machine $2 shutdown" sleep ${try} else if [ "${try}" = "kill" ] ; then echo " Killing Virtual Machine $2 with PWR" kill -PWR $2 fi echo " Virtual Machine $2 is shutdown " rm -f $1 if [ "$3" != "" ] ; then rm -f $3 else rm -f $1,* fi STATUS=0 break fi done # Remove the acess files regardless rm -f $1 if [ "$3" != "" ] ; then rm -f $3 else rm -f $1,* fi return ${STATUS} } #=============================================================================# # Get the information for the QEMU Virtual Machines # #=============================================================================# stop_qemu_vms() { QEMUS=`ps -e -o pid,comm | grep qemu` QPIDS=`vms_extract_pids ${QEMUS}` for pid in ${QPIDS} ; do QPSINFO=`ps -p ${pid} -o pid,args --no-headers ` # echo " " # echo "QPSINFO=${QPSINFO}" # echo " " PIPE=`vms_extract_qemu_pipe ${QPSINFO}` STATUS=$? # echo "STATUS=${STATUS}, PIPE=${PIPE}" # echo " " stop_one_qemu_vm ${PIPE} ${pid} # echo " " done return 0 } #=============================================================================# # # #=============================================================================# stop_vms() { stop_qemu_vms return 0 } #=============================================================================# # # #=============================================================================# VM_CDIR="/etc/virtual_machines" case "$1" in 'start') start_vms ;; 'stop') stop_vms ;; *) echo "usage $0 start|stop" ;; esac ############################################################################### # # ###############################################################################