Debian 9 安装 KVM 虚拟机
KVM
是 Linux x86_64 环境下的全虚拟化方案,面向支持 Intel VT
和 AMD-V
虚拟化技术的硬件。
检查虚拟化支持
# egrep '(vmx|svm)' --color=always /proc/cpuinfo
flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave rdrand lahf_lm abm 3dnowprefetch ida arat epb xsaveopt pln pts dtherm invpcid_single tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust erms invpcid rdseed smap clflushopt
有 flags
输出代表支持虚拟化,可以安装 KVM。
安装
最小化安装只需要 QEMU 和 KVM,但通常用户会需要 libvirt-daemon
提供守护进程,需要 virt-manager
提供 GUI 管理界面。
# apt install qemu-kvm virtinst
virtinst
用于命令行下管理 KVM 虚拟机
加入管理用户组
为了让普通用户能够管理 KVM 虚拟机,需要将用户加入到相关的用户组。
# adduser <youruser> libvirt
# adduser <youruser> libvirt-qemu
列出虚拟机
# virsh list --all
非 root 用户使用 qemu://session
链接访问 KVM:
$ virsh --connect qemu:///system list --all
You can use
LIBVIRT_DEFAULT_URI
to change this.
创建虚拟主机
# virt-install --virt-type kvm --name squeeze-amd64 \
--memory 512 --cdrom ~/iso/debian_live_6.0.10_amd64.iso \
--disk size=4 --os-variant debiansqueeze
使用 --location
参数从远程服务器读取镜像:
# virt-install --virt-type kvm --name squeeze-amd64 \
--location http://httpredir.debian.org/debian/dists/squeeze/main/installer-amd64/ \
--extra-args "console=ttyS0" -v --os-variant debiansqueeze \
--disk size=4 --memory 512
常用管理命令
启动虚拟机
# virsh start VMGUEST
关闭虚拟机
# virsh shutdown VMGUEST
销毁虚拟机
# virsh destroy VMGUEST && virsh undefine VMGUEST
设置桥接网络
虚拟机之间通信
QEMU 默认采用 VEPA 模式下的 macvtap 提供与其他虚拟机的 NAT 或桥接网络访问,但在这种模式下,主机无法与虚拟机通信。
主机与客户机通信
可以在虚拟网卡上设置一个 macvlan
模式的网桥,配置完成后即可将桥接模式的 dummy0 (macvtap) 虚拟网卡用在虚拟机中。
# modprobe dummy
# ip link add dummy0 type dummy
# ip link add link dummy0 macvlan0 type macvlan mode bridge
# ifconfig dummy0 up
# ifconfig macvlan0 192.168.1.2 broadcast 192.168.1.255 netmask 255.255.255.0 up
全局通信
让主机、虚拟机和互联网之间实现通信,需要配置桥接网络。
安装桥接配置工具
使用 brctl
管理桥接网卡,该工具包含在 bridge-utils
软件包中。
$ sudo apt install bridge-utils
创建桥接网卡
# brctl addbr br0
查看当前网卡信息:
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 00:e0:4c:68:42:f7 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:e0:4c:68:42:f8 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.49/24 brd 192.168.1.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::2e0:4cff:fe68:42f8/64 scope link
valid_lft forever preferred_lft forever
4: br0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default
link/ether 42:b8:7f:97:60:e4 brd ff:ff:ff:ff:ff:ff
建立桥接
建立桥接网卡 br0
与主机上两块物理网卡 eth0
和 eth1
之间的网桥。
# brctl addif br0 eth0 eth1
网桥信息配置
修改配置文件 /etc/network/interfaces
,配置物理网卡 eth0
到网卡 br0
之间的网桥。
配置 DHCP 获取 IP
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# Set up interfaces manually, avoiding conflicts with, e.g., network manager
iface eth0 inet manual
iface eth1 inet manual
# Bridge setup
iface br0 inet dhcp
bridge_ports eth0 eth1
配置静态 IP
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo br0
iface lo inet loopback
# Set up interfaces manually, avoiding conflicts with, e.g., network manager
iface eth0 inet manual
iface eth1 inet manual
# Bridge setup
iface br0 inet static
bridge_ports eth0 eth1
address 192.168.1.2
broadcast 192.168.1.255
netmask 255.255.255.0
gateway 192.168.1.1
启用桥接网卡
# ifup br0