KVM创建快照

快照可以把虚拟机某个时间点磁盘文件状态保存为一个镜像文件。通过这个镜像文件,可以在以后的任何时间快速恢复虚拟机在当时创建snapshot的状态,这个在使用虚拟机来做测试的时候很有用。

一、创建快照前提

  • kvm虚拟机的raw格式磁盘文件不支持快照功能,在创建快照前需要先转换为qcow2格式
# qemu-img info test7.raw
image: test7.raw
file format: raw
virtual size: 150G (161061273600 bytes)
disk size: 3.4G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

# 从raw格式转成qcow2格式
# qemu-img convert -f raw -O qcow2 test7.raw test7.qcow2

二、创建虚拟机快照

  • 查看虚拟机快照列表
# virsh snapshot-list test7 
 Name                 Creation Time             State
------------------------------------------------------------
  • 创建快照(其中快照版本信息都是记录在镜像文件中的)
# qemu-img info test7.qcow2 
image: test7.qcow2
file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 1.8G                       // 看这个空间占用
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

# virsh snapshot-create test7         // 默认就以unix时间戳为快照名字
Domain snapshot 1617264088 created

# virsh snapshot-create-as --domain test7 snap1 --description "my first test snapshot"
Domain snapshot snap1 created

# virsh snapshot-list test7
 Name                 Creation Time             State
------------------------------------------------------------
 1617264088           2021-04-01 16:01:28 +0800 running
 snap1                2021-04-01 16:03:38 +0800 running

# qemu-img info test7.qcow2 
image: test7.qcow2
file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 2.7G                  // 可以看到空间增大了
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         1617264088             390M 2021-04-01 16:01:28   00:42:39.548
2         snap1                  390M 2021-04-01 16:03:38   00:44:47.531
Format specific information:
    compat: 1.1
    lazy refcounts: true
  • 快照配置文件
# ls /var/lib/libvirt/qemu/snapshot/test7/
1617264088.xml  snap1.xml

三、恢复虚拟机快照

# virsh snapshot-revert test7 snap1

四、删除虚拟机快照

# virsh snapshot-delete test7

五、快照相关其他命令

# virsh help snapshot     
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

往期相关内容参阅

KVM服务搭建及虚拟机安装
KVM虚拟机扩容硬盘 
KVM虚拟机硬盘压缩 
KVM远程迁移启动报错 
KVM磁盘格式浅析


版权声明:本文为qq_25854057原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>