1 查看系统是否支持虚拟化
cat /proc/cpuinfo | grep -E 'vmx|svm'
1.1 原生态系统
需要开启bios系统中的支持虚拟化选项
1.2 kvm虚拟化系统
在加载kvm_intel模块时,添加nested=1参数
1.3 vmware虚拟机系统
则在虚拟机cpu参数设置中选择vt-x
2 安装虚拟机
三种安装方式,光盘、iso、u盘(以iso为例)
2.1 新建img文件
法一,直接dd创建原始文件
法二,使用qemu-img 创建qcow2、raw等文件
[root@localhost img]# qemu-img create -f qcow2 centos7.img 20G
Formatting 'centos7.img', fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 lazy_refcounts
注意点:
dd创建的文件将按创建文件大小占用磁盘空间,而qemu-img创建的文件所占空间随着IO的读写而逐渐增加。
qcow2相关raw模式而言,qcow2文件中有一张位图表,记录数据占用情况。这样qcow2格式的文件则可创建快照,可以记录文件的变化量。如果需要对raw镜像文件创建快照,则需要使用qemu-img工具进行转换,详请见qemu-img的用法分析。
2.2 安装虚拟机
[root@localhost img]# qemu-kvm -smp 1 -m 1024 ./centos7.img -cdrom /home/CentOS-7-x86_64-DVD-1503-01.iso -usb -usbdevice tablet --vnc :09 --monitor stdio
QEMU 1.5.3 monitor - type 'help' for more information
(qemu) info kvm
kvm support: enabled
-m 内存分配
-smp cpu个数
img文件 vm镜像文件
-cdrom 使用iso安装
-usb -usbdevice tablet 防止鼠标偏移
--vnc :09 vnc端口号
--monitor stdio monitor重定向到当前命令行中
默认支持虚拟化
2.3 启动虚拟机
qemu-kvm -smp 1 -m 512 ./centos7_1-bakup.img -drive file=/img/lv1,if=virtio -usb -usbdevice tablet --vnc :09 --monitor telnet:192.168.1.244:8787,server,nowait
启动虚拟机和安装虚拟机不同之处在于不需要将iso文件加载到命令行中。如果添加iso文件,则iso文件以光驱的方式在虚拟机系统只读显示。
其他参数详见qemu-kvm参数分析
3 快照
两种创建快照的方式
3.1 monitor建快照
monitor使用方式见14节
(qemu) savevm snap1
(qemu) info snapshots
ID TAG VM SIZE DATE VM CLOCK
1 snap1 226M 2015-06-12 14:46:11 24:36:07.374
3.2 qemu-img建快照
见4.3节
4 qemu-img的用法
4.1 查询镜像信息
qemu-img info *.img 检查镜像的具体信息,包括镜像的类型、实际大小、虚拟大小、内部快照等信息
[root@localhost img]# qemu-img info centos7.img
image: centos7.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.2G
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 snap1 226M 2015-06-12 14:46:11 24:36:07.374
4.2 转换镜像格式
qemu-img convert 转换镜像的格式,比如说从raw格式转为qcow2格式
[root@localhost img]# qemu-img info centos7_2.img
image: centos7_2.img
file format: raw
virtual size: 20G (21474836480 bytes)
disk size: 0
[root@localhost img]# qemu-img convert -O qcow2 centos7_2.img centos7_3.img
[root@localhost img]# qemu-img info centos7_3.img
image: centos7_3.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
4.3 管理镜像快照
qemu-img snapshot 创建、使用、删除、列出快照
[root@localhost img]# qemu-img snapshot -c snap2 centos7.img
[root@localhost img]# qemu-img info centos7.img
image: centos7.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.2G
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 snap1 226M 2015-06-12 14:46:11 24:36:07.374
2 snap2 0 2015-06-12 15:15:29 00:00:00.000
4.4 后端镜像
4.4.1 创建后端镜像
[root@localhost img]# qemu-img create -f qcow2 -b centos7_1.img centos7_1-bakup.img
Formatting 'centos7_1-bakup.img', fmt=qcow2 size=21474836480 backing_file='centos7_1.img' encryption=off cluster_size=65536 lazy_refcounts=off
[root@localhost img]# qemu-img info centos7_1-bakup.img
image: centos7_1-bakup.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 196K
cluster_size: 65536
backing file: centos7_1.img
Format specific information:
compat: 1.1
lazy refcounts: false
4.4.2 改变后端镜像文件
qemu-img rebase
改变后端镜像文件,启动后端镜像文件,则以后的写文件操作,将写在新文件中,以前的文件变为只读。
qemu-img create -f qcow2 -b centos63-develop.img centos63-develop-backup.img
4.5 改变镜像大小
qemu-img resize 改变镜像的大小, 缩小空间时,如果空闲空间不足,则会丢失数据。
[root@localhost img]# qemu-img info centos7_3.img
image: centos7_3.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
[root@localhost img]# qemu-img resize centos7_3.img +2G
Image resized.
[root@localhost img]# qemu-img info centos7_3.img
image: centos7_3.img
file format: qcow2
virtual size: 22G (23622320128 bytes)
disk size: 260K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
5 网络配置
网桥
详见9.2节
NAT (略)
qemu内置的网络模式(略)
直接分配网络设备
详见10节
6 VNC
monitor使用方式祥见14节
VNC端口设置
在启动命令行中添加 --vnc :09 参数
VNC端口修改
change vnc localhost:2
VNC密码修改
change vnc password
7 CPU配置和内存配置
-smp n[,maxcpus=cpus][,cores=cores][,threads=threads][,sockets=sockets]
set the number of CPUs to 'n' [default=1]
maxcpus= maximum number of total cpus, including
offline CPUs for hotplug, etc
cores= number of CPU cores on one socket
threads= number of threads on one CPU core
sockets= number of discrete sockets in the system
n 为cpu总数
maxcpus 为cpu的最大数
cores 为cpu的核数
threads 为每个cpu核上的线程数
sockets 为客户机所看到的cpu socket数
e.g.
a. -smp 8 表示8个逻辑cpu
b. -smp 8,sockets=2,cores=2,threads=2
逻辑cpu数为8,2个CPU socket, 每个socket上有2个core