centos kvm 整理小结
这两天一直在折腾kvm,为了不影响公司业务都是等他们下班后才开干【20:00以后】,同时也走不不少弯路所以我在这里整理总结一下给需要的同学。
其实前面的文章都有分开写 只是不太集中 不够流程化所以这里重新整理。
1,安装kvm
前提是你已经安装好 centos系统 我用的还是centos6.5
yum install kvm libvirt python-virtinst qemu-kvm virt-viewer bridge-utils
2,启动守护进程libvirtd
/etc/init.d/libvirtd start
3,配置网桥
#新建网桥配置文件
vi /etc/sysconfig/network-scripts/ifcfg-br0DEVICE=br0
BOOTPROTO=static
BROADCAST=192.168.1.255 #广播地址
IPADDR=192.168.1.102 #服务器管理地址
NETMASK=255.255.255.0 #子网掩码
NETWORK=192.168.1.0 #网络地址
ONBOOT=yes
NETWORKING_IPV6=no
GATEWAY=192.168.1.1 #网关地址
TYPE=Bridge
#修改本地物理网卡配置文件
vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=38:2C:4A:C6:D6:59
#TYPE=Ethernet
#UUID=3ff6bca6-2502-48d6-9182-56c3851c696d
ONBOOT=yes
#NM_CONTROLLED=yes
#BOOTPROTO=dhcp
BRIDGE=br0 #这里是新增的 上面的其他都注释掉只保存4个
#配置dns
vi /etc/resolv.conf
nameserver 192.168.1.198 #dns地址
nameserver 192.168.1.197 #dns地址
#开启转发
echo 1 > /proc/sys/net/ipv4/ip_forward
#重起网络服务生效
service network restart
#检查桥接网络
[root@moniter ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.842b2b74e1b6 no eth0
virbr0 8000.000000000000 yes
br0是我们配置的桥接网卡,virbr0是系统脚本自动配置的NAT网卡
4,配置虚拟机
#创建硬盘映像文件
------使用 qemu-img 命令创建
qemu-img create -f raw /var/lib/libvirt/images/test.img 8G
-------或使用 dd 命令创建
dd bs=1M count=8096 if=/dev/zero of=/var/lib/libvirt/images/test.img
qemu-img 是创建是文件格式是稀疏文件,优点是速度超快,由于是稀疏文件,性能可能会比第二种略差,通过如下命令查看详情
qemu-img info /var/lib/libvirt/images/test.img
输出为:注意其中 disk size 为 0
image: test.img
file format: raw
virtual size: 8.0G (8589934592 bytes)
disk size: 0
#关闭防火墙:
service iptables stop
#创建虚拟机[未优化的虚拟机---既使用ide硬盘和默认100M完全虚拟化网卡]
virt-install --name=test --ram 6144 --vcpus=4 --disk path=/var/lib/libvirt/images/test.img,size=51200 --cdrom /home/cn_windows_server_2012_r2_vl_with_update_x64_dvd_4051059.iso --network bridge=br0 --os-type='windows' --os-variant=win2k8 --graphics vnc,listen=0.0.0.0,port=5920, --force --autostart --connect qemu:///system
执行完成后你会看到提示:
开始安装......
创建域...... | 0 B 00:00
无法打开显示:
运行 'virt-viewer --help' 来查看可用命令行选项的完整列表
域安装仍在进行。请等待 完成安装。
然后再自己电脑上用vnc客户端去链接kvm服务器的5920端口。如果链接后马上退出你需要对当前vnc配置做修改,右击这个配置 然后再属性里面找到colorlevel 把它的值改成full 不同的客户端可能位置不一样。
这个时候你就能看到熟悉的操作系统安装界面。然后正常安装就好了。
#创建虚拟机【优化:用virtio 来半虚拟化scsi硬盘和网卡{千兆卡}】
A,下载virtio驱动:参考地址:http://fedoraproject.org/wiki/Windows_Virtio_Drivers
--------------------非必要,可以直接跳过直接从附件里面下载就好了-----------------------------
wget https://fedorapeople.org/groups/virt/virtio-win/virtio-win.repo -O /etc/yum.repos.d/virtio-win.repo
yum install virtio-win
从国外的站点下N慢,我挂了一晚上,后面我会把这个驱动放到附件里面
完成后可以再下面看到各种驱动文件的位置
RPM contents
/usr/share/virtio-win/*.iso: ISO CDROM containing all the drivers. See details below
/usr/share/virtio-win/*.vfd: VFD floppy images for using during install of Windows XP
/usr/share/virtio-win/drivers: Copy of the extracted VFD driver contents
/usr/share/guest-agent/*.msi: QEMU Guest Agent 32bit and 64bit MSI installers
----------------------------------------------------------------------------------------------------------
你只需要找到virtio-win.iso 和virtio-win-0.1.126_amd64.vfd
virtio-win.iso是CD镜像 virtio-win-0.1.126_amd64.vfd是软驱镜像
其实只要有这两个文件就OK 了也没必要从fedoraproject.org 下载驱动,从附件里面下载这两个镜像就好了。
下载好后我是放在home目录下的
创建虚拟机【virtio】
virt-install --name=test --ram 6144 --vcpus=4 --disk path=/home/vmdata/test.img,size=51200,bus=scsi --cdrom /home/cn_windows_server_2012_r2_vl_with_update_x64_dvd_4051059.iso --disk path=/home/virtio-win_x86.vfd,device=floppy --network bridge=br0,model=virtio --os-type='windows' --os-variant=win2k8 --graphics vnc,listen=0.0.0.0,port=5920, --force --autostart --connect qemu:///system
如果是安装的windows安装的时候回找不到硬盘 直接点击加载驱动 选择软盘里面对应的驱动就好了。linux好像是系统自带,还没测试。
参数解释:
virt-instal #是创建虚拟机指令
--name=test #指定虚拟机名称
--ram 6144 #指定内存大小
--vcpus=4 #指定CPU 核数
--disk path=/home/vmdata/test.img,size=51200,bus=scsi #指定硬盘镜像地址,配置大小以及指定磁盘类型为scsi
--cdrom /home/cn_windows_server_2012_r2_vl_with_update_x64_dvd_4051059.iso #指定启动光盘的镜像
--disk path=/home/virtio-win_x86.vfd,device=floppy #创建软盘并指定软盘镜像文件【这个镜像就是virtio的驱动】
--network bridge=br0,model=virtio #创建一张网卡并指定为桥接,使用virtio网卡
--os-type='windows' #指定客户机操作系统类型【为了更好的支持客户机操作系统】
--os-variant=win2k8 #指定客户家具体的操作系统版本【为了更好的支持客户机操作系统】
--graphics vnc,listen=0.0.0.0,port=5920, #指定启用启用vnc链接并监听本地的5920端口
--autostart #开机自动加载
#查看-os-variant支持的类型
virt-install --os-variant=list
安装完成后会有一个配置文件在/etc/libvirt/qemu/中
这个里面的配置文件都是以虚拟机名称命名的,如果后期要修改或者增减设备的话可以直接编辑这个文件;也可以用命令操作
其中:autostart 在这个目录里的虚拟机是开机自动启动的虚拟机
XML文件内容注释【这里是直接copy的别人的,其实只要看一下配置文件应该都能看懂】
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh edit vm_1
or other application using the libvirt API.
-->
<domain type='kvm'>
<name>vm_1</name> //此处为hostname
<uuid>2f952159-b231-80a4-8086-d49978513fb4</uuid> //此处为uuid标识
<memory unit='KiB'>8388608</memory> //此处为内存
<currentMemory unit='KiB'>8388608</currentMemory>
<vcpu placement='static'>6</vcpu> //此处为cpu核数
<os>
<type arch='x86_64' machine='rhel6.5.0'>hvm</type>
<boot dev='hd'/> //此处为启动项目,若想从光盘启动可设置为<boot dev='cdrom'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff> //此处为关机命令 使用 virsh destroy vm_1 关闭
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'> //此处为硬盘信息
<driver name='qemu' type='raw' cache='none'/>
<source file='/home/kvm/vm_1.qcow2'/>
<target dev='hda' bus='ide'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<disk type='block' device='cdrom'> //此处为cdrom信息,可修改为iso的路径
<driver name='qemu' type='raw'/>
<target dev='hdc' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='1' target='0' unit='0'/>
</disk>
<controller type='usb' index='0'> //此处为usb接口信息
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<interface type='network'> //此处为网卡1信息,使用network也就是nat模式连接virbr0联网
<mac address='52:54:00:9a:d7:82'/>
<source network='default'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<interface type='bridge'> //此处为网卡2信息,使用bridge桥接方式到br1
<mac address='52:54:11:fd:39:23'/>
<source bridge='br1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</interface>
//若要添加网卡可新加配置,注意修改mac地址
<interface type='bridge'>
<mac address='52:54:21:fd:39:23'/>
<source bridge='br0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</interface>
<serial type='pty'>
<target port='0'/>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='mouse' bus='ps2'/>
<graphics type='vnc' port='5920' autoport='no' listen='0.0.0.0'> //此处为VNC连接端口
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>
<model type='cirrus' vram='9216' heads='1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</memballoon>
</devices>
</domain>
修改配置的时候需要关闭KVM
virsh destroy test
使用virsh define test.xml使配置生效
使用virsh start test启动虚拟机
5,虚拟机的基本管理
virsh 是命令行的管理工具
他有两种模式,一种是virsh 后面直接跟操作和操作对象 用空格隔开
比如:
virsh start test
另外一种是输入 virsh 回车 进入控制台操作 ,然后在里面可以后去帮助信息
进入控制台后直接输入操作 和对象就可以了比如
start test
#给vnc添加链接密码:
<graphics type='vnc' port='
基本操作:
virsh start vm10 #启动指定虚拟机
virsh shutdown vm10 #通知客户机系统关机
virsh destroy vm10 #强制关机【相当于直接断电】
virsh suspend vm10 #挂起指定虚拟机
virsh resume vm10 #恢复指定挂起的虚拟机
virsh define vm_1.xml #重新加载虚拟机配置文件
virsh edit vm #编辑指定虚拟机配置文件
virsh list #查看运行的虚拟机
virsh list --all #查看所有的虚拟机
virsh autostart scan1 #把指定的虚拟机设置为开启自动启动
virsh undefine scan1 #卸载指定的虚拟机配置文件【只删除配置文件不删除img数据镜像文件】
Virsh语法参考:
Autostart 自动开始一个域
Create 从一个 XML 文件创建一个域
Define 从一个 XML 文件定义(但不开始)一个域
edit 编辑某个域的 XML 配置
shutdown 关闭一个域
start 开始一个(以前定义的)非活跃的域
reboot 重新启动一个域
suspend 挂起一个域
resume 重新恢复一个域
vncdisplay vnc 显示
以上就是基本的管理了
如何是已经安装好了的windows系统 可以直接修改xlm配置文件:
修改硬盘【这里只是记录一下,千万不要怎么改否则会进不鸟系统,可以先加一块scsi盘,然后装驱动然后再修改配置文件】
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/data/kvmimg/gzxtest04.qcow2'/>
<target dev='hda' bus='ide'/>
<alias name='ide0-0-0'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
修改为
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none' io='native'/>
<source file='/data/kvmimg/gzxtest04.qcow2'/>
<target dev='vda' bus='virtio'/>
</disk>
修改网卡为
<interface type='bridge'>
<mac address='52:54:00:2c:db:4a'/>
<source bridge='br0'/>
<model type='virtio'/>
</interface>
其实就是删除address type这一行,在driver name这一行添加io='native',dev='hda' 改为vda, bus='ide' 改为virtio
然后记得要挂virtio驱动
6,克隆虚拟机
克隆前需要关闭被克隆虚拟机
virt-clone -o Winxp -n winxpclong -f /var/lib/libvirt/images/winxpclong.img
(-o 原始客体的名称,-n新客户端的名称,-f作为新客户端磁盘映像的新文件)
7,kvm不同服务器之间迁移
为了不让篇幅过长我这里直接连接之前的文章的了解 KVM本地存储在线迁移
kvm快照以及恢复
直接创建qcow2格式硬盘
现在开始第一步,创建虚拟机的硬盘。使用如下命令:
qemu-img create -f qcow2 testilanni02.img 15G
qemu-img info /vhost/ilanni02.img
1. 查看现有磁盘镜像格式与转换
(1) 查看磁盘格式
# qemu-img info test01.img
raw格式需要转换成qcow2
(2) 关闭虚拟机并转换磁盘
# virsh shutdown oeltest01
(3) 转换磁盘格式
# qemu-img convert -f raw -O qcow2 test01.img test01.qcow2
-f 源镜像的格式
-O 目标镜像的格式
查看转换后的格式,已经转换成了qcow2, 这里是拷贝一份,并将格式转成qcow2
# qemu-img info test01.qcow2
2. 修改虚拟机配置文件
修改磁盘格式,与新qcow2格式的磁盘。
3. 对虚拟机进行快照管理
(1) 对oeltest01虚拟机创建快照
也可以snapshot-create-as oeltest01 snap1 创建后个快照别名。
(2) 查看虚拟机镜像快照的版本
(3) 查看当前虚拟机镜像快照的版本
可以看到为当前最新的快照版本。
virsh snapshot-create-as jk jk_web_allsnapshot-info centos centos_uninstall_ftp
查看虚拟机centos快照centos_uninstall_ftp详细信息
01.
<
domainsnapshot
>
02.
<
name
>1378579737</
name
>
03.
<
state
>shutoff</
state
>
04.
<
creationTime
>1378579737</
creationTime
>
05.
<
memory
snapshot
=
'no'
/>
06.
<
disks
>
07.
<
disk
name
=
'hda'
snapshot
=
'internal'
/>
08.
<
disk
name
=
'hdc'
snapshot
=
'no'
/>
09.
</
disks
>
10.
<
domain
type
=
'kvm'
>
11.
<
name
>oeltest01</
name
>
12.
<
uuid
>8f2bb4a7-c7ed-32aa-3676-9fb05923269d</
uuid
>
13.
<
memory
unit
=
'KiB'
>524288</
memory
>
14.
<
currentMemory
unit
=
'KiB'
>524288</
currentMemory
>
15.
<
vcpu
placement
=
'static'
>1</
vcpu
>
16.
<
os
>
17.
<
type
arch
=
'x86_64'
machine
=
'rhel6.4.0'
>hvm</
type
>
18.
<
boot
dev
=
'hd'
/>
19.
</
os
>
20.
<
features
>
21.
<
acpi
/>
22.
<
apic
/>
23.
<
pae
/>
24.
</
features
>
25.
<
clock
offset
=
'localtime'
/>
26.
<
on_poweroff
>destroy</
on_poweroff
>
27.
<
on_reboot
>restart</
on_reboot
>
28.
<
on_crash
>restart</
on_crash
>
29.
<
devices
>
30.
<
emulator
>/usr/libexec/qemu-kvm</
emulator
>
31.
<
disk
type
=
'file'
device
=
'disk'
>
32.
<
driver
name
=
'qemu'
type
=
'qcow2'
cache
=
'none'
/>
33.
<
source
file
=
'/data/test01.qcow2'
/>
34.
<
target
dev
=
'hda'
bus
=
'ide'
/>
35.
<
address
type
=
'drive'
controller
=
'0'
bus
=
'0'
target
=
'0'
unit
=
'0'
/>
36.
</
disk
>
37.
<
disk
type
=
'block'
device
=
'cdrom'
>
38.
<
driver
name
=
'qemu'
type
=
'raw'
/>
39.
<
target
dev
=
'hdc'
bus
=
'ide'
/>
40.
<
readonly
/>
41.
<
address
type
=
'drive'
controller
=
'0'
bus
=
'1'
target
=
'0'
unit
=
'0'
/>
42.
</
disk
>
43.
<
controller
type
=
'ide'
index
=
'0'
>
44.
<
address
type
=
'pci'
domain
=
'0x0000'
bus
=
'0x00'
slot
=
'0x01'
function
=
'0x1'
/>
45.
</
controller
>
46.
<
controller
type
=
'usb'
index
=
'0'
/>
47.
<
interface
type
=
'bridge'
>
48.
<
mac
address
=
'52:54:00:82:39:01'
/>
49.
<
source
bridge
=
'br0'
/>
50.
<
model
type
=
'virtio'
/>
51.
<
address
type
=
'pci'
domain
=
'0x0000'
bus
=
'0x00'
slot
=
'0x03'
function
=
'0x0'
/>
52.
</
interface
>
53.
<
serial
type
=
'pty'
>
54.
<
target
port
=
'0'
/>
55.
</
serial
>
56.
<
console
type
=
'pty'
>
57.
<
target
type
=
'serial'
port
=
'0'
/>
58.
</
console
>
59.
<
input
type
=
'tablet'
bus
=
'usb'
/>
60.
<
input
type
=
'mouse'
bus
=
'ps2'
/>
61.
<
graphics
type
=
'vnc'
port
=
'5910'
autoport
=
'no'
listen
=
'0.0.0.0'
>
62.
<
listen
type
=
'address'
address
=
'0.0.0.0'
/>
63.
</
graphics
>
64.
<
video
>
65.
<
model
type
=
'cirrus'
vram
=
'9216'
heads
=
'1'
/>
66.
<
address
type
=
'pci'
domain
=
'0x0000'
bus
=
'0x00'
slot
=
'0x02'
function
=
'0x0'
/>
67.
</
video
>
68.
<
memballoon
model
=
'virtio'
>
69.
<
address
type
=
'pci'
domain
=
'0x0000'
bus
=
'0x00'
slot
=
'0x04'
function
=
'0x0'
/>
70.
</
memballoon
>
71.
</
devices
>
72.
</
domain
>
73.
</
domainsnapshot
>
[root@node1 data]#
(4) 查看当前虚拟机镜像文件
又创建了一个,快照的版本也记录在镜像文件中了。
快照配置文件在/var/lib/libvirt/qemu/snapshot/虚拟机名称/下
4. 恢复虚拟机快照
(1) 恢复虚拟机快照必须关闭虚拟机。
确认虚拟机是关机状态
(2) 确认需要恢复的快照时间,这里恢复到1378579737
(3) 执行恢复,并确认恢复版本
5. 删除虚拟机快照
(1) 查看虚拟机快照
# qemu-img info test01.qcow2
这里删除第一个快照1378579737
(2) 删除快照
到此kvm虚拟机快照测试完毕。kvm虚拟化学习笔记进行到这里了,感觉到kvm虚拟化的内容真的很多。水是越来越深了。
注意要使用virsh shutdown命令进行关闭虚拟机的话,一定要安装acpid服务。同时经过多次测试证明如果虚拟机有快照的话,就无法使用virsh shutdown/reboot,命令进行关机或者重启。
yum -y install acpid 安装acpid服务
/etc/init.d/acpid status 查看acpid服务是否运行