KVM 虚拟化技术(八) 之快照

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

##查看磁盘格式

[root@kvm images]# qemu-img info vm1.img 
image: vm1.img
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 1.4G

转换格式

[root@kvm images]# qemu-img convert -f raw -O qcow2 vm1.img vm1.qcow2
root@kvm images]# ls
vm1.img  vm1.qcow2  vm2.img
[root@kvm images]# qemu-img info vm1.qcow2 
image: vm1.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.2G
cluster_size: 65536

修改配置文件

[root@kvm images]# virsh edit vm1

修改

<disk type='file' device='disk'>
    <driver name='qemu' type='qcow2' cache='none'/>
    <source file='/home/kvm/images/vm1.qcow2'/>
    <target dev='hda' bus='ide'/>
    <address type='drive' controller='0' bus='0' target='0' unit='0'/>
  </disk>

创建快照

与快照相关的命令

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

开始

[root@kvm images]# virsh snapshot-create vm1
Domain snapshot 1489950406 created
[root@kvm images]# virsh snapshot-list vm1
 Name                 Creation Time             State
------------------------------------------------------------
 1489950406           2017-03-20 03:06:46 +0800 shutoff

查看快照

[root@kvm images]# virsh snapshot-current vm1
<domainsnapshot>
  <name>1489950406</name>
  <state>shutoff</state>
  <creationTime>1489950406</creationTime>
  <memory snapshot='no'/>
  <disks>
    <disk name='hda' snapshot='internal'/>
    <disk name='hdc' snapshot='no'/>
  </disks>
  <domain type='kvm'>
    <name>vm1</name>
    <uuid>686c7566-e44d-6ab2-e98f-246e012fa39f</uuid>
    <memory unit='KiB'>1048576</memory>
    <currentMemory unit='KiB'>1048576</currentMemory>
    <vcpu placement='static'>2</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='/home/kvm/images/vm1.qcow2'/>
        <target dev='hda' bus='ide'/>
        <address type='drive' controller='0' bus='0' target='0' unit='0'/>
      </disk>
      <disk type='block' device='cdrom'>
        <driver name='qemu' type='raw'/>
        <target dev='hdc' bus='ide'/>
        <readonly/>
        <address type='drive' controller='0' bus='1' target='0' unit='0'/>
      </disk>
      <controller type='usb' index='0' model='ich9-ehci1'>
        <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
      </controller>
      <controller type='usb' index='0' model='ich9-uhci1'>
        <master startport='0'/>
        <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
      </controller>
      <controller type='usb' index='0' model='ich9-uhci2'>
        <master startport='2'/>
        <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
      </controller>
      <controller type='usb' index='0' model='ich9-uhci3'>
        <master startport='4'/>
        <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
      </controller>
      <controller type='ide' index='0'>
        <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
      </controller>
      <interface type='bridge'>
        <mac address='52:54:00:3a:37:22'/>
        <source bridge='br0'/>
        <model type='virtio'/>
        <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
      </interface>
      <serial type='pty'>
        <target port='0'/>
      </serial>
      <console type='pty'>
        <target type='serial' port='0'/>
      </console>
      <input type='mouse' bus='ps2'/>
      <graphics type='vnc' port='5910' autoport='no'/>
      <video>
        <model type='cirrus' vram='9216' heads='1'/>
        <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
      </video>
      <memballoon model='virtio'>
        <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
      </memballoon>
    </devices>
  </domain>
</domainsnapshot>

快照文件在

[root@kvm images]# cd /var/lib/libvirt/qemu/snapshot/vm1/1489950406.xml 

恢复快照

查看快照

[root@kvm ~]# virsh snapshot-list vm1
 Name                 Creation Time             State
------------------------------------------------------------
 1489950406           2017-03-20 03:06:46 +0800 shutoff

[root@kvm ~]# virsh snapshot-create vm1
Domain snapshot 1489951085 created
[root@kvm ~]# virsh snapshot-list vm1  
 Name                 Creation Time             State
------------------------------------------------------------
 1489950406           2017-03-20 03:06:46 +0800 shutoff
 1489951085           2017-03-20 03:18:05 +0800 shutoff

恢复快照是通过快照的对应id来恢复的。比如我们希望将虚拟机恢复至 2017-03-20 03:06:46,则我们需要知道对应的name是1489950406

使用snapshot-revert 参数 跟上虚拟机名称 后面跟上name

[root@kvm ~]# virsh snapshot-revert vm1 1489950406

[root@kvm ~]# virsh snapshot-list vm1             
 Name                 Creation Time             State
------------------------------------------------------------
 1489950406           2017-03-20 03:06:46 +0800 shutoff
 1489951085           2017-03-20 03:18:05 +0800 shutoff

[root@kvm ~]# virsh snapshot-current vm1
<domainsnapshot>
  <name>1489950406</name>
  <state>shutoff</state>
  <creationTime>1489950406</creationTime>
  <memory snapshot='no'/>
  <disks>
    <disk name='hda' snapshot='internal'/>
    <disk name='hdc' snapshot='no'/>
  </disks>
  <domain type='kvm'>

删除快照

[root@kvm ~]# virsh  snapshot-list vm1    
 Name                 Creation Time             State
------------------------------------------------------------
 1489950406           2017-03-20 03:06:46 +0800 shutoff
 1489951085           2017-03-20 03:18:05 +0800 shutoff

[root@kvm ~]# virsh  snapshot-delete vm1 1489951085
Domain snapshot 1489951085 deleted

[root@kvm ~]# virsh  snapshot-list vm1             
 Name                 Creation Time             State
------------------------------------------------------------
 1489950406           2017-03-20 03:06:46 +0800 shutoff

故障处理

[root@kvm ~]# virsh shutdown vm1


error: Failed to shutdown domain vm1
error: Timed out during operation: cannot acquire state change lock

[root@kvm ~]# 
[root@kvm ~]# 
[root@kvm ~]# virsh list --all  
 Id    Name                           State
----------------------------------------------------
 14    vm2                            running
 15    vm1                            paused

[root@kvm ~]# virsh destroy vm1
Domain vm1 destroyed

[root@kvm ~]# virsh list --all 
 Id    Name                           State
----------------------------------------------------
 14    vm2                            running
 -     vm1                            shut off