KVM快照管理


1、创建快照
virsh snapshot-create daixuan1

[root@daixuan ~]# virsh snapshot-create daixuan1

Domain snapshot 1455801179 created

查看快照

[root@daixuan ~]# qemu-img info /data/daixuan1.qcow2

p_w_picpath: /data/daixuan1.qcow2

file format: qcow2

virtual size: 10G (10737418240 bytes)

disk size: 10G

cluster_size: 65536

Snapshot list:

ID        TAG                 VM SIZE      DATE       VM CLOCK

1         1455801179            0 2016-02-18 21:12:59   00:00:00.000


注:磁盘镜像转换格式,创建快照

创建快照如果报错:
unsupported configuration: internal snapshot for disk vda unsupported for storage type raw
这是因为daixuan1.img文件是raw格式的镜像不能做快照,所以需要先转换一下格式先查看当前子机磁盘镜像格式

qemu-img info /data/centos6.6_1.img  
结果是:
p_w_picpath: /data/centos6.6_1.img
file format: raw(格式必须是qcow2才可以做快照)
virtual size: 30G (32212254720 bytes)
disk size: 1.6G

把raw格式转换为qcow格式(其实是复制了一份):
qemu-img convert -f raw -O qcow2 /data/centos6.6_1.img /data/centos6.6_1.qcow2
-f raw 原来格式

-O qcow2转化的格式


qemu-img info /data/centos6.6_1.qcow2   //再次查看格式,结果如下
p_w_picpath: /data/centos6.6_1.qcow2
file format: qcow2
virtual size: 30G (32212254720 bytes)
disk size: 1.1G
cluster_size: 65536

现在我们还需要编辑子机配置文件,让它使用新格式的虚拟磁盘镜像文件
virsh edit centos6.6_1  //这样就进入了该子机的配置文件,等价使用下面的vi

vi /etc/libvirt/qemu/centos6.6_1 

(/etc/libvirt/qemu/centos6.6_1.xml),跟用vim编辑这个文件一样的用法
需要修改的地方是:
      <driver name='qemu' type='raw' cache='none'/>
      <source file='/data/centos6.6_1.img'/>

改为:
      <driver name='qemu' type='qcow2' cache='none'/>
      <source file='/data/centos6.6_1.qcow2'/>

继续创建快照
virsh snapshot-create centos6.6_1  //这次成功了,提示如下
Domain snapshot 1437248443 created


2、列出快照:

[root@daixuan ~]# virsh snapshot-list daixuan1

 名称               Creation Time             状态

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

 1455801179           2016-02-18 21:12:59 +0800 shutoff


3、恢复快照

首先需要关闭子机
virsh destroy daixuan1

确认子机是否关闭
virsh domstate daixuan1
关闭

[root@daixuan ~]# virsh snapshot-revert daixuan1 1455801179


[root@daixuan ~]# virsh snapshot-list daixuan1   //结果是

 名称               Creation Time             状态

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

 1455801179           2016-02-18 21:12:59 +0800 shutoff


4、查看当前子机的快照版本:

[root@daixuan ~]# virsh snapshot-current daixuan1

<domainsnapshot>

  <name>1455801179</name>


5、 删除快照

[root@daixuan ~]# virsh snapshot-delete daixuan1 1455801179

Domain snapshot 1455801179 deleted


[root@daixuan ~]# virsh snapshot-list daixuan1

 名称               Creation Time             状态


daixuan1子机的快照文件在 /var/lib/libvirt/qemu/snapshot/daixuan1/  目录下

[root@daixuan ~]# ls /var/lib/libvirt/qemu/snapshot/daixuan1/  //已经没有快照了


[root@daixuan ~]# virsh -help | grep snapshot //查看virsh的snapshot的命令

    iface-begin                    create a snapshot of current interfaces settings, which can be later committed (iface-commit) or restored (iface-rollback)

 Snapshot (help keyword 'snapshot')

    snapshot-create                Create a snapshot from XML

    snapshot-create-as             Create a snapshot from a set of args

    snapshot-current               Get or set the current snapshot

    snapshot-delete                Delete a domain snapshot

    snapshot-dumpxml               Dump XML for a domain snapshot

    snapshot-edit                  edit XML for a snapshot

    snapshot-info                  snapshot information

    snapshot-list                  List snapshots for a domain

    snapshot-parent                Get the name of the parent of a snapshot

    snapshot-revert                Revert a domain to a snapshot