×
新网 > 虚机资讯 > 正文

kvm虚拟机vnc和spice配置

一、简介         通过vnc或spice方式访问虚拟主机上的KVM虚拟机,可以直接通过图形化界面virt-manager来设置,但此处通过xml配置文件修改。 二、详解 1、VNC方式访问         vnc方式访问虚拟机不是在kvm虚拟机安装配置vnc服务器,通过虚拟主机的IP地址与端口进行访问,kvm虚拟化对vnc的支持相对来说比xen要好很多,在虚拟主机

 

一、简介

通过vnc或spice方式访问虚拟主机上的KVM虚拟机,可以直接通过图形化界面virt-manager来设置,但此处通过xml配置文件修改。

y2vzmvxcck4.jpg

二、详解 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

 

<graphicstype=\'vnc\'port=\'-1\'autoport=\'yes\'listen=\'0.0.0.0\'>

<listentype=\'address\'address=\'0.0.0.0\'/>

</graphics>

#virsh start smb3.1
(3)查看运行虚拟机的vnc端口
查看vnc端口#virsh vncdisplaysmb3.1

:0

也可以通过virsh命令动态查看虚拟机的xml配置文件#virsh dumpxml smb3.1

(4)vnc登录
windows下可以通过vnc viewer或TightVNC或RealVNC等工具登录。

linux下可以安装tigervnc,然后通过#vncviewer 127.0.0.1:5901登录。

linux下也可以通过#virt-viewer --connect qemu:///system smb3.1访问,非本机的linux通过#virt-viewerqemu+ssh://root@192.168.40.125/systemsmb3.1访问。
(5)vnc源码登录
kde桌面的源码包kdenetwork中可以找到krdc/vnc中关于vnc的源码,提取vncview.cpp、vncclientthread.cpp和remoteview.cpp即可运行vnc。

#include"widget.h" #include"vncview.h" Widget::Widget(QWidget*parent) :QWidget(parent,Qt::WindowMinimizeButtonHint|Qt::WindowCloseButtonHint) { resize(900+20,900+20); VncView*vncView=newVncView(this,QUrl("vnc://:@192.168.40.125:5901")); vncView->enableScaling(true); vncView->scaleResize(900,900); vncView->show(); vncView->start(); } Widget::~Widget() { }

 

 

2、SPICE方式访问

(1)修改目标虚拟机smb3.1的配置文件
#virsh list --all
#virsh edit smb3.0

[html]view plaincopy

<graphicstype=\'spice\'autoport=\'yes\'listen=\'0.0.0.0\'> <listentype=\'address\'address=\'0.0.0.0\'/> </graphics>

#virsh start smb3.0
(2)查看运行虚拟机的vnc端口
#netstat -tunlp

 

或通过virsh命令动态查看虚拟机的xml配置文件#virsh dumpxml smb3.0
(3)spice登录
linux下通过#virt-viewer --connect qemu:///system smb3.0登录到图形化界面。
也可以通过命令#spicy -h 127.0.0.1 -p 5900(需安装spice-gtk-tools软件包)。
(4)spice源码登录
spice-gtk提供了完整的gtk界面。

[html]view plaincopy

#include<stdio.h> #include<stdlib.h> #include<getopt.h> #include<gtk/gtk.h> #include<spice-channel.h> #include<spice-session.h> #include<spice-widget.h> staticGtkWidget*main_window; staticSpiceSession*spice_session; staticSpiceDisplay*spice_display; staticchar*host; staticchar*port; staticvoidchannel_new(SpiceSession*s,SpiceChannel*c,gpointer*data) { intid=0; g_object_get(c,"channel-id",&id,NULL); if(SPICE_IS_MAIN_CHANNEL(c)){ fprintf(stdout,"newmainchanneln"); return; } if(SPICE_IS_DISPLAY_CHANNEL(c)){ fprintf(stdout,"newdisplaychannel(#%d),creatingwindown",id); spice_display=spice_display_new(s,id); gtk_container_add(GTK_CONTAINER(main_window),GTK_WIDGET(spice_display)); gtk_widget_show_all(main_window); return; } } staticvoidusage() { fprintf(stdout,"spice-client:Aspiceclientn" "Usage:spice-client[options]...n" "-h,--hostn" "Setaddressofspiceservern" "-p,--portn" "Setportofspiceservern" "-e,--helpn" "Printhelpandexitn" ); } staticvoidparse_cmd(intargc,char*argv[]) { intc,e=0; if(argc==1){ usage(); exit(1); } conststructoptionlong_options[]={ {"help",0,0,\'e\'}, {"host",0,0,\'h\'}, {"port",0,0,\'p\'}, {0,0,0,0}, }; while((c=getopt_long(argc,argv,"eh:p:", long_options,NULL))!=EOF){ switch(c){ case\'e\': gotofailed; case\'h\': host=optarg; break; case\'p\': port=optarg; break; default: e++; break; } } if(e||argc>optind){ gotofailed; } if(host==NULL||port==NULL){ fprintf(stderr,"Nohostorportfoundn"); gotofailed; } return; failed: usage(); exit(1); } intmain(intargc,char*argv[]) { parse_cmd(argc,argv); gtk_init(&argc,&argv); main_window=gtk_window_new(GTK_WINDOW_TOPLEVEL); spice_session=spice_session_new(); g_object_set(spice_session,"host",host,NULL); g_object_set(spice_session,"port",port,NULL); g_signal_connect(spice_session,"channel-new", G_CALLBACK(channel_new),NULL); if(!spice_session_connect(spice_session)){ fprintf(stderr,"spice_session_connectfailedn"); exit(1); } gtk_main(); return0; } gcc-ospice-clientclient.c`pkg-config--cflags--libsspice-client-gtk-2.0` ./spice-client-h127.0.0.1-p5900

  • 相关专题

免责声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,也不承认相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,请发送邮件至:operations@xinnet.com进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

免费咨询获取折扣

Loading