XiZi's Blog
QEMU Service on Centos
1.先放个配置文件上来
# /etc/qemu.conf
# VMs that should be started on boot
# use the ! prefix to disable starting/stopping a VM
# QEMU_MACHINES=(kms)
QEMU_MACHINES=(kms)
# NOTE: following options will be prepended to qemu_${vm}
# -name ${vm} -pidfile /var/run/qemu/${vm}.pid -daemonize -nographic
qemu_kms_type="system-x86_64"
qemu_kms="-enable-kvm -hda /opt/qemu/kmsm4WOen.rom \
-smbios type=1,manufacturer=Intel,version=1.01234,uuid=564d81c6-cd3a-d8e4-db29-756df139acb9 \
-uuid 564d81c6-cd3a-d8e4-db29-756df139acb9 \
-net nic -net user,hostfwd=tcp::1688-:1688 \
-m 256 -rtc base=localtime,clock=host -M pc \
-monitor telnet:localhost:7100,server,nowait,nodelay"
qemu_kms_haltcmd="echo 'system_powerdown' | nc localhost 7100" # or netcat/ncat
2.服务所需文件。
#!/bin/sh
# /etc/init.d/qemu
. /etc/rc.d/init.d/functions
[ -f /etc/qemu.conf ] && source /etc/qemu.conf
piddir=/var/run/qemu
lock='/var/lock/subsys/qemu-1.5rc1'
QEMU_PATH=/opt/qemu/qemu-1.5rc1
QEMU_DEFAULT_FLAGS='-name ${vm} -pidfile ${piddir}/${vm}.pid -daemonize -vga none -display none'
# -nographic
QEMU_HALTCMD_WAIT=30
case "$1" in
start)
[ -d "${piddir}" ] || mkdir -p "${piddir}"
if [ -f $lock ]; then
# we were not shut down correctly
for pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; do
if [ -s $pidf ]; then
kill `cat $pidf` >/dev/null 2>&1
fi
rm -f $pidf
done
rm -f $lock
sleep 2
fi
rm -f $piddir/*.pid
errors=0
sucesses=0
for vm in "${QEMU_MACHINES[@]}"; do
if [ "${vm}" = "${vm#!}" ]; then
echo -n $"Starting QEMU VM: ${vm}"
eval vm_cmdline="\$qemu_${vm}"
eval vm_type="\$qemu_${vm}_type"
if [ -n "${vm_type}" ]; then
vm_cmd="${QEMU_PATH}/bin/qemu-${vm_type}"
else
vm_cmd="${QEMU_PATH}/bin/qemu"
fi
eval "qemu_flags=\"${QEMU_DEFAULT_FLAGS}\""
${vm_cmd} ${qemu_flags} ${vm_cmdline} >/dev/null
if [ $? = 0 ]; then
successes=1
else
errors=1
fi
fi
done
if [ $errors = 1 ]; then
failure; echo
else
success; echo
fi
if [ $successes = 1 ]; then
touch $lock
fi
;;
stop)
for vm in "${QEMU_MACHINES[@]}"; do
if [ "${vm}" = "${vm#!}" ]; then
# check pidfile presence and permissions
if [ ! -r "${piddir}/${vm}.pid" ]; then
continue
fi
echo -n $"Stopping QEMU VM: ${vm}"
eval vm_haltcmd="\$qemu_${vm}_haltcmd"
eval vm_haltcmd_wait="\$qemu_${vm}_haltcmd_wait"
vm_haltcmd_wait=${vm_haltcmd_wait:-${QEMU_HALTCMD_WAIT}}
vm_pid=$(cat ${piddir}/${vm}.pid)
# check process existence
if ! kill -0 ${vm_pid} 2>/dev/null; then
stat_done
rm -f "${piddir}/${vm}.pid"
continue
fi
# Try to shutdown VM safely
_vm_running='yes'
if [ -n "${vm_haltcmd}" ]; then
eval ${vm_haltcmd} >/dev/null
_w=0
while [ "${_w}" -lt "${vm_haltcmd_wait}" ]; do
sleep 1
if ! kill -0 ${vm_pid} 2>/dev/null; then
# no such process
_vm_running=''
break
fi
_w=$((_w + 1))
done
else
# No haltcmd - kill VM unsafely
_vm_running='yes'
fi
if [ -n "${_vm_running}" ]; then
# kill VM unsafely
kill ${vm_pid} 2>/dev/null
sleep 1
fi
# report status
if kill -0 ${vm_pid} 2>/dev/null; then
# VM is still alive
#kill -9 ${vm_pid}
failure; echo
else
success; echo
fi
# remove pidfile
rm -f "${piddir}/${vm}.pid"
fi
done
rm -f $lock
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "usage: $0 {start|stop|restart}"
esac
PS:
# 如果你没安装过nc的话,就yum一下要么nc用不了,无法关闭服务器。 # yum install -y nc