前段时间在本地vmware环境中安装kvm,打算测试kvm的IO性能,安装kvm过程中还是遇到不少问题,本文在此做下记录,以便给他人提供便利。
环境:
[[email protected] network-scripts]# uname -a
Linux bogon 3.10.0-327.18.2.el7.x86_64 #1 SMP Thu May 12 11:03:55 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[[email protected]]#rpm -q centos-release
centos-release-7-2.1511.el7.centos.2.10.x86_64
前置条件:
- 确认硬件的虚拟属性是否打开
支持的情况:> [[email protected] ~]# dmesg | grep kvm> [ 3707.357987] kvm: zapping shadow pages for mmio generation wraparound[ 3716.951304] kvm [4081]: vcpu0 unhandled rdmsr: 0x1ad
不支持的情况:
(1)[ 4790.548000] kvm: disabled by bios
or cpu不支持:
(2)kvm: no hardware support
如果出现情况(1),重启进bios开启virtualization选项
还可以使用grep vmx /proc/cpuinfo 查看CPU对虚拟技术的支持,对于vmware虚拟机而言,cpu要支持vmx,需要开启下面选项:
- 安装相关服务
yum install kvm libvirt python-virtinst qemu-kvm virt-viewer bridge-utils virt-manager libvirt-python virt-install
service libvirtd start - 校验kvm模块是否已加载
[[email protected] network-scripts]# lsmod | grep kvm
kvm_intel 162153 10kvm 525409 1 kvm_intel
开始安装
- 建立网桥,并添加对应的口:
在/etc/sysconfig/network-scripts目录下增加ifcfg-br0:> DEVICE=br0TYPE=Bridge
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPADDR=10.24.67.95
NETMASK=255.255.0.0
GATEWAY=10.24.255.254
DNS1=192.168.1.1
NAME=br0
UUID=69cf4ec9-6f6e-4bfe-9777-f85f77376a79
ONBOOT=yes
同时把物理口的IP去掉.
完成配置后,重启网络服务:
service network resart
将物理口添加到br口中:
brctl addif br0 eno16777736
完成后应该是这样的:
[[email protected] ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c294acdab no eno16777736
vnet0
virbr0 8000.000000000000 yes[[email protected] ~]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.24.67.95 netmask 255.255.0.0 broadcast 10.24.255.255—-ip添加到了br0口
inet6 fe80::20c:29ff:fe4a:cdab prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:4a:cd:ab txqueuelen 0 (Ethernet)
RX packets 63503 bytes 7695341 (7.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7474 bytes 11605872 (11.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0eno16777736:—没有IP了 flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:4a:cd:ab txqueuelen 1000 (Ethernet)
RX packets 619209 bytes 736728595 (702.5 MiB)
RX errors 0 dropped 511 overruns 0 frame 0
TX packets 154646 bytes 31473841 (30.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- 建立虚拟机
划分硬盘:qemu-img create -f qcow2 -o preallocation=metadata /home/kvm/guest.qcow2 100G
安装kvm bridge网络模式(在宿主机上启动服务,远程使用VNC工具连接配置):
> virt-install --name=test--ram 4096 --vcpus=8 -f /home/kvm/spark-1.qcow2--cdrom /home/kvm/CentOS-7.0-1406-x86_64-Minimal.iso --graphics vnc,listen=0.0.0.0,port=5920, --network bridge=br0
如果提示下面的错误:
> starting install...
ERROR Cannot access storage file ‘/root/work/kvm/data/guest.qcow2’ (as uid:107, gid:107): Permission denied
Domain installation does not appear to have been successful.
可修改/etc/libvirt/qemu.conf,然后重启libvirtd服务:
> user = "root"
group = “root”
此时在客户端使用vncviewer.exe即可连上做相关安装配置:
如果连接不上,一般是防火墙的配置问题。
启动domain
使用virsh list –all查看已安装的kvm
使用virsh start test启动虚拟机
使用virsh destory test 关闭虚拟机
使用vrish undefine test 删除虚拟机
KVM的配置文件存储在/etc/libvirt/qemu/*
使用virsh define test.xml使配置生效
kvm克隆
假定已存在spark1,新克隆spark-4:
需要指定存放spark-4的硬盘名称及位置
virt-clone -o spark-1 -n spark-4 -f /home/kvm/spark-4.qcow2
修改配置文件:
virsh edit spark-4 (不要使用vi spark-4.xml,不能立即生效)
[[email protected] kvm]# cat /etc/libvirt/qemu/spark-4.xml | grep vnc
<graphics type=’vnc’ port=’5928’ autoport=’no’ listen=’0.0.0.0’>
[[email protected] kvm]# cat /etc/libvirt/qemu/spark-4.xml | grep spark-4
<source mode=’bind’ path=’/var/lib/libvirt/qemu/channel/target/domain-spark-4/org.qemu.guest_agent.0’/>
开启即可:
virsh start spark-4
总结
通过上述方式就可以自由创建kvm了,
原文地址:https://www.cnblogs.com/chinatrump/p/11584642.html