接触虚拟机的朋友都知道快照的重要性和方便性,它能很方便的让虚拟系统还原到过去某个状态。在kvm虚拟化技术里使用snapshot- *命令可以很方便创建恢复以及删除快照。

a. 创建快照
virsh snapshot-create centos6.6_1
如果报错:
unsupported configuration: internal snapshot for disk vda unsupported for storage type raw
这是因为raw格式的镜像不能做快照,所以需要先转换一下格式

b. 磁盘镜像转换格式
先查看当前子机磁盘镜像格式
qemu-img info /data/centos6.6_1.img  
结果是:
p_w_picpath: /data/centos6.6_1.img
file format: raw
virtual size: 30G (32212254720 bytes)
disk size: 1.6G

把raw格式转换为qcow2格式(其实是复制了一份):
qemu-img convert -f raw -O qcow2 /data/centos6.6_1.img /data/centos6.6_1.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  //这样就进入了该子机的配置文件(/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'/>


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

列出快照:
virsh snapshot-list centos6.6_1

centos6.6_1子机的快照文件在  /var/lib/libvirt/qemu/snapshot/centos6.6_1/  目录下

d.  恢复快照
首先需要关闭子机
virsh shutdown centos6.6_1

vish snapshot-list centos6.6_1  //结果是
名称               Creation Time             状态
------------------------------------------------------------
1437248443           2015-07-19 03:40:43 +0800 shutoff
1437248847           2015-07-19 03:47:27 +0800 running

还原快照

virsh snapshot-revert centos6.6_1  1437248443

e. 删除快照
virsh snapshot-delete centos6.6_1  1437248847(同时 /var/lib/libvirt/qemu/snapshot/centos6.6_1/下的快照文件会消失)