温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

kvm虚拟机vnc和spice如何配置

发布时间:2021-12-07 14:12:15 来源:亿速云 阅读:383 作者:小新 栏目:云计算

这篇文章主要为大家展示了“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命令动态查看虚拟机的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命令动态查看虚拟机的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如何配置”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI