实践KVM on Deepin

环境

主机:Deepin 15.11 64位

guest:CentOS 7.6.1810 64位

前置条件

检查cpu是否支持虚拟化

1
2
3
4
5
6
# intel vmx,amd svm
$ egrep '(vmx|svm)' /proc/cpuinfo
...vmx...

$ lscpu | grep Virtualization
Virtualization:        VT-x

检查kvm模块是否已加载

1
2
3
4
$ lsmod | grep -i kvm
kvm_intel             183621  31 
kvm                   586948  1 kvm_intel
irqbypass              13503  21 kvm

安装KVM

1
$ sudo apt install virt-manager bridge-utils libvirt-clients qemu qemu-kvm

启动服务

1
$ sudo systemctl status libvirtd

配置网络

virbr0

默认会生成一个虚拟网卡virbr0

1
2
3
4
5
6
7
virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:e2:ad:e1  txqueuelen 1000  (Ethernet)
        RX packets 200  bytes 6552 (6.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 103  bytes 17430 (17.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

控制virbr0状态

kvm-deepin-connection-details

创建网桥

默认模式下是作为NAT模式,如果需要使用网桥模式,则需要配置,参考如下:

/etc/sysconfig/network-scripts/下新建ifcfg-virbr0

1
2
3
4
5
6
7
8
9
TYPE=Bridge
BOOTPROTO=static
DEVICE=virbr0
ONBOOT=yes
IPADDR=192.168.1.172
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=210.22.84.3
DELAY=0

修改网络配置(比如:ifcfg-em1),注释5行,最后添加1行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=em1
UUID=c509254c-88e3-4718-8e51-e38df2424569
DEVICE=em1
ONBOOT=yes
#IPADDR=192.168.1.172
#DNS1=210.22.84.3
#NETMASK=255.255.255.0
#PREFIX=24
#GATEWAY=192.168.1.1
IPV6_PRIVACY=no
BRIDGE=virbr0

安装guest

启动virt-manager,利用X11会得到界面

1
2
3
4
$ sudo virt-manager

# 当需要连接远程的kvm时
$ sudo virt-manager --no-fork

网路配置

使用NAT网络模式,采用dhcp获取ip,也可以指定ip(static)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0
UUID=5609329a-0c31-4f41-9990-21627f6451bb
DEVICE=eth0
ONBOOT=yes
#IPADDR=192.168.122.200
#NETMASK=255.255.255.0
DNS1=210.22.84.3
#GATEWAY=192.168.122.1
ZONE=public

使用网桥模式,网桥名称virbr0,指定ip(static)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0
UUID=5609329a-0c31-4f41-9990-21627f6451bb
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.200
NETMASK=255.255.255.0
DNS1=210.22.84.3
GATEWAY=192.168.1.1
ZONE=public

new image

clone image

kvm-clone-1

kvm-clone-2

import image

kvm-import-1

kvm-import-2

kvm-import-3

kvm-import-4

kvm-import-5

使用NAT模式

kvm-import-6

使用网桥模式,填写网桥名称

kvm-import-6

Q&A

CPU mode “custom” not supported

Unable to complete install: ‘unsupported configuration: CPU mode ‘custom’ for x86_64 kvm domain on x86_64 host is not supported by hypervisor’

修改/etc/libvirt/qemu.conf,配置user和group,例如:

1
2
user = "ray"
group = "ray"

重启后解决。