---
title: "kvm虚拟化学习笔记(十)之kvm虚拟机快照备份"
author: "2002wmj"
date: "2015-03-24T17:26:15Z"
tags: []
categories: ["操作系统"]
slug: "kvm-kvm"
draft: false
lastmod: "2015-03-24T17:26:15Z"
id: 2195146
url: "https://2002wmj.iteye.com/blog/2195146"
---

- kvm虚拟机默认使用raw格式的镜像格式，性能最好，速度最快，它的缺点就是不支持一些新的功能，如支持镜像,zlib磁盘压缩,AES加密等。  
  要使用镜像功能，磁盘格式必须为qcow2。下面开始kvm虚拟机快照备份的过程。
  
  本文出自：http://koumm.blog.51cto.com
  
  进一步的学习参考：kvm+libvirt虚拟机快照浅析 http://itxx.sinaapp.com/blog/content/130
  
  ### 1. 查看现有磁盘镜像格式与转换
  
  (1) 查看磁盘格式
  
  \# qemu-img info test01.img
  
  raw格式需要转换成qcow2
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074607185.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074607747.png)
  
  (2) 关闭虚拟机并转换磁盘
  
  \# virsh shutdown oeltest01
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074611954.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074611135.png)
  
   
  
  (3) 转换磁盘格式
  
  \# qemu-img convert -f raw -O qcow2 test01.img test01.qcow2
  
  -f 源镜像的格式  
  \-O 目标镜像的格式
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074612959.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074611663.png)
  
  查看转换后的格式，已经转换成了qcow2, 这里是拷贝一份，并将格式转成qcow2
  
  \# qemu-img info test01.qcow2
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074612607.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074612705.png)
  
  ### 2. 修改虚拟机配置文件
  
  修改磁盘格式，与新qcow2格式的磁盘。
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074613112.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074613788.png)
  
  ### 3. 对虚拟机进行快照管理
  
  **(1) 对oeltest01虚拟机创建快照**
  
  也可以virsh snapshot-create as oeltest01 snap1 创建后个快照别名。
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074614216.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074614813.png)
  
  **(2) 查看虚拟机镜像快照的版本**
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074616756.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074616867.png)
  
  **(3) 查看当前虚拟机镜像快照的版本**
  
  可以看到为当前最新的快照版本。
  
  \[root@node1 data]#  
  \[root@node1 data]# virsh snapshot-current oeltest01
  
  [view source](http://www.it165.net/os/html/201309/6189.html#viewSource "view source")[print](http://www.it165.net/os/html/201309/6189.html#printSource "print")[?](http://www.it165.net/os/html/201309/6189.html#about "?")
  
  `01.<domainsnapshot>`
  
  `02.<name>1378579737</name>`
  
  `03.<state>shutoff</state>`
  
  `04.<creationTime>1378579737</creationTime>`
  
  `05.<memory` `snapshot='no'/>`
  
  `06.<disks>`
  
  `07.<disk` `name='hda'` `snapshot='internal'/>`
  
  `08.<disk` `name='hdc'` `snapshot='no'/>`
  
  `09.</disks>`
  
  `10.<domain` `type='kvm'>`
  
  `11.<name>oeltest01</name>`
  
  `12.<uuid>8f2bb4a7-c7ed-32aa-3676-9fb05923269d</uuid>`
  
  `13.<memory` `unit='KiB'>524288</memory>`
  
  `14.<currentMemory` `unit='KiB'>524288</currentMemory>`
  
  `15.<vcpu` `placement='static'>1</vcpu>`
  
  `16.<os>`
  
  `17.<type` `arch='x86_64'` `machine='rhel6.4.0'>hvm</type>`
  
  `18.<boot` `dev='hd'/>`
  
  `19.</os>`
  
  `20.<features>`
  
  `21.<acpi/>`
  
  `22.<apic/>`
  
  `23.<pae/>`
  
  `24.</features>`
  
  `25.<clock` `offset='localtime'/>`
  
  `26.<on_poweroff>destroy</on_poweroff>`
  
  `27.<on_reboot>restart</on_reboot>`
  
  `28.<on_crash>restart</on_crash>`
  
  `29.<devices>`
  
  `30.<emulator>/usr/libexec/qemu-kvm</emulator>`
  
  `31.<disk` `type='file'` `device='disk'>`
  
  `32.<driver` `name='qemu'` `type='qcow2'` `cache='none'/>`
  
  `33.<source` `file='/data/test01.qcow2'/>`
  
  `34.<target` `dev='hda'` `bus='ide'/>`
  
  `35.<address` `type='drive'` `controller='0'` `bus='0'` `target='0'` `unit='0'/>`
  
  `36.</disk>`
  
  `37.<disk` `type='block'` `device='cdrom'>`
  
  `38.<driver` `name='qemu'` `type='raw'/>`
  
  `39.<target` `dev='hdc'` `bus='ide'/>`
  
  `40.<readonly/>`
  
  `41.<address` `type='drive'` `controller='0'` `bus='1'` `target='0'` `unit='0'/>`
  
  `42.</disk>`
  
  `43.<controller` `type='ide'` `index='0'>`
  
  `44.<address` `type='pci'` `domain='0x0000'` `bus='0x00'` `slot='0x01'` `function='0x1'/>`
  
  `45.</controller>`
  
  `46.<controller` `type='usb'` `index='0'/>`
  
  `47.<interface` `type='bridge'>`
  
  `48.<mac` `address='52:54:00:82:39:01'/>`
  
  `49.<source` `bridge='br0'/>`
  
  `50.<model` `type='virtio'/>`
  
  `51.<address` `type='pci'` `domain='0x0000'` `bus='0x00'` `slot='0x03'` `function='0x0'/>`
  
  `52.</interface>`
  
  `53.<serial` `type='pty'>`
  
  `54.<target` `port='0'/>`
  
  `55.</serial>`
  
  `56.<console` `type='pty'>`
  
  `57.<target` `type='serial'` `port='0'/>`
  
  `58.</console>`
  
  `59.<input` `type='tablet'` `bus='usb'/>`
  
  `60.<input` `type='mouse'` `bus='ps2'/>`
  
  `61.<graphics` `type='vnc'` `port='5910'` `autoport='no'` `listen='0.0.0.0'>`
  
  `62.<listen` `type='address'` `address='0.0.0.0'/>`
  
  `63.</graphics>`
  
  `64.<video>`
  
  `65.<model` `type='cirrus'` `vram='9216'` `heads='1'/>`
  
  `66.<address` `type='pci'` `domain='0x0000'` `bus='0x00'` `slot='0x02'` `function='0x0'/>`
  
  `67.</video>`
  
  `68.<memballoon` `model='virtio'>`
  
  `69.<address` `type='pci'` `domain='0x0000'` `bus='0x00'` `slot='0x04'` `function='0x0'/>`
  
  `70.</memballoon>`
  
  `71.</devices>`
  
  `72.</domain>`
  
  `73.</domainsnapshot>`
  
  \[root@node1 data]#
  
  **(4) 查看当前虚拟机镜像文件**
  
  又创建了一个，快照的版本也记录在镜像文件中了。
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074617157.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074617753.png)
  
  **快照配置文件在/var/lib/libvirt/qemu/snapshot/虚拟机名称/下**
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074619616.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074618939.png)
  
  ### 4. 恢复虚拟机快照
  
  **(1) 恢复虚拟机快照必须关闭虚拟机。**
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074620166.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074619755.png)
  
  确认虚拟机是关机状态
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074621898.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074621975.png)
  
  **(2) 确认需要恢复的快照时间，这里恢复到1378579737**
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074622130.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074621606.png)
  
  **(3) 执行恢复，并确认恢复版本**
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074623135.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074622413.png)
  
  ### 5. 删除虚拟机快照
  
  **(1) 查看虚拟机快照**
  
  \# qemu-img info test01.qcow2
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074625791.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074624206.png)
  
  这里删除第一个快照1378579737
  
  **(2) 删除快照**
  
  [![image](http://www.it165.net/uploadfile/2013/0908/20130908074626741.png "image")](http://www.it165.net/uploadfile/2013/0908/20130908074625664.png)
  
  到此kvm虚拟机快照测试完毕。kvm虚拟化学习笔记进行到这里了，感觉到kvm虚拟化的内容真的很多。水是越来越深了。