1. 程式人生 > 實用技巧 >kvm虛擬化學習筆記(四)之kvm虛擬機器日常管理與配置

kvm虛擬化學習筆記(四)之kvm虛擬機器日常管理與配置

KVM虛擬機器的管理主要是通過virsh命令對虛擬機器進行管理。

1.檢視KVM虛擬機器配置檔案及執行狀態

(1)KVM虛擬機器預設配置檔案位置:/etc/libvirt/qemu/

autostart目錄是配置kvm虛擬機器開機自啟動目錄。

fd7e73c0ce330a92248c9b3dcc8917c6.png-wh_

(2)virsh命令幫助

#virsh-help

或直接virsh命令和,再執行子命令。如下所示。

[[email protected]~]#virsh
歡迎使用virsh,虛擬化的互動式終端。

輸入:'help'來獲得命令的幫助資訊
'quit'退出

virsh#help
……

(3)檢視kvm虛擬機器狀態

#virshlist--all

8d6465daa466938c35e0a153430edd73.png-wh_

2.KVM虛擬機器開機

#virshstartCentOS6.5-01

ad5ff1101464f3554d03884dc3a3753e.png-wh_

3.KVM虛擬機器關機或斷電

(1)關機

預設情況下virsh工具不能對linux虛擬機器進行關機操作,linux作業系統需要開啟與啟動acpid服務。在安裝KVMlinux虛擬機器必須配置此服務。

#chkconfigacpidon
#serviceacpidrestart

virsh關機

#virshshutdownCentOS6.5-01

ebc882998c43cdf2b977e82f4b7b3a57.png-wh_

(2)強制關閉電源

#virshdestroySer08R2-01

4d994aa7329c7743349d6212ed0567c7.png-wh_

4.通過配置檔案啟動虛擬機器

#virshcreate/etc/libvirt/qemu/Ser08R2-01.xml

#virsh create /etc/libvirt/qemu/CentOS6.5-01.xml

1cc2e7e52b526a59563c34258c7260db.png-wh_

5.配置開機自啟動虛擬機器

#virshautostartCentOS6.5-01

autostart目錄是kvm虛擬機器開機自啟動目錄,可以看到該目錄中有KVM配置檔案連結。

3059c042a66a16da2da41180f8f8a0b7.png-wh_

6.匯出KVM虛擬機器配置檔案

#virshdumpxmlSer08R2-01>/etc/libvirt/qemu/Ser08R2-02.xml

KVM虛擬機器配置檔案可以通過這種方式進行備份。

b334ba21c8b665801ff88c9d33736d09.png-wh_

7.新增與刪除KVM虛擬機器

(1)刪除kvm虛擬機器

#virshundefineSer08R2-01

說明:該命令只是刪除wintest01的配置檔案,並不刪除虛擬磁碟檔案。如下圖所示。

2cb6cbb6463a0b3229873ab029bd34dc.png-wh_

(2)重新定義虛擬機器配置檔案

通過匯出備份的配置檔案恢復原KVM虛擬機器的定義,並重新定義虛擬機器。

#mv/etc/libvirt/qemu/Ser08R2-02.xml/etc/libvirt/qemu/Ser08R2-01.xml

#virshdefine/etc/libvirt/qemu/Ser08R2-01.xml

99a5995481a87df3a65b3631930c477d.png-wh_

8.編輯KVM虛擬機器配置檔案

#virsheditSer08R2-01

virshedit將呼叫vi命令編輯/etc/libvirt/qemu/Ser08R2-01.xml配置檔案。也可以直接通過vi命令進行編輯,修改,儲存。

可以但不建議直接通過vi編輯。

f68e934863ea0177551faba7d3ae2f54.png-wh_

[[email protected] qemu]# vim /etc/libvirt/qemu/Ser08R2-01.xml

<!--
WARNING:THISISANAUTO-GENERATEDFILE.CHANGESTOITARELIKELYTOBE
OVERWRITTENANDLOST.Changestothisxmlconfigurationshouldbemadeusing:
virsheditSer08R2-01
orotherapplicationusingthelibvirtAPI.
-->

<domaintype='kvm'>
<name>Ser08R2-01</name>
<uuid>9814b16e-d573-ace0-7bb3-b206fc66b8e1</uuid>
<memoryunit='KiB'>2097152</memory>
<currentMemoryunit='KiB'>2097152</currentMemory>
<vcpuplacement='static'>2</vcpu>
<os>
<typearch='x86_64'machine='rhel6.6.0'>hvm</type>
<bootdev='hd'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clockoffset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disktype='file'device='disk'>
<drivername='qemu'type='raw'cache='none'/>
<sourcefile='/data/kvm/Ser08R2-01.img'/>
<targetdev='hda'bus='ide'/>
<addresstype='drive'controller='0'bus='0'target='0'unit='0'/>
</disk>
<disktype='block'device='cdrom'>
<drivername='qemu'type='raw'/>
<targetdev='hdc'bus='ide'/>
<readonly/>
<addresstype='drive'controller='0'bus='1'target='0'unit='0'/>
</disk>
<controllertype='usb'index='0'model='ich9-ehci1'>
<addresstype='pci'domain='0x0000'bus='0x00'slot='0x04'function='0x7'/>
</controller>
<controllertype='usb'index='0'model='ich9-uhci1'>
<masterstartport='0'/>
<addresstype='pci'domain='0x0000'bus='0x00'slot='0x04'function='0x0'multifunction='on'/>
</controller>
<controllertype='usb'index='0'model='ich9-uhci2'>
<masterstartport='2'/>
<addresstype='pci'domain='0x0000'bus='0x00'slot='0x04'function='0x1'/>
</controller>
<controllertype='usb'index='0'model='ich9-uhci3'>
<masterstartport='4'/>
<addresstype='pci'domain='0x0000'bus='0x00'slot='0x04'function='0x2'/>
</controller>
<controllertype='ide'index='0'>
<addresstype='pci'domain='0x0000'bus='0x00'slot='0x01'function='0x1'/>
</controller>
<interfacetype='bridge'>
<macaddress='52:54:00:cd:11:41'/>
<sourcebridge='br0'/>
<addresstype='pci'domain='0x0000'bus='0x00'slot='0x03'function='0x0'/>
</interface>
<serialtype='pty'>
<targetport='0'/>
</serial>
<consoletype='pty'>
<targettype='serial'port='0'/>
</console>
<inputtype='mouse'bus='ps2'/>
<graphicstype='vnc'port='5911'autoport='no'listen='0.0.0.0'>
<listentype='address'address='0.0.0.0'/>
</graphics>
<video>
<modeltype='cirrus'vram='9216'heads='1'/>
<addresstype='pci'domain='0x0000'bus='0x00'slot='0x02'function='0x0'/>
</video>
<memballoonmodel='virtio'>
<addresstype='pci'domain='0x0000'bus='0x00'slot='0x05'function='0x0'/>
</memballoon>
</devices>
</domain>

9.virshconsole控制檯管理linux虛擬機器

#virshconsoleoeltest01

10.其它virsh命令

(1)掛起伺服器

#virshsuspendCentOS6.5-01

ccac7a39f768c9338e3d77a8f639ee97.png-wh_

(2)恢復伺服器

#virshresumeCentOS6.5-01

7be2bef954ca6c5c4a5db6db704d0dd4.png-wh_

virsh命令豐富。可以執行各種維護任務,本文只是從維護與管理的角度例舉了常用的命令,為該命令的使用提供一個思路。

轉載於:https://blog.51cto.com/dengaosky/1984011