kvm学习
1. 查看cpu是否支持kvm
[root@sorl1 ~]$ grep -E 'vmx|svm' /proc/cpuinfo
2. 查看yum kvm
[root@sorl1 ~]# yum list | grep kvm libvirt-daemon-kvm.x86_64 1.2.17-13.el7 @anaconda pcp-pmda-kvm.x86_64 3.10.6-2.el7 @anaconda qemu-kvm.x86_64 10:1.5.3-105.el7 @anaconda qemu-kvm-common.x86_64 10:1.5.3-105.el7 @anaconda libvirt-daemon-kvm.x86_64 4.5.0-36.el7_9.5 updates oci-kvm-hook.x86_64 0.3-1.el7 epel qemu-kvm.x86_64 10:1.5.3-175.el7_9.5 updates qemu-kvm-common.x86_64 10:1.5.3-175.el7_9.5 updates qemu-kvm-tools.x86_64 10:1.5.3-175.el7_9.5 updates
3. 安装kvm
[root@sorl1 ~]# yum -y install qemu-kvm qemu-kvm-tools libvirt
4. 启动libvirtd
[root@sorl1 ~]# systemctl start libvirtd
5.下载tightvnc工具
https://www.tightvnc.com/download/2.8.63/tightvnc-2.8.63-gpl-setup-64bit.msi
6. 创建虚拟机硬盘
[root@sorl1 kvm]# qemu-img create -f raw /opt/kvm/CentOS-7.qcow2 10G
7. 安装virt-install
yum -y install virt-install libvirt-python
8.创建
virt-install --virt-type kvm --name kvm-test --ram 10240 --vcpus 1 --cdrom=/opt/kvm/CentOS-7-x86_64-Minimal-2009.iso --disk /opt/kvm/CentOS-7.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=rhel7
装报错2:
Could not open '/opt/kvm/CentOS-7.qcow2': Permission denied
解决办法:qemu.conf中将user和group前面的#去掉,让root用户可以操作,修改后重启libvirtd服务;
[root@sorl1 kvm]# grep -v "#" /etc/libvirt/qemu.conf | sed '/^$/d' vnc_listen = "0.0.0.0" user = "qemu" group = "qemu"
chown -R qemu.qemu /opt/kvm
systemctl restart libvirtd
9. 使用vpn 连接到虚拟机中
实现 kvm 虚拟机
[root@sorl1 ~]# virsh list --all
Id 名称 状态
----------------------------------------------------
1 kvm-test 关闭
启动虚拟机
[root@sorl1 ~]# virsh start kvm-test
再次vnc链接
libevirt
导出kvm xml
virsh dumpxml kvm-test > kvm-test.xml
删除kvm虚拟机
virsh shutdown kvm-test
virsh undefine kvm-test
恢复虚拟机
virsh define kvm-test.xml
virsh list --all
挂起虚拟机
virsh suspend kvm-test
恢复
virsh resume kvm-test
更改kvm cpu个数
编辑kvm-test
virsh edit kvm-test
<vcpu placement='auto' current='1'>6</vcpus>
virsh setvcpus kvm-test 2 --live
动态调整内存
#这是内存1024 virsh qemu-monitor-command kvm-test --hmp --cmd balloon 1024
virsh qemu-monitor-command kvm-test --hmp --cmd info balloon
qcow2 基本原理
qcow2 镜像格式是 QEMU 模拟器支持的一种磁盘镜像, 它也是可以用一个文件的形式来表示一块固定大小的块设备磁盘
Raw 格式与 qcow2格式转发
qemu-img convert -f raw -O qcow2 test.raw test.raw.qcow2
查看qcow2文件
qemu-img info test.qcow2
kvm网络管理
[root@sorl1 ~]# brctl show bridge name bridge id STP enabled interfaces virbr0 8000.5254003a1fb9 yes virbr0-nic vnet0
创建桥接网卡
[root@sorl1 ~]# brctl addbr br0
[root@sorl1 ~]# brctl show
bridgename bridge id STP enabled interfaces
br0 8000.000000000000 no
virbr0 8000.5254003a1fb9 yes virbr0-nic
vnet0
把eth0 桥接到br0 上
[root@sorl1 ~]# brctl addif br0 eth0
[root@sorl1 ~]# brctl show
bridgename bridge id STP enabled interfaces
br0 8000.000000000000 no eth0
virbr0 8000.5254003a1fb9 yes virbr0-nic
vnet0
把eth0 IP地址删除, 把IP地址配置到 br0上
ip addr del 10.20.1.111/24 dev eth0
ip addr add 10.20.1.111/24 dev br0
route add default gw 10.20.1.254
编辑 kvm虚拟机,修改xml
virsh edit kvm-test <interface type='network'> <mac address='11:33:54:77:cc:66' /> <source network='default' /> <model type='virtio' /> <address type='pci' domain='0x0000' bus='0x00...' /> </interface> 替换成 <interface type='bridge'> <mac address='11:33:54:77:cc:66' /> <source bridge='br0' /> <model type='virtio' /> <address type='pci' domain='0x0000' bus='0x00...' /> </interface>