在KVM虛擬機中使用spice

1.spice介紹

1.1 spice簡介

spice是由Qumranet開發的開源網絡協議,2008年紅帽收購了Qumranet得到了這個協議。SPICE是紅帽在虛擬化領域除了KVM的又一「新興技術」,它提供與虛擬桌面設備的遠程交互實現.php

目前,spice主要目標是爲qemu虛擬機提供高質量的遠程桌面訪問,它致力於克服傳統虛擬桌面的一些弊端,而且強調用戶體驗html

spice包含有3個組件:node

SPICE Driver:SPICE驅動器 存在於每一個虛擬桌面內的組件vim

SPICE server:SPICE服務器 存在於紅帽企業虛擬化Hypervisor內的組件windows

SPICE Client: SPICE客戶端 存在於終端設備上的組件,能夠是瘦客戶機或專用的PC,用於接入每一個虛擬桌面。centos

這三個組件協做運行,肯定處理圖形的最高效位置,以可以最大程度改善用戶體驗並下降系統負荷。若是客戶機足夠強大,SPICE向客戶機發送圖形命 令,並在客戶機中對圖形進行處理,顯著減輕服務器的負荷。另外一方面,若是客戶機不夠強大,SPICE在主機處理圖形,從CPU的角度講,圖形處理並不須要 太多費用api

以上簡介參考http://os.51cto.com/art/201201/311464.htm安全

1.2 spice架構

Spice agent運行在客戶機(虛擬機)操做系統中。Spice server和Spice client利用spice agent來執行一些須要在虛擬機裏執行的任務,如配置分辨率,另外還有經過剪貼板來拷貝文件等。從上圖能夠看出,Spice client與server與Spice Agent的通訊須要藉助一些其餘的軟件模塊,如在客戶機裏面,Spice Agent須要經過VDIPort Driver與主機上 QEMU的VDIPort Device進行交互,他們的交互經過一種叫作輸入/輸出的環進行。Spice Client和Server產生的消息被寫入到設備的輸出環中,由VDI Port Driver讀取;而Spice Agent發出的消息則經過VDI Port Driver先寫入到VDI Port Device輸入環中,被QEMU讀入到Spice server的緩衝區中,而後再根據消息決定由Spice Server直接處理,仍是被髮往Spice Client中。

以上參考http://blog.csdn.net/hbsong75/article/details/9465683bash

1.3 spice的不足

  • spice目標是提供一個高性能,高用戶體驗的遠程桌面鏈接,就像本地桌面同樣展示給用戶. 其目前實現的功能有usb重定向,音視頻傳輸,剪貼板,鼠標同步,2D圖形支持,任意調整分辨率(qxl驅動)等服務器

  • spice目前不支持虛擬機中的3D效果,對於windows7系統虛擬機,其aero桌面特效也沒法啓用,由於spice使用遠程鏈接,因此其高度依賴網絡,若是網絡環境很差,使用起來將會是一間很痛苦的事情

2 spice安裝配置

2.1 系統準備

系統版本

  • RHEL6

  • IP:192.168.11.166

安裝KVM軟件

#安裝kvm/qemu工具,以及virt-manager,libvirtdyum install qemu-kvm qemu-kvm-tools virt-manager libvirt libvirt-devel libvirt-client virt-manager virt-viewer

系統環境檢查

  • 運行命令 lsmod | grep kvm 檢查 KVM 模塊是否成功安裝。若是有輸出結果,那麼 KVM 模塊已成功安裝

  • 須要服務器硬件支持虛擬化技術(Intel VT-d,AMD-V)。可使用以下命令檢查,有輸出信息表示支持

egrep "(vmx|svm)" --color /proc/cpuinfo

2.2 服務器上安裝spice-server

yum -y install spice-protocol spice-server xorg-x11-drv-qxl spice-glib

2.3 客戶端安裝spice client

centos客戶端安裝virt-viewer

yum -y install virt-viewer

windows7客戶端安裝virt-viewer

下載連接: http://virt-manager.org/download/sources/virt-viewer/virt-viewer-x64-1.0.msi

2.4 新建centos6.4虛擬機

關於virt-manager工具

virt-manager是一個圖形化的虛擬機管理工具,它能夠方便地建立虛擬機,修改虛擬機配置,添加新設備等. 可是因爲其是圖形界面管理,因此效率不是很高,並且對網絡也有要求. libvirt提供了一個xml文件用來定義虛擬機的配置細節,而且能夠經過virsh命令對虛擬機進行管理

編寫一個虛擬機的xml文件

以下是一個centos6.4桌面版的xml文件,默認配置

<domain type=‘kvm‘>
  <name>cdesk1</name>
  <uuid>56f6c45d-aa4d-8b23-85d9-87500199f089</uuid>
  <memory unit=‘KiB‘>2097152</memory>
  <currentMemory unit=‘KiB‘>2097152</currentMemory>
  <vcpu placement=‘static‘>2</vcpu>
  <os>
    <type arch=‘x86_64‘ machine=‘rhel6.5.0‘>hvm</type>
    <boot dev=‘hd‘/>
    <bootmenu enable=‘yes‘/>
  </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=‘qcow2‘ cache=‘none‘/>
      <source file=‘/data_lij/vhosts/cdesk1.qcow2‘/>
      <target dev=‘vda‘ bus=‘virtio‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x04‘ function=‘0x0‘/>
    </disk>
    <controller type=‘usb‘ index=‘0‘>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x01‘ function=‘0x2‘/>
    </controller>
    <interface type=‘network‘>
      <mac address=‘52:54:00:92:32:a9‘/>
      <source network=‘default‘/>
      <model type=‘virtio‘/>
      <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=‘-1‘ autoport=‘yes‘ 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=‘0x05‘ function=‘0x0‘/>
    </memballoon>
  </devices></domain>

經過xml文件啓動虛擬機

#加入virsh管理
virsh define cdesk1.xml
#啓動虛擬機
virsh start cdesk1
#查看端口
netstat -ntpl | grep qemu

查看進程

[root@controller2 qemu]# ps -ef | grep qemu
qemu     21866     1 10 19:26 ?        00:00:00 /usr/libexec/qemu-kvm -name cdesk1 -S -M rhel6.5.0 -enable-kvm -m 2048 -realtime mlock=off -smp 2,sockets=2,cores=1,threads=1 -uuid 56f6c45d-aa4d-8b23-85d9-87500199f089 -nodefconfig -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/cdesk1.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -no-shutdown -boot order=c,menu=on -device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 -drive file=/data_lij/vhosts/cdesk1.qcow2,if=none,id=drive-virtio-disk0,format=qcow2,cache=none -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x4,drive=drive-virtio-disk0,id=virtio-disk0 -netdev tap,fd=25,id=hostnet0,vhost=on,vhostfd=26 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:92:32:a9,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -vnc 0.0.0.0:0 -vga cirrus -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x5

默認狀況下,qemu使用vnc創建遠程鏈接,監聽地址爲0.0.0.0,其端口爲自動分配(xml文件中定義graphics type=‘vnc‘ port=‘-1‘, -1表示端口自動分配5900+N)

客戶端使用TightVNC工具遠程鏈接

3 啓用spice進行遠程鏈接

3.1 修改xml文件,使用spice進行遠程鏈接

首先virsh命令關閉虛擬機

<!--  添加 -->  
 <channel type=‘spicevmc‘>
      <target type=‘virtio‘ name=‘com.redhat.spice.0‘/>
      <alias name=‘channel0‘/>
      <address type=‘virtio-serial‘ controller=‘0‘ bus=‘0‘ port=‘1‘/>
    </channel><!--  修改 -->
    <graphics type=‘spice‘ port=‘5902‘ tlsPort=‘5903‘ autoport=‘yes‘ listen=‘0.0.0.0‘>
      <listen type=‘address‘ address=‘0.0.0.0‘/>
    </graphics>
    <video>
      <model type=‘qxl‘ ram=‘65536‘ vram=‘65536‘ heads=‘1‘/>
      <alias name=‘video0‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x02‘ function=‘0x0‘/>
    </video>

3.2 修改xml文件支持音頻傳輸

<sound model=‘ich6‘>
   <alias name=‘sound0‘/>
 </sound>

Linux下model=‘ich6‘,windows下model=‘ac97‘

3.3 使用windows7客戶端遠程鏈接

首先啓動虛擬機

virsh start cdesk1

使用virt-viewer工具,其支持vnc和spice兩種協議,因此這裏須要指定使用spice。

3.4 使用virt-manager圖形工具配置spice

以上步驟3.1-3.3中是修改xml文件配置spice鏈接的,也可使用virt-manager圖形界面操做

  • 首先關閉虛擬機

  • 服務器上運行virt-manager命令,打開圖形界面(須要開啓服務器上的X11轉發)

  • 以下圖Display中更改Type爲spice

  • video中更改Model爲qxl,修改完成以後,啓動虛擬機

4 提升虛擬機性能(鼠標同步,共享剪貼板,音視頻傳輸等)

咱們在客戶端使用spice client遠程鏈接虛擬機,若是想要虛擬機中播放的音視頻傳輸到本地客戶端,或者在虛擬機和客戶機之間共享剪貼板,則須要在虛擬機中安裝相應加強工具,對於windows系統和Linux系統,須要安裝的加強工具不太同樣

 4.1 windows虛擬機配置

windows虛擬機須要安裝加強工具spice-guest-tools(相似vmware中的vmtool工具)

下載地址: http://www.spice-space.org/download/windows/spice-guest-tools/spice-guest-tools-0.74.exe

這個軟件包包含了一個qxl視頻卡驅動,還包括SPICE guest agent,能夠實現同步剪貼板,鼠標,任意調整虛擬機分辨率等功能

4.2 Linux虛擬機配置

centos gnome桌面版虛擬機,須要安裝如下軟件包

yum install xorg-x11-drv-qxl spice-vdagent
#設置開機啓動/etc/init.d/spice-vdagentd start
chkconfig spice-vdagentd on

4.3 把虛擬機中的音視頻傳輸到客戶端

修改xml文件實現音視頻傳輸

前面是經過在虛擬機xml文件中添加sound標籤,實現虛擬機和客戶機的音視頻傳輸,也可使用virt-manager工具

用virt-manager工具添加音頻設備

選擇Sound的Model(ac97或者ich6)

5.spice的USB重定向

5.1 介紹

使用usb重定向,在client上插入的U盤會被重定向到虛擬機中. 其有兩種實現方式,自動重定向(全部插入client中的U盤都被重定向),或者手動選擇須要重定向的U盤

USB重定向須要爲虛擬機添加USB2 EHCI驅動,以及若干個Spice channels,Spice channels的個數決定了客戶端一次能夠有多少個USB設備被重定向到guest

更多參考:

http://people.freedesktop.org/~teuf/spice-doc/html/ch02s06.html

http://www.spice-space.org/page/UsbRedir

http://fedoraproject.org/wiki/Features/UsbNetworkRedirection

5.2服務器上安裝軟件

[root@controller2 vhosts]# rpm -qa | grep usb
usbredir-0.5.1-1.el6.x86_64
libusb-0.1.12-23.el6.x86_64
usbutils-003-4.el6.x86_64
libusb1-1.0.9-0.6.rc1.el6.x86_64

5.3 虛擬機xml文件中添加USB redirection驅動

#首先關閉虛擬機,而後修改其xml文件,添加下面標籤
<!-- 移除xml文件中其它的USB設備,而後添加下面的部分 --><controller type=‘usb‘ index=‘0‘ model=‘ich9-ehci1‘/><controller type=‘usb‘ index=‘0‘ model=‘ich9-uhci1‘>
  <master startport=‘0‘/></controller><controller type=‘usb‘ index=‘0‘ model=‘ich9-uhci2‘>
  <master startport=‘2‘/></controller><controller type=‘usb‘ index=‘0‘ model=‘ich9-uhci3‘>
  <master startport=‘4‘/></controller><redirdev bus=‘usb‘ type=‘spicevmc‘/><redirdev bus=‘usb‘ type=‘spicevmc‘/><redirdev bus=‘usb‘ type=‘spicevmc‘/><redirdev bus=‘usb‘ type=‘spicevmc‘/>

1.3中是在xml文件中添加usb驅動,其對應的命令行參數以下(當使用qemu-kvm命令行啓動虛擬機時須要):

-device ich9-usb-ehci1,id=usb -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,multifunction=on -device ich9-usb-uhci2,masterbus=usb.0,firstport=2 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4 -chardev spicevmc,name=usbredir,id=usbredirchardev1 -device usb-redir,chardev=usbredirchardev1,id=usbredirdev1 -chardev spicevmc,name=usbredir,id=usbredirchardev2 -device usb-redir,chardev=usbredirchardev2,id=usbredirdev2 -chardev spicevmc,name=usbredir,id=usbredirchardev3 -device usb-redir,chardev=usbredirchardev3,id=usbredirdev3

5.4 客戶端配置

客戶端鏈接工具使用virt-viewer

windows7版本的virt-viewer默認不支持USB重定向,須要手動從新編譯,Linux下的客戶端能夠編譯源碼支持USB重定向

virt-viewer源碼: http://virt-manager.org/download/sources/virt-viewer/virt-viewer-1.0.tar.gz

virt-viewer windows客戶端: http://virt-manager.org/download/sources/virt-viewer/virt-viewer-x64-1.0.msi

6 spice使用TLS和密碼實現雙重認證

默認狀況下,客戶端和虛擬機傳輸的數據是未加密的,下面的步驟中將使用TLS加密客戶端和虛擬機之間的鏈接

6.1 生成CA證書,服務器證書

6.1.1 建立證書存放目錄
cd /etc/pkimkdir libvirt-spice
cd libvirt-spice
6.1.2 使用下面腳本建立證書

注意:腳本生成的ca-cert.pem文件,最後輸出的變量」SUBJECT「值都須要拷貝到客戶端

#!/bin/bash
 
SERVER_KEY=server-key.pem
# creating a key for our caif [ ! -e ca-key.pem ]; then
 openssl genrsa -des3 -out ca-key.pem 1024fi# creating a caif [ ! -e ca-cert.pem ]; then
 openssl req -new -x509 -days 1095 -key ca-key.pem -out ca-cert.pem  -subj "/C=IL/L=Raanana/O=Red Hat/CN=my CA"fi# create server keyif [ ! -e $SERVER_KEY ]; then
 openssl genrsa -out $SERVER_KEY 1024fi# create a certificate signing request (csr)if [ ! -e server-key.csr ]; then
 openssl req -new -key $SERVER_KEY -out server-key.csr -subj "/C=IL/L=Raanana/O=Red Hat/CN=my server"fi# signing our server certificate with this caif [ ! -e server-cert.pem ]; then
 openssl x509 -req -days 1095 -in server-key.csr -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pemfi
 # now create a key that doesn‘t require a passphraseopenssl rsa -in $SERVER_KEY -out $SERVER_KEY.insecuremv $SERVER_KEY $SERVER_KEY.securemv $SERVER_KEY.insecure $SERVER_KEY
 
# show the results (no other effect)
openssl rsa -noout -text -in $SERVER_KEY
openssl rsa -noout -text -in ca-key.pem
openssl req -noout -text -in server-key.csr
openssl x509 -noout -text -in server-cert.pem
openssl x509 -noout -text -in ca-cert.pem
 
# copy *.pem file to /etc/pki/libvirt-spiceif [[ -d "/etc/pki/libvirt-spice" ]]then
 cp ./*.pem /etc/pki/libvirt-spice
else
 mkdir /etc/pki/libvirt-spice
     cp ./*.pem /etc/pki/libvirt-spice
fi
 
# echo SUBJECT
echo "SUBJECT is:" \" `openssl x509 -noout -text -in server-cert.pem | grep Subject: | cut -f 10- -d " "` \"

6.2虛擬機加載證書

#默認無論vnc仍是spice都是監聽在127.0.0.1上,這樣確定不能從網絡中訪問

#下面的設置默認會使全部的虛擬機開啓兩個端口,一個普通端口,一個爲使用ssl加密的安全端口,而且監聽全部地址

#vim /etc/libvirt/qemu.conf
spice_listen="0.0.0.0"spice_tls=1spice_tls_x509_cert_dir="/etc/pki/libvirt-spice"
 #下面的爲默認密碼認證,僅當虛擬機xml文件中沒有設置passwd參數時才生效,爲了可以使用不一樣密碼,這裏不啓用,改在xml文件中設置密碼
#spice_password = "123456"
 #重啓libvirtd加載證書/etc/init.d/libvirtd restart

6.3 在虛擬機xml文件中設置密碼及安全端口

xml文件中安全端口能夠有不一樣設置方法

A <graphics type=‘spice‘ autoport=‘yes‘ listen=‘0.0.0.0‘ passwd=‘123456‘>     B <graphics type=‘spice‘ port=‘5901‘ autoport=‘no‘ listen=‘0.0.0.0‘ passwd=‘123456‘>C <graphics type=‘spice‘ tlsPort=‘-1‘ autoport=‘no‘ listen=‘0.0.0.0‘ passwd=‘123456‘>

A: 每臺虛擬機自動配置兩個端口,普通端口和安全端口,而且端口號自動分配(5900+N)

B: 不自動配置端口,手動指定一個普通端口,不開啓安全端口

C: 不自動配置端口,只開啓安全端口,而且安全端口自動分配(5900+N)

passwd=123456  設置使用密碼認證,即客戶端鏈接虛擬機時,會彈出密碼驗證窗口

6.4 windows客戶端中使用spice加密鏈接

6.4.1 拷貝ca-cert.pem證書

拷貝服務器上腳本生成的ca-cert.pem文件到windows下某個目錄,好比 F:\files\ca\

6.4.2 windows中添加環境變量
=IL, L=Raanana, O=Red Hat, CN=my server  

#(變量值爲腳本最後輸出內容),添加環境變量不是必須的操做,是爲了下面可以使用%SUBJECT%這個變量
6.4.3 在cmd中測試鏈接

打開cmd, 進入remote-viewer.exe程序所在目錄,默認爲 C:\Program Files\VirtViewer\bin

#運行命令
remote-viewer.exe --spice-ca-file F:\ca\ca-cert.pem spice://192.168.11.166?tls-port=5905 --spice-host-subject="%SUBJECT%"

6.5 Linux客戶端中使用spice加密鏈接

首先安裝virt-viewer客戶端

  virt--viewer --spice-ca- ca-cert.pem --spice-host-subject  spice:
#也能夠把‘C=IL,L=Raanana,O=Red Hat,CN=my server‘部分設置爲一個全局環境變量SUBJECT,以簡化命令

7 spice的多客戶端支持

7.1 多顯示器支持

spice容許客戶端使用多個顯示器鏈接到同一臺虛擬機,爲了實現這個功能,虛擬機必須配置有多個qxl設備驅動(對於Windows虛擬機),或者有一個配置爲支持多個heads的qxl設備驅動(Linux虛擬機)

爲了支持多顯示器,必須爲虛擬機配置qxl驅動,虛擬機中也須要安裝qxl驅動支持(xorg-x11-drv-qxl),參考http://www.spice-space.org/download.html中guest部分

7.1.1 Linux虛擬機配置

對於Linux虛擬機,配置好qxl驅動以後,默認會啓用多顯示器支持.若是Linux系統版本過舊,能夠參考這個http://hansdegoede.livejournal.com/12969.html

技術分享

7.1.2 windows虛擬機配置

修改xml文件,添加多個video標籤,而後從新啓動虛擬機

<video>
    <model type=‘qxl‘>
</video>
<video>
    <model type=‘qxl‘>
</video>

7.2 多客戶端支持

多客戶端支持容許多個用戶鏈接同一臺虛擬機,參考http://www.spice-space.org/page/Features/MultipleClients

7.2.1 使用qemu-kvm命令行

對於使用qemu-kvm命令行建立的虛擬機,只須要給宿主機添加下面的環境變量

export SPICE_DEBUG_ALLOW_MC=1

添加以後,用qemu-kvm命令建立虛擬機,能夠看到輸出中多了一行,表示spice已經啓用多客戶端支持

技術分享

7.2.2 使用libvirt
對於使用libvirt管理的虛擬機,添加上面的環境變量不生效,須要修改虛擬機xml文件
使用qemu:commandline標籤傳遞變量"SPICE_DEBUG_ALLOW_MC"值給虛擬機
<!-- 更改第一行爲下面 --><domain type=‘kvm‘ xmlns:qemu=‘http://libvirt.org/schemas/domain/qemu/1.0‘><!-- 在下面相似位置添加 --><domain>
  <devices>
  ...  </devices>
  <qemu:commandline>
    <qemu:env name=‘SPICE_DEBUG_ALLOW_MC‘ value=‘1‘/>
  </qemu:commandline></domain>

添加上面的以後,重啓虛擬機,便可

若是要驗證添加的參數是否生效,能夠在啓動虛擬機(cos_v1)時,查看虛擬機日誌輸出

tail -f /var/log/libvirt/qemu/cos_v1.log
#下面是輸出2014-12-26 10:06:10.763+0000: starting up
LC_ALL=C PATH=/sbin:/usr/sbin:/bin:/usr/bin HOME=/root USER=root LOGNAME=root QEMU_AUDIO_DRV=spice SPICE_DEBUG_ALLOW_MC=1 /usr/libexec/qemu-kvm -name cos_v1 -S -M rhel6.5.0 ...
...char device redirected to /dev/pts/7((null):29858): Spice-Warning **: reds.c:4010:do_spice_init: spice: allowing multiple client connections (crashy)    #這行代表添加成功
相關文章
相關標籤/搜索