安装
检测cpu支持虚拟化不,通常都支持,不支持从bios设置角度排障; cat /proc/cpuinfo | grep -E "(vmx|svm)"
一:通过yum
centos7下简单安装
yum groupinstall "Virtualization Host" -y
yum -y install guestfish virt-install virt-viewer
centos6下核心程序安装: yum -y install qemu-kvm #用户空间 yum -y install qemu-img #磁盘管理工具 yum -y install libvirt libvirt-python libvirt-client libvirt-devel #KVM管理工具 yum -y install libguestfs libguestfs-tools-c libguestfs-tools libguestfs-deve libguestfs-winsupport #guestfish套件
二:编译安装
篇幅较长暂留
三、添加网桥
#cat ifcfg-em1
DEVICE=em1
TYPE=Ethernet
HWADDR=52:54:00:EE:1F:15
ONBOOT=yes
BOOTPROTO=none
NM_CONTROLLED=no
BRIDGE=br0
cat ifcfg-br0
DEVICE=br0
TYPE=Bridge
#NM_CONTROLLED=no
BOOTPROTO=static
IPADDR=xxxx
NETMASK=xxxx
GATEWAY=xxxx
ONBOOT=yes
使用
一:通过定义XML文件增加一台虚拟机
二:通过virt-install 新增一台虚拟机
1.准备磁盘:通常选择支持快照的qcow2格式,性能最好的是raw格式
qemu-img create -f qcow2 win7a.disk 100G
2.安装虚拟机
virt-install -n centos1 -r 4096 -c /data/isos/CentOS-6.5-x86_64-minimal.iso --disk path=/data/vms/centos1.disk,device=disk,bus=virtio,size=100,format=qcow2 --vnc --vnclisten=0.0.0.0 -v --network bridge=br0,model=virtio
virt-install -n win7a -r 4096 --vcpus sockets=1,cores=2,threads=2 --os-type=windows --disk path=/data/vhosts/test/win7a.disk,device=disk,bus=virtio,size=100,format=qcow2 --disk path=/data/isos/virtio-win-0.1.126.iso,device=cdrom -c /data/isos/win7.iso --graphics spice,listen=0.0.0.0 --video qxl -v --network bridge=br-ex,model=virtio
3.驱动安装
SCSI——网卡——GPU
4.创建快照、还原和删除快照
在完成优化后,执行创建:
virsh snapshot-create-as win7a win7a_sn1 description
virsh snapshot-delete win7a win7a_sn1 virsh snapshot-revert win7a win7a_sn1
5.新增各种硬件
a.cd-rom
attach-disk win7b /data/isos/virtio-win-0.1.126.iso hdc --driver qemu --subdriver raw --type cdrom
attach-disk win7a /data/isos/virtio-win-0.1.126.iso vdd
b.添加和删除磁盘
qcow: virsh attach-disk win7a /data/vhosts/test/win7_1.disk vdb --cache none --subdriver qcow2 --config --persistent
raw : virsh attach-disk win7a /data/vhosts/test/win7_1.disk vdc --cache none --subdriver raw --config --persistent
domblklist win7a
detach-disk win7a vdb
c.网卡
virsh attach-interface --domain win7a --type bridge --source br-ex --model virtio --config
virsh domiflist win7a
6.(在线)调整
a.cpu
setvcpus win7a 2 --config (--live #centos7支持在线增加不支持减少,centos6不支持在线增加和减少)
#如果定义了cpu topology 就要重定义一下,如果windows系统vcpu超过2就要设定topology,因为win7最多sockets=2,而KVM如果不定义topology默认每vcpu每socket
<cpu> <topology sockets='1' cores='2' threads='1'/> </cpu>
b.内存
virsh setmem win7a 4G --config --live
6.虚拟机克隆
virt-clone -o centos1 -n centos3 -f /data/vms/centos3.disk
修改主机名/etc/sysconfig/network
修改/etc/udev/rules.d/70-persistent-net.rules 文件
修改网卡mac和ip
7.虚拟机迁移
1.复制磁盘文件 cp centos1.disk centos4.disk
2.复制xml文件 cp /etc/libvirt/qemu/centos1.xml centos4.xml
3.修改xml文件后define 虚拟机,然后start
4.修改host,mac,net.rules,ip
8.spice拖拽和文字复制
客户机
要安装 spice-guest-tools
下载地址
https://www.spice-space.org/download/windows/spice-guest-tools/spice-guest-tools-latest.exe
服务端:
channel & virtio-serial
<channel type='spicevmc'> <target type='virtio' name='com.redhat.spice.0'/> <address type='virtio-serial' controller='0' bus='0' port='1'/> </channel> </controller> <controller type='virtio-serial' index='0'> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </controller>
优化
一.cpu
1.查看cpu topology
virsh capabilities
2.将vcpu绑定到物理cpu的同一node上禁止跨node使用内存(先virsh freecell --all看每node内存情况,被绑定的node上的剩余内存要大余虚拟机内存)
a.一对多
<vcpu placement='static' cpuset='0,2,4,6,8,10,12,14,16,18,20,22'>4</vcpu>
b.一对一
virsh vcpupin win7a 0 23 virsh vcpupin win7a 1 11 virsh vcpupin win7a 2 1 virsh vcpupin win7a 3 13 virsh vcpupin win7a 0 22 virsh vcpupin win7a 1 10 virsh vcpupin win7a 2 0 virsh vcpupin win7a 3 12
3.taskset
yum -y install util-linux-ng taskset -cp 1,3,5,7,9,11,23 `ps aux | grep 'win7a' | awk -F' ' 'NR==1{ print $2}'`
#测试会跟vcpupin冲突
#进程和cpu绑定
二.内存
ln -s /usr/local/qemu/bin/qemu-system-x86_64 /usr/bin/qemu-kvm
ln -s /usr/local/qemu/bin/qemu-img /usr/bin/qemu-img
相关好的文章:
http://blog.csdn.net/beginning1126/article/details/41983547 (综合)
http://blog.csdn.net/u012398613/article/details/51773474 (sysbench)
http://mt.sohu.com/20170509/n492410429.shtml (磁盘IO)