差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

下次修改
前次修改
tech:kvm_snapshot [2012/06/21 14:32] – 建立 jonathantech:kvm_snapshot [2016/10/30 11:21] (目前版本) jonathan
行 1: 行 1:
 +====== CentOS 6 KVM Snapshot ======
  
 +===== 確認 VM Image 格式 =====
 +  * 執行 snapshot 的語法<code sh>virsh snapshot-create vmname</code> 範例:<file>
 +[root@asus-ts100e7 ~]# virsh snapshot-create e-plast-mail
 +錯誤:Requested operation is not valid: Disk '/var/lib/libvirt/images/e-plast-mail.img' does not support snapshotting
 +</file> 要能執行 snapshot 的 VM image 必須是 qcow2 的格式, 出現這樣的訊息, 就要去確認與轉換.
 +  * 確認 image 的格式語法<code sh>qemu-img info yourdisk.img</code> 範例:<file>
 +[root@asus-ts100e7 ~]# qemu-img info /var/lib/libvirt/images/e-plast-mail.img
 +image: /var/lib/libvirt/images/e-plast-mail.img
 +file format: raw
 +virtual size: 9.8G (10485760000 bytes)
 +disk size: 9.8G
 +</file>
 +  * 如果是 raw 要先轉成 qcow2 格式, 語法<code sh>qemu-img convert -f raw -O qcow2 yourdisk.img newdisk.qcow2</code> 範例:<file>
 +[root@asus-ts100e7 ~]# qemu-img convert -f raw -O qcow2 /var/lib/libvirt/images/e-plast-mail.img /var/lib/libvirt/images/e-plast-mail.qcow2
 +[root@asus-ts100e7 ~]# qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2
 +image: /var/lib/libvirt/images/e-plast-mail.qcow2
 +file format: qcow2
 +virtual size: 9.8G (10485760000 bytes)
 +disk size: 3.0G
 +cluster_size: 65536
 +</file>
 +  * 更改 vm config file 範例: <code sh>virsh edit e-plast-mail</code><file>
 +:
 +    <disk type='file' device='disk'>
 +      <driver name='qemu' type='qcow2' cache='none'/>
 +      <source file='/var/lib/libvirt/images/e-plast-mail.qcow2'/>
 +      <target dev='vda' bus='virtio'/>
 +    </disk>
 +:
 +</file>
 +  * 重新啟動 vm 範例: <code sh>virsh restart e-plast-mail</code>
 +
 +===== 建立 snapshot =====
 +  * 目前版本進行 snapshot 過程 vm 會無法運作
 +  * 執行 snapshot 的語法<code sh>virsh snapshot-create vmname</code> 範例:<file>
 +[root@asus-ts100e7 ~]# virsh snapshot-create e-plast-mail
 +Domain snapshot 1349058343 created
 +</file>
 +  * 這時會在 /var/lib/libvirt/qemu/snapshot/e-plast-mail 產生 1349058343.xml, 內容如下<file>
 +<domainsnapshot>
 +  <name>1349058343</name>
 +  <state>running</state>
 +  <creationTime>1349058343</creationTime>
 +  <domain>
 +    <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid>
 +  </domain>
 +  <active>0</active>
 +</domainsnapshot>
 +</file>
 +
 +===== 查詢目前 snapshot =====
 +  * 可以查看目前已經存在多少份 snapshot<code sh>
 +virsh snapshot-list e-plast-mail
 +</code><file>
 +[root@asus-ts100e7 images]# virsh snapshot-list e-plast-mail
 + 名稱               Creation Time             狀態
 +---------------------------------------------------
 + 1349058343           2012-10-01 10:25:43 +0800 running
 + 1349059256           2012-10-01 10:40:56 +0800 running
 +</file>
 +  * 目前是使用哪個 snapshot 版本<code sh>
 +virsh snapshot-current e-plast-mail
 +</code><file>
 +[root@asus-ts100e7 images]# virsh snapshot-current e-plast-mail
 +<domainsnapshot>
 +  <name>1349059256</name>
 +  <state>running</state>
 +  <parent>
 +    <name>1349058343</name>
 +  </parent>
 +  <creationTime>1349059256</creationTime>
 +  <domain>
 +    <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid>
 +  </domain>
 +</domainsnapshot>
 +</file>
 +
 +===== 復原到特定版本 snapshot =====
 +  * 經過驗證, libvirt 0.8.2-25.el5 在 VM 運行中執行 revert 後, VM 會當掉無法運作, 因此需要先關閉 VM 後再進行 revert
 +  * 確認 VM 目前運作狀態<code sh>
 +virsh domstate e-plast-mail
 +</code><file>
 +[root@asus-ts100e7 libvirt]# virsh domstate e-plast-mail
 +執行中
 +</file>
 +  * 執行關閉 VM 指令<code sh>
 +virsh shutdown e-plast-mail
 +</code><file>
 +[root@asus-ts100e7 libvirt]# virsh shutdown e-plast-mail
 +區域 e-plast-mail 正在執行關機
 +</file>
 +  * 確認 VM 目前已經是關機狀態<code sh>
 +virsh domstate e-plast-mail
 +</code><file>
 +[root@asus-ts100e7 save]# virsh domstate e-plast-mail
 +關機
 +</file>
 +  * 確定要回覆哪份 snapshot 版本<code sh>
 +virsh snapshot-list e-plast-mail
 +</code><file>
 +[root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail
 + 名稱               Creation Time             狀態
 +---------------------------------------------------
 + 1349058343           2012-10-01 10:25:43 +0800 running
 + 1349059256           2012-10-01 10:40:56 +0800 running
 + 1349071788           2012-10-01 14:09:48 +0800 running
 +</file>
 +  * 執行 snapshot-revert 指令<code sh>
 +virsh snapshot-revert e-plast-mail 1349071788
 +</code>
 +  * 確認目前執行的 snapshot 版本<code sh>
 +virsh snapshot-current e-plast-mail
 +</code><file>
 +[root@asus-ts100e7 save]# virsh snapshot-current e-plast-mail
 +<domainsnapshot>
 +  <name>1349071788</name>
 +  <state>running</state>
 +  <parent>
 +    <name>1349059256</name>
 +  </parent>
 +  <creationTime>1349071788</creationTime>
 +  <domain>
 +    <uuid>8dd0c9a8-c3d3-b6c2-1112-c7876db57444</uuid>
 +  </domain>
 +</domainsnapshot>
 +</file>
 +
 +<note>
 +  * revirt 後 VM 會自動啟動在當時 snapshot-create 的狀態
 +  * 原本在 1349071788 (2012-10-01 14:09:48) 時所執行的程式還會繼續運作
 +  * VM 內的系統時間還是在 1349071788 (2012-10-01 14:09:48) 所以要考慮時間矯正議題
 +</note>
 +
 +===== 刪除不需要的 snapshot =====
 +  * 原有的 snapshot 清單<code sh>
 +virsh snapshot-list e-plast-mail
 +</code><file>
 +[root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail
 + 名稱               Creation Time             狀態
 +---------------------------------------------------
 + 1349058343           2012-10-01 10:25:43 +0800 running
 + 1349059256           2012-10-01 10:40:56 +0800 running
 + 1349071788           2012-10-01 14:09:48 +0800 running
 +</file>
 +  * 打算移除掉 1349059256 這份版本<code sh>
 +virsh snapshot-delete e-plast-mail 1349059256
 +</code><file>
 +[root@asus-ts100e7 save]# virsh snapshot-list e-plast-mail
 + 名稱               Creation Time             狀態
 +---------------------------------------------------
 + 1349058343           2012-10-01 10:25:43 +0800 running
 + 1349071788           2012-10-01 14:09:48 +0800 running
 +</file>
 +
 +<note>
 +  * snapshot 主要在 image file 內增加 tag, 因此可以透過 qemu-img info 指令來瞭解<code sh>
 +qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2
 +</code><file>
 +[root@asus-ts100e7 images]# qemu-img info /var/lib/libvirt/images/e-plast-mail.qcow2
 +image: /var/lib/libvirt/images/e-plast-mail.qcow2
 +file format: qcow2
 +virtual size: 9.8G (10485760000 bytes)
 +disk size: 7.0G
 +cluster_size: 65536
 +Snapshot list:
 +ID        TAG                 VM SIZE                DATE       VM CLOCK
 +1         1349058343             977M 2012-10-01 10:25:43 1290:29:38.005
 +3         1349071788             965M 2012-10-01 14:09:48 1291:18:26.283
 +</file>
 +</note>
 +===== 參考網址 =====
 +  * http://forums.fedoraforum.org/showthread.php?t=260126
 +  * http://blog.yam.com/keynes0918/article/45173318
 +
 +
 +{{tag>kvm snapshot libvirt virsh qcow2 linux-kvm}}