kvm

Kvm日常管理

Posted by ZhangShun Blog on August 31, 2018

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
创建新镜像:qemu-img create -f qcow2 /var/lib/libvirt/images/$1.img 100G  

创建新镜像:qemu-img create -f qcow2 -o preallocation=metadata /var/lib/libvirt/images/$1.img 200G (真实大小)

创建虚拟机:virt-install --name $1 --ram 4096 --vcpus=2 --disk path=/var/lib/libvirt/images/$1.img,size=100 --accelerate --cdrom /data/iso/ubuntu-14.04.5-server-amd64.iso --graphics vnc,listen=0.0.0.0

基于老镜像创建镜像:qemu-img create -f qcow2 -b /var/lib/libvirt/images/old.img /var/lib/libvirt/images/new.img

启动虚拟机:virsh start vmname

关闭虚拟机:virsh shutdown vmname

强制关闭虚拟机:virsh destroy vmname

删除虚拟机:virsh undefine vmname

查看虚拟机网卡:virsh domiflist vmname

添加网卡:virsh attach-interface vmname --type bridge --source br1 --config

删除网卡:virsh detach-interface vmname --type bridge --mac 52:54:00:14:86:cf

克隆虚拟机:virt-clone -o old_vmname -n new_vmname -f /var/lib/libvirt/images/new_vmname.img

创建快照:virsh snapshot-create-as vmname snapshot_name

查看虚拟机快照:virsh snapshot-list vmname

回滚快照:virsh snapshot-revert --domain vmname snapshot_name

删除快照:virsh snapshot-delete vmname snapshot_name

根据配置文件起虚拟机: virsh define /etc/libvirt/qemu/INIT-CASSANDRA-01.xml

virsh edit vmname    动态修改kvm配置文件

virsh setvcpus $vmname 4 –live –config 热添加cpu到4核

virsh setmem $vmname 4194304 –config –live 热添加内存到4G

硬盘

全镜像模式 raw 不支持快照

稀疏模式 qcow2 支持快照

通过宿主机iptables forward限制kvm

echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables

echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables

kvm优化

cpu:将虚拟机指定到特定的cpu上运行,减少cache miss

方法:1.编写xml,<vcpu placement='static' cpuset='1,3,5,7'>4</vcpu>

2.taskset -cp 1,2 $pid 将虚拟机进程绑到cpu1,2上

内存(不需要设置):EPT——寻址

KSM 内存合并

大页内存

I/O:virtio virtio-net virtio-blk 半虚拟化

添加磁盘

方法一:virsh attach-disk $kvmname $镜像位置 vda –subdriver qcow2 –config –live

方法二:

1,创建磁盘:

qemu-img create -f qcow2 /data/vm/huge.img 500G

2,编写一个xml文件(disk.xml):

1
2
3
4
5
6
7
8
9
<disk type='file' device='disk'>

  <driver name='qemu' type='qcow2' cache='none'/>

  <source file='/data/vm/huge.img'/>

  <target dev='vdb' bus='virtio'/>

</disk>

3,添加磁盘:

virsh attach-device –persistent vm-name disk.xml

在kvm中做DNAT不生效

在虚拟机中做DNAT不生效,如果网卡是bridge模式,需要设置

echo 0 > /proc/sys/net/bridge/bridge-nf-call-iptables

echo 0 > /proc/sys/net/bridge/bridge-nf-call-ip6tables

echo 0 > /proc/sys/net/bridge/bridge-nf-call-arptables

虚拟机网卡配置

ubuntu网卡配置:

root@ubuntu:/etc# cat /etc/network/interfaces

source /etc/network/interfaces.d/*

auto lo

iface lo inet loopback

auto ens3

iface ens3 inet static

address 192.168.0.90

netmask 255.255.255.0

gateway 192.168.0.254

dns-nameservers 192.168.0.25

dns-search redmine

auto ens6

iface ens6 inet static

address 10.0.18.90

netmask 255.255.252.0

#gateway 10.0.16.1

network 10.0.16.0

broadcast 10.0.19.255

centos网卡配置

[root@kvm3 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 NAME=eth0

DEVICE=eth0

ONBOOT=yes

IPADDR=192.168.0.53

NETMASK=255.255.255.0

rc.local添加命令

route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.0.16.1

报错

报错

是因为配置文件中的os部分系统信息不正确导致,可以先virsh-install一台虚拟机,将os部分拷贝即可

vmname.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
1 <domain type='kvm'>          ##描述hypervisor
2   <name>centos7.0</name>        ##定义虚拟机整体信息
3   <uuid>c2d264d3-5c61-4d2e-8678-b28673c1f64b</uuid>
4   <memory unit='KiB'>1048576</memory>
5   <currentMemory unit='KiB'>1048576</currentMemory>
6   <vcpu placement='static'>1</vcpu>
7   <os>                  ##系统信息
8     <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
9     <boot dev='hd'/>      ##开机从哪里启动
10   </os>
11   <features>          ##硬件资源特性
12     <acpi/>
13     <apic/>
14   </features>
15   <cpu mode='custom' match='exact'>
16     <model fallback='allow'>Haswell-noTSX</model>
17   </cpu>
18   <clock offset='utc'>
19     <timer name='rtc' tickpolicy='catchup'/>
20     <timer name='pit' tickpolicy='delay'/>
21     <timer name='hpet' present='no'/>
22   </clock>
23   <on_poweroff>destroy</on_poweroff>      ##突发事件处理
24   <on_reboot>restart</on_reboot>
25     <suspend-to-mem enabled='no'/>
26     <suspend-to-disk enabled='no'/>
27   </pm>
28   <devices>                ##外设资源
29     <emulator>/usr/libexec/qemu-kvm</emulator>
30     <disk type='file' device='disk'>    ##描述虚拟磁盘image
31       <driver name='qemu' type='qcow2'/>
32       <source file='/var/lib/libvirt/images/centos7.0.qcow2'/>
33       <target dev='vda' bus='virtio'/>
34       <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
35     </disk>
36     <controller type='usb' index='0' model='ich9-ehci1'>
37       <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x7'/>
38     </controller>
39     <controller type='usb' index='0' model='ich9-uhci1'>
40       <master startport='0'/>
41     </controller>
42     <controller type='usb' index='0' model='ich9-uhci2'>
43       <master startport='2'/>
44       <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x1'/>
45     </controller>
46     <controller type='usb' index='0' model='ich9-uhci3'>
47       <master startport='4'/>
48       <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x2'/>
49     <controller type='virtio-serial' index='0'>
50       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
51     </controller>
52     <interface type='bridge'>    ##虚拟网络,基于网桥
53       <mac address='52:54:00:6a:1e:54'/>
54       <source bridge='br0'/>
55       <model type='virtio'/>
56       <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
57     </interface>
58     <serial type='pty'>      ##串口信息可以不用更改
59       <target port='0'/>
60     </serial>
61     <console type='pty'>
62       <target type='serial' port='0'/>
63     </console>
64     <channel type='unix'>
65       <target type='virtio' name='org.qemu.guest_agent.0'/>
66       <address type='virtio-serial' controller='0' bus='0' port='1'/>
67     </channel>
68     <channel type='spicevmc'>
69       <target type='virtio' name='com.redhat.spice.0'/>
70       <address type='virtio-serial' controller='0' bus='0' port='2'/>
71     </channel>
72     <input type='tablet' bus='usb'/>
73     <input type='mouse' bus='ps2'/>
74     <input type='keyboard' bus='ps2'/>
75     <graphics type='vnc' port='-1' autoport='yes' keymap='en-us'/>
76     <sound model='ich6'>      ##从此往下的内容可以不用更改
77       <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
78     </sound>
79     <video>
80       <model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1'/>
81       <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
82     </video>
83     <redirdev bus='usb' type='spicevmc'>
84     </redirdev>
85     <redirdev bus='usb' type='spicevmc'>
86     </redirdev>
87     <memballoon model='virtio'>
88       <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
89     </memballoon>
90   </devices>
91 </domain>