使用QEMU/KVM虛擬化平臺(tái)啟動(dòng)虛擬機(jī)雌澄,通常需要手動(dòng)輸入一行很長(zhǎng)的命令,里面可能有幾十個(gè)參數(shù)杯瞻,要想啟動(dòng)多個(gè)虛擬機(jī)镐牺,以及監(jiān)視多個(gè)虛擬機(jī)運(yùn)行狀態(tài)會(huì)非常困難,這時(shí)候就需要virsh這樣一個(gè)虛擬機(jī)的管理工具魁莉。
由于前面的文章已經(jīng)介紹了如何安裝QEMU以及如何啟動(dòng)虛擬機(jī)睬涧,這里就直接從QEMU/KVM安裝好以后的地方開始。
1.安裝libvirt
在ubuntu系統(tǒng)中旗唁,使用apt-get安裝的libvirt還是1.3版本的畦浓,有很多兼容性的問題。這里選擇源碼安裝libvirt逆皮。
先卸載系統(tǒng)默認(rèn)安裝的libvirt(如果沒有安裝忽略這一步):
sudo apt-get purge libvirt
安裝所需環(huán)境(其他需要的configure的時(shí)候會(huì)提示安裝的):
sudo apt-get install libpolkit-agent-1-0 libpolkit-backend-1-0 libpolkit-gobject-1-0 libpolkit-gtk-mate-1-0 libxml++2.6-dev libdevmapper-dev libnl-3-dev libgnutls-dev libpciaccess-dev libnl-route-3-dev libyajl-dev xsltproc libxml2-utils
前往官網(wǎng)下載libvirt源碼:https://libvirt.org/sources/
我這里下載的是 libvirt-4.10.0.tar.xz宅粥,在ubuntu系統(tǒng)中解壓,編譯电谣,安裝:
tar xvJf libvirt-4.10.0.tar.xz
cd libvirt-4.10.0/
./configure --prefix=/usr --localstatedir=/var --sysconfdir=/etc #不指定的話默認(rèn)會(huì)給安裝到/usr/local目錄下
make && make install
安裝完成后檢查是否安裝成功:
# which libvirtd
/usr/sbin/libvirtd
# libvirtd --version
libvirtd (libvirt) 4.10.0
# which virsh
/usr/bin/virsh
# virsh -v
4.10.0
安裝成功后啟動(dòng)libvirtd服務(wù):
sudo systemctl restart libvirtd
2.通過虛擬機(jī)模板xml文件啟動(dòng)虛擬機(jī)
用virsh啟動(dòng)虛擬機(jī)需要有對(duì)應(yīng)的xml文件秽梅,virsh可以生成這些模板文件。
如何用iso創(chuàng)建虛擬磁盤直接使用qemu-img即可剿牺,之前文章講過企垦。這里直接默認(rèn)虛擬磁盤已創(chuàng)建并完成安裝。使用vhost-user網(wǎng)絡(luò)接口的虛擬機(jī)xml文件如下:
<domain type='kvm'>
<name>demovm</name>
<uuid>4a9b3f53-fa2a-47f3-a757-dd87720d9d1d</uuid>
<memory unit='KiB'>4194304</memory>
<currentMemory unit='KiB'>4194304</currentMemory>
<memoryBacking>
<hugepages>
<page size='1' unit='G' nodeset='0'/>
</hugepages>
</memoryBacking>
<vcpu placement='static'>2</vcpu>
<cputune>
<shares>4096</shares>
<vcpupin vcpu='0' cpuset='4'/>
<vcpupin vcpu='1' cpuset='5'/>
<emulatorpin cpuset='4,5'/>
</cputune>
<os>
<type arch='x86_64' machine='pc'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='host-model'>
<model fallback='allow'/>
<topology sockets='2' cores='1' threads='1'/>
<numa>
<cell id='0' cpus='0-1' memory='4194304' unit='KiB' memAccess='shared'/>
</numa>
</cpu>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/var/iso/virtual1.qcow2'/>
<target dev='vda' bus='virtio'/>
</disk>
<interface type='vhostuser'>
<mac address='00:00:00:00:00:01'/>
<source type='unix' path='/tmp/sock0' mode='server'/>
<model type='virtio'/>
<driver queues='2'>
<host mrg_rxbuf='on'/>
</driver>
</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='-1' autoport='yes' listen='0.0.0.0' keymap='en-us'/>
</devices>
</domain>
這個(gè)xml文件給虛擬機(jī)配置了各種物理設(shè)備信息:給虛擬機(jī)起名為demovm晒来,配置了綁定物理核的兩塊虛擬CPU核钞诡,內(nèi)存使用預(yù)分配的1GB大頁(yè)面,總內(nèi)存4GB湃崩,虛擬磁盤文件在'/var/iso/virtual1.qcow2'荧降,使用virtio的vhost-user網(wǎng)絡(luò)接口,一個(gè)串口console和一個(gè)VNC端口攒读。虛擬機(jī)的xml文件非常復(fù)雜朵诫,只要配置有一項(xiàng)不兼容或者設(shè)置出錯(cuò)就會(huì)導(dǎo)致啟動(dòng)失敗。
先定義該模板虛擬機(jī):
virsh define test.xml
在已經(jīng)啟動(dòng)了OVS并開放vhost端口情況下薄扁,啟動(dòng)虛擬機(jī):
virsh start demovm
通過virsh list已經(jīng)可以看到虛擬機(jī)運(yùn)行起來了:
3.遠(yuǎn)程終端和關(guān)閉虛擬機(jī)
開啟虛擬機(jī)以后剪返,我們可以通過連接到剛才設(shè)置的console來訪問虛擬機(jī)的終端:
virsh console demovm --devname serial0
但是問題出現(xiàn)了,為什么光標(biāo)一直卡在這邓梅,沒有進(jìn)入終端:
通過VNC連接到虛擬機(jī)(這里用的是tightVNC)脱盲,終端輸入:
sudo systemctl disable systemd-networkd-wait-online
sudo systemctl enable serial-getty@ttyS0.service
sudo systemctl start serial-getty@ttyS0.service
重新啟動(dòng)虛擬機(jī)再次嘗試,成功:
嘗試用virsh關(guān)閉虛擬機(jī):
virsh shutdown demovm
發(fā)現(xiàn)虛擬機(jī)并沒有關(guān)機(jī)還在運(yùn)行日缨,登錄VNC查看钱反,原來是圖形化界面的原因,開啟了用戶選擇界面:
查閱資料virsh的shutdown命令是通過acpi傳遞到虛擬機(jī)里的,進(jìn)入虛擬機(jī)終端面哥,進(jìn)入acpi事件處理目錄乙各,修改powerbtn事件處理配置文件:
cd /etc/acpi/events/
vim powerbtn
重啟回到virsh控制臺(tái),成功關(guān)機(jī)幢竹。