操作系统:CentOS 6.4
宿主机:10.1.6.16
虚拟机:10.1.6.20-22
虚拟方式:kvm

安装kvm

cd /home/jm/src/qemu-kvm/qemu-kvm
./configure
make -j 20
make install | tee make-install.log

挂载centos iso镜像:

mount -o loop /home/jm/src/CentOS-6.4-x86_64-bin-DVD1.iso /mnt

创建虚拟镜像:

qemu-img create /home/jm/kvm/etl1.img 200G
qemu-img create /home/jm/kvm/etl2.img 200G
qemu-img create /home/jm/kvm/etl3.img 200G

安装虚拟机

/usr/local/bin/qemu-system-x86_64 --enable-kvm -m 20480 -smp 8 /home/jm/kvm/etl1.img \
-cdrom /home/jm/src/CentOS-6.4-x86_64-bin-DVD1.iso \
--nographic -bios /usr/local/share/qemu/bios.bin -append console=ttyS0 -kernel /mnt/isolinux/vmlinuz -initrd /mnt/isolinux/initrd.img

/usr/local/bin/qemu-system-x86_64 --enable-kvm -m 20480 -smp 8 /home/jm/kvm/etl2.img \
-cdrom /home/jm/src/CentOS-6.4-x86_64-bin-DVD1.iso \
--nographic -bios /usr/local/share/qemu/bios.bin -append console=ttyS0 -kernel /mnt/isolinux/vmlinuz -initrd /mnt/isolinux/initrd.img

/usr/local/bin/qemu-system-x86_64 --enable-kvm -m 20480 -smp 8 /home/jm/kvm/etl3.img \
-cdrom /home/jm/src/CentOS-6.4-x86_64-bin-DVD1.iso \
--nographic -bios /usr/local/share/qemu/bios.bin -append console=ttyS0 -kernel /mnt/isolinux/vmlinuz -initrd /mnt/isolinux/initrd.img

配置宿主机网卡,使用桥接模式

vi /etc/sysconfig/network-scripts/ifcfg-em1

DEVICE="em1"
BOOTPROTO="none"
ONBOOT="yes"
BRIDGE=br0
TYPE="ethernet"

vi /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE="br0"
BOOTPROTO="static"
ONBOOT="yes"
TYPE="Bridge"
IPADDR="10.1.6.16"
NETMASK="255.255.255.0"
GATEWAY="10.1.6.254"
DELAY=0

重启网卡

service network restart

配置虚拟机网卡

需要先给虚拟机分配mac地址,将mac绑定到虚拟机上

etl1配置

启动

/usr/local/bin/qemu-system-x86_64 /home/jm/kvm/etl1.img -m 20480 -smp 8 -k en-us -net nic,macaddr=DE:AD:BE:EF:24:16 -net tap,script=no,downscript=no -nographic 

配置网卡:

DEVICE="eth0"
BOOTPROTO="static"
ONBOOT="yes"
TYPE="ethernet"
IPADDR="10.1.6.20"
NETMASK="255.255.255.0"
GATEWAY="10.1.6.254"

删除/etc/udev/rules.d/70-persistent-net.rules

reboot

etl2配置

启动

/usr/local/bin/qemu-system-x86_64 /home/jm/kvm/etl2.img -m 20480 -smp 8 -k en-us -net nic,macaddr=DE:AD:BE:EF:28:85 -net tap,script=no,downscript=no -nographic 

配置网卡:

DEVICE="eth0"
BOOTPROTO="static"
ONBOOT="yes"
TYPE="ethernet"
IPADDR="10.1.6.21"
NETMASK="255.255.255.0"
GATEWAY="10.1.6.254"

删除/etc/udev/rules.d/70-persistent-net.rules

reboot

etl3配置

启动

/usr/local/bin/qemu-system-x86_64 /home/jm/kvm/etl3.img -m 20480 -smp 8 -k en-us -net nic,macaddr=DE:AD:BE:EF:39:53 -net tap,script=no,downscript=no -nographic

配置网卡:

DEVICE="eth0"
BOOTPROTO="static"
ONBOOT="yes"
TYPE="ethernet"
IPADDR="10.1.6.21"
NETMASK="255.255.255.0"
GATEWAY="10.1.6.254"

删除/etc/udev/rules.d/70-persistent-net.rules

reboot

网络测试没问题后可以关闭虚拟机,以后台方式重新启动,方法如下:

后台启动/停止:

kvm-manager {etl1|etl2|etl3} {start|stop|restart}

备注:

etl1 = l-etl1.bi.prod.cn1 (10.1.6.20)
etl2 = l-etl2.bi.prod.cn1 (10.1.6.21)
etl3 = l-etl3.bi.prod.cn1 (10.1.6.22)

kvm-manager脚本内容(放在/usr/bin/):

#!/bin/bash
VirtualName=$1

etl1_start() {
        /usr/local/bin/qemu-system-x86_64 /home/jm/kvm/etl1.img -m 20480 -smp 8 -k en-us -net nic,macaddr=DE:AD:BE:EF:24:16 -net tap,ifname=tap0,script=/etc/qemu-ifup,downscript=no -localtime -nographic -daemonize
}
etl2_start() {
        /usr/local/bin/qemu-system-x86_64 /home/jm/kvm/etl2.img -m 20480 -smp 8 -k en-us -net nic,macaddr=DE:AD:BE:EF:28:85 -net tap,ifname=tap1,script=/etc/qemu-ifup,downscript=no -localtime -nographic -daemonize
}
etl3_start() {
        /usr/local/bin/qemu-system-x86_64 /home/jm/kvm/etl3.img -m 20480 -smp 8 -k en-us -net nic,macaddr=DE:AD:BE:EF:39:53 -net tap,ifname=tap2,script=/etc/qemu-ifup,downscript=no -localtime -nographic -daemonize
}

stop() {
        pid=`ps -ef |grep /home/jm/kvm/${VirtualName}.img |grep -v grep |awk '{print $2}'`
        if [ -z "$pid" ];then
                echo "$VirtualName Not running"
        else
                kill -9 $pid && echo "$VirtualName killed" || echo "$VirtualName stop fail"
        fi
}

case "$2" in
  start)
        ${VirtualName}_start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        ${VirtualName}_start
        ;;
  *)
        echo $"Usage: $0 {etl1|etl2|etl3} {start|stop|restart}"
        echo ""
        echo "  etl1 = l-etl1.bi.prod.cn1 (10.1.6.20)"
        echo "  etl2 = l-etl2.bi.prod.cn1 (10.1.6.21)"
        echo "  etl3 = l-etl3.bi.prod.cn1 (10.1.6.22)"
        exit 2
esac

[内部资料]