KVM虚拟机快照管理

来自CloudWiki
跳转至: 导航搜索

简介

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

查看现有磁盘镜像格式与转换

查看磁盘格式

[root@victory ~]# ls  /var/lib/libvirt/images
demo.img
[root@victory ~]# qemu-img info /var/lib/libvirt/images/demo.img
image: /var/lib/libvirt/images/demo.img
file format: raw
virtual size: 8.0G (8589934592 bytes)
disk size: 8.0G

关闭虚拟机并转换磁盘

[root@centos2 ~]# yum -y install acpid ##安装电源服务

Acpid是一个用户空间的服务进程,它充当linux内核与应用程序之间通信的接口,负责将kernel中的电源管理事件转发给应用程序

查看运行的虚拟机

[root@victory ~]# virsh list
 Id    Name                           State
----------------------------------------------------

查看所有的虚拟机

[root@victory ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     demo                           shut off


[root@kvm ~]# virsh shutdown demo

Domain demo is being shutdown

转换磁盘格式

[root@KVM ~]# qemu-img convert -f raw -O qcow2 /var/lib/libvirt/images/demo.img /var/lib/libvirt/images/demo.qcow2

查看转换后磁盘大小:

[root@victory ~]# du -sh /var/lib/libvirt/images/demo.qcow2

2.1G    /var/lib/libvirt/images/demo.qcow2


查看转换后的格式,已经转换成了qcow2,这里拷贝一份,并将格式转成qcow2。

[root@kvm-max ~]# qemu-img info /var/lib/libvirt/images/demo.qcow2

image: /var/lib/libvirt/images/demo.qcow2
file format: qcow2
virtual size: 8.0G (8589934592 bytes)
disk size: 2.1G
cluster_size: 65536

修改虚拟机配置文件

修改磁盘格式

virsh edit demo

# 
<domain type='kvm'>
  <name>test</name>
  <uuid>ee2b4c8e-5263-fc20-7296-1dc7d1b8bad6</uuid>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='x86_64' machine='rhel6.6.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='none'/>
      <source file='/var/lib/libvirt/images/demo.qcow2'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>

对虚拟机进行快照管理

创建test虚拟机快照

[root@victory ~]# virsh snapshot-create-as demo demo_snaphshot

Domain snapshot demo_snaphshot created


查看虚拟机镜像快照的版本

[root@kvm-max ~]# virsh snapshot-list demo

 Name                 Creation Time             State
------------------------------------------------------------
 demo_snaphshot       2019-02-25 16:46:27 +0800 shutoff

查看当前虚拟机镜像快照的版本

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

[root@kvm-max ~]# virsh snapshot-current demo

<domainsnapshot>
  <name>test_snaphshot</name>
  <state>shutoff</state>
  <creationTime>1502894424</creationTime>
  <memory snapshot='no'/>
  <disks>
    <disk name='hda' snapshot='internal'/>
    <disk name='hdc' snapshot='no'/>
  </disks>

查看快照

[root@kvm-max ~]# virsh snapshot-info demo demo_snaphshot

Name:           demo_snaphshot
Domain:         demo
Current:        yes
State:          shutoff
Location:       internal
Parent:         -
Children:       0
Descendants:    0
Metadata:       yes


恢复虚拟机快照

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

[root@KVM ~]# virsh shutdown demo

确认虚拟机是关机状态。

[root@kvm-max ~]# virsh domstate demo

shut off

确认需要恢复的快照时间,恢复到demo_snapshot

[root@kvm-max ~]# virsh snapshot-list demo

 Name                 Creation Time             State
------------------------------------------------------------
 demo_snaphshot       2019-02-25 16:46:27 +0800 shutoff

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

[root@kvm-max ~]# virsh snapshot-revert demo demo_snaphshot

[root@kvm-max ~]# virsh list –all

 Id    Name                           State
----------------------------------------------------
 -     demo                           shut off

[root@kvm-max ~]# virsh snapshot-current demo
<domainsnapshot>
  <name>demo_snaphshot</name>
  <state>shutoff</state>
  <creationTime>1551084387</creationTime>
  <memory snapshot='no'/>
  <disks>
    <disk name='hda' snapshot='internal'/>
    <disk name='hdc' snapshot='no'/>
  </disks>
  <domain type='kvm'>
    <name>demo</name>
    <uuid>6880e3f0-a4e0-c3f8-01c9-0eef6692d4a1</uuid>
    <memory unit='KiB'>1048576</memory>

删除虚拟机快照

查看虚拟机快照

[root@kvm-max ~]# qemu-img info /var/lib/libvirt/images/demo.qcow2 
image: /var/lib/libvirt/images/demo.qcow2
file format: qcow2
virtual size: 8.0G (8589934592 bytes)
disk size: 2.1G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         demo_snaphshot            0 2019-02-25 16:46:27   00:00:00.000

删除快照

[root@kvm-max ~]# virsh snapshot-delete test test_snaphshot
Domain snapshot demo_snaphshot deleted

[root@kvm-max ~]# qemu-img info /var/lib/libvirt/images/demo.qcow2 
image: /var/lib/libvirt/images/demo.qcow2
file format: qcow2
virtual size: 8.0G (8589934592 bytes)
disk size: 2.1G
cluster_size: 65536