安装完kvm虚拟机后,对虚拟机做快照(关闭虚拟机):


kvm快照,分两种:

1种lvm快照,如果分区是lvm,可以利用lvm进行kvm的快照备份

2种由于raw格式不支持镜像,所以需要将格式转换为qcow2才可以创建快照。

方法2具体操作如下:

kvm虚拟机默认使用raw格式的镜像格式,性能最好,速度最快,它的缺点就是不支持一些新的功能,如支持镜像,zlib磁盘压缩,AES加密等。   
要使用镜像功能,磁盘格式必须为qcow2。下面开始kvm虚拟机快照备份的过程。


查看磁盘格式

[root@localhost p_w_picpaths]# qemu-img infotest-script.img

raw格式需要转换成qcow2

转换磁盘格式

[root@localhost p_w_picpaths]# qemu-img convert -fraw -O qcow2 test-script.img test-script.qcow2

-f  源镜像的格式   
-O 目标镜像的格式

在使用raw格式的虚拟机镜像文件,然后又转换为qcow2格式后,需要修改原来的配置文件,才能使用虚拟机。如果之前使用的就是qcow2格式的虚拟机镜像文件不需要更改配置文件。

修改虚拟机配置文件:

修改磁盘格式,与新qcow2格式的磁盘。

[root@localhost p_w_picpaths]# virsh edit test-script
<disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/vhost/ws2012_re.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</disk>


对虚拟机进行快照管理 

对oeltest01虚拟机创建快照

也可以virsh snapshot-create-as oeltest01 snap1 创建后个快照别名。

[root@localhost p_w_picpaths]# virsh snapshot-create test-script

可以看到为当前最新的快照版本。

[root@localhost p_w_picpaths]#virsh snapshot-current test-script

查看当前虚拟机镜像文件

[root@localhost p_w_picpaths]# qemu-img info test-script.qcow2

会出现快照名字也会在里面。

快照配置文件在/var/lib/libvirt/qemu/snapshot/虚拟机名称下


恢复虚拟机快照

恢复虚拟机快照必须关闭虚拟机。

[root@localhost test-script]#virsh domstate test-script


确认需要恢复的快照时间,这里恢复到1419995560

[root@localhost test-script]# virsh snapshot-list test-script


执行恢复,并确认恢复版本

[root@localhost test-script]# virsh snapshot-revert test-script 1419995560
[root@localhost test-script]# virsh snapshot-current test-script


常用virsh指令:

1)virsh list 列出当前虚拟机列表,不包括未启动的
2)virsh list --all 列出所有虚拟机,包括所有已经定义的虚拟机
3)virsh destroy vm-name 关闭虚拟机
4)virsh start vm-name 启动虚拟机
5)virsh edit vm-name 编辑虚拟机xml文件
6)virsh undefine vm-name 删除虚拟机
7)virsh shutdown vm-name 停止虚拟机
8)virsh reboot vm-name 重启虚拟机
9)virsh autostart vm-name 虚拟机随宿主机启动


转载于:https://blog.51cto.com/ting2junshui/1841464