|
一、KVM定义
基于内核的虚拟机(英语:Kernel-based Virtual Machine,简称KVM),是一种用于Linux内核中的虚拟化基础设施。KVM目前支持Intel VT及AMD-V的原生虚拟技术。
- 是x86架构且硬件支持虚拟化技术(如 intel VT 或 AMD-V)的Linux全虚拟化解决方案。
- 它包含一个为处理器提供底层虚拟化 可加载的核心模块kvm.ko(kvm-intel.ko或kvm-AMD.ko)。
- KVM还需要一个经过修改的QEMU软件(qemu-kvm),作为虚拟机上层控制和界面。
- 在主流的Linux内核,如2.6.20以上的内核均已包含了KVM核心。
- KVM能在不改变linux或windows镜像的情况下同时运行多个虚拟机,(它的意思是多个虚拟机使用同一镜像)并为每一个虚拟机配置个性化硬件环境(网卡、磁盘、图形适配器……)。
可以对kvm进行控制管理的组件主要有两个:
QEMU-KVM:
在 Linux 系统中,首先我们可以用 modprobe 系统工具去加载 KVM 模块,如果用 RPM 安装 KVM 软件包,系统会在启动时自动加载模块。加载了模块后,才能进一步通过其他工具创建虚拟机。但仅有 KVM 模块是远远不够的,因为用户无法直接控制内核模块去做事情,还必须有一个用户空间的工具。关于用户空间的工具,KVM 的开发者选择了已经成型的开源虚拟化软件 QEMU。QEMU 是一个强大的虚拟化软件,它可以虚拟不同的 CPU 构架。比如说在 x86 的 CPU 上虚拟一个 Power 的 CPU,并利用它编译出可运行在 Power 上的程序。KVM 使用了 QEMU 的基于 x86 的部分,并稍加改造,形成可控制 KVM 内核模块的用户空间工具 QEMU-KVM。所以 Linux 发行版中分为 kernel 部分的 KVM 内核模块和 QEMU-KVM 工具。这就是 KVM 和 QEMU 的关系。
Libvirt/Virsh/Virt-manager:
尽管 QEMU-KVM 工具可以创建和管理 KVM 虚拟机,RedHat 为 KVM 开发了更通用的辅助工具libvirt。Libvirt 是一套提供了多种语言接口的 API,为各种虚拟化工具提供一套方便、可靠的编程接口,不仅支持 KVM,而且支持 Xen 等其他虚拟机。使用 libvirt,你只需要通过 libvirt 提供的函数连接到 KVM 或 Xen 宿主机,便可以用同样的命令控制不同的虚拟机了。Libvirt 不仅提供了 API,还自带一套基于文本的管理虚拟机的命令 virsh,你可以通过使用 virsh 命令来使用 libvirt 的全部功能。同时还能使用图形界面进行管理操作,其工具是 Virt-manager。他是一套用 python 编写的虚拟机管理图形界面,用户可以通过它直观地操作不同的虚拟机。Virt-manager 就是利用 libvirt 的 API 实现的。
二、安装软件
1、需要先查看主机是否支持虚拟化
1
2
3
4
5
6
7
8
9
| # grep -Ei --color=auto "vmx|svm" /proc/cpuinfo
如果能grep到相关关键字就说明支持
可以手动装载模块
# modprobe kvm_intel
# modprobe kvm
# lsmod | grep kvm
kvm_intel 54285 0
kvm 333172 1 kvm_intel
装载完成后kernel就已经转换成hypervisor;可以使用kvm虚拟机
|
2、安装配置qemu-kvm
1
2
3
| # yum -y install qemu-kvm qemu-kvm-tools
由于安装完成后默认的目录不在PATH环境变量中;需要做个链接才能使用qemu虚拟机
# ln -sv /usr/libexec/qemu-kvm /usr/sbin/
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| # qemu-kvm -help
usage: qemu [options] [disk_image]
'disk_image' is a raw hard image image for IDE hard disk 0
Standard options:
-h or -help display this help and exit
-version display version information and exit
-M machine select emulated machine (-M ? for list)
-cpu cpu select CPU (-cpu ? for list)
......
# qemu-kvm -cpu ?
x86 Opteron_G5 AMD Opteron 63xx class CPU
x86 Opteron_G4 AMD Opteron 62xx class CPU
x86 Opteron_G3 AMD Opteron 23xx (Gen 3 Class Opteron)
x86 Opteron_G2 AMD Opteron 22xx (Gen 2 Class Opteron)
....
选项太多;这里不做一一解释;帮助信息说明也很详细;后续用到的做说明
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| 为了显示方便;需要安装vnc软件
# yum -y install tigervnc tigervnc-server
# mkdir /images/vm1 -pv 创建目录存储镜像文件
# ls rhel-server-6.4-i386-dvd.iso 本地光盘镜像文件
# qemu-img create -f qcow2 -o size=100G,preallocation=metadata /images/vm1/redhat6.qcow2
创建一个100G的文件
# qemu-kvm -name "redhat" -m 512 -smp 1 -drive file=/images/vm1/redhat6.qcow2,
> if=virtio,index=0,media=disk,format=qcow2
> -drive file=/root/rhel-server-6.4-i386-dvd.iso,media=cdrom,index=1
> -boot order=d
VNC server running on `::1:5900'
-m 512:指定内存
-smp 1:指定vcpu
-drive:指定磁盘
if=virtio,index=0,media=disk,format=qcow2 #磁盘参数;具体可以qemu-kvm -help查看
-boot order=d:指定引导次序;a、b表示软驱、c表示第一块硬盘,d表示第一个光驱设备,n-p表示网络适配器
VNC server running on `::1:5900':vnc server启动运行在5900端口
#另起一个ssh窗口连接本地连接vnc测试
# vncviewer :5900
|
测试可以正常启动安装。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| #需要网卡支持;所以需要写一个配置网卡接口的脚本;默认在/etc/qemu-ifup
# vim /etc/qemu-ifup
#!/bin/bash
#
switch=br0
if [ -n $1 ];then
ifconfig $1 up
sleep 1
brctl addif $switch $1
exit 0
else
echo "Error: No Specifed interface."
exit 1
fi
# chmod +x /etc/qemu-ifup
#脚本能自动调用命令中ifname=""网卡的名称
|
1
2
3
4
5
| # qemu-kvm -name "redhat" -m 512 -smp 1 -drive file=/images/vm1/redhat6.qcow2,if=virtio,index=0,media=disk,format=qcow2 -boot order=n -net nic,model=virtio -net tap,ifname=vnet0,downscript=no
VNC server running on `::1:5900'
# downscript是在关闭虚拟机是自动调用一个down的脚本;所以在此关闭就不会报错
# 参数与上述的光盘安装没有太多不同;唯一启动次序更改为网络启动n
# vncviewer :5900 登陆测试
|
测试pxe引导安装正常。
3、登陆测试
注意:在系统安装完成后再次启动时;还是一样的命令;指定各种属性。只是引导方式改变了。
1
2
3
4
5
6
7
8
9
10
11
| # qemu-kvm -name "rhel6" -m 512 -smp 2 -drive file=/images/vm1/rhel6.qcow2,media=disk,if=virtio,index=0,format=qcow2 -net nic,model=virtio -net tap,ifname=vnet0,downscript -vnc :1 --daemonize
# ss -tunlp | grep 5901
tcp LISTEN 0 1 *:5901 *:* users:(("qemu-kvm",6528,13))
测试在后台启动正常
# ifconfig vnet0 可以看到网卡已创建成功
vnet0 Link encap:Ethernet HWaddr 92:3A:BB:E4:2D:6A
# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c295e1e4f no eth0
vnet0
#查看已关联到桥br0
|
在windows客户端登陆vnc,测试可以正常登陆虚拟机。
关闭虚拟机;只需kill掉进程即可
1
2
3
| # netstat -tunlp | grep 5901
tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN 6528/qemu-kvm
# kill 6528
|
测试启动windows
1
2
| # qemu-kvm -name "winxp" -m 512 -smp 2 -drive file=/images/vm1/winxp.qcow2,if=virtio,media=disk,index=0,format=qcow2 -drive file=/root/winxp_ghost.iso,media=cdrom -boot order=d -vnc :1
#使用winxp的Ghost镜像镜像引导启动
|
为了演示,这里仅启动的是PE;并没有真正的安装。
4、虚拟机详细信息查看
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| # qemu-kvm -name "rhel6" -m 512 -smp 2 -drive file=/images/vm1/rhel6.qcow2,if=virtio,media=disk,index=0,format=qcow2 -net nic,model=virtio -net tap,ifname=vnet0,downscript=no -vnc none -monitor stdio
QEMU 0.12.1 monitor - type 'help' for more information
(qemu)
-vnc none:表示启动时不启动vnc
-monitor stdio:表示监控界面输出到标准输入输出上
(qemu) change vnc :1 #在监控界面下启动vnc
# ss -tunlp | grep 5901
tcp LISTEN 0 1 *:5901 *:* users:(("qemu-kvm",3215,16))
(qemu) info help #info信息能查看虚拟机很多状态
info version -- show the version of QEMU
info commands -- list QMP available commands
info network -- show the network state
info chardev -- show the character devices
info block -- show the block devices
info blockstats -- show block device statistics
info registers -- show the cpu registers
.....
(qemu) info cpus #显示虚拟cpu;可以看到cpu线程号
* CPU #0: pc=0xffffffff8103eacb (halted) thread_id=3246
CPU #1: pc=0xffffffff8103eacb (halted) thread_id=3247
|
三、qemu-img的使用
1
2
3
4
5
6
7
8
9
10
| # qemu-img --help
usage: qemu-img command [command options]
QEMU disk image utility
Command syntax:
check [-f fmt] [--output=ofmt] [-r [leaks | all]] filename
create [-f fmt] [-o options] filename [size]
commit [-f fmt] [-t cache] filename
convert [-c] [-p] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-S sparse_size] filename [filename2 [...]] output_filename
....
#可以看到qemu-img除了创建磁盘;可以检查、转换等...
|
1
2
3
4
5
6
7
8
9
| # qemu-img create -f qcow2 -o preallocation=metadata /images/vm2/test.qcow2 10G
# qemu-img resize /images/vm2/test.qcow2 +10G 增到10G
Image resized.
# qemu-img info /images/vm2/test.qcow2
image: /images/vm2/test.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.7M
cluster_size: 65536
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # qemu-img create -f vmdk -o ? test.vmdk #模拟查看相关的帮助信息
Supported options:
size Virtual disk size
adapter_type Virtual adapter type, can be one of ide (default), lsilogic, buslogic or legacyESX
backing_file File name of a base image
compat6 VMDK version 6 image
# qemu-img convert -O vmdk -o adapter_type=lsilogic /images/vm2/test.qcow2 /images/vm2/test.vmdk
转换成VMware的vmdk格式
# qemu-img info /images/vm2/test.vmdk
image: /images/vm2/test.vmdk
file format: vmdk
virtual size: 20G (21474836480 bytes)
disk size: 16K
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| # qemu-img snapshot -l /images/vm1/rhel6.qcow2 创建之前查看是否有快照;建议不要同名
# qemu-img snapshot -c rhel6-1.snap /images/vm1/rhel6.qcow2
# qemu-img snapshot -l /images/vm1/rhel6.qcow2
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 rhel6-1.snap 0 2014-05-25 10:51:34 00:00:00.000
Parameters to snapshot subcommand: #详细帮助
'snapshot' is the name of the snapshot to create, apply or delete
'-a' applies a snapshot (revert disk to saved state) 应用快照
'-c' creates a snapshot 创建快照
'-d' deletes a snapshot 删除快照
'-l' lists all snapshots in the given image 查看快照列表
|
四、实时迁移
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| node1:
# vim /etc/exports 启动nfs;共享镜像目录
/images/vm1 192.168.0.0/24(rw,no_root_squash)
# service nfs start
# exportfs -v
/images/vm1 192.168.0.0/24(rw,wdelay,no_root_squash,no_subtree_check)
-----------------------------------------------------------------------------------------
node2:
# mount -t nfs 192.168.0.111:/images/vm1/ /images/vm1/
# ls /images/vm1/
redhat6.qcow2 rhel6.qcow2
# qemu-kvm -name "rhel6" -m 512 -smp 2 -drive file=/images/vm1/rhel6.qcow2,if=virtio,media=disk,index=0 -boot order=c -net nic,model=virtio -net tap,ifname=vnet0,downscript=no -monitor stdio -incoming tcp:0:6767
启动一个虚拟机实例,这里要与node1启动配置相同,可以copy node1的命令
-incoming:等待别的虚拟机迁移过来;并非正真的启动虚拟机
tcp:tcp协议
0:表示运行所有主机往本机迁移
6767:监听在一个没有被占用的端口等待迁移
|
1
2
3
4
5
6
7
8
9
10
11
| node1:
# qemu-kvm -name "rhel6" -m 512 -smp 2 -drive file=/images/vm1/rhel6.qcow2,if=virtio,media=disk,index=0 -boot order=c -net nic,model=virtio -net tap,ifname=vnet0,downscript=no -nographic -monitor stdio
(qemu) migrate tcp:192.168.0.112:6767
node2:
QEMU 0.12.1 monitor - type 'help' for more information
(qemu) Unknown savevm section or instance 'kvm-tpr-opt' 0
load of migration failed
#报错;找了好久;没找到答案;貌似说是bug;具体也不太清楚
如有知道的大神;感谢解答
|
-------------------------------------------------------------------------------------------
五、Libvirt机器相关软件包的安装使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| # yum -y install libvirt virt-manager virt-viewer python-virtinst
# service libvirtd start 启动libvirt
# virsh 添加网桥
virsh # help iface-bridge
SYNOPSIS
iface-bridge
[--no-stp] [--delay ] [--no-start]
DESCRIPTION
bridge an existing network device
OPTIONS
[--interface] existing interface name
[--bridge] new bridge device name
--no-stp do not enable STP for this bridge
--delay number of seconds to squelch traffic on newly connected ports
--no-start don't start the bridge immediately
virsh # iface-bridge eth0 br0
Created bridge br0 with attached device eth0
Bridge interface br0 started
# ifconfig br0 创建成功
br0 Link encap:Ethernet HWaddr 00:0C:29:DF:70:B6
inet addr:192.168.0.112 Bcast:192.168.255.255 Mask:255.255.0.0
|
1
2
3
4
5
| # virt-install -n "centos6" --vcpus 2 -r 512 -l http://172.16.0.1/cobbler/ks_mirror/centos-6.5-x86_64 --disk path=/images/vm2/cents6.qcow2,bus=virtio,size=10,sparse --network bridge=br0,model=virtio --force
Starting install...
Retrieving file .treeinfo... | 676 B 00:00 ...
Retrieving file vmlinuz... | 7.9 MB 00:00 ...
.................安装正在进行中
|
1
2
3
4
5
6
| # virt-install --name "centos6" -r 512 --vcpus 2 --disk path=/images/vm2/centos.img,size=120 --network bridge=br0,model=virtio --pxe --force
WARNING KVM acceleration not available, using 'qemu'
Starting install...
Creating domain... | 0 B 00:00
Xlib: extension "RANDR" missing on display "localhost:13.0".
|
1
2
3
4
| # virsh list 查看虚拟机状态信息
Id Name State
----------------------------------------------------
7 centos6 running
|
帮助信息查看
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
| # virsh help 帮助信息很详细
Grouped commands:
Domain Management (help keyword 'domain'):
attach-device attach device from an XML file
attach-disk attach disk device
attach-interface attach network interface
autostart autostart a domain
blkdeviotune Set or query a block device I/O tuning parameters.
blkiotune Get or set blkio parameters
# virsh help monitor 可以仅查看关键字的信息
Domain Monitoring (help keyword 'monitor'):
domblkerror Show errors on block devices
domblkinfo domain block device size information
domblklist list all domain blocks
domblkstat get device block stats for a domain
domcontrol domain control interface state
domif-getlink get link state of a virtual interface
domiflist list all domain virtual interfaces
domifstat get network interface stats for a domain
dominfo domain information
dommemstat get memory statistics for a domain
domstate domain state
list
|
到此;基本的kvm配置以完成。
|
|