KVM虛擬機的日常管理與配置


1. 查看KVM虛擬機配置文件及運行狀態
(1) KVM虛擬機默認配置文件位置: /etc/libvirt/qemu/

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

(2) virsh命令幫助

# virsh -help

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

[root@node1 ~]# virsh 

歡迎使用 virsh,虛擬化的交互式終端。

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

virsh # help 

(3) 查看kvm虛擬機狀態

# virsh list --all

2. KVM虛擬機開機

# virsh start oeltest01

3. KVM虛擬機關機或斷電
(1) 關機

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

# chkconfig acpid on 
# service acpid restart

virsh關機

# virsh shutdown oeltest01

 (2) 強制關閉電源

# virsh destroy wintest01

4. 通過配置文件啟動虛擬機

# virsh create /etc/libvirt/qemu/wintest01.xml 

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

# virsh autostart oeltest01 

autostart目錄是kvm虛擬機開機自啟動目錄,可以看到該目錄中有KVM配置文件鏈接。

6. 導出KVM虛擬機配置文件

# virsh dumpxml wintest01 > /etc/libvirt/qemu/wintest02.xml

KVM虛擬機配置文件可以通過這種方式進行備份。

7. 添加與刪除KVM虛擬機
(1) 刪除kvm虛擬機

# virsh undefine wintest01

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

 (2) 重新定義虛擬機配置文件

通過導出備份的配置文件恢復原KVM虛擬機的定義,並重新定義虛擬機。

# mv /etc/libvirt/qemu/wintest02.xml /etc/libvirt/qemu/wintest01.xml

# virsh define /etc/libvirt/qemu/wintest01.xml 

8. 編輯KVM虛擬機配置文件

# virsh edit wintest01

virsh edit將調用vi命令編輯/etc/libvirt/qemu/wintest01.xml配置文件。也可以直接通過vi命令進行編輯,修改,保存。

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

[root@node1 qemu]# vi /etc/libvirt/qemu/wintest01.xml

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh edit wintest01
or other application using the libvirt API.
-->
<domain type='kvm'>
<name>wintest01</name>
<uuid>fe31ea48-7d6a-f3cb-cede-2f9bd9dec2bd</uuid>
<memory unit='KiB'>524288</memory>
<currentMemory unit='KiB'>524288</currentMemory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='rhel6.4.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='raw' cache='none'/>
<source file='/data/wintest01.img'/>
<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'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' 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:2b:2f:fe'/>
<source bridge='br0'/>
<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='5911' 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='0x04' function='0x0'/>
</memballoon>
</devices>
<seclabel type='none'/>
</domain>

9. virsh console 控制台管理linux虛擬機

配置virsh console

# virsh console oeltest01 

10. 其它virsh命令
(1) 掛起服務器

# virsh suspend oeltest01 

(2) 恢復服務器

# virsh resume oeltest01 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM