使用virsh命令创建KVM虚拟机快照

查看虚拟机所在主机和虚拟机名称:
[root@node-1 ~]# nova show a88dcf5d-c8b2-46a5-af27-a176d8235c9d|grep hyper
| OS-EXT-SRV-ATTR:hypervisor_hostname  | node-5.evecom.net                                             |
[root@node-1 ~]# nova show a88dcf5d-c8b2-46a5-af27-a176d8235c9d|grep instance_name
| OS-EXT-SRV-ATTR:instance_name        | instance-000006b7                                             |
[root@node-1 ~]#
[root@node-1 ~]# ssh 168.5.23.8

查看虚拟机的磁盘挂载情况:
[root@node-5 ~]# virsh domblklist instance-000006b7 --details
Type       Device     Target     Source
------------------------------------------------
file       disk       vda        /var/lib/nova/instances/a88dcf5d-c8b2-46a5-af27-a176d8235c9d/disk

如果是挂载了Cinder卷的虚拟机:
[root@node-5 ~]# virsh domblklist instance-000006a8 --details
Type       Device     Target     Source
------------------------------------------------
file       disk       vda        /var/lib/nova/instances/406e6d64-9ccf-44a5-a0d4-027ee4de3d24/disk
file       disk       vdb        /var/lib/nova/mnt/348597c68c44fd4c679cd81c3989af6f/volume-b827d6fb-cb61-4b40-97e5-4574f9877abb

对磁盘做快照:
[root@node-5 ~]# virsh snapshot-create-as instance-000006b7 snapshot1 "just for test snapshot" --disk-only --atomic
Domain snapshot snapshot1 created
[root@node-5 ~]#

观察到虚拟机创建快照后,磁盘引用路径已经指向快照文件:
[root@node-5 ~]# virsh domblklist instance-000006b7 --details
Type       Device     Target     Source
------------------------------------------------
file       disk       vda        /var/lib/nova/instances/a88dcf5d-c8b2-46a5-af27-a176d8235c9d/disk.snapshot1

qemu-img命令可以看出快照文件的源文件:
[root@node-5 ~]# qemu-img info /var/lib/nova/instances/a88dcf5d-c8b2-46a5-af27-a176d8235c9d/disk.snapshot1
image: /var/lib/nova/instances/a88dcf5d-c8b2-46a5-af27-a176d8235c9d/disk.snapshot1
file format: qcow2
virtual size: 30G (32212254720 bytes)
disk size: 384K
cluster_size: 65536
backing file: /var/lib/nova/instances/a88dcf5d-c8b2-46a5-af27-a176d8235c9d/disk
backing file format: qcow2
Format specific information:
    compat: 1.1
    lazy refcounts: false

查看快照文件大小:
[root@node-5 ~]# du -ms /var/lib/nova/instances/a88dcf5d-c8b2-46a5-af27-a176d8235c9d/disk.snapshot1
1       /var/lib/nova/instances/a88dcf5d-c8b2-46a5-af27-a176d8235c9d/disk.snapshot1

此时可对源文件进行备份。

登录虚拟机,设置不同颜色的桌面背景以示区别,再正常关闭虚拟机,并使用“nova start <instance_id>”命令重新启动虚拟机:
[root@node-5 ~]# virsh domuuid instance-000006b7
a88dcf5d-c8b2-46a5-af27-a176d8235c9d

[root@node-5 ~]# exit
logout
Connection to 168.5.23.8 closed.
[root@node-1 ~]# nova get-vnc-console a88dcf5d-c8b2-46a5-af27-a176d8235c9d novnc
+-------+---------------------------------------------------------------------------------+
| Type  | Url                                                                             |
+-------+---------------------------------------------------------------------------------+
| novnc | http://168.5.24.2:6080/vnc_auto.html?token=9161bc2d-6d2c-4825-801c-51b50e2862bf |
+-------+---------------------------------------------------------------------------------+
[root@node-1 ~]# nova start a88dcf5d-c8b2-46a5-af27-a176d8235c9d

经“nova start <instance_id>”命令启动虚拟机,虚拟机的磁盘又引用到了初始的磁盘文件:
[root@node-5 ~]# virsh domblklist instance-000006b7 --details                   Type       Device     Target     Source
------------------------------------------------
file       disk       vda        /var/lib/nova/instances/a88dcf5d-c8b2-46a5-af27-a176d8235c9d/disk

删除快照:
[root@node-5 ~]# virsh snapshot-delete instance-000006b7 --metadata snapshot1
[root@node-5 ~]# virsh snapshot-list instance-000006b7

快照文件仍残留在磁盘,可手动删除:
[root@node-5 ~]# du -ms /var/lib/nova/instances/a88dcf5d-c8b2-46a5-af27-a176d8235c9d/*
0       /var/lib/nova/instances/a88dcf5d-c8b2-46a5-af27-a176d8235c9d/console.log
49      /var/lib/nova/instances/a88dcf5d-c8b2-46a5-af27-a176d8235c9d/disk
1       /var/lib/nova/instances/a88dcf5d-c8b2-46a5-af27-a176d8235c9d/disk.info
28      /var/lib/nova/instances/a88dcf5d-c8b2-46a5-af27-a176d8235c9d/disk.snapshot1
1       /var/lib/nova/instances/a88dcf5d-c8b2-46a5-af27-a176d8235c9d/libvirt.xml

快照的升级版使用参考:
https://kashyapc.fedorapeople.org/virt/lc-2012/snapshots-handout.html

学习了以上链接后,了解到QEMU的快照还分成内部快照和外部快照。上文创建的是外部快照,现在尝试创建内部快照,内部快照能够保存内存状态。

[root@node-1 ~]# ssh 168.5.23.7
root@168.5.23.7's password:
Last login: Tue Mar 29 11:06:48 2016 from node-1.evecom.net
[root@node-4 ~]# virsh snapshot-create-as instance-000006ba snapshot1 "Just For Test"
Domain snapshot snapshot1 created
(SUSE官网的文章是:virsh snapshot-create-as --domain admin_server --name "Snapshot 2" --description "First live snapshot" --live)

内部快照创建完后,并没有在原来的磁盘目录生成快照文件:
[root@node-4 ~]# ls -l /var/lib/nova/instances/31b56c19-5789-4407-b350-e83b043f1165
total 471177
-rw-rw---- 1 root root         0 Mar 29 11:05 console.log
-rw-r--r-- 1 root root 482476032 Mar 29  2016 disk
-rw-r--r-- 1 nova nova        79 Mar 29 11:05 disk.info
-rw-r--r-- 1 nova nova      2595 Mar 29 11:05 libvirt.xml
[root@node-4 ~]#

也没有改变磁盘文件的引用:
[root@node-4 ~]# virsh domblklist instance-000006ba
Target     Source
------------------------------------------------
vda        /var/lib/nova/instances/31b56c19-5789-4407-b350-e83b043f1165/disk

[root@node-4 ~]#

查看快照清单:
[root@node-4 ~]# virsh snapshot-list instance-000006ba
 Name                 Creation Time             State
------------------------------------------------------------
 snapshot1            2016-03-29 11:22:23 +0800 running

修改虚拟机的桌面背景颜色,然后还原快照:
[root@node-4 ~]# virsh snapshot-revert --domain instance-000006ba snapshot1
桌面背景色的修改随着还原快照被还原。

删除快照:
[root@node-4 ~]# virsh snapshot-delete --domain instance-000006ba snapshot1
Domain snapshot snapshot1 deleted
[root@node-4 ~]# virsh snapshot-list instance-000006ba
 Name                 Creation Time             State
------------------------------------------------------------

[root@node-4 ~]#

官网又一文档,展示了使用blockcommit合并快照:
http://wiki.libvirt.org/page/Live-merge-an-entire-disk-image-chain-including-current-active-disk
文章太多,很乱,需要系统梳理。因时间有限,暂不考虑续写。

上一篇:Java应用中使用ShutdownHook友好地清理现场(转)


下一篇:YUI Compressor