bhyve: Add support for VNC autoport

This patch adds support for automatic VNC port assignment for bhyve guests.

Signed-off-by: Roman Bogorodskiy <bogorodskiy@gmail.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Alexander Nusov 2017-05-08 13:36:43 +03:00 committed by Roman Bogorodskiy
parent d8e8b63d30
commit ed210660d6
10 changed files with 138 additions and 3 deletions

View File

@ -330,15 +330,19 @@ bhyveBuildLPCArgStr(const virDomainDef *def ATTRIBUTE_UNUSED,
} }
static int static int
bhyveBuildGraphicsArgStr(const virDomainDef *def ATTRIBUTE_UNUSED, bhyveBuildGraphicsArgStr(const virDomainDef *def,
virDomainGraphicsDefPtr graphics, virDomainGraphicsDefPtr graphics,
virDomainVideoDefPtr video, virDomainVideoDefPtr video,
virConnectPtr conn, virConnectPtr conn,
virCommandPtr cmd) virCommandPtr cmd,
bool dryRun)
{ {
virBuffer opt = VIR_BUFFER_INITIALIZER; virBuffer opt = VIR_BUFFER_INITIALIZER;
virDomainGraphicsListenDefPtr glisten = NULL; virDomainGraphicsListenDefPtr glisten = NULL;
bool escapeAddr; bool escapeAddr;
unsigned short port;
bhyveConnPtr driver = conn->privateData;
if (!(bhyveDriverGetCaps(conn) & BHYVE_CAP_LPC_BOOTROM) || if (!(bhyveDriverGetCaps(conn) & BHYVE_CAP_LPC_BOOTROM) ||
def->os.bootloader || def->os.bootloader ||
@ -401,6 +405,20 @@ bhyveBuildGraphicsArgStr(const virDomainDef *def ATTRIBUTE_UNUSED,
virBufferAdd(&opt, glisten->address, -1); virBufferAdd(&opt, glisten->address, -1);
} }
if (!dryRun) {
if (graphics->data.vnc.autoport) {
if (virPortAllocatorAcquire(driver->remotePorts, &port) < 0)
return -1;
graphics->data.vnc.port = port;
} else {
if (virPortAllocatorSetUsed(driver->remotePorts,
graphics->data.vnc.port,
true) < 0)
VIR_WARN("Failed to mark VNC port '%d' as used by '%s'",
graphics->data.vnc.port, def->name);
}
}
virBufferAsprintf(&opt, ":%d", graphics->data.vnc.port); virBufferAsprintf(&opt, ":%d", graphics->data.vnc.port);
break; break;
default: default:
@ -557,7 +575,8 @@ virBhyveProcessBuildBhyveCmd(virConnectPtr conn,
if (def->ngraphics && def->nvideos) { if (def->ngraphics && def->nvideos) {
if (def->ngraphics == 1 && def->nvideos == 1) { if (def->ngraphics == 1 && def->nvideos == 1) {
if (bhyveBuildGraphicsArgStr(def, def->graphics[0], def->videos[0], conn, cmd) < 0) if (bhyveBuildGraphicsArgStr(def, def->graphics[0], def->videos[0],
conn, cmd, dryRun) < 0)
goto error; goto error;
add_lpc = true; add_lpc = true;
} else { } else {

View File

@ -52,6 +52,7 @@
#include "viraccessapicheck.h" #include "viraccessapicheck.h"
#include "virhostcpu.h" #include "virhostcpu.h"
#include "virhostmem.h" #include "virhostmem.h"
#include "virportallocator.h"
#include "conf/domain_capabilities.h" #include "conf/domain_capabilities.h"
#include "bhyve_conf.h" #include "bhyve_conf.h"
@ -1219,6 +1220,7 @@ bhyveStateCleanup(void)
virObjectUnref(bhyve_driver->closeCallbacks); virObjectUnref(bhyve_driver->closeCallbacks);
virObjectUnref(bhyve_driver->domainEventState); virObjectUnref(bhyve_driver->domainEventState);
virObjectUnref(bhyve_driver->config); virObjectUnref(bhyve_driver->config);
virObjectUnref(bhyve_driver->remotePorts);
virMutexDestroy(&bhyve_driver->lock); virMutexDestroy(&bhyve_driver->lock);
VIR_FREE(bhyve_driver); VIR_FREE(bhyve_driver);
@ -1265,6 +1267,9 @@ bhyveStateInitialize(bool privileged,
if (!(bhyve_driver->domainEventState = virObjectEventStateNew())) if (!(bhyve_driver->domainEventState = virObjectEventStateNew()))
goto cleanup; goto cleanup;
if (!(bhyve_driver->remotePorts = virPortAllocatorNew(_("display"), 5900, 65535, 0)))
goto cleanup;
bhyve_driver->hostsysinfo = virSysinfoRead(); bhyve_driver->hostsysinfo = virSysinfoRead();
if (!(bhyve_driver->config = virBhyveDriverConfigNew())) if (!(bhyve_driver->config = virBhyveDriverConfigNew()))

View File

@ -293,6 +293,16 @@ virBhyveProcessStop(bhyveConnPtr driver,
/* Cleanup network interfaces */ /* Cleanup network interfaces */
bhyveNetCleanup(vm); bhyveNetCleanup(vm);
/* VNC autoport cleanup */
if ((vm->def->ngraphics == 1) &&
vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
if (virPortAllocatorRelease(driver->remotePorts,
vm->def->graphics[0]->data.vnc.port) < 0) {
VIR_WARN("Failed to release VNC port for '%s'",
vm->def->name);
}
}
ret = 0; ret = 0;
virCloseCallbacksUnset(driver->closeCallbacks, vm, virCloseCallbacksUnset(driver->closeCallbacks, vm,
@ -412,6 +422,16 @@ virBhyveProcessReconnect(virDomainObjPtr vm,
if (STREQ(expected_proctitle, proc_argv[0])) { if (STREQ(expected_proctitle, proc_argv[0])) {
ret = 0; ret = 0;
priv->mon = bhyveMonitorOpen(vm, data->driver); priv->mon = bhyveMonitorOpen(vm, data->driver);
if (vm->def->ngraphics == 1 &&
vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
int vnc_port = vm->def->graphics[0]->data.vnc.port;
if (virPortAllocatorSetUsed(data->driver->remotePorts,
vnc_port,
true) < 0) {
VIR_WARN("Failed to mark VNC port '%d' as used by '%s'",
vnc_port, vm->def->name);
}
}
} }
} }

View File

@ -28,6 +28,7 @@
# include "virdomainobjlist.h" # include "virdomainobjlist.h"
# include "virthread.h" # include "virthread.h"
# include "virclosecallbacks.h" # include "virclosecallbacks.h"
# include "virportallocator.h"
# define BHYVE_AUTOSTART_DIR SYSCONFDIR "/libvirt/bhyve/autostart" # define BHYVE_AUTOSTART_DIR SYSCONFDIR "/libvirt/bhyve/autostart"
# define BHYVE_CONFIG_DIR SYSCONFDIR "/libvirt/bhyve" # define BHYVE_CONFIG_DIR SYSCONFDIR "/libvirt/bhyve"
@ -58,6 +59,8 @@ struct _bhyveConn {
virCloseCallbacksPtr closeCallbacks; virCloseCallbacksPtr closeCallbacks;
virPortAllocatorPtr remotePorts;
unsigned bhyvecaps; unsigned bhyvecaps;
unsigned grubcaps; unsigned grubcaps;
}; };

View File

@ -0,0 +1,12 @@
/usr/sbin/bhyve \
-c 1 \
-m 214 \
-u \
-H \
-P \
-s 0:0,hostbridge \
-l bootrom,/path/to/test.fd \
-s 2:0,ahci,hd:/tmp/freebsd.img \
-s 3:0,virtio-net,faketapdev,mac=52:54:00:00:00:00 \
-s 4:0,fbuf,tcp=127.0.0.1:5900 \
-s 1,lpc bhyve

View File

@ -0,0 +1 @@
dummy

View File

@ -0,0 +1,26 @@
<domain type='bhyve'>
<name>bhyve</name>
<uuid>df3be7e7-a104-11e3-aeb0-50e5492bd3dc</uuid>
<memory>219136</memory>
<vcpu>1</vcpu>
<os>
<type>hvm</type>
<loader readonly="yes" type="pflash">/path/to/test.fd</loader>
</os>
<devices>
<disk type='file'>
<driver name='file' type='raw'/>
<source file='/tmp/freebsd.img'/>
<target dev='hda' bus='sata'/>
<address type='drive' controller='0' bus='0' target='2' unit='0'/>
</disk>
<interface type='bridge'>
<model type='virtio'/>
<source bridge="virbr0"/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<graphics type='vnc' port='-1' autoport='yes'>
<listen type='address' address='127.0.0.1'/>
</graphics>
</devices>
</domain>

View File

@ -145,6 +145,11 @@ mymain(void)
if ((driver.xmlopt = virBhyveDriverCreateXMLConf(&driver)) == NULL) if ((driver.xmlopt = virBhyveDriverCreateXMLConf(&driver)) == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
if (!(driver.remotePorts = virPortAllocatorNew("display", 5900, 65535,
VIR_PORT_ALLOCATOR_SKIP_BIND_CHECK)))
return EXIT_FAILURE;
# define DO_TEST_FULL(name, flags) \ # define DO_TEST_FULL(name, flags) \
do { \ do { \
static struct testInfo info = { \ static struct testInfo info = { \
@ -196,6 +201,7 @@ mymain(void)
DO_TEST("vnc-vgaconf-on"); DO_TEST("vnc-vgaconf-on");
DO_TEST("vnc-vgaconf-off"); DO_TEST("vnc-vgaconf-off");
DO_TEST("vnc-vgaconf-io"); DO_TEST("vnc-vgaconf-io");
DO_TEST("vnc-autoport");
/* Address allocation tests */ /* Address allocation tests */
DO_TEST("addr-single-sata-disk"); DO_TEST("addr-single-sata-disk");
@ -234,6 +240,7 @@ mymain(void)
virObjectUnref(driver.caps); virObjectUnref(driver.caps);
virObjectUnref(driver.xmlopt); virObjectUnref(driver.xmlopt);
virObjectUnref(driver.remotePorts);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
} }

View File

@ -0,0 +1,41 @@
<domain type='bhyve'>
<name>bhyve</name>
<uuid>df3be7e7-a104-11e3-aeb0-50e5492bd3dc</uuid>
<memory unit='KiB'>219136</memory>
<currentMemory unit='KiB'>219136</currentMemory>
<vcpu placement='static'>1</vcpu>
<os>
<type arch='x86_64'>hvm</type>
<loader readonly='yes' type='pflash'>/path/to/test.fd</loader>
<boot dev='hd'/>
</os>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<disk type='file' device='disk'>
<driver name='file' type='raw'/>
<source file='/tmp/freebsd.img'/>
<target dev='hda' bus='sata'/>
<address type='drive' controller='0' bus='0' target='2' unit='0'/>
</disk>
<controller type='pci' index='0' model='pci-root'/>
<controller type='sata' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</controller>
<interface type='bridge'>
<mac address='52:54:00:00:00:00'/>
<source bridge='virbr0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'>
<listen type='address' address='127.0.0.1'/>
</graphics>
<video>
<model type='gop' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</video>
</devices>
</domain>

View File

@ -108,6 +108,7 @@ mymain(void)
DO_TEST_DIFFERENT("vnc-vgaconf-on"); DO_TEST_DIFFERENT("vnc-vgaconf-on");
DO_TEST_DIFFERENT("vnc-vgaconf-off"); DO_TEST_DIFFERENT("vnc-vgaconf-off");
DO_TEST_DIFFERENT("vnc-vgaconf-io"); DO_TEST_DIFFERENT("vnc-vgaconf-io");
DO_TEST_DIFFERENT("vnc-autoport");
/* Address allocation tests */ /* Address allocation tests */
DO_TEST_DIFFERENT("addr-single-sata-disk"); DO_TEST_DIFFERENT("addr-single-sata-disk");