溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

kvm虛擬機vnc和spice如何配置

發(fā)布時間:2021-12-07 14:12:15 來源:億速云 閱讀:416 作者:小新 欄目:云計算

這篇文章主要為大家展示了“kvm虛擬機vnc和spice如何配置”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“kvm虛擬機vnc和spice如何配置”這篇文章吧。

一、簡介

        通過vnc或spice方式訪問虛擬主機上的KVM虛擬機,可以直接通過圖形化界面virt-manager來設置,但此處通過xml配置文件修改。

二、詳解

1、VNC方式訪問

        vnc方式訪問虛擬機不是在kvm虛擬機安裝配置vnc服務器,通過虛擬主機的IP地址與端口進行訪問,kvm虛擬化對vnc的支持相對來說比xen要好很多,在虛擬主機上配置VNC訪問虛擬機,也是為了多提供一種方式訪問虛擬機而已。
(1)修改qemu.conf(也可不修改,默認是127.0.0.1)

#vi /etc/libvirt/qemu.conf  

vnc_listen = "0.0.0.0"  

重啟libvirt

#systemctl restart libvirtd.service 

       vnclisten默認綁定127.0.0.1,在配置文件里指定VNC綁定0.0.0.0,就不用在安裝kvm虛擬機時指定vnclisten參數了。當在虛擬主機上有很多個虛擬機的時候,若指定每個虛擬機的端口,將會很亂,所以采用0.0.0.0自動分配端口。
(2)修改目標虛擬機smb3.1的配置文件
#virsh list --all
#virsh edit smb3.1

<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>  

  <listen type='address' address='0.0.0.0'/>  

</graphics>  

#virsh start smb3.1
(3)查看運行虛擬機的vnc端口
查看vnc端口#virsh vncdisplay smb3.1

:0

也可以通過virsh命令動態(tài)查看虛擬機的xml配置文件#virsh dumpxml smb3.1
kvm虛擬機vnc和spice如何配置
(4)vnc登錄
windows下可以通過vnc viewer或TightVNC或RealVNC等工具登錄。
kvm虛擬機vnc和spice如何配置
linux下也可以通過#virt-viewer --connect qemu:///system smb3.1訪問,非本機的linux通過#virt-viewer qemu+ssh://root@192.168.40.125/system smb3.1訪問。
(5)vnc源碼登錄
kde桌面的源碼包kdenetwork中可以找到krdc/vnc中關于vnc的源碼,提取vncview.cpp、vncclientthread.cpp和remoteview.cpp即可運行vnc。

  1. #include "widget.h"  

  2. #include "vncview.h"  

  3.   

  4. Widget::Widget(QWidget *parent)  

  5.     : QWidget(parent,  Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint)  

  6. {  

  7.     resize(900+20, 900+20);  

  8.   

  9.     VncView *vncView = new VncView(this, QUrl("vnc://:@192.168.40.125:5901"));  

  10.     vncView->enableScaling(true);  

  11.     vncView->scaleResize(900, 900);  

  12.     vncView->show();  

  13.     vncView->start();  

  14. }  

  15.   

  16. Widget::~Widget()  

  17. {  

  18.   

  19. }  

kvm虛擬機vnc和spice如何配置

2、SPICE方式訪問

(1)修改目標虛擬機smb3.1的配置文件
#virsh list --all
#virsh edit smb3.0

[html] view plain copy

  1. <graphics type='spice' autoport='yes' listen='0.0.0.0'>  

  2.     <listen type='address' address='0.0.0.0'/>  

  3. </graphics>  

#virsh start smb3.0
(2)查看運行虛擬機的vnc端口
#netstat -tunlp
kvm虛擬機vnc和spice如何配置a

或通過virsh命令動態(tài)查看虛擬機的xml配置文件#virsh dumpxml smb3.0
kvm虛擬機vnc和spice如何配置
也可以通過命令#spicy -h 127.0.0.1 -p 5900(需安裝spice-gtk-tools軟件包)。
(4)spice源碼登錄
spice-gtk提供了完整的gtk界面。

[html] view plain copy

  1. #include <stdio.h>  

  2. #include <stdlib.h>  

  3. #include <getopt.h>  

  4. #include <gtk/gtk.h>  

  5. #include <spice-channel.h>  

  6. #include <spice-session.h>  

  7. #include <spice-widget.h>  

  8.   

  9. static GtkWidget *main_window;  

  10. static SpiceSession *spice_session;  

  11. static SpiceDisplay *spice_display;  

  12. static char *host;  

  13. static char *port;  

  14.   

  15.   

  16. static void channel_new(SpiceSession *s, SpiceChannel *c, gpointer *data)  

  17. {  

  18.     int id = 0;  

  19.   

  20.     g_object_get(c, "channel-id", &id, NULL);  

  21.   

  22.     if (SPICE_IS_MAIN_CHANNEL(c)) {  

  23.         fprintf(stdout, "new main channel\n");  

  24.         return;  

  25.     }  

  26.   

  27.     if (SPICE_IS_DISPLAY_CHANNEL(c)) {  

  28.         fprintf(stdout, "new display channel (#%d), creating window\n", id);  

  29.         spice_display = spice_display_new(s, id);  

  30.         gtk_container_add(GTK_CONTAINER(main_window), GTK_WIDGET(spice_display));  

  31.         gtk_widget_show_all(main_window);  

  32.         return;  

  33.   

  34.     }  

  35.   

  36. }  

  37.   

  38. static void usage()  

  39. {  

  40.     fprintf(stdout, "spice-client: A spice client\n"  

  41.             "Usage: spice-client [options]...\n"  

  42.             "  -h, --host\n"  

  43.             "      Set address of spice server\n"  

  44.             "  -p, --port\n"  

  45.             "      Set port of spice server\n"  

  46.             "  -e, --help\n"  

  47.             "      Print help and exit\n"  

  48.         );  

  49. }  

  50.   

  51. static void parse_cmd(int argc, char *argv[])  

  52. {  

  53.     int c, e = 0;  

  54.   

  55.     if (argc == 1) {  

  56.         usage();  

  57.         exit(1);  

  58.     }  

  59.   

  60.     const struct option long_options[] = {  

  61.         { "help", 0, 0, 'e' },  

  62.         { "host", 0, 0, 'h' },  

  63.         { "port", 0, 0, 'p' },  

  64.         { 0, 0, 0, 0 },  

  65.     };  

  66.   

  67.     while ((c = getopt_long(argc, argv, "eh:p:",  

  68.                             long_options, NULL)) != EOF) {  

  69.         switch (c) {  

  70.         case 'e':  

  71.             goto failed;  

  72.         case 'h':  

  73.             host = optarg;  

  74.             break;  

  75.         case 'p':  

  76.             port = optarg;  

  77.             break;  

  78.         default:  

  79.             e++;  

  80.             break;  

  81.         }  

  82.     }  

  83.   

  84.     if (e || argc > optind) {  

  85.         goto failed;  

  86.     }  

  87.   

  88.     if (host == NULL || port == NULL) {  

  89.         fprintf(stderr, "No host or port found\n");  

  90.         goto failed;  

  91.     }  

  92.   

  93.     return ;  

  94.   

  95. failed:  

  96.     usage();  

  97.     exit(1);  

  98. }  

  99.   

  100. int main(int argc, char *argv[])  

  101. {  

  102.     parse_cmd(argc, argv);  

  103.   

  104.     gtk_init(&argc, &argv);  

  105.     main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);  

  106.   

  107.     spice_session = spice_session_new();  

  108.     g_object_set(spice_session, "host", host, NULL);  

  109.     g_object_set(spice_session, "port", port, NULL);  

  110.     g_signal_connect(spice_session, "channel-new",  

  111.                      G_CALLBACK(channel_new), NULL);  

  112.   

  113.     if (!spice_session_connect(spice_session)) {  

  114.         fprintf(stderr, "spice_session_connect failed\n");  

  115.         exit(1);  

  116.     }  

  117.   

  118.     gtk_main();  

  119.     return 0;  

  120. }  

  121. gcc -o spice-client client.c `pkg-config --cflags --libs spice-client-gtk-2.0`  

  122. ./spice-client -h 127.0.0.1 -p 5900  

以上是“kvm虛擬機vnc和spice如何配置”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI