noNVC基礎用法:
1、下載noVNC
git clone https://github.com/novnc/noVNC.git
2、編輯qemu.conf配置文件
Vim /etc/libvirt/qemu.conf
將配置文件中的vnc_listen = "0.0.0.0" 啟用
3、查看虛擬機vnc端口
Virsh vncdisplay vm001
:0 // 表示5900
也可以在創建虛擬機xml文件是指定vm的vnc端口
<graphics type='vnc' port='5910' autoport='no' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
4、啟動noVNC
.noVNC/utils/launch.sh --vnc host_ip:5910
noVNC高級用法:
在基礎用法用需要為每啟用一個代理,在實際應用中不可能為每台虛擬機都架一個代理,這種方式對端口號的消耗也是巨大的。
因此我們需要做的是使用一個端口來使用多個代理,因此需要引入Token的概念。
這里需要用的是websockify,在 Github 上 noVNC 和 websockify 本來就是獨立的兩個項目。
在我們第一次運行./noVNC/utils/launch.sh時會自動下載websockify

目錄結構:

1、創建token文件
在websockify目錄中創建目錄token,並在該目錄下創建token文件,寫入以下內容:
token: host_ip:port
這里需要注意的坑是token:后面是有一個空格的(少了這個空格讓我調試了一個夜晚)。
2、啟動websockify
進入websockify目錄 --web ../ 是為了使用noVNC的vnc.html和vnc_lite.html文件
./run --web ../ --target-config ./token/token localhost:8888
3、瀏覽器測試
http://localhost:8888/vnc_lite.html?path=?token=vm004 (這里我直接使用的是虛擬機vm004的名稱來作為Token,只需要修改Token就能夠訪問相應的虛擬機)

擴展Token類型
Custom plugins should match the format of the BasePlugin class found in websockify/token_plugins.py. Namely, they should implement an __init__(self, src) method (where src is the value of --token-source), and a lookup(self, token) method which either returns a (host, port) tuple, or None.
The plugin can be placed in any module that's importable by websockify, and then be used as such:
$ ./run ... --token-plugin some.module.TokenClass --token-source 'some information here'
Example:
import memcache import simplejson class TokenMemc(object): def __init__(self, src): self._server = src def lookup(self, token): client = memcache.Client([self._server], debug=0) stuff = client.get(token) combo = simplejson.loads(stuff) pair = combo["host"] return pair.split(':')
