分享

KVM虚拟机快照备份

 浸心阁 2015-05-10
快照:
一。确认虚拟机镜像文件格式

[root@WWW.ESOJOURN.ORG vps]# qemu-img info centos1.img
image: centos1.img
file format: raw
virtual size: 200G (214748364800 bytes)
disk size: 6.5G


RAW格式是最原始的镜像格式,好处是速度快。但不支持很多新的功能。现在qcow2格式效率有很大提升了,而且还支持一些新的功能
1 更小的存储空间,即使是不支持holes的文件系统也可以(这下du -h和ls -lh看到的就一样了)
2 Copy-on-write support, where the image only represents changes made to an underlying disk image(这个特性SUN ZFS表现的淋漓尽致)
3 支持多个snapshot,对历史snapshot进行管理
4 支持zlib的磁盘压缩
5 支持AES的加密

《qcow2、raw、vmdk等镜像格式》对比。

二。想要启用快照功能,需要先转换镜像文件格式为qcow2。

[root@WWW.ESOJOURN.ORG vps]# virsh shutdown
先关掉VM
[root@WWW.ESOJOURN.ORG vps]# qemu-img     convert -f raw       -O qcow2      centos1.img     centos1qcow2.img
转换格式


三。常用快照命令
这里有一份libvirt官方的命令文档。完整,但说明不详细:http://wiki./page/VM_lifecycle

具体示例:

1. 列出快照:
[root@WWW.ESOJOURN.ORG vps]# virsh snapshot-list CentOS1
Name                 Creation Time             State
------------------------------------------------------------
centos1.snap1        2012-10-08 17:25:11 +0800 running
snap2                2012-10-08 17:33:14 +0800 running
snap3                2012-10-08 17:57:21 +0800 running

2. 创建快照
virsh snapshot-create-as             CentOS1            snap2
virsh snapshot-create-as --domain CentOS1 --name snap2 --description "URL: www."

3. 查看快照配置
virsh snapshot-current CentOS1
4. 恢复快照
virsh snapshot-revert CentOS1 snap2
5. 删除快照
birsh snapshot-delete CentOS1 snap2
6. 获取帮助
virsh help snapshot


四。关于qemu-img snapshot -c和savevm
很多互相抄袭的教程里,都提到了使用qemu-img snapshot -c的命令来创建快照。但我自己测试的结果 ,不管虚拟机是运行中,还是关闭状态,这个命令创建的快照字节都是0。也就是说什么也没保存下来。对此,我还没有找到原因。但找到Red hat员工Kashyap Chamarthy的一篇文章。文章里提到virsh在不同情况下,会调用不同方式来保存快照。其中至少包括‘qemu-img snapshot -c‘,qemu的 ‘savevm‘和qemu的 ‘snapshot_blkdev‘这三种方式。所以看起来快照保存,还是使用virsh snapshot-create的方式比较好。

原文引用

Also, discussed with Eric, in what cases does virsh invoke Qemu’s ‘savevm‘ and ‘qemu-img snapshot -c‘ commands while creating different types of snapshots discussed earlier above. Here is the outline:

- it uses ‘qemu-img snapshot -c‘ if the domain is offline and –disk-only was not specified
- it uses qemu’s ‘savevm‘ if the domain is online and –disk-only was not specified
- it uses qemu’s ‘snapshot_blkdev‘ if the domain is online and –disk-only is specified

http://kashyapc./2011/10/04/snapshotting-with-libvirt-for-qcow2-images/


qemu-img snapshot相关命令格式:

qemu-img snapshot -c snap1 centos1-qcow2.img

qemu-img  snapshot -l centos1-qcow2.img

Snapshot list:
ID        TAG       VM SIZE      DATE       VM CLOCK
1         snap1      0              2011-07-21 23:17:38   00:00:00.000


恢复快照:
qemu-img  snapshot   -a   CentOS5.5_64bit_Qcow2_basesys.img    CentOS5.5_64bit_Qcow2.img

其他操作:
  '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



CentOS 6 KVM Snapshot

確認 VM Image 格式

  • 執行 snapshot 的語法
    virsh snapshot-create vmname

    範例:

    [root@asus-ts100e7 ~]# virsh snapshot-create e-plast-mail
    錯誤:Requested operation is not valid: Disk '/var/lib/libvirt/images/e-plast-mail.img' does not support snapshotting

    要能執行 snapshot 的 VM image 必須是 qcow2 的格式, 出現這樣的訊息, 就要去確認與轉換.

  • 確認 image 的格式語法
    qemu-img info yourdisk.img

    範例:

    [root@asus-ts100e7 ~]# qemu-img info /var/lib/libvirt/images/e-plast-mail.img
    image: /var/lib/libvirt/images/e-plast-mail.img
    file format: raw
    virtual size: 9.8G (10485760000 bytes)
    disk size: 9.8G
  • 如果是 raw 要先轉成 qcow2 格式, 語法
    qemu-img convert -f raw -O qcow2 yourdisk.img newdisk.qcow2

    範例:

    [root@asus-ts100e7 ~]# qemu-img convert -f raw -O qcow2 /var/lib/libvirt/images/e-plast-mail.img /var/lib/libvirt/images/e-plast-mail.qcow2
    [root@asus-ts100e7 ~]# qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2
    image: /var/lib/libvirt/images/e-plast-mail.qcow2
    file format: qcow2
    virtual size: 9.8G (10485760000 bytes)
    disk size: 3.0G
    cluster_size: 65536
  • 更改 vm config file 範例:
    virsh edit e-plast-mail
    :
        <disk type='file' device='disk'>
          <driver name='qemu' type='qcow2' cache='none'/>
          <source file='/var/lib/libvirt/images/e-plast-mail.qcow2'/>
          <target dev='vda' bus='virtio'/>
        </disk>
    :
  • 重新啟動 vm 範例:
    virsh restart e-plast-mail

建立 snapshot

  • 目前版本進行 snapshot 過程 vm 會無法運作
  • 執行 snapshot 的語法
    virsh snapshot-create vmname

    範例:

    [root@asus-ts100e7 ~]# virsh snapshot-create e-plast-mail
    Domain snapshot 1349058343 created
  • 這時會在 /var/lib/libvirt/qemu/snapshot/e-plast-mail 產生 1349058343.xml, 內容如下
    <domainsnapshot>
      <name>1349058343</name>
      <state>running</state>
      <creationTime>1349058343</creationTime>
      <domain>
        <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid>
      </domain>
      <active>0</active>
    </domainsnapshot>

查詢目前 snapshot

  • 可以查看目前已經存在多少份 snapshot
    virsh snapshot-list e-plast-mail
    [root@asus-ts100e7 images]# virsh snapshot-list e-plast-mail
     名稱               Creation Time             狀態
    ---------------------------------------------------
     1349058343           2012-10-01 10:25:43 +0800 running
     1349059256           2012-10-01 10:40:56 +0800 running
  • 目前是使用哪個 snapshot 版本
    virsh snapshot-current e-plast-mail
    [root@asus-ts100e7 images]# virsh snapshot-current e-plast-mail
    <domainsnapshot>
      <name>1349059256</name>
      <state>running</state>
      <parent>
        <name>1349058343</name>
      </parent>
      <creationTime>1349059256</creationTime>
      <domain>
        <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid>
      </domain>
    </domainsnapshot>

復原到特定版本 snapshot

  • 經過驗證, libvirt 0.8.2-25.el5 在 VM 運行中執行 revert 後, VM 會當掉無法運作, 因此需要先關閉 VM 後再進行 revert
  • 確認 VM 目前運作狀態
    virsh domstate e-plast-mail
    [root@asus-ts100e7 libvirt]# virsh domstate e-plast-mail
    執行中
  • 執行關閉 VM 指令
    virsh shutdown e-plast-mail
    [root@asus-ts100e7 libvirt]# virsh shutdown e-plast-mail
    區域 e-plast-mail 正在執行關機
  • 確認 VM 目前已經是關機狀態
    virsh domstate e-plast-mail
    [root@asus-ts100e7 save]# virsh domstate e-plast-mail
    關機
  • 確定要回覆哪份 snapshot 版本
    virsh snapshot-list e-plast-mail
    [root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail
     名稱               Creation Time             狀態
    ---------------------------------------------------
     1349058343           2012-10-01 10:25:43 +0800 running
     1349059256           2012-10-01 10:40:56 +0800 running
     1349071788           2012-10-01 14:09:48 +0800 running
  • 執行 snapshot-revert 指令
    virsh snapshot-revert e-plast-mail 1349071788
  • 確認目前執行的 snapshot 版本
    virsh snapshot-current e-plast-mail
    [root@asus-ts100e7 save]# virsh snapshot-current e-plast-mail
    <domainsnapshot>
      <name>1349071788</name>
      <state>running</state>
      <parent>
        <name>1349059256</name>
      </parent>
      <creationTime>1349071788</creationTime>
      <domain>
        <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid>
      </domain>
    </domainsnapshot>
  • revirt 後 VM 會自動啟動在當時 snapshot-create 的狀態
  • 原本在 1349071788 (2012-10-01 14:09:48) 時所執行的程式還會繼續運作
  • VM 內的系統時間還是在 1349071788 (2012-10-01 14:09:48) 所以要考慮時間矯正議題

刪除不需要的 snapshot

  • 原有的 snapshot 清單
    virsh snapshot-list e-plast-mail
    [root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail
     名稱               Creation Time             狀態
    ---------------------------------------------------
     1349058343           2012-10-01 10:25:43 +0800 running
     1349059256           2012-10-01 10:40:56 +0800 running
     1349071788           2012-10-01 14:09:48 +0800 running
  • 打算移除掉 1349059256 這份版本
    virsh snapshot-delete e-plast-mail 1349059256
    [root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail
     名稱               Creation Time             狀態
    ---------------------------------------------------
     1349058343           2012-10-01 10:25:43 +0800 running
     1349071788           2012-10-01 14:09:48 +0800 running
  • snapshot 主要在 image file 內增加 tag, 因此可以透過 qemu-img info 指令來瞭解
    qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2
    [root@asus-ts100e7 images]# qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2
    image: /var/lib/libvirt/images/e-plast-mail.qcow2
    file format: qcow2
    virtual size: 9.8G (10485760000 bytes)
    disk size: 7.0G
    cluster_size: 65536
    Snapshot list:
    ID        TAG                 VM SIZE                DATE       VM CLOCK
    1         1349058343             977M 2012-10-01 10:25:43 1290:29:38.005
    3         1349071788             965M 2012-10-01 14:09:48 1291:18:26.283

參考網址

KVM虚拟机的基本镜像和增量镜像

KVM虚拟机的基本镜像和增量镜像
1、概述
实验目的:通过一个基础镜像(node.img),里面把各个虚拟机都需要的环境都搭建好,然后基于这个镜像建立起一个个增量镜像,每个增量镜像对应一个虚拟机,虚拟机对镜像中所有的改变都记录在增量镜像里面,基础镜像始终保持不变。
功能:节省磁盘空间,快速复制虚拟机。

环境:
基本镜像文件:node.img  虚拟机ID:node  
增量镜像文件:node4.img 虚拟机ID:node4
要求:以基本镜像文件node.omg为基础,创建一个镜像文件node4.img,以此创建一个虚拟机机node4,虚拟机node4的改变将存储于node4.img中。

2、创建增量镜像文件
[root@target kvm_node]#qemu-img create -b node.img -f qcow2 node4.img
[root@target kvm_node]# qemu-img info node4.img 
image: node4.img
file format: qcow2
virtual size: 20G (21495808000 bytes)
disk size: 33M
cluster_size: 65536
backing file: node.img (actual path: node.img)
#注:该实验只是针对qcow2格式的镜像文件,未测试raw格式的镜像文件是否可行。

3、创建虚拟机node4的XML配置文件
[root@target kvm_node]# cp /etc/libvirt/qemu/node.xml /etc/libvirt/qemu/node4.xml
[root@target kvm_node]# vim /etc/libvirt/qemu/node4.xml 
<domain type='kvm'>
  <name>node4</name>                                  #node4的虚拟机名,须修改,否则与基本虚拟机冲突
  <uuid>4b7e91eb-6521-c2c6-cc64-c1ba72707fe4</uuid>   #node4的UUID,必须修改,否则与基本虚拟机冲突
  <memory>524288</memory>
  <currentMemory>524288</currentMemory>
  <vcpu cpuset='0-1'>2</vcpu>
  <os>
    <type arch='x86_64' machine='rhel5.4.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='localtime'/>
  <on_poweroff>destroy</on_poweroff>
  <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='qcow2'/>
      <source file='/virhost/kvm_node/node4.img'/>    #将原指向/virhost/kvm_node/node.img改为node4.img
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </disk>
    <interface type='bridge'>
      <mac address='54:52:00:69:d5:f4'/>             #修改网卡MAC,防止冲突
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <interface type='bridge'>
      <mac address='54:52:00:69:d5:e4'/>            #修改网卡MAC,防止冲突
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' 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='5904' autoport='no' listen='0.0.0.0' passwd='xiaobai'>
      <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='0x06' function='0x0'/>
    </memballoon>
  </devices>
</domain>

4、根据xml配置定义虚拟机node4
[root@target kvm_node]#virsh define /etc/libvirt/qemu/node4.xml
[root@target kvm_node]#virsh start node4  

5、测试 
[root@target kvm_node]# du -h node.img 
6.3G    node.img
[root@target kvm_node]# du -h node4.img
33M     node4.img
[root@node4 ~]# dd if=/dev/zero of=test bs=1M count=200   #在虚拟机node4上增量200M大小文件
200+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 1.00361 seconds, 209 MB/s
[root@target kvm_node]# du -h node.img                    #基本镜像文件node.img大小未变
6.3G    node.img
[root@target kvm_node]# du -h node.img                    #增量镜像文件node4.img增加200M了
234M    node4.img

复制 克隆:


首先把需要克隆的源虚拟机先关闭,然后使用以下命令来进行克隆,注意我这里使用的是相对路径。

virsh shutdown VM02
virt-clone -o VM02 -n VM05 -f VM05.img --connect=qemu:///system
chown qemu.qemu VM05.img

需要修改一些东西,把 vnc 的端口号修改一下,避免两个产生冲突,并记录一下这里面的 MAC 地址备用。

virsh edit VM05

先启动 VM05,目前两个虚拟机还不能同时启动。

virsh start VM05
rm /etc/udev/rules.d/70-persistent-net.rules
vi /etc/sysconfig/network-scripts/ifcfg-eth0

修改 eth0 的 MAC 地址与刚才 VM05 配置文件中的 MAC 一致,并重启计算机。

这时再启动 VM02(源虚拟机)时会报以下错误:

error: Failed to start domain VM02
error: Unable to read from monitor: Connection reset by peer

原因在于 IDE 的光驱设备不可共享产生了冲突所致,删除 IDE 光驱即可。

virsh shutdown VM05
virsh edit VM05

删除其中关于 ide cdrom 相关的一段设备描述,同时需注意内存是否自己期望的大小。

virsh start VM05
virsh start VM02

源虚拟机与目标虚拟机都没有报错,正常启动,则本次克隆完成。



当我们需要批量的部署我们的XEN 或者KVM的时候,LVM的snap功能是个不错的选择.
 
#lvcreat -L 2G -s -n  lv-virt2 /dev/vg01/lv-virt1
为/dev/vg01/lv-virt1创建一个大小为2G的快照lv-virt2
修改Domain-U的配置文件,uuid得用uuidgen生成.修改内容很简单.
注意:依据写时复制的原理,当快照逻辑卷不能容纳父卷改变的块时,快照将不可用.避免这种情况的发生,注意用lvdisplay查看及时用lvextend扩展尺寸或者创建块大小与父卷块一致的不死快照.



系统版本

[root@desktop2 ~]# cat /proc/version 
Linux version 2.6.32-220.el6.x86_64 (mockbuild@x86-004.build.bos.redhat.com) (gcc version 4.4.5 20110214 (Red Hat 4.4.5-6) (GCC) ) #1 SMP Wed Nov 9 08:03:13 EST 2011
[root@desktop2 ~]# 

为虚拟机创建快照

[root@desktop2 ~]# lvs
  LV           VG   Attr   LSize   Origin  Snap%  Move Log Copy%  Convert
  home         vol0 -wi-ao 512.00m                                       
  root         vol0 -wi-ao   8.00g                                       
  vserver      vol0 owi-a-  10.00g                                       
  vserver-snap vol0 swi-a-   4.00g vserver   0.89                        
[root@desktop2 ~]# lvcreate -L 1G -s -n vserver2 /dev/vol0/vserver
  Logical volume "vserver2" created
[root@desktop2 ~]# lvs
  LV           VG   Attr   LSize   Origin  Snap%  Move Log Copy%  Convert
  home         vol0 -wi-ao 512.00m                                       
  root         vol0 -wi-ao   8.00g                                       
  vserver      vol0 owi-a-  10.00g                                       
  vserver-snap vol0 swi-a-   4.00g vserver   0.89                        
  vserver2     vol0 swi-a-   1.00g vserver   0.00   


生成新的uuid

[root@desktop2 ~]# uuidgen 
1440f78a-3f93-4a84-be09-bef93c3188e3

导出vserver虚拟机的配置信息,并修改4处客户化信息

[root@desktop2 ~]# virsh dumpxml vserver > /tmp/vserver2.xml
[root@desktop2 ~]# vim /tmp/vserver2.xml
<!--
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 vserver2
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>vserver2</name>
  <uuid>1440f78a-3f93-4a84-be09-bef93c3188e3</uuid>
  <memory>2097152</memory>
  <currentMemory>1048576</currentMemory>
  <vcpu>2</vcpu>
  <os>
    <type arch='x86_64' machine='rhel6.2.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw' cache='none' io='native'/>
      <source dev='/dev/vol0/vserver2'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </disk>
    <interface type='bridge'>
      <mac address='52:54:00:00:00:02'/>
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <input type='tablet' bus='usb'/>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes'/>
    <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='0x05' function='0x0'/>
    </memballoon>
  </devices>
</domain>
[root@desktop2 ~]# 


生成克隆虚拟机

[root@desktop2 ~]# virsh define /tmp/vserver2.xml
Domain vserver2 defined from /tmp/vserver2.xml


启动克隆虚拟机
[root@desktop2 ~]# virsh start vserver2
Domain vserver2 started

[root@desktop2 ~]# virsh list
 Id Name                 State
----------------------------------
  3 vserver2             running


[root@server ~]# virt-install --debug --hvm --vnc --name virt1.example.com --os-type=linux --os-variant=rhel6 --pxe --network network=default,model=e1000,mac=02:54:00:13:be:e4 --disk pool=pool0,size=20 --ram 1024 --vcpus=1 


virt-install    --name=kvm3-rhel7    --disk path=/vm/kvm3/kvm1.img,size=12,sparse=true \
 --graphics=vnc    --vcpus=1 --ram=800    --network bridge=br0    --os-type=linux  \
--os-variant=rhel6    --location=nfs:10.1.1.1:/share/rhel6.1 --extra-args "ks=ftp://cs:cs@10.1.1.10/my.cfg"



为了是虚拟机可被外部网络访问,有时后nat不能满足生产和实验要求,这时候就需要网卡的桥接设置了。
实验环境:

centos 5.5 64位 , DELL R510 32G RAM 2.4*8 XEON, KVM,
1、首先配置bridge,

#cd /etc/sysconfig/network-scripts/
# vim ifcfg-eth0

DEVICE=eth0
ONBOOT=yes
BRIDGE=br0
HWADDR=b8:ac:6f:65:31:e5
 
#vim ifcfg-br0
DEVICE=br0   
 TYPE=Bridge 
BOOTPROTO=static
ONBOOT=yes 
IPADDR=10.10.21.70 
NETMASK=255.255.255.192
GATEWAY=10.10.21.1
重启network,在创建虚拟机的时候就可以选择eth0 br0了,
3、开启防火墙允许通过这块bridge设备转发
# iptables -I FORWARD -m physdev --physdev-is-bridged -j ACCEPT
# service iptables save
4、为linux kvm做的网络bridge就好了,so easy!
5、既然做好了bridge,就来进行pxe安装:

#virt-install --name centos5 --vcpus=2 --ram=2048 --accelerate --vnc --network bridge:br0 --disk path=/var/lib/libvirt/images/centos5.img,size=10 --network bridge:br0 --pxe

巧妙的利用ks.cfg自动安装的配置文件,可以省区平时自己安装个系统的大量时间,当然在进行几十台上百台的批量安装时候,还是直接pxe-autoinstall好。

#virt-install --name centos5 --vcpus=2 --ram=2048 --accelerate --vnc --network bridge:br0 --disk path=/var/lib/libvirt/images/centos5.img,size=10 --network bridge:br0 --cdrom /iso/centos-5.5-dvd.iso -x "ks=ftp://10.10.21.1/pub/ks.cfg"


一、概念

Kvm:完全虚拟化,内核虚拟机,为什么很快?

 1.内存的调度直接交给内核空间;
 2.客户机与客户机是进程与进程之间的关系,减少了I/O;
 3.cpu由硬件直接支持;

安装kvm的条件:

 1.RHEL6以上版本;
 2.64位操作系统;
 3.最少2个GB的内存;
 4.CPU支持虚拟化,Inter vmx,AMD svm;
 5.CPU支持物理地址扩展,pae;

4、5项通过查看/proc/cpuinfo可知;
 

二、安装

安装内核模块:
         #yum -y install kvm

安装虚拟机库,已经管理工具:
         #yum -y install libvirt libvirt-python python-virtinst virt-viewer libvirt-client virt-manager

加载模块:
        #modprobe kvm

永久生效建议写入/etc/rc.local:
        #echo "modprobe kvm" >> /etc/rc.local

三、配置虚拟网络

因默认没有配置虚拟网络;
新建br0跟物理网卡eth0桥接:br0 —桥接到—> eth0

1、关闭rhel6 NetworkManager服务
至少要关闭桥接网络相关的设备(如eth0 bro<准备建立的网络>)

      # chkconfig NetworkManager off
      # service NetworkManager stop

2、建立桥接设备br0
     # vim /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE=br0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.4
NETMASK=255.255.255.0
TYPE=Bridge     #注意字符大小写,第一个字母大写,其他小写
NM_CONTROLLED="no"    #明确指定不用NetworkManager服务管理

3、修改物理网卡配置文件:
     # vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE="eth0"
BOOTPROTO="none"
HWADDR="48:5B:39:B9:41:31"
NM_CONTROLLED="no"
ONBOOT="yes"
BRIDGE=br0

    # service network restart        –>ifconfig检查是否有br0产生,如没有,把libvirtd服务也重启下
    # service libvirtd start

四、客户机安装

例子:图形界面安装,同xen
安装方式支持光盘,网络,PXE等
   # virt-manager &    –>打开图形管理窗口

例子:命令行安装
# virt-install –help
# virt-install -v -n node2 –pxe –disk path=/var/lib/libvirt/images/node2.img,size=8  –network bridge=br0 –vnc –os-type=linux –os-variant=rhel5 -r 512 –vcpus=1

五、在线迁移

现在在线迁移一般都有错误,还没有解决,官方也没有文档。待续…



使用qemu-img管理虚拟机磁盘镜像(创建虚拟机,虚拟机快照)

分类: LINUX 2013-01-03 15:40 364人阅读 评论(0) 收藏 举报

目录(?)[+]

一台虚拟机的核心就是一个磁盘镜像,这个镜像可以理解成虚拟机的磁盘,里面有虚拟机的操作系统和驱动等重要文件。本文主要介绍创建虚拟机的一般过程。


创建虚拟机镜像



 

要在一台host上跑起一个虚拟机一般需要两个步骤:

第一步:创建虚拟机镜像

qemu-img create -f raw /images/vm1.raw 8G

qmeu-img创建的镜像是一个稀疏文件,也就是说刚创建出来的文件并没有8G,它会随着数据的增多慢慢增加,直到8G

 

第二步:启动虚拟机

kvm /imges/vm1.raw

运行结果: 因为镜像里面没有任何内容,所以提示找不到可引导设备。

 

使用qemu-img管理镜像



 

qemu-img基本命令


 

上节介绍了使用qemu-img创建镜像,这一节将会介绍qemu-img在镜像管理上的强大功能。

qemu-img有很多命令,包括下面常用的,当然qemu-img -h你懂得。

info

查看镜像的信息

create

创建镜像

check

检查镜像

convert

转化镜像的格式,(raw,qcow ……)

snapshot

管理镜像的快照

rebase

在已有的镜像的基础上创建新的镜像

resize

增加或减小镜像大小


 创建镜像


 

qemu-img create -f <fmt> -o <options> <fname> <size>

 举例:

qemu-img create -f raw -o size=4G /images/vm2.raw

 

hzgatt@hzgatt:~/images$ ll
total 0-rw-r--r-- 1 hzgatt hzgatt 4.0G  6月 29 14:11 vm2.raw
hzgatt@hzgatt:~/images$ ll -s
total 00 -rw-r--r-- 1 hzgatt hzgatt 4.0G  6月 29 14:11 vm2.raw

 

hzgatt@hzgatt:~/images$ qemu-img info vm2.raw 
image: vm2.raw
file format: raw
virtual size: 4.0G (4294967296 bytes)
disk size: 0

 

虽然ls中看到文件的大小是4G,但是实际上磁盘大小是0。这就是稀疏文件

 

 

转化


将一个镜像文件转化为另外一种格式,qemu-img支持的格式可以看qemu-img -h最后一行。

Supported formats: vvfat vpc vmdk vdi sheepdog rbd raw host_cdrom host_floppy host_device file qed qcow2 qcow parallels nbd dmg tftp ftps ftp https http cow cloop bochs blkverify blkdebug

 

转化命令:

qemu-img convert -c -f fmt -O out_fmt -o options fname out_fname

 

-c:采用压缩,只有qcow和qcow2才支持

-f:源镜像的格式,它会自动检测,所以省略之

-O 目标镜像的格式

-o 其他选先

fname:源文件

out_fname:转化后的文件

看例子:

hzgatt@hzgatt:~/images$ qemu-img convert -c -O qcow2 vm2.raw vm2.qcow2

 

hzgatt@hzgatt:~/images$ ll -s
total 136K
   0 -rw-r--r-- 1 hzgatt hzgatt 5.0G  6月 29 13:55 vm1.raw
136K -rw-r--r-- 1 hzgatt hzgatt 193K  6月 29 14:22 vm2.qcow2
   0 -rw-r--r-- 1 hzgatt hzgatt 4.0G  6月 29 14:11 vm2.raw
hzgatt@hzgatt:~/images$ qemu-img info vm2.qcow2 
image: vm2.qcow2
file format: qcow2
virtual size: 4.0G (4294967296 bytes)
disk size: 136K
cluster_size: 65536

 


如果想看要转化的格式支持的-o选项有哪些,可以在命令末尾加上 -o ?

复制代码
hzgatt@hzgatt:~/images$ qemu-img convert -c -O qcow2 vm2.raw vm2.qcow2 -o ?
Supported options:
size             Virtual disk size
backing_file     File name of a base image
backing_fmt      Image format of the base image
encryption       Encrypt the image
cluster_size     qcow2 cluster size
preallocation    Preallocation mode (allowed values: off, metadata)
复制代码

 


增加减少镜像大小


注意:只有raw格式的镜像才可以改变大小

hzgatt@hzgatt:~/images$ qemu-img resize vm2.raw +2GB
复制代码
hzgatt@hzgatt:~/images$ ll -s
total 136K
   0 -rw-r--r-- 1 hzgatt hzgatt 5.0G  6月 29 13:55 vm1.raw
136K -rw-r--r-- 1 hzgatt hzgatt 193K  6月 29 14:22 vm2.qcow2
   0 -rw-r--r-- 1 hzgatt hzgatt 6.0G  6月 29 14:28 vm2.raw
hzgatt@hzgatt:~/images$ qemu-img info vm2.raw 
image: vm2.raw
file format: raw
virtual size: 6.0G (6442450944 bytes)
disk size: 0
复制代码

 

快照


查看快照

qemu-img snapshot -l /images/vm2.qcow2

注意:只有qcow2才支持快照

打快照

qemu-img snapshot -c booting vm2.qcow2

 

举例:

hzgatt@hzgatt:~/images$ qemu-img snapshot -c booting vm2.qcow2 
hzgatt@hzgatt:~/images$ qemu-img snapshot -l vm2.qcow2 
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         booting                   0 2012-06-29 14:35:04   00:00:00.000

 

从快照恢复:

qemu-img snapshot -a 1 /images/vm2.qcow2

然后从kvm启动这个虚拟机,会发现虚拟机又在打快照时的状态了

 

删除快照:

qemu-img snapshot -d 2 /images/vm2.qcow

 

 

使用派生镜像(qcow2)


    当创建的虚拟机越来越多,并且你发现好多虚拟机都是同一个操作系统,它们的区别就是安装的软件不大一样,那么你肯定会希望把他们公共的部分提取出来,只保存那些与公共部分不同的东西,这样镜像大小下去了,空间变多了,管理也方便了。派生镜像就是用来干这事的!

首先看一个原始镜像

hzgatt@hzgatt:~/images$ qemu-img info vm3_base.raw 
image: vm3_base.raw
file format: raw
virtual size: 2.0G (2147483648 bytes)
disk size: 2.0G

现在我们新建一个镜像,但是派生自它

hzgatt@hzgatt:~/images$ qemu-img create -f qcow2 vm3_5.qcow2 -o backing_file=vm3_base.raw 5G
Formatting 'vm3_5.qcow2', fmt=qcow2 size=5368709120 backing_file='vm3_base.raw' encryption=off cluster_size=65536

 

hzgatt@hzgatt:~/images$ ll-rw-r--r-- 1 hzgatt hzgatt 193K  6月 29 15:00 vm3_5.qcow2
-rw-r--r-- 1 hzgatt hzgatt 2.0G  6月 29 14:51 vm3_base.raw

 

复制代码
hzgatt@hzgatt:~/images$ qemu-img info vm3_5.qcow2 
image: vm3_5.qcow2
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 136K
cluster_size: 65536
backing file: vm3_base.raw (actual path: vm3_base.raw)
复制代码

 

 ^_^,这个镜像才136K,够省了吧。DRY永远的真理啊!


现在我们在vm3_5.qcow2上打了很多安全补丁,然后发现我又想在vm3_5.qcow2上派生新的虚拟机,o(∩∩)o...哈哈,这下怎么办呢?

hzgatt@hzgatt:~/images$ qemu-img convert -O raw vm3_5.qcow2 vm3_base2.raw

 

hzgatt@hzgatt:~/images$ qemu-img info vm3_base2.raw 
image: vm3_base2.raw
file format: raw
virtual size: 5.0G (5368709120 bytes)
disk size: 592M

 

这个转化将会将vm3_5和base合并,生成新的vm3_base2.raw,然后你就可以继续无穷无尽的派生之旅了!


------------------------------------------------------------------------------------------------------

kvm快照应用
kvm也具有快速恢复的方法,前提是必须处于关机状态才可以执行,否则会出现各种莫名其妙的问题
创建镜像:
qemu-img snapshot -c initial smokeping_falcon_test0917.qcow2 
恢复镜像:
qemu-img snapshot -a initial smokeping_falcon_test0917.qcow2
删除镜像
qemu-img snapshot -d initial smokeping_falcon_test0917.qcow2
状态查看
qemu-img snapshot -l smokeping_falcon_test0917.qcow2 
创建前的大小
[root@smokeping]# ll
total 7578488
-rw-r--r-- 1 root root 171825168384 Sep 17 17:01 smokeping_falcon_test0917.xml
-rw-r--r-- 1 root root 171825168384 Sep 17 16:01 smokeping_falcon_test_187.qcow2
-rw-r--r-- 1 root root 171825168384 Sep  6 11:14 smokeping_falcon_test.qcow2
以下是创建后的文件
[root@smokeping]# ll
total 7603284
-rw-r--r-- 1 qemu qemu 171850530816 Sep 17 17:22 smokeping_falcon_test0917.qcow2
-rw-r--r-- 1 root root 171825168384 Sep 17 16:01 smokeping_falcon_test_187.qcow2
-rw-r--r-- 1 root root 171825168384 Sep  6 11:14 smokeping_falcon_test.qcow2
查看镜像方法1
[root@smokeping]# qemu-img info smokeping_falcon_test0917.qcow2 
image: smokeping_falcon_test0917.qcow2
file format: qcow2
virtual size: 160G (171798691840 bytes)
disk size: 2.2G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         initial                   0 2012-09-17 17:08:49   00:00:00.000
查看镜像方法2
[root@smokeping]# qemu-img snapshot -l smokeping_falcon_test0917.qcow2 
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         initial                   0 2012-09-17 17:08:49   00:00:00.000
利用这个镜像,可以迅速还原服务器状态,使用空间也不大。
实测中,若虚拟机为启动状态制作快照,恢复后会无法载入系统,关闭服务器,再次启动,服务器直接崩溃。



CentOS 6 KVM Snapshot

確認 VM Image 格式

  • 執行 snapshot 的語法
    virsh snapshot-create vmname

    範例:

    [root@asus-ts100e7 ~]# virsh snapshot-create e-plast-mail
    錯誤:Requested operation is not valid: Disk '/var/lib/libvirt/images/e-plast-mail.img' does not support snapshotting

    要能執行 snapshot 的 VM image 必須是 qcow2 的格式, 出現這樣的訊息, 就要去確認與轉換.

  • 確認 image 的格式語法
    qemu-img info yourdisk.img

    範例:

    [root@asus-ts100e7 ~]# qemu-img info /var/lib/libvirt/images/e-plast-mail.img
    image: /var/lib/libvirt/images/e-plast-mail.img
    file format: raw
    virtual size: 9.8G (10485760000 bytes)
    disk size: 9.8G

  • 如果是 raw 要先轉成 qcow2 格式, 語法
    qemu-img convert -f raw -O qcow2 yourdisk.img newdisk.qcow2

    範例:

    [root@asus-ts100e7 ~]# qemu-img convert -f raw -O qcow2 /var/lib/libvirt/images/e-plast-mail.img /var/lib/libvirt/images/e-plast-mail.qcow2
    [root@asus-ts100e7 ~]# qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2
    image: /var/lib/libvirt/images/e-plast-mail.qcow2
    file format: qcow2
    virtual size: 9.8G (10485760000 bytes)
    disk size: 3.0G
    cluster_size: 65536

  • 更改 vm config file 範例:
    virsh edit e-plast-mail
    :
        <disk type='file' device='disk'>
          <driver name='qemu' type='qcow2' cache='none'/>
          <source file='/var/lib/libvirt/images/e-plast-mail.qcow2'/>
          <target dev='vda' bus='virtio'/>
        </disk>
    :

  • 重新啟動 vm 範例:
    virsh restart e-plast-mail

建立 snapshot

  • 目前版本進行 snapshot 過程 vm 會無法運作
  • 執行 snapshot 的語法
    virsh snapshot-create vmname

    範例:

    [root@asus-ts100e7 ~]# virsh snapshot-create e-plast-mail
    Domain snapshot 1349058343 created

  • 這時會在 /var/lib/libvirt/qemu/snapshot/e-plast-mail 產生 1349058343.xml, 內容如下
    <domainsnapshot>
      <name>1349058343</name>
      <state>running</state>
      <creationTime>1349058343</creationTime>
      <domain>
        <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid>
      </domain>
      <active>0</active>
    </domainsnapshot>

查詢目前 snapshot

  • 可以查看目前已經存在多少份 snapshot
    virsh snapshot-list e-plast-mail
    [root@asus-ts100e7 images]# virsh snapshot-list e-plast-mail
     名稱               Creation Time             狀態
    ---------------------------------------------------
     1349058343           2012-10-01 10:25:43 +0800 running
     1349059256           2012-10-01 10:40:56 +0800 running

  • 目前是使用哪個 snapshot 版本
    virsh snapshot-current e-plast-mail
    [root@asus-ts100e7 images]# virsh snapshot-current e-plast-mail
    <domainsnapshot>
      <name>1349059256</name>
      <state>running</state>
      <parent>
        <name>1349058343</name>
      </parent>
      <creationTime>1349059256</creationTime>
      <domain>
        <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid>
      </domain>
    </domainsnapshot>

復原到特定版本 snapshot

  • 經過驗證, libvirt 0.8.2-25.el5 在 VM 運行中執行 revert 後, VM 會當掉無法運作, 因此需要先關閉 VM 後再進行 revert
  • 確認 VM 目前運作狀態
    virsh domstate e-plast-mail
    [root@asus-ts100e7 libvirt]# virsh domstate e-plast-mail
    執行中

  • 執行關閉 VM 指令
    virsh shutdown e-plast-mail
    [root@asus-ts100e7 libvirt]# virsh shutdown e-plast-mail
    區域 e-plast-mail 正在執行關機

  • 確認 VM 目前已經是關機狀態
    virsh domstate e-plast-mail
    [root@asus-ts100e7 save]# virsh domstate e-plast-mail
    關機

  • 確定要回覆哪份 snapshot 版本
    virsh snapshot-list e-plast-mail
    [root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail
     名稱               Creation Time             狀態
    ---------------------------------------------------
     1349058343           2012-10-01 10:25:43 +0800 running
     1349059256           2012-10-01 10:40:56 +0800 running
     1349071788           2012-10-01 14:09:48 +0800 running

  • 執行 snapshot-revert 指令
    virsh snapshot-revert e-plast-mail 1349071788

  • 確認目前執行的 snapshot 版本
    virsh snapshot-current e-plast-mail
    [root@asus-ts100e7 save]# virsh snapshot-current e-plast-mail
    <domainsnapshot>
      <name>1349071788</name>
      <state>running</state>
      <parent>
        <name>1349059256</name>
      </parent>
      <creationTime>1349071788</creationTime>
      <domain>
        <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid>
      </domain>
    </domainsnapshot>
  • revirt 後 VM 會自動啟動在當時 snapshot-create 的狀態
  • 原本在 1349071788 (2012-10-01 14:09:48) 時所執行的程式還會繼續運作
  • VM 內的系統時間還是在 1349071788 (2012-10-01 14:09:48) 所以要考慮時間矯正議題

刪除不需要的 snapshot

  • 原有的 snapshot 清單
    virsh snapshot-list e-plast-mail
    [root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail
     名稱               Creation Time             狀態
    ---------------------------------------------------
     1349058343           2012-10-01 10:25:43 +0800 running
     1349059256           2012-10-01 10:40:56 +0800 running
     1349071788           2012-10-01 14:09:48 +0800 running

  • 打算移除掉 1349059256 這份版本
    virsh snapshot-delete e-plast-mail 1349059256
    [root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail
     名稱               Creation Time             狀態
    ---------------------------------------------------
     1349058343           2012-10-01 10:25:43 +0800 running
     1349071788           2012-10-01 14:09:48 +0800 running
  • snapshot 主要在 image file 內增加 tag, 因此可以透過 qemu-img info 指令來瞭解
    qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2
    [root@asus-ts100e7 images]# qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2
    image: /var/lib/libvirt/images/e-plast-mail.qcow2
    file format: qcow2
    virtual size: 9.8G (10485760000 bytes)
    disk size: 7.0G
    cluster_size: 65536
    Snapshot list:
    ID        TAG                 VM SIZE                DATE       VM CLOCK
    1         1349058343             977M 2012-10-01 10:25:43 1290:29:38.005
    3         1349071788             965M 2012-10-01 14:09:48 1291:18:26.283

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多