Sam,
I make use of QMP to do the shutdown of the QEMU VMs on Linux. The RC script
is what I use to start and stop the VMs I run when starting and stopping the
systems running the VMs. Look at the function stop_one_qemu_vm in the attached
RC script and see if that would help do what you want to do. The file that the
script reads to start VMs contains command that are simply executed such as:
/vms/Repositories/Repository/VM_Start.sh
sleep 60
Each line is the command that is executed.
Also, if you have VNC capabilities enabled, or another console access method
such as spice enabled, and you are at the VM's console, you should be able to
use the command sequence:
ctrl-alt-shift-2
to get the QEMU command screen at the VM's console (VNC, Spice, etc). You can
enter a command such as:
system_powerdown
system_wakeup
system_reset
to stop or reset the VM. You can use the command sequence:
ctrl-alt-shift-1
to get back to the VM's console. I would also use "kill -PWR" before using "kill
-9" if you need to manually force a shutdown. Remember the shutdown process will behave just
like shutting down a physical machine.
If you can ssh into the VM and become the root user you should be able to
tell the OS to shutdown with something like:
/sbin/shutdown -t 5 -P now
for Linux and Unix based VMs. For Windows you should be able, if you log in as
an administrator, to do a clean shutdown and power off the system.
Don
Sam wrote on 07/12/2017 05:24 AM:
hi all,
I'm running `qemu-system-x86_64 ...` to start a guest vm. Now I want to
stop and destroy this vm, I found there is no `qemu-stop` related command,
so I have to `kill -9` this process.
How could I stop `qemu-system-x86_64` instead of `kill -9`? Use `virsh`
command or something?
Thank you~
#!/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
###############################################################################
# #
###############################################################################