查看CPU是否支持虚拟化技术

cat /proc/cpuinfo | egrep 'vmx|svm' 

如之前安装过,清理环境

yum erase `rpm -qa | egrep 'qemu|virt|kvm'`
rm   -rf   /var/lib/libvirt    /etc/libvirt/

安装软件

 # centos6      
  yum groupinstall "Virtualization" "Virtualization Client" "Virtualization Platform" "Virtualization Tools" -y
 # centos7       
  yum -y install kvm qemu-kvm python-virtinst libvirt libvirt-python virt-manager libguestfs-tools bridge-utils virt-install

启动服务

centos6:# /etc/init.d/libvirtd start
centos7:# systemctl start libvirtd
ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm

利用iso制作镜像

qemu-kvm -m 512 -smp 2 -name ceontos-test -drive file=/data/img/centos7.qcow2,if=virtio,media=disk,cache=writeback,format=qcow2 -drive file=/data/os/CentOS-7-x86_64-DVD-2009.iso,media=cdrom -boot order=dc,once=d -vnc :5

vncviewer连接地址:192.168.0.250:5905

添加网桥并为其设置ip地址

brctl addbr br0

ip addr add 172.16.0.1 dev br0

设置网卡脚本

[root@localhost ~]# cat /etc/qemu-ifup 
#!/bin/bash
# 桥接网络设备
switch=br0
if [ -n $1 ]; then          //$1为qemu-kvm传递值,这里是tap
    ip link set $1 up
    brctl addif ${switch} $1
    exit 0
else
   echo "no interface!"
   exit 1
fi
[root@localhost ~]# cat /etc/qemu-ifdown 
#!/bin/bash
#set your bridge name
switch=br0
if [ -n "$1" ]; then
    # Delete the specified interfacename
    tunctl -d $1
    #release TAP interface from bridge
    brctl delif ${switch} $1
    #shutdown the TAP interface
    ip link set $1 down
    exit 0
else
    echo "Error: no interface specified"
    exit 1
fi

启动已制作好的镜像

qemu-kvm -m 2048 -smp 2 -drive file=/data/img/centos7.qcow2,if=virtio,index=1 -net nic -net tap,ifname=v1f0.0,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown -nographic -vnc :5

参数介绍

-smp 2 处理器的个数,也可以设置cores threds

sockets,即每个cpu的核心数以及cpu的线程数

if=virtio 指定硬盘设备所连接的接口类型

index=1 指定接口类型的标识号

启用snat转换

firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=172.16.0.0/24 masquerade'

虚拟机ping 测试

#先测试虚拟机和网桥是否能通
#测试虚拟机能否通过snat转换出网  ping 114.114.114.114
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。