我们使用virsh工具管理kvm, 下面是使用命令行管理快照的案例:
1、查看现有虚拟机, --all参数列出所有虚拟机包括关机的,不加all则列出正在运行的。
root@hs01:~# virsh list --all
Id Name State
----------------------------------------------------
...
22 axtest99 running
- tacgui shut off
2、列出虚拟机axtest99的快照,可以看到有两个快照
root@hs01:~# virsh snapshot-list axtest99
Name Creation Time State
------------------------------------------------------------
axtest99-20190426 2019-04-26 12:10:36 +0100 running
init 2019-03-30 00:59:33 +0000 shutoff
3、查看虚拟机存储信息
root@hs01:~# virsh domblklist axtest99
Target Source
------------------------------------------------
hda /var/lib/libvirt/images/axtest99.qcow2
hdb -
4、快照只能用在qcow2存储格式,先检查一下
root@hs01:~# virsh dumpxml axtest99 | grep qemu
<driver name='qemu' type='qcow2'/>
<driver name='qemu' type='raw'/>
5、创建新快照,可以看到创建成功
root@hs01:~# virsh snapshot-create-as --domain axtest99 --name "test2" --description "This is test"
Domain snapshot test2 created
root@hs01:~# virsh snapshot-list axtest99
Name Creation Time State
------------------------------------------------------------
axtest99-20190426 2019-04-26 12:10:36 +0100 running
init 2019-03-30 00:59:33 +0000 shutoff
test2 2019-04-26 14:00:40 +0100 running
6、查看快照信息
root@hs01:~# virsh snapshot-info --domain axtest99 --snapshotname test2
Name: test2
Domain: axtest99
Current: yes
State: running
Location: internal
Parent: axtest99-20190426
Children: 0
Descendants: 0
Metadata: yes
7、恢复快照,先关机,再恢复
root@hs01:~# virsh shutdown --domain axtest99
Domain axtest99 is being shutdown
root@hs01:~# virsh snapshot-revert --domain axtest99 --snapshotname test2 --running
root@hs01:~# virsh list
Id Name State
----------------------------------------------------
...
23 axtest99 running
8、查看虚拟机快照占用磁盘空间,记得我们刚才用“virsh domblklist axtest99” 查看存储?
root@hs01:~# qemu-img info /var/lib/libvirt/images/axtest99.qcow2
image: /var/lib/libvirt/images/axtest99.qcow2
file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 12G
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 init 0 2019-03-30 00:59:33 00:00:00.000
2 axtest99-20190426 2.1G 2019-04-26 12:10:36 38:13:21.690
3 test2 2.9G 2019-04-26 14:00:40 40:03:16.887
Format specific information:
compat: 1.1
lazy refcounts: true
refcount bits: 16
corrupt: false
root@hs01:~#
9、删除快照
root@hs01:~# virsh snapshot-delete --domain axtest99 --snapshotname test2
Domain snapshot test2 deleted
root@hs01:~# virsh snapshot-list axtest99
Name Creation Time State
------------------------------------------------------------
axtest99-20190426 2019-04-26 12:10:36 +0100 running
init 2019-03-30 00:59:33 +0000 shutoff
root@hs01:~# qemu-img info /var/lib/libvirt/images/axtest99.qcow2
image: /var/lib/libvirt/images/axtest99.qcow2
file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 12G
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 init 0 2019-03-30 00:59:33 00:00:00.000
2 axtest99-20190426 2.1G 2019-04-26 12:10:36 38:13:21.690
Format specific information:
compat: 1.1
lazy refcounts: true
refcount bits: 16
corrupt: false
root@hs01:~#