1. 程式人生 > >KVM虛擬機快照

KVM虛擬機快照

磁盤格式 main format 加密 emulator 其他 類型 lB gin

快照應該是所有虛擬化技術都不可或缺的一個部分吧,無論是VMware、Citrix還是開源的如KVM、Xen等,都提供了快照技術。快照技術提供了虛擬機某一時間點的映像,方便在其他時間我們可以回溯到該時間點,也給數據備份和恢復提供了便利。


下面說說KVM虛擬化的快照功能:

1.快照實際上做的是虛擬機的XML配置文件,默認快照XML文件在/var/lib/libvirt/qemu/snapshot/[vmachine_name]/下

2.快照只支持qcow2磁盤格式的系統


KVM虛擬化快照的實現方式:

kvm快照有兩種方式創建,分別是:

  virsh snapshot 相關命令進行生成快照

qemu-img snapshot 命令進行生成快照


使用virsh進行KVM虛擬機快照相關命令:

iface-begin 創建一個當前網卡的快照,它可以用來稍後提交或者恢復

snapshot-create 創建一個快照

snapshot-create-as 使用一組參數創建一個快照

snapshot-current 取得當前快照的集合

snapshot-delete 刪除一個快照

snapshot-dumpxml 從一個虛擬機的快照導出xml文件

snapshot-edit 編輯一個快照的xml文件

snapshot-info 快照信息

snapshot-list 列出虛擬機的快照列表

snapshot-parent 獲取一個快照的父快照名稱

snapshot-revert 將虛擬機恢復到一個快照


使用virsh管理虛擬機快照:

kvm虛擬機默認使用raw格式的鏡像格式,性能最好,速度最快,它的缺點就是不支持一些新的功能,如支持鏡像,zlib磁盤壓縮,AES加密等。要使用鏡像功能,磁盤格式必須為qcow2。

1、磁盤格式轉換

查看磁盤映像格式:

[root@kvm-node1 ~]# qemu-img info /images/kvm/centos6.img 
image: /images/kvm/centos6.img
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 1.5G

關閉虛擬機:

[root@kvm-node1 ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 1     centos6.5                      running
[root@kvm-node1 ~]# virsh destroy centos6.5
Domain centos6.5 destroyed
[root@kvm-node1 ~]# virsh list
 Id    Name                           State
----------------------------------------------------

將虛擬機磁盤映像轉換為qcow2格式:

[root@kvm-node1 ~]# qemu-img convert -f raw -O qcow2 /images/kvm/centos6.img /images/kvm/centos6.qcow2
[root@kvm-node1 ~]# qemu-img info /images/kvm/centos6.qcow2 
image: /images/kvm/centos6.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.3G
cluster_size: 65536
[root@kvm-node1 ~]# ll /images/kvm/centos6*
-rwxr-xr-x 1 root root 10737418240 Jul  1 20:13 /images/kvm/centos6.img
-rw-r--r-- 1 root root  1394081792 Jul  1 20:17 /images/kvm/centos6.qcow2

使用qemu-img轉換鏡像只是對原有的鏡像做了備份,原有的磁盤映像文件還在。

2、修改虛擬機xml配置文件

修改虛擬機的xml配置文件,主要是把虛擬機的磁盤映像文件修改為我們轉換後的qcow2格式,然後啟動虛擬機,才能執行快照操作。

[root@kvm-node1 ~]# virsh edit centos6.5
Domain centos6.5 XML configuration not changed.
[root@kvm-node1 ~]# awk -F"'" '/<driver name(.*)cache=/{print $4}' /etc/libvirt/qemu/centos6.5.xml      #獲取磁盤映像的類型,原來是raw,已經修改為qcow2
qcow2
[root@kvm-node1 ~]# awk -F"'" '/<source file/{print $2}' /etc/libvirt/qemu/centos6.5.xml      #獲取磁盤映像文件路徑,原來是centos6.img,這裏已經修改為centos6.qcow2
/images/kvm/centos6.qcow2

3、啟動虛擬機

[root@kvm-node1 ~]# virsh start centos6.5
Domain centos6.5 started

4、使用virsh命令創建快照

[root@kvm-node1 ~]# virsh snapshot-create centos6.5
Domain snapshot 1530448868 created

5、查看快照

[root@kvm-node1 ~]# virsh snapshot-list centos6.5
 Name                 Creation Time             State
------------------------------------------------------------
 1530448868           2018-07-01 20:41:08 +0800 running
[root@kvm-node1 ~]# virsh snapshot-info --current centos6.5
Name:           1530448868
Domain:         centos6.5
Current:        yes
State:          running
Location:       internal
Parent:         -
Children:       0
Descendants:    0
Metadata:       yes

6、查看當前虛擬機快照的版本

[root@kvm-node1 ~]# virsh snapshot-current centos6.5
<domainsnapshot>
  <name>1530448868</name>
  <state>running</state>
  <creationTime>1530448868</creationTime>
  <memory snapshot='internal'/>
  <disks>
    <disk name='vda' snapshot='internal'/>
    <disk name='hdc' snapshot='no'/>
  </disks>
  <domain type='kvm'>
    <name>centos6.5</name>
    <uuid>be73fe50-fb80-d468-9e8a-e542f413d735</uuid>
    <memory unit='KiB'>1024000</memory>
    <currentMemory unit='KiB'>524288</currentMemory>
    <vcpu placement='static' current='1'>4</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='/images/kvm/centos6.qcow2'/>
        <target dev='vda' bus='virtio'/>
        <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
      </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:26:5a:8d'/>
        <source bridge='virbr0'/>
        <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='tablet' bus='usb'/>
      <input type='mouse' bus='ps2'/>
      <graphics type='vnc' port='5900' autoport='no' listen='0.0.0.0'>
        <listen type='address' address='0.0.0.0'/>
      </graphics>
      <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='0x06' function='0x0'/>
      </memballoon>
    </devices>
  </domain>
</domainsnapshot>

7、查看快照配置文件所在目錄

[root@kvm-node1 ~]# ll /var/lib/libvirt/qemu/snapshot/
total 4
drwxr-xr-x 2 root root 4096 Jul  1 20:41 centos6.5
[root@kvm-node1 ~]# ll /var/lib/libvirt/qemu/snapshot/centos6.5/
total 4
-rw------- 1 root root 3492 Jul  1 20:41 1530448868.xml

8、刪除一個快照

[root@kvm-node1 ~]# virsh snapshot-list centos6.5
 Name                 Creation Time             State
------------------------------------------------------------
 1530448868           2018-07-01 20:41:08 +0800 running
[root@kvm-node1 ~]# virsh snapshot-delete centos6.5 1530448868
Domain snapshot 1530448868 deleted
[root@kvm-node1 ~]# virsh snapshot-list centos6.5
 Name                 Creation Time             State
------------------------------------------------------------

9、恢復一個快照

1)首先,我們在centos6.5上創建一個文件,例如:

echo "This is a SNAPSHOT test." > /root/testfile

技術分享圖片

2)使用virsh snapshot-create-as創建一個快照

[root@kvm-node1 ~]# virsh snapshot-create-as --domain centos6.5 --name "centos6.5_snapshot_01" --description "snapshot with testfile saved."
Domain snapshot centos6.5_snapshot_01 created

3)查看當前虛擬機的快照列表

[root@kvm-node1 ~]# virsh snapshot-list centos6.5
 Name                 Creation Time             State
------------------------------------------------------------
 centos6.5_snapshot_01 2018-07-01 21:43:54 +0800 running

4)查看當前虛擬機的快照信息

[root@kvm-node1 ~]# virsh snapshot-info centos6.5 --current
Name:           centos6.5_snapshot_01
Domain:         centos6.5
Current:        yes
State:          running
Location:       internal
Parent:         -
Children:       0
Descendants:    0
Metadata:       yes

5)在虛擬機上刪除testfile

技術分享圖片

6)使用virsh snapshot-revert恢復虛擬機快照

[root@kvm-node1 ~]# virsh snapshot-revert centos6.5 centos6.5_snapshot_01 --running

snapshot-revert用法:snapshot-revert <domain> [<snapshotname>] [--current] [--running] [--paused] [--force]

選項:

[--domain] <string> :指定domain名稱,id或者uuid

[--snapshotname] <string> : 指定快照名稱

--current :恢復到當前快照

--running :在恢復快照後自動運行虛擬機

--paused :在恢復快照後自動暫停虛擬機

--force :強制執行快照,有風險的恢復選項


[root@kvm-node1 ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 2     centos6.5                      running

7)到虛擬機中查看testfile的內容

技術分享圖片


使用qemu-img管理虛擬機快照:

qemu-img snapshot選項:

-a:應用一個快照,即使用該快照還原(還原磁盤文件快照時的狀態)
-c:創建一個快照
-d:刪除一個快照
-l:列出給定磁盤映像的快照列表

1、為虛擬機創建快照

[root@kvm-node1 ~]# qemu-img snapshot -c centos6.5_snapshot_02 /images/kvm/centos6.qcow2

2、查看虛擬機快照

[root@kvm-node1 ~]# qemu-img snapshot -l /images/kvm/centos6.qcow2 
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         centos6.5_snapshot_01   166M 2018-07-01 21:43:54   01:08:54.228      
2         centos6.5_snapshot_02      0 2018-07-01 22:01:34   00:00:00.000

上面的兩個快照,第一個是使用vrish創建,第二個是使用qemu-img創建

3、恢復快照

[root@kvm-node1 ~]# qemu-img snapshot -a centos6.5_snapshot_02 /images/kvm/centos6.qcow2

4、刪除快照

[root@kvm-node1 ~]# qemu-img snapshot -d centos6.5_snapshot_02 /images/kvm/centos6.qcow2


KVM虛擬機快照