XiZi's Blog
QEMU Service on Centos
1.先放个配置文件上来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # /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.服务所需文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | #!/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:
1 2 3 | # 如果你没安装过nc的话,就yum一下要么nc用不了,无法关闭服务器。 # yum install -y nc |