Skip to content
Kei Kamikawa edited this page Nov 2, 2022 · 4 revisions

(Virtio) Socket configure services and other communication endpoints in your virtual machine. Host computers make services available using ports, which identify the type of service and the protocol to use when transmitting data. Use this object to specify the ports available to your guest operating system, and to register handlers to manage the communication on those ports. To enable socket device communication support in Linux, configure the Linux guest kernel to enable the CONFIG_VIRTIO_VSOCKETS support.

Overview

Use a vz.VirtioSocketDeviceConfiguration to implement port-based communication between the guest operating system and the host computer. When you add this object to the socketDevices property of your vz.VirtualMachineConfiguration, the virtual machine provides a corresponding vz.VirtioSocketDevice to use to configure the ports. Add only one vz.VirtioSocketDeviceConfiguration to your virtual machine’s configuration.

The example below shows the creation of a vz.VirtioSocketDeviceConfiguration:

socketDevice, err := vz.NewVirtioSocketDeviceConfiguration()
if err != nil {
	return nil, err
}
config.SetSocketDevicesVirtualMachineConfiguration([]vz.SocketDeviceConfiguration{
	socketDevice,
})

Once the configuration is complete and your virtual machine is created, the socket device is available. The virtual machine creates it and stores it in the socketDevices property. You can obtain the socket device with the following code:

socketDevices := vm.SocketDevices()
if len(socketDevices) != 1 {
	log.Fatalf("want the number of socket devices is 1 but got %d", len(socketDevices))
}
socketDevice := socketDevices[0]
Clone this wiki locally