爛泥:KVM快照的建立與恢複

來源:互聯網
上載者:User

標籤:虛擬機器   爛泥   kvm   快照   建立   

本文首發於爛泥行天下。

因為要做有關KVM虛擬機器的實驗,所以需要虛擬機器產生快照。查詢相關資料,說KVM可以使用兩種方法產生虛擬機器的快照。

方法一、使用qemu-img snapshot命令進行產生快照

方法二、使用virsh snapshot-create-as命令進行產生快照

其實經過我的測試,發現使用qemu-img snapshot命令產生快照後,即使恢複快照也是沒有用的。虛擬機器還是當前的狀態,而不是快照之前的狀態。不知道為什麼,而且還不穩定,經常報錯。

注意要使用KVM的快照功能,虛擬機器的硬碟一定要是qcow2格式,否則無法使用快照功能。

有關硬碟的格式,可以在建立虛擬機器硬碟時指定,也可以使用相關命令進行轉換。這個可以參考我之前的文章《爛泥:KVM中安裝Windows Server 2008 R2系統》。

在這篇文章中,我們重點介紹有關virsh snapshot-create-as命令產生快照。

不過在介紹之前,我們還是把第一種方法使用qemu-img snapshot命令產生快照的相關命令介紹下。

其實我們是可以直接通過命令的協助查詢到有關qemu-img snapshot使用方法的。

本次實驗我們已虛擬機器centos為例,已centos虛擬機器安裝ftp命令前後做快照。

650) this.width=650;" title="clip_image001[4]" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image001[4]" src="http://img1.51cto.com/attachment/201408/4/526870_1407142459RfjO.png" width="723" height="269" />

650) this.width=650;" title="clip_image002[4]" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image002[4]" src="http://img1.51cto.com/attachment/201408/4/526870_1407142459sISr.png" width="494" height="120" />

我們先來建立一個快照,使用如下命令:

qemu-img snapshot -c centos_install_ftp /vhostdisk/centos.img

建立快照

qemu-img snapshot -l /vhostdisk/centos.img

查詢快照

qemu-img snapshot -d centos_install_ftp /vhostdisk/centos.img

刪除快照

650) this.width=650;" title="clip_image003[4]" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image003[4]" src="http://img1.51cto.com/attachment/201408/4/526870_1407142459GaEI.png" width="490" height="58" />

650) this.width=650;" title="clip_image004[4]" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image004[4]" src="http://img1.51cto.com/attachment/201408/4/526870_1407142460YRLL.png" width="708" height="89" />

注意,其中centos_install_ftp為快照名稱,/vhostdisk/centos.img為原虛擬機器。

剛剛在使用qemu-img snapshot進行相關測試發現,居然把我的虛擬機器給搞壞了。唉,真是坑爹啊,怪不得網路上的資料顯示使用這個有問題呢。強烈建議,還是使用第二種方法吧。

以上是qemu-img snapshot建立KVM快照的方法,現在我們來介紹使用virsh snapshot-create-as來產生虛擬機器快照的方法。

Centos虛擬機器未安裝ftp命令,建立快照如下:

650) this.width=650;" title="clip_image005[4]" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image005[4]" src="http://img1.51cto.com/attachment/201408/4/526870_1407142461QMuk.png" width="359" height="212" />

snapshot-create-as centos centos_install_ftp

為虛擬機器centos建立快照,快照名稱為centos_install_ftp

snapshot-list centos查看虛擬機器centos所有的快照

snapshot-info centos centos_uninstall_ftp

查看虛擬機器centos快照centos_uninstall_ftp詳細資料

650) this.width=650;" title="clip_image006[4]" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image006[4]" src="http://img1.51cto.com/attachment/201408/4/526870_1407142461XBzu.png" width="643" height="445" />

Centos虛擬機器安裝ftp命令,建立快照如下:

650) this.width=650;" title="clip_image007[4]" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image007[4]" src="http://img1.51cto.com/attachment/201408/4/526870_1407142462X9dv.png" width="349" height="284" />

snapshot-create-as centos centos_install_ftp

為虛擬機器centos建立快照,快照名稱為centos_install_ftp

650) this.width=650;" title="clip_image008[4]" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image008[4]" src="http://img1.51cto.com/attachment/201408/4/526870_1407142463Pgz5.png" width="431" height="332" />

以上我們把centos這台虛擬機器在安裝FTP命令前後的快照已經做好,現在虛擬機器centos處於已經安裝FTP命令狀態,如果我們此時要把虛擬機器centos恢複到安裝FTP命令之前的話,我們只需要使用virsh snapshot-revert命令即可。但是在使用virsh snapshot-revert命令恢複虛擬機器centos狀態之前,我們必須要把虛擬機器關閉。

關閉虛擬機器,我們可以使用virsh destroy命令進行強制性關機,我們也可以使用virsh shutdown命令進行關機。

注意要使用virsh shutdown命令進行關閉虛擬機器的話,一定要安裝acpid服務。同時經過多次測試證明如果虛擬機器有快照的話,就無法使用virsh shutdown/reboot,命令進行關機或者重啟。

yum -y install acpid 安裝acpid服務

/etc/init.d/acpid status 查看acpid服務是否運行

650) this.width=650;" title="clip_image009[4]" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image009[4]" src="http://img1.51cto.com/attachment/201408/4/526870_1407142464t5eV.png" width="586" height="222" />

destroy centos強制關閉虛擬機器centos

650) this.width=650;" title="clip_image010[4]" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image010[4]" src="http://img1.51cto.com/attachment/201408/4/526870_14071424642fwi.png" width="430" height="275" />

Centos虛擬機器關閉後,我們開始恢複虛擬機器的狀態到未安裝FTP命令之前,即恢複虛擬機器centos的快照。使用如下命令:

snapshot-revert centos centos_uninstall_ftp

650) this.width=650;" title="clip_image011[4]" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image011[4]" src="http://img1.51cto.com/attachment/201408/4/526870_1407142465cvaX.png" width="456" height="252" />

注意:快照恢複完畢後,強烈建議把現在的虛擬機器關閉重新啟動下。

650) this.width=650;" title="clip_image012[4]" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image012[4]" src="http://img1.51cto.com/attachment/201408/4/526870_1407142466ezBj.png" width="434" height="131" />

以上是有關KVM虛擬機器的建立與恢複,如果要刪除一個快照,可以使用如下命令:

snapshot-delete centos centos_uninstall_ftp

至此KVM虛擬機器的快照介紹完畢。

本文出自 “爛泥行天下” 部落格,請務必保留此出處http://ilanni.blog.51cto.com/526870/1535627

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.