在我们使用KVM虚拟机使用过程中,为保留当前系统配置或进行当前系统数据备份等,我们会使用到快照功能,以便出异常以后我们进行快照恢复。这点也是非常重要的。
要使用镜像功能,磁盘格式必须为qcow2。下面开始进行kvm虚拟机快照备份前的操作以及快照管理过程。
1,查看KVM虚拟机磁盘格式以及进行磁盘格式转换
1.1 查看KVM虚拟机磁盘格式
使用如下方法查看 qemu-img info linux66.img
[root@21yunwei vps]# qemu-img info linuxt66.img image: linuxt66.img file format: raw virtual size: 8.0G (8589934592 bytes) disk size: 8.0G<span style="font-family: 'microsoft yahei'; font-size: 14px; line-height: 24px; background-color: #ffffff;">1</span>
由于快照要使用,磁盘格式必须为qcow2,需要进行磁盘格式转换。
1.2 关闭虚拟机并转换磁盘
[root@21yunwei vps]# virsh destroy linux66 Domain linux66 destroyed [root@21yunwei vps]# virsh list --all Id Name State ---------------------------------------------------- 2 win2003 running 6 linux65 running - linux66 shut off [root@21yunwei vps]# ll total 11468860 -rwxr-xr-x 1 qemu qemu 8589934592 May 11 19:22 linuxt65.img -rwxr-xr-x 1 root root 8589934592 May 11 19:21 linuxt66.img -rwxr-xr-x 1 qemu qemu 10737418240 May 11 19:23 win2003.img [root@21yunwei vps]# qemu-img convert -f raw -O qcow2 linuxt66.img linuxt66.qcow2 [root@21yunwei vps]# [root@21yunwei vps]# ll total 12711880 -rwxr-xr-x 1 qemu qemu 8589934592 May 11 19:25 linuxt65.img -rwxr-xr-x 1 root root 8589934592 May 11 19:21 linuxt66.img -rw-r--r-- 1 root root 1272971264 May 11 19:24 linuxt66.qcow2 -rwxr-xr-x 1 qemu qemu 10737418240 May 11 19:25 win2003.img
转换完成。注意,这里的转换时针对源文件拷贝一份,并将格式转成qcow2。
1.3 修改配置文件,启用新格式磁盘。
virsh edit linux 进去搜索img 找到以后替换文件格式名:
<disk type=’file’ device=’disk’>
<driver name=’qemu’ type=’qcow2′ cache=’none’/>
<source file=’/home/vps/linuxt66.qcow2’/>
2,管理快照以及快照恢复
首先我们先查看下该快照命令用到了哪些:
[root@21yunwei ~]# virsh --help | grep snap 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
2.1 创建快照
根据上述命令,我们打算使用snapshot-cteate 来对linux66主机创建快照
[root@21yunwei vps]# virsh snapshot-create linux66 Domain snapshot 1463020507 created
查看快照列表
[root@21yunwei vps]# virsh snapshot-list linux66 Name Creation Time State ------------------------------------------------------------ 1463020507 2016-05-11 19:35:07 -0700 shutoff 1463020969 2016-05-11 19:42:49 -0700 shutoff
其中快照配置文件放到了/var/lib/libvirt/qemu/snapshot/
查看 最近的快照
2.2 恢复快照
恢复快照之前必须先关闭虚拟机,我们可以先关闭以后并查看下快照列表,选中要恢复哪个快照。
[root@21yunwei snapshot]# virsh list --all Id Name State ---------------------------------------------------- 2 win2003 running 6 linux65 running - linux66 shut off [root@21yunwei snapshot]# virsh snapshot-list linux66 Name Creation Time State ------------------------------------------------------------ 1463020507 2016-05-11 19:35:07 -0700 shutoff 1463020969 2016-05-11 19:42:49 -0700 shutoff
比如我们这里恢复 1463020969这个快照。操作如下:
[root@21yunwei snapshot]# virsh snapshot-revert linux66 1463020969 [root@21yunwei snapshot]# virsh snapshot-current linux66 1463020969 shutoff 1463020507
恢复完成。
2.3快照删除
[root@21yunwei vps]# virsh snapshot-list linux66 Name Creation Time State ------------------------------------------------------------ 1463020507 2016-05-11 19:35:07 -0700 shutoff 1463020969 2016-05-11 19:42:49 -0700 shutoff [root@21yunwei vps]# virsh snapshot-delete linux66 1463020969 Domain snapshot 1463020969 deleted [root@21yunwei vps]# virsh snapshot-list linux66 Name Creation Time State ------------------------------------------------------------ 1463020507 2016-05-11 19:35:07 -0700 shutoff
本文KVM克隆操作得以顺利实现,主要操作参考了如下资料,这里做下资料参考以便后边的朋友可以自行查看。
阿铭KVM克隆:https://www.apelearn.com/bbs/thread-8299-1-1.html
koumm的BLOG:http://koumm.blog.51cto.com/703525/1291893
对于KVM主机创建和平时日常管理,可以查看本站之前的文章:《linux系统下kvm创建虚拟机以及两款虚拟机win2003/centos系统安装》和《kvm虚拟机日常管理常用命令和配置说明以及搭建注意事项总结》